Commit bdc50913 authored by hangaoshuai's avatar hangaoshuai

check error when parse field failed

parent dfd6f581
...@@ -62,6 +62,7 @@ go_test( ...@@ -62,6 +62,7 @@ go_test(
"//vendor/k8s.io/api/scheduling/v1alpha1:go_default_library", "//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/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors: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/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1: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", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
......
...@@ -277,13 +277,16 @@ func (r *RuntimeSort) Less(i, j int) bool { ...@@ -277,13 +277,16 @@ func (r *RuntimeSort) Less(i, j int) bool {
iObj := r.objs[i] iObj := r.objs[i]
jObj := r.objs[j] jObj := r.objs[j]
parser := jsonpath.New("sorting").AllowMissingKeys(true)
parser.Parse(r.field)
var iValues [][]reflect.Value var iValues [][]reflect.Value
var jValues [][]reflect.Value var jValues [][]reflect.Value
var err error 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 { if unstructured, ok := iObj.(*unstructured.Unstructured); ok {
iValues, err = parser.FindResults(unstructured.Object) iValues, err = parser.FindResults(unstructured.Object)
} else { } else {
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"testing" "testing"
api "k8s.io/api/core/v1" api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
...@@ -408,3 +409,67 @@ func TestSortingPrinter(t *testing.T) { ...@@ -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)
}
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment