Skip to content

Commit 07658b7

Browse files
committed
test: add test to ensure commonLabels do not overwrite standard labels
1 parent 0f09e3e commit 07658b7

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

internal/controller/operator/factory/build/defaults_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
corev1 "k8s.io/api/core/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
79
"k8s.io/utils/ptr"
810

911
vmv1 "github.com/VictoriaMetrics/operator/api/operator/v1"
@@ -549,3 +551,42 @@ func TestClusterComponentVersionDefaults(t *testing.T) {
549551
})
550552
}
551553
}
554+
555+
func TestAddDefaultMetadata(t *testing.T) {
556+
cfg := config.MustGetBaseConfig()
557+
defaultCfg := *cfg
558+
defer func() {
559+
*config.MustGetBaseConfig() = defaultCfg
560+
}()
561+
562+
cfg.CommonLabels = map[string]string{
563+
"common-label": "common-value",
564+
"existing-label": "should-not-overwrite",
565+
}
566+
cfg.CommonAnnotations = map[string]string{
567+
"common-annotation": "common-value",
568+
"existing-annotation": "should-not-overwrite",
569+
}
570+
571+
obj := &corev1.Pod{
572+
ObjectMeta: metav1.ObjectMeta{
573+
Labels: map[string]string{
574+
"existing-label": "existing-value",
575+
},
576+
Annotations: map[string]string{
577+
"existing-annotation": "existing-value",
578+
},
579+
},
580+
}
581+
582+
addDefaultMetadata(obj)
583+
584+
assert.Equal(t, map[string]string{
585+
"common-label": "common-value",
586+
"existing-label": "existing-value",
587+
}, obj.Labels)
588+
assert.Equal(t, map[string]string{
589+
"common-annotation": "common-value",
590+
"existing-annotation": "existing-value",
591+
}, obj.Annotations)
592+
}

internal/controller/operator/factory/vmsingle/vmsingle_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,35 @@ func TestCreateOrUpdate(t *testing.T) {
158158
}, got.Labels)
159159
assert.Equal(t, map[string]string{"controller": "true"}, got.Annotations)
160160
}})
161+
162+
// common labels cannot overwrite standard labels
163+
f(opts{
164+
cfgMutator: func(c *config.BaseOperatorConf) {
165+
c.CommonLabels = map[string]string{
166+
"env": "prod",
167+
"app.kubernetes.io/name": "hacked",
168+
"app.kubernetes.io/instance": "hacked",
169+
"app.kubernetes.io/component": "hacked",
170+
"managed-by": "hacked",
171+
}
172+
},
173+
cr: &vmv1beta1.VMSingle{
174+
ObjectMeta: metav1.ObjectMeta{
175+
Name: "base",
176+
Namespace: "default",
177+
},
178+
},
179+
validate: func(ctx context.Context, rclient client.Client, cr *vmv1beta1.VMSingle) {
180+
var got appsv1.Deployment
181+
assert.NoError(t, rclient.Get(ctx, types.NamespacedName{Namespace: cr.Namespace, Name: cr.PrefixedName()}, &got))
182+
assert.Equal(t, map[string]string{
183+
"env": "prod",
184+
"app.kubernetes.io/name": "vmsingle",
185+
"app.kubernetes.io/instance": "base",
186+
"app.kubernetes.io/component": "monitoring",
187+
"managed-by": "vm-operator",
188+
}, got.Labels)
189+
}})
161190
}
162191

163192
func TestCreateOrUpdateService(t *testing.T) {

0 commit comments

Comments
 (0)