Skip to content

Commit ad5695c

Browse files
author
jennybuckley
committed
Add test for a type with known and unknown struct fields
1 parent cd8ff5d commit ad5695c

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

merge/preserve_unknown_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 merge_test
18+
19+
import (
20+
"testing"
21+
22+
"sigs.k8s.io/structured-merge-diff/fieldpath"
23+
. "sigs.k8s.io/structured-merge-diff/internal/fixture"
24+
"sigs.k8s.io/structured-merge-diff/typed"
25+
)
26+
27+
var preserveUnknownParser = func() typed.ParseableType {
28+
parser, err := typed.NewParser(`types:
29+
- name: type
30+
map:
31+
fields:
32+
- name: num
33+
type:
34+
scalar: numeric
35+
elementType:
36+
scalar: string
37+
`)
38+
if err != nil {
39+
panic(err)
40+
}
41+
return parser.Type("type")
42+
}()
43+
44+
func TestPreserveUnknownFields(t *testing.T) {
45+
tests := map[string]TestCase{
46+
"preserve_unknown_fields": {
47+
Ops: []Operation{
48+
Apply{
49+
Manager: "default",
50+
Object: `
51+
num: 5
52+
unknown: value
53+
`,
54+
APIVersion: "v1",
55+
},
56+
Apply{
57+
Manager: "default",
58+
Object: `
59+
num: 6
60+
unknown: new
61+
`,
62+
APIVersion: "v1",
63+
},
64+
},
65+
Object: `
66+
num: 6
67+
unknown: new
68+
`,
69+
Managed: fieldpath.ManagedFields{
70+
"default": &fieldpath.VersionedSet{
71+
Set: _NS(
72+
_P("num"),
73+
_P("unknown"),
74+
),
75+
APIVersion: "v1",
76+
},
77+
},
78+
},
79+
}
80+
81+
for name, test := range tests {
82+
t.Run(name, func(t *testing.T) {
83+
if err := test.Test(preserveUnknownParser); err != nil {
84+
t.Fatal(err)
85+
}
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)