test: improve TestLabelsApply

To clarify the spec of ApplyToObjectMeta This also fixes the misleading
comments on the method.

Signed-off-by: Yusei Nishiyama <yusei.nishiyama@soundcloud.com>
This commit is contained in:
Yusei Nishiyama committed 2020-10-30 13:40:42 +01:00
1 parent 1c562512e1
commit 2967bceca8
3 files changed
+51 -11

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@ func (a AnnotationsSpec) All() Annotations {
return a[KeyAll]
}
// ApplyToObjectMeta adds or overwrites if exists annotations to object meta.
// ApplyToObjectMeta adds annotations to object meta unless the keys are already defined.
func (a Annotations) ApplyToObjectMeta(t *metav1.ObjectMeta) {
if t.Annotations == nil {
t.Annotations = map[string]string{}
+1 -1
View File
@@ -24,7 +24,7 @@ func (a LabelsSpec) All() Labels {
return a[KeyAll]
}
// ApplyToObjectMeta adds or overwrites if exists Labels to object meta.
// ApplyToObjectMeta adds labels to object meta unless the keys are already defined.
func (a Labels) ApplyToObjectMeta(t *metav1.ObjectMeta) {
if t.Labels == nil {
t.Labels = map[string]string{}
+49 -9
View File
@@ -54,17 +54,57 @@ mon:
}
func TestLabelsApply(t *testing.T) {
objMeta := &metav1.ObjectMeta{}
testLabels := Labels{
"foo": "bar",
"hello": "world",
tcs := []struct {
name string
target *metav1.ObjectMeta
input Labels
expected Labels
}{
{
name: "it should be able to update meta with no label",
target: &metav1.ObjectMeta{},
input: Labels{
"foo": "bar",
},
expected: Labels{
"foo": "bar",
},
},
{
name: "it should keep the original labels when new labels are set",
target: &metav1.ObjectMeta{
Labels: Labels{
"foo": "bar",
},
},
input: Labels{
"hello": "world",
},
expected: Labels{
"foo": "bar",
"hello": "world",
},
},
{
name: "it should NOT overwrite the existing keys",
target: &metav1.ObjectMeta{
Labels: Labels{
"foo": "bar",
},
},
input: Labels{
"foo": "baz",
},
expected: Labels{
"foo": "bar",
},
},
}
testLabels.ApplyToObjectMeta(objMeta)
assert.Equal(t, testLabels.getMapStringString(), objMeta.Labels)
testLabels["isthisatest"] = "test"
testLabels.ApplyToObjectMeta(objMeta)
assert.Equal(t, testLabels.getMapStringString(), objMeta.Labels)
for _, tc := range tcs {
tc.input.ApplyToObjectMeta(tc.target)
assert.Equal(t, tc.expected.getMapStringString(), tc.target.Labels)
}
}
func TestLabelsMerge(t *testing.T) {