Merge pull request #62025 from hanxiaoshuai/bugfix0402

Automatic merge from submit-queue (batch tested with PRs 62025, 63851, 64077, 63967, 63991). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

check error when parse field failed

**What this PR does / why we need it**:
check error when parse field failed
**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/8/head
Kubernetes Submit Queue 2018-05-22 08:40:16 -07:00 committed by GitHub
commit 48b5fd9182
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 3 deletions

View File

@ -63,6 +63,7 @@ go_test(
"//vendor/k8s.io/api/scheduling/v1alpha1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",

View File

@ -264,13 +264,16 @@ func (r *RuntimeSort) Less(i, j int) bool {
iObj := r.objs[i]
jObj := r.objs[j]
parser := jsonpath.New("sorting").AllowMissingKeys(true)
parser.Parse(r.field)
var iValues [][]reflect.Value
var jValues [][]reflect.Value
var err error
parser := jsonpath.New("sorting").AllowMissingKeys(true)
err = parser.Parse(r.field)
if err != nil {
panic(err)
}
if unstructured, ok := iObj.(*unstructured.Unstructured); ok {
iValues, err = parser.FindResults(unstructured.Object)
} else {

View File

@ -22,6 +22,7 @@ import (
"testing"
api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
@ -408,3 +409,67 @@ func TestSortingPrinter(t *testing.T) {
}
}
}
func TestRuntimeSortLess(t *testing.T) {
var testobj runtime.Object
testobj = &api.PodList{
Items: []api.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "b",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "c",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "a",
},
},
},
}
testobjs, err := meta.ExtractList(testobj)
if err != nil {
t.Fatalf("ExtractList testobj got unexpected error: %v", err)
}
testfield := "{.metadata.name}"
testruntimeSort := NewRuntimeSort(testfield, testobjs)
tests := []struct {
name string
runtimeSort *RuntimeSort
i int
j int
expectResult bool
expectErr bool
}{
{
name: "test less true",
runtimeSort: testruntimeSort,
i: 0,
j: 1,
expectResult: true,
},
{
name: "test less false",
runtimeSort: testruntimeSort,
i: 1,
j: 2,
expectResult: false,
},
}
for i, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.runtimeSort.Less(test.i, test.j)
if result != test.expectResult {
t.Errorf("case[%d]:%s Expected result: %v, Got result: %v", i, test.name, test.expectResult, result)
}
})
}
}