|
| 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/merge" |
| 25 | +) |
| 26 | + |
| 27 | +func TestMultipleAppliersSet(t *testing.T) { |
| 28 | + tests := map[string]TestCase{ |
| 29 | + "remove_one": { |
| 30 | + Ops: []Operation{ |
| 31 | + Apply{ |
| 32 | + Manager: "apply-one", |
| 33 | + APIVersion: "v1", |
| 34 | + Object: ` |
| 35 | + list: |
| 36 | + - name: a |
| 37 | + - name: b |
| 38 | + `, |
| 39 | + }, |
| 40 | + Apply{ |
| 41 | + Manager: "apply-two", |
| 42 | + APIVersion: "v2", |
| 43 | + Object: ` |
| 44 | + list: |
| 45 | + - name: c |
| 46 | + `, |
| 47 | + }, |
| 48 | + Apply{ |
| 49 | + Manager: "apply-one", |
| 50 | + APIVersion: "v3", |
| 51 | + Object: ` |
| 52 | + list: |
| 53 | + - name: a |
| 54 | + `, |
| 55 | + }, |
| 56 | + }, |
| 57 | + Object: ` |
| 58 | + list: |
| 59 | + - name: a |
| 60 | + - name: c |
| 61 | + `, |
| 62 | + Managed: fieldpath.ManagedFields{ |
| 63 | + "apply-one": &fieldpath.VersionedSet{ |
| 64 | + Set: _NS( |
| 65 | + _P("list", _KBF("name", _SV("a")), "name"), |
| 66 | + ), |
| 67 | + APIVersion: "v3", |
| 68 | + }, |
| 69 | + "apply-two": &fieldpath.VersionedSet{ |
| 70 | + Set: _NS( |
| 71 | + _P("list", _KBF("name", _SV("c")), "name"), |
| 72 | + ), |
| 73 | + APIVersion: "v2", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + "same_value_no_conflict": { |
| 78 | + Ops: []Operation{ |
| 79 | + Apply{ |
| 80 | + Manager: "apply-one", |
| 81 | + APIVersion: "v1", |
| 82 | + Object: ` |
| 83 | + list: |
| 84 | + - name: a |
| 85 | + value: 0 |
| 86 | + `, |
| 87 | + }, |
| 88 | + Apply{ |
| 89 | + Manager: "apply-two", |
| 90 | + APIVersion: "v2", |
| 91 | + Object: ` |
| 92 | + list: |
| 93 | + - name: a |
| 94 | + value: 0 |
| 95 | + `, |
| 96 | + }, |
| 97 | + }, |
| 98 | + Object: ` |
| 99 | + list: |
| 100 | + - name: a |
| 101 | + value: 0 |
| 102 | + `, |
| 103 | + Managed: fieldpath.ManagedFields{ |
| 104 | + "apply-one": &fieldpath.VersionedSet{ |
| 105 | + Set: _NS( |
| 106 | + _P("list", _KBF("name", _SV("a")), "name"), |
| 107 | + _P("list", _KBF("name", _SV("a")), "value"), |
| 108 | + ), |
| 109 | + APIVersion: "v1", |
| 110 | + }, |
| 111 | + "apply-two": &fieldpath.VersionedSet{ |
| 112 | + Set: _NS( |
| 113 | + _P("list", _KBF("name", _SV("a")), "name"), |
| 114 | + _P("list", _KBF("name", _SV("a")), "value"), |
| 115 | + ), |
| 116 | + APIVersion: "v2", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + "change_value_yes_conflict": { |
| 121 | + Ops: []Operation{ |
| 122 | + Apply{ |
| 123 | + Manager: "apply-one", |
| 124 | + APIVersion: "v1", |
| 125 | + Object: ` |
| 126 | + list: |
| 127 | + - name: a |
| 128 | + value: 0 |
| 129 | + `, |
| 130 | + }, |
| 131 | + Apply{ |
| 132 | + Manager: "apply-two", |
| 133 | + APIVersion: "v2", |
| 134 | + Object: ` |
| 135 | + list: |
| 136 | + - name: a |
| 137 | + value: 1 |
| 138 | + `, |
| 139 | + Conflicts: merge.Conflicts{ |
| 140 | + merge.Conflict{Manager: "apply-one", Path: _P("list", _KBF("name", _SV("a")), "value")}, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + Object: ` |
| 145 | + list: |
| 146 | + - name: a |
| 147 | + value: 0 |
| 148 | + `, |
| 149 | + Managed: fieldpath.ManagedFields{ |
| 150 | + "apply-one": &fieldpath.VersionedSet{ |
| 151 | + Set: _NS( |
| 152 | + _P("list", _KBF("name", _SV("a")), "name"), |
| 153 | + _P("list", _KBF("name", _SV("a")), "value"), |
| 154 | + ), |
| 155 | + APIVersion: "v1", |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for name, test := range tests { |
| 162 | + t.Run(name, func(t *testing.T) { |
| 163 | + if err := test.Test(associativeListParser); err != nil { |
| 164 | + t.Fatal(err) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestMultipleAppliersSetBroken(t *testing.T) { |
| 171 | + tests := map[string]TestCase{ |
| 172 | + "remove_one_keep_one": { |
| 173 | + Ops: []Operation{ |
| 174 | + Apply{ |
| 175 | + Manager: "apply-one", |
| 176 | + APIVersion: "v1", |
| 177 | + Object: ` |
| 178 | + list: |
| 179 | + - name: a |
| 180 | + - name: b |
| 181 | + - name: c |
| 182 | + `, |
| 183 | + }, |
| 184 | + Apply{ |
| 185 | + Manager: "apply-two", |
| 186 | + APIVersion: "v2", |
| 187 | + Object: ` |
| 188 | + list: |
| 189 | + - name: c |
| 190 | + - name: d |
| 191 | + `, |
| 192 | + }, |
| 193 | + Apply{ |
| 194 | + Manager: "apply-one", |
| 195 | + APIVersion: "v3", |
| 196 | + Object: ` |
| 197 | + list: |
| 198 | + - name: a |
| 199 | + `, |
| 200 | + }, |
| 201 | + }, |
| 202 | + Object: ` |
| 203 | + list: |
| 204 | + - name: a |
| 205 | + - name: c |
| 206 | + - name: d |
| 207 | + `, |
| 208 | + Managed: fieldpath.ManagedFields{ |
| 209 | + "apply-one": &fieldpath.VersionedSet{ |
| 210 | + Set: _NS( |
| 211 | + _P("list", _KBF("name", _SV("a")), "name"), |
| 212 | + ), |
| 213 | + APIVersion: "v3", |
| 214 | + }, |
| 215 | + "apply-two": &fieldpath.VersionedSet{ |
| 216 | + Set: _NS( |
| 217 | + _P("list", _KBF("name", _SV("c")), "name"), |
| 218 | + _P("list", _KBF("name", _SV("d")), "name"), |
| 219 | + ), |
| 220 | + APIVersion: "v2", |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + for name, test := range tests { |
| 227 | + t.Run(name, func(t *testing.T) { |
| 228 | + if test.Test(associativeListParser) == nil { |
| 229 | + t.Fatal("Broken test passed") |
| 230 | + } |
| 231 | + }) |
| 232 | + } |
| 233 | +} |
0 commit comments