github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/codec_test.go (about)

     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 runtime_test
    18  
    19  import (
    20  	"io"
    21  	"testing"
    22  
    23  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime"
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  	runtimetesting "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/testing"
    26  )
    27  
    28  func gv(group, version string) schema.GroupVersion {
    29  	return schema.GroupVersion{Group: group, Version: version}
    30  }
    31  func gvk(group, version, kind string) schema.GroupVersionKind {
    32  	return schema.GroupVersionKind{Group: group, Version: version, Kind: kind}
    33  }
    34  func gk(group, kind string) schema.GroupKind {
    35  	return schema.GroupKind{Group: group, Kind: kind}
    36  }
    37  
    38  func TestCoercingMultiGroupVersioner(t *testing.T) {
    39  	testcases := []struct {
    40  		name           string
    41  		target         schema.GroupVersion
    42  		preferredKinds []schema.GroupKind
    43  		kinds          []schema.GroupVersionKind
    44  		expectKind     schema.GroupVersionKind
    45  		expectedId     string
    46  	}{
    47  		{
    48  			name:           "matched preferred group/kind",
    49  			target:         gv("mygroup", "__internal"),
    50  			preferredKinds: []schema.GroupKind{gk("mygroup", "Foo"), gk("anothergroup", "Bar")},
    51  			kinds:          []schema.GroupVersionKind{gvk("yetanother", "v1", "Baz"), gvk("anothergroup", "v1", "Bar")},
    52  			expectKind:     gvk("mygroup", "__internal", "Bar"),
    53  			expectedId:     "{\"accepted\":\"Foo.mygroup,Bar.anothergroup\",\"coerce\":\"true\",\"name\":\"multi\",\"target\":\"mygroup/__internal\"}",
    54  		},
    55  		{
    56  			name:           "matched preferred group",
    57  			target:         gv("mygroup", "__internal"),
    58  			preferredKinds: []schema.GroupKind{gk("mygroup", ""), gk("anothergroup", "")},
    59  			kinds:          []schema.GroupVersionKind{gvk("yetanother", "v1", "Baz"), gvk("anothergroup", "v1", "Bar")},
    60  			expectKind:     gvk("mygroup", "__internal", "Bar"),
    61  			expectedId:     "{\"accepted\":\".mygroup,.anothergroup\",\"coerce\":\"true\",\"name\":\"multi\",\"target\":\"mygroup/__internal\"}",
    62  		},
    63  		{
    64  			name:           "no preferred group/kind match, uses first kind in list",
    65  			target:         gv("mygroup", "__internal"),
    66  			preferredKinds: []schema.GroupKind{gk("mygroup", ""), gk("anothergroup", "")},
    67  			kinds:          []schema.GroupVersionKind{gvk("yetanother", "v1", "Baz"), gvk("yetanother", "v1", "Bar")},
    68  			expectKind:     gvk("mygroup", "__internal", "Baz"),
    69  			expectedId:     "{\"accepted\":\".mygroup,.anothergroup\",\"coerce\":\"true\",\"name\":\"multi\",\"target\":\"mygroup/__internal\"}",
    70  		},
    71  	}
    72  
    73  	for _, tc := range testcases {
    74  		t.Run(tc.name, func(t *testing.T) {
    75  			v := runtime.NewCoercingMultiGroupVersioner(tc.target, tc.preferredKinds...)
    76  			kind, ok := v.KindForGroupVersionKinds(tc.kinds)
    77  			if !ok {
    78  				t.Error("got no kind")
    79  			}
    80  			if kind != tc.expectKind {
    81  				t.Errorf("expected %#v, got %#v", tc.expectKind, kind)
    82  			}
    83  			if e, a := tc.expectedId, v.Identifier(); e != a {
    84  				t.Errorf("unexpected identifier: %s, expected: %s", a, e)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  type mockEncoder struct{}
    91  
    92  func (m *mockEncoder) Encode(obj runtime.Object, w io.Writer) error {
    93  	_, err := w.Write([]byte("mock-result"))
    94  	return err
    95  }
    96  
    97  func (m *mockEncoder) Identifier() runtime.Identifier {
    98  	return runtime.Identifier("mock-identifier")
    99  }
   100  
   101  func TestCacheableObject(t *testing.T) {
   102  	serializer := runtime.NewBase64Serializer(&mockEncoder{}, nil)
   103  	runtimetesting.CacheableObjectTest(t, serializer)
   104  }