44package set
55
66import (
7+ "reflect"
78 "testing"
89
910 valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
@@ -28,26 +29,92 @@ func makeKustomization(t *testing.T) *types.Kustomization {
2829 return m
2930}
3031
31- func TestRunSetLabel (t * testing.T ) {
32+ func TestRunSetLabels (t * testing.T ) {
3233 var o setLabelOptions
34+ o .includeSelectors = true
3335 o .metadata = map [string ]string {"owls" : "cute" , "otters" : "adorable" }
3436
3537 m := makeKustomization (t )
3638 err := o .setLabels (m )
3739 if err != nil {
3840 t .Errorf ("unexpected error: could not write to kustomization file" )
3941 }
42+
43+ // assert content
44+ expectedContent := map [string ]string {"app" : "helloworld" , "owls" : "cute" , "otters" : "adorable" }
45+ if ! reflect .DeepEqual (m .CommonLabels , expectedContent ) {
46+ t .Log ("m.CommonLabels" , m .CommonLabels )
47+ t .Log ("expectedContent" , expectedContent )
48+ t .Errorf ("commonLabels does not contain expected content" )
49+ }
50+
51+ // adding the same test input should work
52+ err = o .setLabels (m )
53+ if err != nil {
54+ t .Errorf ("unexpected error: could not write to kustomization file" )
55+ }
56+ // adding new labels should work
57+ o .metadata = map [string ]string {"new" : "label" , "owls" : "not cute" }
58+ err = o .setLabels (m )
59+ if err != nil {
60+ t .Errorf ("unexpected error: could not write to kustomization file" )
61+ }
62+ }
63+
64+ func TestRunSetLabelsNoSelector (t * testing.T ) {
65+ var o setLabelOptions
66+ o .includeSelectors = false
67+ o .metadata = map [string ]string {"owls" : "cute" , "otters" : "adorable" }
68+
69+ m := makeKustomization (t )
70+ err := o .setLabels (m )
71+ if err != nil {
72+ t .Errorf ("unexpected error: could not write to kustomization file" )
73+ }
74+
75+ // assert content
76+ expectedContent := make ([]types.Label , 2 )
77+ expectedContent [0 ] = types.Label {Pairs : map [string ]string {"owls" : "cute" }, IncludeSelectors : false }
78+ expectedContent [1 ] = types.Label {Pairs : map [string ]string {"otters" : "adorable" }, IncludeSelectors : false }
79+ if ! reflect .DeepEqual (m .Labels , expectedContent ) {
80+ t .Log ("m.Labels" , m .Labels )
81+ t .Log ("expectedContent" , expectedContent )
82+ t .Errorf ("labels does not contain expected content" )
83+ }
84+
4085 // adding the same test input should work
4186 err = o .setLabels (m )
4287 if err != nil {
4388 t .Errorf ("unexpected error: could not write to kustomization file" )
4489 }
90+
91+ // assert content
92+ expectedContent2 := make ([]types.Label , 2 )
93+ expectedContent2 [0 ] = types.Label {Pairs : map [string ]string {"owls" : "cute" }, IncludeSelectors : false }
94+ expectedContent2 [1 ] = types.Label {Pairs : map [string ]string {"otters" : "adorable" }, IncludeSelectors : false }
95+ if ! reflect .DeepEqual (m .Labels , expectedContent2 ) {
96+ t .Log ("m.Labels" , m .Labels )
97+ t .Log ("expectedContent" , expectedContent2 )
98+ t .Errorf ("labels does not contain expected content" )
99+ }
100+
45101 // adding new labels should work
46102 o .metadata = map [string ]string {"new" : "label" , "owls" : "not cute" }
47103 err = o .setLabels (m )
48104 if err != nil {
49105 t .Errorf ("unexpected error: could not write to kustomization file" )
50106 }
107+
108+ // assert content
109+ expectedContent3 := make ([]types.Label , 3 )
110+ expectedContent3 [0 ] = types.Label {Pairs : map [string ]string {"owls" : "not cute" }, IncludeSelectors : false }
111+ expectedContent3 [1 ] = types.Label {Pairs : map [string ]string {"otters" : "adorable" }, IncludeSelectors : false }
112+ expectedContent3 [2 ] = types.Label {Pairs : map [string ]string {"new" : "label" }, IncludeSelectors : false }
113+ if ! reflect .DeepEqual (m .Labels , expectedContent3 ) {
114+ t .Log ("m.Labels" , m .Labels )
115+ t .Log ("expectedContent" , expectedContent3 )
116+ t .Errorf ("labels does not contain expected content" )
117+ }
51118}
52119
53120func TestSetLabelNoArgs (t * testing.T ) {
0 commit comments