|
| 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 associativeListParser = func() typed.ParseableType { |
| 28 | + parser, err := typed.NewParser(`types: |
| 29 | +- name: type |
| 30 | + struct: |
| 31 | + fields: |
| 32 | + - name: list |
| 33 | + type: |
| 34 | + namedType: associativeList |
| 35 | +- name: associativeList |
| 36 | + list: |
| 37 | + elementType: |
| 38 | + namedType: myElement |
| 39 | + elementRelationship: associative |
| 40 | + keys: |
| 41 | + - name |
| 42 | +- name: myElement |
| 43 | + struct: |
| 44 | + fields: |
| 45 | + - name: name |
| 46 | + type: |
| 47 | + scalar: string |
| 48 | + - name: value |
| 49 | + type: |
| 50 | + scalar: numeric |
| 51 | +`) |
| 52 | + if err != nil { |
| 53 | + panic(err) |
| 54 | + } |
| 55 | + return parser.Type("type") |
| 56 | +}() |
| 57 | + |
| 58 | +func TestUpdateAssociativeLists(t *testing.T) { |
| 59 | + tests := map[string]TestCase{ |
| 60 | + "removing_obsolete_applied_structs": { |
| 61 | + Ops: []Operation{ |
| 62 | + Apply{ |
| 63 | + Manager: "default", |
| 64 | + Object: ` |
| 65 | + list: |
| 66 | + - name: a |
| 67 | + value: 1 |
| 68 | + `, |
| 69 | + APIVersion: "v1", |
| 70 | + }, |
| 71 | + Apply{ |
| 72 | + Manager: "default", |
| 73 | + Object: ` |
| 74 | + list: |
| 75 | + - name: b |
| 76 | + value: 2 |
| 77 | + `, |
| 78 | + APIVersion: "v1", |
| 79 | + }, |
| 80 | + }, |
| 81 | + Object: ` |
| 82 | + list: |
| 83 | + - name: b |
| 84 | + value: 2 |
| 85 | + `, |
| 86 | + Managed: fieldpath.ManagedFields{ |
| 87 | + "default": &fieldpath.VersionedSet{ |
| 88 | + Set: _NS( |
| 89 | + _P("list", _KBF("name", _SV("b")), "name"), |
| 90 | + _P("list", _KBF("name", _SV("b")), "value"), |
| 91 | + ), |
| 92 | + APIVersion: "v1", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for name, test := range tests { |
| 99 | + t.Run(name, func(t *testing.T) { |
| 100 | + if err := test.Test(associativeListParser); err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments