-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathannotations_test.go
More file actions
142 lines (122 loc) · 4.89 KB
/
Copy pathannotations_test.go
File metadata and controls
142 lines (122 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package collector
import (
"testing"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/aws/amazon-cloudwatch-agent-operator/apis/v1alpha1"
)
func TestDefaultAnnotations(t *testing.T) {
// prepare
otelcol := v1alpha1.AmazonCloudWatchAgent{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "my-ns",
},
Spec: v1alpha1.AmazonCloudWatchAgentSpec{
Config: "test",
},
}
// test
annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(otelcol)
//verify
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["amazon-cloudwatch-agent-operator-config/sha256"])
//verify propagation from metadata.annotations to spec.template.spec.metadata.annotations
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["amazon-cloudwatch-agent-operator-config/sha256"])
}
func TestUserAnnotations(t *testing.T) {
// prepare
otelcol := v1alpha1.AmazonCloudWatchAgent{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "my-ns",
Annotations: map[string]string{
"amazon-cloudwatch-agent-operator-config/sha256": "shouldBeOverwritten",
},
},
Spec: v1alpha1.AmazonCloudWatchAgentSpec{
Config: "test",
},
}
// test
annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(otelcol)
//verify
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["amazon-cloudwatch-agent-operator-config/sha256"])
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["amazon-cloudwatch-agent-operator-config/sha256"])
}
func TestAnnotationsPropagateDown(t *testing.T) {
// prepare
otelcol := v1alpha1.AmazonCloudWatchAgent{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"myapp": "mycomponent"},
},
Spec: v1alpha1.AmazonCloudWatchAgentSpec{
PodAnnotations: map[string]string{"pod_annotation": "pod_annotation_value"},
},
}
// test
annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(otelcol)
// verify
assert.Len(t, annotations, 2)
assert.Equal(t, "mycomponent", annotations["myapp"])
assert.Equal(t, "mycomponent", podAnnotations["myapp"])
assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"])
}
func promConfig(t *testing.T, replacement string) v1alpha1.PrometheusConfig {
t.Helper()
cfg := map[string]interface{}{
"scrape_configs": []interface{}{
map[string]interface{}{
"job_name": "kubernetes-pods-annotated",
"relabel_configs": []interface{}{
map[string]interface{}{
"target_label": "bug2probe",
"replacement": replacement,
},
},
},
},
}
return v1alpha1.PrometheusConfig{
Config: &v1alpha1.AnyConfig{Object: cfg},
}
}
// TestPrometheusConfigChangeBumpsHash verifies a Prometheus-only change bumps the pod-template hash.
func TestPrometheusConfigChangeBumpsHash(t *testing.T) {
base := v1alpha1.AmazonCloudWatchAgent{
ObjectMeta: metav1.ObjectMeta{Name: "my-instance", Namespace: "my-ns"},
Spec: v1alpha1.AmazonCloudWatchAgentSpec{
Config: "agent-config",
Prometheus: promConfig(t, "value2"),
},
}
// same spec twice -> stable hash
h1 := PodAnnotations(base)["amazon-cloudwatch-agent-operator-config/sha256"]
h2 := PodAnnotations(base)["amazon-cloudwatch-agent-operator-config/sha256"]
assert.Equal(t, h1, h2, "hash must be stable when nothing changes")
// change ONLY the prometheus config -> hash must change
changed := base
changed.Spec.Prometheus = promConfig(t, "value3")
h3 := PodAnnotations(changed)["amazon-cloudwatch-agent-operator-config/sha256"]
assert.NotEqual(t, h1, h3, "pod annotation hash must change when only Spec.Prometheus changes")
// metadata annotations hash must also reflect the prometheus change
a1 := Annotations(base)["amazon-cloudwatch-agent-operator-config/sha256"]
a3 := Annotations(changed)["amazon-cloudwatch-agent-operator-config/sha256"]
assert.NotEqual(t, a1, a3, "metadata annotation hash must change when only Spec.Prometheus changes")
}
// TestEmptyPrometheusHashUnchanged verifies non-Prometheus agents keep a stable hash.
func TestEmptyPrometheusHashUnchanged(t *testing.T) {
otelcol := v1alpha1.AmazonCloudWatchAgent{
ObjectMeta: metav1.ObjectMeta{Name: "my-instance", Namespace: "my-ns"},
Spec: v1alpha1.AmazonCloudWatchAgentSpec{Config: "test"},
}
// sha256("test") == 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
Annotations(otelcol)["amazon-cloudwatch-agent-operator-config/sha256"])
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
PodAnnotations(otelcol)["amazon-cloudwatch-agent-operator-config/sha256"])
}