| 1 |
|
| 2 |
|
| 3 |
package merge2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 10 |
) |
| 11 |
|
| 12 |
func Test_determineSmpDirective(t *testing.T) { |
| 13 |
var cases = map[string]struct { |
| 14 |
patch string |
| 15 |
elided string |
| 16 |
expected smpDirective |
| 17 |
errExpected string |
| 18 |
}{ |
| 19 |
`scalar`: { |
| 20 |
patch: "dumb", |
| 21 |
expected: smpMerge, |
| 22 |
errExpected: "no implemented strategic merge patch strategy", |
| 23 |
}, |
| 24 |
`list merge`: { |
| 25 |
patch: ` |
| 26 |
- one |
| 27 |
- two |
| 28 |
- three |
| 29 |
- $patch: merge |
| 30 |
`, |
| 31 |
expected: smpMerge, |
| 32 |
elided: `- one |
| 33 |
- two |
| 34 |
- three |
| 35 |
`, |
| 36 |
}, |
| 37 |
"list replace": { |
| 38 |
patch: ` |
| 39 |
- one |
| 40 |
- two |
| 41 |
- three |
| 42 |
- $patch: replace |
| 43 |
`, |
| 44 |
expected: smpReplace, |
| 45 |
elided: `- one |
| 46 |
- two |
| 47 |
- three |
| 48 |
`, |
| 49 |
}, |
| 50 |
"list delete": { |
| 51 |
patch: ` |
| 52 |
- one |
| 53 |
- two |
| 54 |
- three |
| 55 |
- $patch: delete |
| 56 |
`, |
| 57 |
expected: smpDelete, |
| 58 |
elided: `- one |
| 59 |
- two |
| 60 |
- three |
| 61 |
`, |
| 62 |
}, |
| 63 |
"list default": { |
| 64 |
patch: ` |
| 65 |
- one |
| 66 |
- two |
| 67 |
- three |
| 68 |
`, |
| 69 |
expected: smpMerge, |
| 70 |
elided: `- one |
| 71 |
- two |
| 72 |
- three |
| 73 |
`, |
| 74 |
}, |
| 75 |
`map replace`: { |
| 76 |
patch: ` |
| 77 |
metal: heavy |
| 78 |
$patch: replace |
| 79 |
veggie: carrot |
| 80 |
`, |
| 81 |
expected: smpReplace, |
| 82 |
elided: `metal: heavy |
| 83 |
veggie: carrot |
| 84 |
`, |
| 85 |
}, |
| 86 |
`map delete`: { |
| 87 |
patch: ` |
| 88 |
metal: heavy |
| 89 |
$patch: delete |
| 90 |
veggie: carrot |
| 91 |
`, |
| 92 |
expected: smpDelete, |
| 93 |
elided: `metal: heavy |
| 94 |
veggie: carrot |
| 95 |
`, |
| 96 |
}, |
| 97 |
`map merge`: { |
| 98 |
patch: ` |
| 99 |
metal: heavy |
| 100 |
$patch: merge |
| 101 |
veggie: carrot |
| 102 |
`, |
| 103 |
expected: smpMerge, |
| 104 |
elided: `metal: heavy |
| 105 |
veggie: carrot |
| 106 |
`, |
| 107 |
}, |
| 108 |
`map default`: { |
| 109 |
patch: ` |
| 110 |
metal: heavy |
| 111 |
veggie: carrot |
| 112 |
`, |
| 113 |
expected: smpMerge, |
| 114 |
elided: `metal: heavy |
| 115 |
veggie: carrot |
| 116 |
`, |
| 117 |
}, |
| 118 |
} |
| 119 |
|
| 120 |
for n := range cases { |
| 121 |
tc := cases[n] |
| 122 |
t.Run(n, func(t *testing.T) { |
| 123 |
p, err := yaml.Parse(tc.patch) |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("unexpected parse err %v", err) |
| 126 |
} |
| 127 |
unwrapped := yaml.NewRNode(p.YNode()) |
| 128 |
actual, err := determineSmpDirective(unwrapped) |
| 129 |
if err == nil { |
| 130 |
if tc.errExpected != "" { |
| 131 |
t.Fatalf("should have seen an error") |
| 132 |
} |
| 133 |
if tc.expected != actual { |
| 134 |
t.Fatalf("expected %s, got %s", tc.expected, actual) |
| 135 |
} |
| 136 |
if tc.elided != unwrapped.MustString() { |
| 137 |
t.Fatalf( |
| 138 |
"expected %s, got %s", |
| 139 |
tc.elided, unwrapped.MustString()) |
| 140 |
} |
| 141 |
} else { |
| 142 |
if tc.errExpected == "" { |
| 143 |
t.Fatalf("unexpected err: %v", err) |
| 144 |
} |
| 145 |
if !strings.Contains(err.Error(), tc.errExpected) { |
| 146 |
t.Fatalf("expected some error other than: %v", err) |
| 147 |
} |
| 148 |
} |
| 149 |
}) |
| 150 |
} |
| 151 |
} |