@@ -6,6 +6,7 @@ package set
66import (
77 "testing"
88
9+ "github.com/stretchr/testify/require"
910 valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
1011 "sigs.k8s.io/kustomize/api/types"
1112 "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -152,3 +153,81 @@ func TestSetLabelExisting(t *testing.T) {
152153 t .Errorf ("unexpected error: %v" , err .Error ())
153154 }
154155}
156+
157+ func TestSetLabelWithoutSelector (t * testing.T ) {
158+ var o setLabelOptions
159+ o .metadata = map [string ]string {"key1" : "foo" , "key2" : "bar" }
160+ o .labelsWithoutSelector = true
161+
162+ m := makeKustomization (t )
163+ require .NoError (t , o .setLabels (m ))
164+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" , "key2" : "bar" }})
165+ }
166+
167+ func TestSetLabelWithoutSelectorWithExistingLabels (t * testing.T ) {
168+ var o setLabelOptions
169+ o .metadata = map [string ]string {"key1" : "foo" , "key2" : "bar" }
170+ o .labelsWithoutSelector = true
171+
172+ m := makeKustomization (t )
173+ require .NoError (t , o .setLabels (m ))
174+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" , "key2" : "bar" }})
175+
176+ o .metadata = map [string ]string {"key3" : "foobar" }
177+ require .NoError (t , o .setLabels (m ))
178+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" , "key2" : "bar" , "key3" : "foobar" }})
179+ }
180+
181+ func TestSetLabelWithoutSelectorWithDuplicateLabel (t * testing.T ) {
182+ var o setLabelOptions
183+ o .metadata = map [string ]string {"key1" : "foo" , "key2" : "bar" }
184+ o .labelsWithoutSelector = true
185+ o .includeTemplates = true
186+
187+ m := makeKustomization (t )
188+ require .NoError (t , o .setLabels (m ))
189+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" , "key2" : "bar" }, IncludeTemplates : true })
190+
191+ o .metadata = map [string ]string {"key2" : "bar" }
192+ o .includeTemplates = false
193+ require .NoError (t , o .setLabels (m ))
194+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" }, IncludeTemplates : true })
195+ require .Equal (t , m .Labels [1 ], types.Label {Pairs : map [string ]string {"key2" : "bar" }})
196+ }
197+
198+ func TestSetLabelWithoutSelectorWithCommonLabel (t * testing.T ) {
199+ var o setLabelOptions
200+ o .metadata = map [string ]string {"key1" : "foo" , "key2" : "bar" }
201+
202+ m := makeKustomization (t )
203+ require .NoError (t , o .setLabels (m ))
204+ require .Empty (t , m .Labels )
205+ //nolint:staticcheck
206+ require .Equal (t , m .CommonLabels , map [string ]string {"app" : "helloworld" , "key1" : "foo" , "key2" : "bar" })
207+
208+ o .metadata = map [string ]string {"key2" : "bar" }
209+ o .labelsWithoutSelector = true
210+ require .NoError (t , o .setLabels (m ))
211+ //nolint:staticcheck
212+ require .Equal (t , m .CommonLabels , map [string ]string {"app" : "helloworld" , "key1" : "foo" })
213+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key2" : "bar" }})
214+ }
215+
216+ func TestSetLabelCommonLabelWithWithoutSelector (t * testing.T ) {
217+ var o setLabelOptions
218+ o .metadata = map [string ]string {"key1" : "foo" , "key2" : "bar" }
219+
220+ m := makeKustomization (t )
221+ o .labelsWithoutSelector = true
222+ require .NoError (t , o .setLabels (m ))
223+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" , "key2" : "bar" }})
224+ //nolint:staticcheck
225+ require .Equal (t , m .CommonLabels , map [string ]string {"app" : "helloworld" })
226+
227+ o .metadata = map [string ]string {"key2" : "bar2" }
228+ o .labelsWithoutSelector = false
229+ require .NoError (t , o .setLabels (m ))
230+ require .Equal (t , m .Labels [0 ], types.Label {Pairs : map [string ]string {"key1" : "foo" }})
231+ //nolint:staticcheck
232+ require .Equal (t , m .CommonLabels , map [string ]string {"app" : "helloworld" , "key2" : "bar2" })
233+ }
0 commit comments