2015-08-08 06:04:25 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-08-08 06:04:25 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2017-01-26 19:26:00 +00:00
|
|
|
"strings"
|
2015-08-08 06:04:25 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
2018-04-19 02:02:28 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-26 19:26:00 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2015-08-08 06:04:25 +00:00
|
|
|
)
|
|
|
|
|
2015-10-20 05:44:48 +00:00
|
|
|
func encodeOrDie(obj runtime.Object) []byte {
|
2017-10-16 11:41:50 +00:00
|
|
|
data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(api.SchemeGroupVersion), obj)
|
2015-10-20 05:44:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2015-08-08 06:04:25 +00:00
|
|
|
func TestSortingPrinter(t *testing.T) {
|
2015-11-18 18:24:54 +00:00
|
|
|
intPtr := func(val int32) *int32 { return &val }
|
2015-08-13 21:11:23 +00:00
|
|
|
|
2015-10-20 05:44:48 +00:00
|
|
|
a := &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-10-20 05:44:48 +00:00
|
|
|
Name: "a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-10-20 05:44:48 +00:00
|
|
|
Name: "b",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-10-20 05:44:48 +00:00
|
|
|
Name: "c",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-08 06:04:25 +00:00
|
|
|
tests := []struct {
|
2017-01-26 19:26:00 +00:00
|
|
|
obj runtime.Object
|
|
|
|
sort runtime.Object
|
|
|
|
field string
|
|
|
|
name string
|
|
|
|
expectedErr string
|
2015-08-08 06:04:25 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "in-order-already",
|
|
|
|
obj: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-13 21:11:23 +00:00
|
|
|
field: "{.metadata.name}",
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reverse-order",
|
|
|
|
obj: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-08-08 06:04:25 +00:00
|
|
|
Name: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-13 21:11:23 +00:00
|
|
|
field: "{.metadata.name}",
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
2016-05-01 06:18:03 +00:00
|
|
|
{
|
|
|
|
name: "random-order-timestamp",
|
|
|
|
obj: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Unix(300, 0),
|
2016-05-01 06:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Unix(100, 0),
|
2016-05-01 06:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Unix(200, 0),
|
2016-05-01 06:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &api.PodList{
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Unix(100, 0),
|
2016-05-01 06:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Unix(200, 0),
|
2016-05-01 06:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Unix(300, 0),
|
2016-05-01 06:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
field: "{.metadata.creationTimestamp}",
|
|
|
|
},
|
2015-08-08 06:04:25 +00:00
|
|
|
{
|
|
|
|
name: "random-order-numbers",
|
|
|
|
obj: &api.ReplicationControllerList{
|
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-08-13 21:11:23 +00:00
|
|
|
Replicas: intPtr(5),
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-08-13 21:11:23 +00:00
|
|
|
Replicas: intPtr(1),
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-08-13 21:11:23 +00:00
|
|
|
Replicas: intPtr(9),
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &api.ReplicationControllerList{
|
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-08-13 21:11:23 +00:00
|
|
|
Replicas: intPtr(1),
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-08-13 21:11:23 +00:00
|
|
|
Replicas: intPtr(5),
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-08-13 21:11:23 +00:00
|
|
|
Replicas: intPtr(9),
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-13 21:11:23 +00:00
|
|
|
field: "{.spec.replicas}",
|
2015-08-08 06:04:25 +00:00
|
|
|
},
|
2015-10-20 05:44:48 +00:00
|
|
|
{
|
|
|
|
name: "v1.List in order",
|
|
|
|
obj: &api.List{
|
|
|
|
Items: []runtime.RawExtension{
|
2016-03-17 10:32:14 +00:00
|
|
|
{Raw: encodeOrDie(a)},
|
|
|
|
{Raw: encodeOrDie(b)},
|
|
|
|
{Raw: encodeOrDie(c)},
|
2015-10-20 05:44:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &api.List{
|
|
|
|
Items: []runtime.RawExtension{
|
2016-03-17 10:32:14 +00:00
|
|
|
{Raw: encodeOrDie(a)},
|
|
|
|
{Raw: encodeOrDie(b)},
|
|
|
|
{Raw: encodeOrDie(c)},
|
2015-10-20 05:44:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
field: "{.metadata.name}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "v1.List in reverse",
|
|
|
|
obj: &api.List{
|
|
|
|
Items: []runtime.RawExtension{
|
2016-03-17 10:32:14 +00:00
|
|
|
{Raw: encodeOrDie(c)},
|
|
|
|
{Raw: encodeOrDie(b)},
|
|
|
|
{Raw: encodeOrDie(a)},
|
2015-10-20 05:44:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &api.List{
|
|
|
|
Items: []runtime.RawExtension{
|
2016-03-17 10:32:14 +00:00
|
|
|
{Raw: encodeOrDie(a)},
|
|
|
|
{Raw: encodeOrDie(b)},
|
|
|
|
{Raw: encodeOrDie(c)},
|
2015-10-20 05:44:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
field: "{.metadata.name}",
|
|
|
|
},
|
2017-01-26 19:26:00 +00:00
|
|
|
{
|
|
|
|
name: "some-missing-fields",
|
|
|
|
obj: &unstructured.UnstructuredList{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "List",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
},
|
2017-03-31 19:18:51 +00:00
|
|
|
Items: []unstructured.Unstructured{
|
2017-01-26 19:26:00 +00:00
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{
|
|
|
|
"availableReplicas": 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{
|
|
|
|
"availableReplicas": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: &unstructured.UnstructuredList{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "List",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
},
|
2017-03-31 19:18:51 +00:00
|
|
|
Items: []unstructured.Unstructured{
|
2017-01-26 19:26:00 +00:00
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{
|
|
|
|
"availableReplicas": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{
|
|
|
|
"availableReplicas": 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
field: "{.status.availableReplicas}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all-missing-fields",
|
|
|
|
obj: &unstructured.UnstructuredList{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "List",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
},
|
2017-03-31 19:18:51 +00:00
|
|
|
Items: []unstructured.Unstructured{
|
2017-01-26 19:26:00 +00:00
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{
|
|
|
|
"replicas": 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"kind": "ReplicationController",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"status": map[string]interface{}{
|
|
|
|
"replicas": 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
field: "{.status.availableReplicas}",
|
|
|
|
expectedErr: "couldn't find any field with path \"{.status.availableReplicas}\" in the list of objects",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "model-invalid-fields",
|
|
|
|
obj: &api.ReplicationControllerList{
|
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
Status: api.ReplicationControllerStatus{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: api.ReplicationControllerStatus{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: api.ReplicationControllerStatus{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
field: "{.invalid}",
|
|
|
|
expectedErr: "couldn't find any field with path \"{.invalid}\" in the list of objects",
|
|
|
|
},
|
2015-08-08 06:04:25 +00:00
|
|
|
}
|
2018-05-12 09:24:28 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
sort := &SortingPrinter{SortField: tt.field, Decoder: legacyscheme.Codecs.UniversalDecoder()}
|
|
|
|
err := sort.sortObj(tt.obj)
|
|
|
|
if err != nil {
|
|
|
|
if len(tt.expectedErr) > 0 {
|
|
|
|
if strings.Contains(err.Error(), tt.expectedErr) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatalf("%s: expected error containing: %q, got: \"%v\"", tt.name, tt.expectedErr, err)
|
2017-01-26 19:26:00 +00:00
|
|
|
}
|
2018-05-12 09:24:28 +00:00
|
|
|
t.Fatalf("%s: unexpected error: %v", tt.name, err)
|
2017-01-26 19:26:00 +00:00
|
|
|
}
|
2018-05-12 09:24:28 +00:00
|
|
|
if len(tt.expectedErr) > 0 {
|
|
|
|
t.Fatalf("%s: expected error containing: %q, got none", tt.name, tt.expectedErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.obj, tt.sort) {
|
|
|
|
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v", tt.name, tt.sort, tt.obj)
|
|
|
|
}
|
|
|
|
})
|
2015-08-08 06:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 02:02:28 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|