|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + "encoding/base64" |
| 22 | + "io" |
| 23 | + "os/exec" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | + |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + testapigroupv1 "k8s.io/apimachinery/pkg/apis/testapigroup/v1" |
| 30 | + "k8s.io/apimachinery/pkg/runtime" |
| 31 | +) |
| 32 | + |
| 33 | +func TestCollectionsEncoding(t *testing.T) { |
| 34 | + t.Run("Normal", func(t *testing.T) { |
| 35 | + testCollectionsEncoding(t, NewSerializer(nil, nil)) |
| 36 | + }) |
| 37 | + // Leave place for testing streaming collection serializer proposed as part of KEP-5116 |
| 38 | +} |
| 39 | + |
| 40 | +func testCollectionsEncoding(t *testing.T, s *Serializer) { |
| 41 | + var remainingItems int64 = 1 |
| 42 | + testCases := []struct { |
| 43 | + name string |
| 44 | + in runtime.Object |
| 45 | + // expect is base64 encoded protobuf bytes |
| 46 | + expect string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "CarpList items nil", |
| 50 | + in: &testapigroupv1.CarpList{ |
| 51 | + Items: nil, |
| 52 | + }, |
| 53 | + expect: "azhzAAoECgASABIICgYKABIAGgAaACIA", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "CarpList slice nil", |
| 57 | + in: &testapigroupv1.CarpList{ |
| 58 | + Items: []testapigroupv1.Carp{ |
| 59 | + { |
| 60 | + Status: testapigroupv1.CarpStatus{ |
| 61 | + Conditions: nil, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + expect: "azhzAAoECgASABJBCgYKABIAGgASNwoQCgASABoAIgAqADIAOABCABIXGgBCAEoAUgBYAGAAaACCAQCKAQCaAQAaCgoAGgAiACoAMgAaACIA", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "CarpList map nil", |
| 70 | + in: &testapigroupv1.CarpList{ |
| 71 | + Items: []testapigroupv1.Carp{ |
| 72 | + { |
| 73 | + Spec: testapigroupv1.CarpSpec{ |
| 74 | + NodeSelector: nil, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + expect: "azhzAAoECgASABJBCgYKABIAGgASNwoQCgASABoAIgAqADIAOABCABIXGgBCAEoAUgBYAGAAaACCAQCKAQCaAQAaCgoAGgAiACoAMgAaACIA", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "CarpList items empty", |
| 83 | + in: &testapigroupv1.CarpList{ |
| 84 | + Items: []testapigroupv1.Carp{}, |
| 85 | + }, |
| 86 | + expect: "azhzAAoECgASABIICgYKABIAGgAaACIA", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "CarpList slice empty", |
| 90 | + in: &testapigroupv1.CarpList{ |
| 91 | + Items: []testapigroupv1.Carp{ |
| 92 | + { |
| 93 | + Status: testapigroupv1.CarpStatus{ |
| 94 | + Conditions: []testapigroupv1.CarpCondition{}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + expect: "azhzAAoECgASABJBCgYKABIAGgASNwoQCgASABoAIgAqADIAOABCABIXGgBCAEoAUgBYAGAAaACCAQCKAQCaAQAaCgoAGgAiACoAMgAaACIA", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "CarpList map empty", |
| 103 | + in: &testapigroupv1.CarpList{ |
| 104 | + Items: []testapigroupv1.Carp{ |
| 105 | + { |
| 106 | + Spec: testapigroupv1.CarpSpec{ |
| 107 | + NodeSelector: map[string]string{}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + expect: "azhzAAoECgASABJBCgYKABIAGgASNwoQCgASABoAIgAqADIAOABCABIXGgBCAEoAUgBYAGAAaACCAQCKAQCaAQAaCgoAGgAiACoAMgAaACIA", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "List just kind", |
| 116 | + in: &testapigroupv1.CarpList{ |
| 117 | + TypeMeta: metav1.TypeMeta{ |
| 118 | + Kind: "List", |
| 119 | + }, |
| 120 | + }, |
| 121 | + expect: "azhzAAoICgASBExpc3QSCAoGCgASABoAGgAiAA==", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "List just apiVersion", |
| 125 | + in: &testapigroupv1.CarpList{ |
| 126 | + TypeMeta: metav1.TypeMeta{ |
| 127 | + APIVersion: "v1", |
| 128 | + }, |
| 129 | + }, |
| 130 | + expect: "azhzAAoGCgJ2MRIAEggKBgoAEgAaABoAIgA=", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "List no elements", |
| 134 | + in: &testapigroupv1.CarpList{ |
| 135 | + TypeMeta: metav1.TypeMeta{ |
| 136 | + Kind: "List", |
| 137 | + APIVersion: "v1", |
| 138 | + }, |
| 139 | + ListMeta: metav1.ListMeta{ |
| 140 | + ResourceVersion: "2345", |
| 141 | + }, |
| 142 | + Items: []testapigroupv1.Carp{}, |
| 143 | + }, |
| 144 | + expect: "azhzAAoKCgJ2MRIETGlzdBIMCgoKABIEMjM0NRoAGgAiAA==", |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "List one element with continue", |
| 148 | + in: &testapigroupv1.CarpList{ |
| 149 | + TypeMeta: metav1.TypeMeta{ |
| 150 | + Kind: "List", |
| 151 | + APIVersion: "v1", |
| 152 | + }, |
| 153 | + ListMeta: metav1.ListMeta{ |
| 154 | + ResourceVersion: "2345", |
| 155 | + Continue: "abc", |
| 156 | + RemainingItemCount: &remainingItems, |
| 157 | + }, |
| 158 | + Items: []testapigroupv1.Carp{ |
| 159 | + {TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Carp"}, ObjectMeta: metav1.ObjectMeta{ |
| 160 | + Name: "pod", |
| 161 | + Namespace: "default", |
| 162 | + }}, |
| 163 | + }, |
| 164 | + }, |
| 165 | + expect: "azhzAAoKCgJ2MRIETGlzdBJUCg8KABIEMjM0NRoDYWJjIAESQQoaCgNwb2QSABoHZGVmYXVsdCIAKgAyADgAQgASFxoAQgBKAFIAWABgAGgAggEAigEAmgEAGgoKABoAIgAqADIAGgAiAA==", |
| 166 | + }, |
| 167 | + { |
| 168 | + name: "List two elements", |
| 169 | + in: &testapigroupv1.CarpList{ |
| 170 | + TypeMeta: metav1.TypeMeta{ |
| 171 | + Kind: "List", |
| 172 | + APIVersion: "v1", |
| 173 | + }, |
| 174 | + ListMeta: metav1.ListMeta{ |
| 175 | + ResourceVersion: "2345", |
| 176 | + }, |
| 177 | + Items: []testapigroupv1.Carp{ |
| 178 | + {TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Carp"}, ObjectMeta: metav1.ObjectMeta{ |
| 179 | + Name: "pod", |
| 180 | + Namespace: "default", |
| 181 | + }}, |
| 182 | + {TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Carp"}, ObjectMeta: metav1.ObjectMeta{ |
| 183 | + Name: "pod2", |
| 184 | + Namespace: "default2", |
| 185 | + }}, |
| 186 | + }, |
| 187 | + }, |
| 188 | + expect: "azhzAAoKCgJ2MRIETGlzdBKUAQoKCgASBDIzNDUaABJBChoKA3BvZBIAGgdkZWZhdWx0IgAqADIAOABCABIXGgBCAEoAUgBYAGAAaACCAQCKAQCaAQAaCgoAGgAiACoAMgASQwocCgRwb2QyEgAaCGRlZmF1bHQyIgAqADIAOABCABIXGgBCAEoAUgBYAGAAaACCAQCKAQCaAQAaCgoAGgAiACoAMgAaACIA", |
| 189 | + }, |
| 190 | + } |
| 191 | + |
| 192 | + for _, tc := range testCases { |
| 193 | + t.Run(tc.name, func(t *testing.T) { |
| 194 | + var buf bytes.Buffer |
| 195 | + if err := s.Encode(tc.in, &buf); err != nil { |
| 196 | + t.Fatalf("unexpected error: %v", err) |
| 197 | + } |
| 198 | + actualBytes := buf.Bytes() |
| 199 | + expectBytes, err := io.ReadAll(base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(tc.expect))) |
| 200 | + if err != nil { |
| 201 | + t.Fatal(err) |
| 202 | + } |
| 203 | + if !bytes.Equal(expectBytes, actualBytes) { |
| 204 | + t.Errorf("expected:\n%s\ngot:\n%s", tc.expect, base64.StdEncoding.EncodeToString(actualBytes)) |
| 205 | + t.Log(cmp.Diff(dumpProto(t, actualBytes[4:]), dumpProto(t, expectBytes[4:]))) |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +// dumpProto does a best-effort dump of the given proto bytes using protoc if it can be found in the path. |
| 212 | +// This is only used when the test has already failed, to try to give more visibility into the diff of the failure. |
| 213 | +func dumpProto(t *testing.T, data []byte) string { |
| 214 | + t.Helper() |
| 215 | + protoc, err := exec.LookPath("protoc") |
| 216 | + if err != nil { |
| 217 | + t.Logf("cannot find protoc in path to dump proto contents: %v", err) |
| 218 | + return "" |
| 219 | + } |
| 220 | + cmd := exec.Command(protoc, "--decode_raw") |
| 221 | + cmd.Stdin = bytes.NewBuffer(data) |
| 222 | + d, err := cmd.CombinedOutput() |
| 223 | + if err != nil { |
| 224 | + t.Logf("protoc invocation failed: %v", err) |
| 225 | + return "" |
| 226 | + } |
| 227 | + return string(d) |
| 228 | +} |
0 commit comments