Fix sorting printer with missing fields

This commit is contained in:
Fabiano Franz
2017-01-26 17:26:00 -02:00
parent 0107e93cc6
commit 5eeef81edf
3 changed files with 176 additions and 29 deletions

View File

@@ -18,9 +18,11 @@ package kubectl
import (
"reflect"
"strings"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
internal "k8s.io/kubernetes/pkg/api"
api "k8s.io/kubernetes/pkg/api/v1"
@@ -56,10 +58,11 @@ func TestSortingPrinter(t *testing.T) {
}
tests := []struct {
obj runtime.Object
sort runtime.Object
field string
name string
obj runtime.Object
sort runtime.Object
field string
name string
expectedErr string
}{
{
name: "in-order-already",
@@ -265,12 +268,140 @@ func TestSortingPrinter(t *testing.T) {
},
field: "{.metadata.name}",
},
{
name: "some-missing-fields",
obj: &unstructured.UnstructuredList{
Object: map[string]interface{}{
"kind": "List",
"apiVersion": "v1",
},
Items: []*unstructured.Unstructured{
{
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",
},
Items: []*unstructured.Unstructured{
{
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",
},
Items: []*unstructured.Unstructured{
{
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",
},
}
for _, test := range tests {
sort := &SortingPrinter{SortField: test.field, Decoder: internal.Codecs.UniversalDecoder()}
if err := sort.sortObj(test.obj); err != nil {
t.Errorf("unexpected error: %v (%s)", err, test.name)
continue
err := sort.sortObj(test.obj)
if err != nil {
if len(test.expectedErr) > 0 {
if strings.Contains(err.Error(), test.expectedErr) {
continue
}
t.Fatalf("%s: expected error containing: %q, got: \"%v\"", test.name, test.expectedErr, err)
}
t.Fatalf("%s: unexpected error: %v", test.name, err)
}
if len(test.expectedErr) > 0 {
t.Fatalf("%s: expected error containing: %q, got none", test.name, test.expectedErr)
}
if !reflect.DeepEqual(test.obj, test.sort) {
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v", test.name, test.sort, test.obj)