github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/serializer/protobuf/protobuf_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 protobuf 18 19 import ( 20 "bytes" 21 "reflect" 22 "testing" 23 24 metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1" 25 testapigroupv1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/testapigroup/v1" 26 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 27 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 28 runtimetesting "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/testing" 29 ) 30 31 func TestCacheableObject(t *testing.T) { 32 gvk := schema.GroupVersionKind{Group: "group", Version: "version", Kind: "MockCacheableObject"} 33 creater := &mockCreater{obj: &runtimetesting.MockCacheableObject{}} 34 typer := &mockTyper{gvk: &gvk} 35 36 encoders := []runtime.Encoder{ 37 NewSerializer(creater, typer), 38 NewRawSerializer(creater, typer), 39 } 40 41 for _, encoder := range encoders { 42 runtimetesting.CacheableObjectTest(t, encoder) 43 } 44 } 45 46 type mockCreater struct { 47 apiVersion string 48 kind string 49 err error 50 obj runtime.Object 51 } 52 53 func (c *mockCreater) New(kind schema.GroupVersionKind) (runtime.Object, error) { 54 c.apiVersion, c.kind = kind.GroupVersion().String(), kind.Kind 55 return c.obj, c.err 56 } 57 58 type mockTyper struct { 59 gvk *schema.GroupVersionKind 60 err error 61 } 62 63 func (t *mockTyper) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind, bool, error) { 64 if t.gvk == nil { 65 return nil, false, t.err 66 } 67 return []schema.GroupVersionKind{*t.gvk}, false, t.err 68 } 69 70 func (t *mockTyper) Recognizes(_ schema.GroupVersionKind) bool { 71 return false 72 } 73 74 func TestSerializerEncodeWithAllocator(t *testing.T) { 75 testCases := []struct { 76 name string 77 obj runtime.Object 78 }{ 79 { 80 name: "encode a bufferedMarshaller obj", 81 obj: &testapigroupv1.Carp{ 82 TypeMeta: metav1.TypeMeta{APIVersion: "group/version", Kind: "Carp"}, 83 ObjectMeta: metav1.ObjectMeta{ 84 Name: "name", 85 Namespace: "namespace", 86 }, 87 Spec: testapigroupv1.CarpSpec{ 88 Subdomain: "carp.k8s.io", 89 }, 90 }, 91 }, 92 93 { 94 name: "encode a runtime.Unknown obj", 95 obj: &runtime.Unknown{TypeMeta: runtime.TypeMeta{APIVersion: "group/version", Kind: "Unknown"}, Raw: []byte("hello world")}, 96 }, 97 } 98 for _, tc := range testCases { 99 t.Run(tc.name, func(t *testing.T) { 100 target := NewSerializer(nil, nil) 101 102 writer := &bytes.Buffer{} 103 if err := target.Encode(tc.obj, writer); err != nil { 104 t.Fatal(err) 105 } 106 107 writer2 := &bytes.Buffer{} 108 alloc := &testAllocator{} 109 if err := target.EncodeWithAllocator(tc.obj, writer2, alloc); err != nil { 110 t.Fatal(err) 111 } 112 if alloc.allocateCount != 1 { 113 t.Fatalf("expected the Allocate method to be called exactly 1 but it was executed: %v times ", alloc.allocateCount) 114 } 115 116 // to ensure compatibility of the new method with the old one, serialized data must be equal 117 // also we are not testing decoding since "roundtripping" is tested elsewhere for all known types 118 if !reflect.DeepEqual(writer.Bytes(), writer2.Bytes()) { 119 t.Fatal("data mismatch, data serialized with the Encode method is different than serialized with the EncodeWithAllocator method") 120 } 121 }) 122 } 123 } 124 125 func TestRawSerializerEncodeWithAllocator(t *testing.T) { 126 testCases := []struct { 127 name string 128 obj runtime.Object 129 }{ 130 { 131 name: "encode a bufferedReverseMarshaller obj", 132 obj: &testapigroupv1.Carp{ 133 TypeMeta: metav1.TypeMeta{APIVersion: "group/version", Kind: "Carp"}, 134 ObjectMeta: metav1.ObjectMeta{ 135 Name: "name", 136 Namespace: "namespace", 137 }, 138 Spec: testapigroupv1.CarpSpec{ 139 Subdomain: "carp.k8s.io", 140 }, 141 }, 142 }, 143 } 144 for _, tc := range testCases { 145 t.Run(tc.name, func(t *testing.T) { 146 writer := &bytes.Buffer{} 147 target := NewRawSerializer(nil, nil) 148 149 if err := target.Encode(tc.obj, writer); err != nil { 150 t.Fatal(err) 151 } 152 153 writer2 := &bytes.Buffer{} 154 alloc := &testAllocator{} 155 if err := target.EncodeWithAllocator(tc.obj, writer2, alloc); err != nil { 156 t.Fatal(err) 157 } 158 if alloc.allocateCount != 1 { 159 t.Fatalf("expected the Allocate method to be called exactly 1 but it was executed: %v times ", alloc.allocateCount) 160 } 161 162 // to ensure compatibility of the new method with the old one, serialized data must be equal 163 // also we are not testing decoding since "roundtripping" is tested elsewhere for all known types 164 if !reflect.DeepEqual(writer.Bytes(), writer2.Bytes()) { 165 t.Fatal("data mismatch, data serialized with the Encode method is different than serialized with the EncodeWithAllocator method") 166 } 167 }) 168 } 169 } 170 171 type testAllocator struct { 172 buf []byte 173 allocateCount int 174 } 175 176 func (ta *testAllocator) Allocate(n uint64) []byte { 177 ta.buf = make([]byte, n) 178 ta.allocateCount++ 179 return ta.buf 180 }