| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
. "sigs.k8s.io/kustomize/api/types" |
| 10 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 11 |
) |
| 12 |
|
| 13 |
func TestPatchEquals(t *testing.T) { |
| 14 |
selector := Selector{ |
| 15 |
ResId: resid.ResId{ |
| 16 |
Gvk: resid.Gvk{ |
| 17 |
Group: "group", |
| 18 |
Version: "version", |
| 19 |
Kind: "kind", |
| 20 |
}, |
| 21 |
Name: "name", |
| 22 |
Namespace: "namespace", |
| 23 |
}, |
| 24 |
LabelSelector: "selector", |
| 25 |
AnnotationSelector: "selector", |
| 26 |
} |
| 27 |
type testcase struct { |
| 28 |
patch1 Patch |
| 29 |
patch2 Patch |
| 30 |
expect bool |
| 31 |
name string |
| 32 |
} |
| 33 |
testcases := []testcase{ |
| 34 |
{ |
| 35 |
name: "empty patches", |
| 36 |
patch1: Patch{}, |
| 37 |
patch2: Patch{}, |
| 38 |
expect: true, |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "full patches", |
| 42 |
patch1: Patch{ |
| 43 |
Path: "foo", |
| 44 |
Patch: "bar", |
| 45 |
Target: &Selector{ |
| 46 |
ResId: resid.ResId{ |
| 47 |
Gvk: resid.Gvk{ |
| 48 |
Group: "group", |
| 49 |
Version: "version", |
| 50 |
Kind: "kind", |
| 51 |
}, |
| 52 |
Name: "name", |
| 53 |
Namespace: "namespace", |
| 54 |
}, |
| 55 |
LabelSelector: "selector", |
| 56 |
AnnotationSelector: "selector", |
| 57 |
}, |
| 58 |
}, |
| 59 |
patch2: Patch{ |
| 60 |
Path: "foo", |
| 61 |
Patch: "bar", |
| 62 |
Target: &Selector{ |
| 63 |
ResId: resid.ResId{ |
| 64 |
Gvk: resid.Gvk{ |
| 65 |
Group: "group", |
| 66 |
Version: "version", |
| 67 |
Kind: "kind", |
| 68 |
}, |
| 69 |
Name: "name", |
| 70 |
Namespace: "namespace", |
| 71 |
}, |
| 72 |
LabelSelector: "selector", |
| 73 |
AnnotationSelector: "selector", |
| 74 |
}, |
| 75 |
}, |
| 76 |
expect: true, |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: "same target", |
| 80 |
patch1: Patch{ |
| 81 |
Path: "foo", |
| 82 |
Patch: "bar", |
| 83 |
Target: &selector, |
| 84 |
}, |
| 85 |
patch2: Patch{ |
| 86 |
Path: "foo", |
| 87 |
Patch: "bar", |
| 88 |
Target: &selector, |
| 89 |
}, |
| 90 |
expect: true, |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "omit target", |
| 94 |
patch1: Patch{ |
| 95 |
Path: "foo", |
| 96 |
Patch: "bar", |
| 97 |
}, |
| 98 |
patch2: Patch{ |
| 99 |
Path: "foo", |
| 100 |
Patch: "bar", |
| 101 |
}, |
| 102 |
expect: true, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "same options", |
| 106 |
patch1: Patch{ |
| 107 |
Path: "foo", |
| 108 |
Patch: "bar", |
| 109 |
Target: &selector, |
| 110 |
Options: &PatchArgs{ |
| 111 |
AllowNameChange: true, |
| 112 |
AllowKindChange: true, |
| 113 |
}, |
| 114 |
}, |
| 115 |
patch2: Patch{ |
| 116 |
Path: "foo", |
| 117 |
Patch: "bar", |
| 118 |
Target: &selector, |
| 119 |
Options: &PatchArgs{ |
| 120 |
AllowNameChange: true, |
| 121 |
AllowKindChange: true, |
| 122 |
}, |
| 123 |
}, |
| 124 |
expect: true, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "one nil target", |
| 128 |
patch1: Patch{ |
| 129 |
Path: "foo", |
| 130 |
Patch: "bar", |
| 131 |
Target: &selector, |
| 132 |
}, |
| 133 |
patch2: Patch{ |
| 134 |
Path: "foo", |
| 135 |
Patch: "bar", |
| 136 |
}, |
| 137 |
expect: false, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "different path", |
| 141 |
patch1: Patch{ |
| 142 |
Path: "foo", |
| 143 |
}, |
| 144 |
patch2: Patch{ |
| 145 |
Path: "bar", |
| 146 |
}, |
| 147 |
expect: false, |
| 148 |
}, |
| 149 |
{ |
| 150 |
name: "different options", |
| 151 |
patch1: Patch{ |
| 152 |
Path: "foo", |
| 153 |
Patch: "bar", |
| 154 |
Target: &selector, |
| 155 |
Options: &PatchArgs{ |
| 156 |
AllowNameChange: false, |
| 157 |
AllowKindChange: true, |
| 158 |
}, |
| 159 |
}, |
| 160 |
patch2: Patch{ |
| 161 |
Path: "foo", |
| 162 |
Patch: "bar", |
| 163 |
Target: &selector, |
| 164 |
Options: &PatchArgs{ |
| 165 |
AllowNameChange: true, |
| 166 |
AllowKindChange: true, |
| 167 |
}, |
| 168 |
}, |
| 169 |
expect: false, |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
for _, tc := range testcases { |
| 174 |
if tc.expect != tc.patch1.Equals(tc.patch2) { |
| 175 |
t.Fatalf("%s: unexpected result %v", tc.name, !tc.expect) |
| 176 |
} |
| 177 |
} |
| 178 |
} |