...

Source file src/k8s.io/cli-runtime/pkg/resource/scheme_test.go

Documentation: k8s.io/cli-runtime/pkg/resource

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  )
    29  
    30  func gvk(group, version, kind string) *schema.GroupVersionKind {
    31  	return &schema.GroupVersionKind{Group: group, Version: version, Kind: kind}
    32  }
    33  
    34  func TestDynamicCodecDecode(t *testing.T) {
    35  	testcases := []struct {
    36  		name string
    37  		data []byte
    38  		gvk  *schema.GroupVersionKind
    39  		obj  runtime.Object
    40  
    41  		expectErr string
    42  		expectGVK *schema.GroupVersionKind
    43  		expectObj runtime.Object
    44  	}{
    45  		{
    46  			name:      "v1 Status",
    47  			data:      []byte(`{"apiVersion":"v1","kind":"Status"}`),
    48  			expectGVK: gvk("", "v1", "Status"),
    49  			expectObj: &metav1.Status{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Status"}},
    50  		},
    51  		{
    52  			name:      "meta.k8s.io/v1 Status",
    53  			data:      []byte(`{"apiVersion":"meta.k8s.io/v1","kind":"Status"}`),
    54  			expectGVK: gvk("meta.k8s.io", "v1", "Status"),
    55  			expectObj: &metav1.Status{TypeMeta: metav1.TypeMeta{APIVersion: "meta.k8s.io/v1", Kind: "Status"}},
    56  		},
    57  		{
    58  			name:      "example.com/v1 Status",
    59  			data:      []byte(`{"apiVersion":"example.com/v1","kind":"Status"}`),
    60  			expectGVK: gvk("example.com", "v1", "Status"),
    61  			expectObj: &unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "example.com/v1", "kind": "Status"}},
    62  		},
    63  		{
    64  			name:      "example.com/v1 Foo",
    65  			data:      []byte(`{"apiVersion":"example.com/v1","kind":"Foo"}`),
    66  			expectGVK: gvk("example.com", "v1", "Foo"),
    67  			expectObj: &unstructured.Unstructured{Object: map[string]interface{}{"apiVersion": "example.com/v1", "kind": "Foo"}},
    68  		},
    69  	}
    70  
    71  	for _, test := range testcases {
    72  		t.Run(test.name, func(t *testing.T) {
    73  			obj, gvk, err := dynamicCodec{}.Decode(test.data, test.gvk, test.obj)
    74  			if (err == nil) != (test.expectErr == "") {
    75  				t.Fatalf("expected err=%v, got %v", test.expectErr, err)
    76  			}
    77  			if err != nil && !strings.Contains(err.Error(), test.expectErr) {
    78  				t.Fatalf("expected err=%v, got %v", test.expectErr, err)
    79  			}
    80  			if err != nil {
    81  				return
    82  			}
    83  
    84  			if !reflect.DeepEqual(test.expectGVK, gvk) {
    85  				t.Errorf("expected\n\tgvk=%#v\ngot\n\t%#v", test.expectGVK, gvk)
    86  			}
    87  			if !reflect.DeepEqual(test.expectObj, obj) {
    88  				t.Errorf("expected\n\t%#v\n\t%#v", test.expectObj, obj)
    89  			}
    90  		})
    91  	}
    92  }
    93  

View as plain text