forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotations_test.go
161 lines (145 loc) · 5.76 KB
/
annotations_test.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collector
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
)
func TestDefaultAnnotations(t *testing.T) {
// prepare
otelcol := v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "my-ns",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: func() *[]string {
res := []string{"test"}
return &res
}(),
},
},
},
}
// test
annotations, err := Annotations(otelcol)
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol)
require.NoError(t, err)
//verify
assert.Equal(t, "true", annotations["prometheus.io/scrape"])
assert.Equal(t, "8888", annotations["prometheus.io/port"])
assert.Equal(t, "/metrics", annotations["prometheus.io/path"])
assert.Equal(t, "5b3b62aa5e0a3c7250084c2b49190e30b72fc2ad352ffbaa699224e1aa900834", annotations["opentelemetry-operator-config/sha256"])
//verify propagation from metadata.annotations to spec.template.spec.metadata.annotations
assert.Equal(t, "true", podAnnotations["prometheus.io/scrape"])
assert.Equal(t, "8888", podAnnotations["prometheus.io/port"])
assert.Equal(t, "/metrics", podAnnotations["prometheus.io/path"])
assert.Equal(t, "5b3b62aa5e0a3c7250084c2b49190e30b72fc2ad352ffbaa699224e1aa900834", podAnnotations["opentelemetry-operator-config/sha256"])
}
func TestNonDefaultPodAnnotation(t *testing.T) {
// prepare
otelcol := v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "my-ns",
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
DisablePrometheusAnnotations: true,
DisableAutomaticPrometheusAnnotations: true,
},
},
},
}
// test
annotations, err := Annotations(otelcol)
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol)
require.NoError(t, err)
//verify
assert.NotContains(t, annotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist")
assert.NotContains(t, annotations, "prometheus.io/port", "Prometheus port annotation should not exist")
assert.NotContains(t, annotations, "prometheus.io/path", "Prometheus path annotation should not exist")
assert.Equal(t, "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d", annotations["opentelemetry-operator-config/sha256"])
//verify propagation from metadata.annotations to spec.template.spec.metadata.annotations
assert.NotContains(t, podAnnotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist in pod annotations")
assert.NotContains(t, podAnnotations, "prometheus.io/port", "Prometheus port annotation should not exist in pod annotations")
assert.NotContains(t, podAnnotations, "prometheus.io/path", "Prometheus path annotation should not exist in pod annotations")
assert.Equal(t, "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d", podAnnotations["opentelemetry-operator-config/sha256"])
}
func TestUserAnnotations(t *testing.T) {
// prepare
otelcol := v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "my-ns",
Annotations: map[string]string{"prometheus.io/scrape": "false",
"prometheus.io/port": "1234",
"prometheus.io/path": "/test",
"opentelemetry-operator-config/sha256": "shouldBeOverwritten",
},
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Config: v1beta1.Config{
Service: v1beta1.Service{
Extensions: func() *[]string {
res := []string{"test2"}
return &res
}(),
},
},
},
}
// test
annotations, err := Annotations(otelcol)
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol)
require.NoError(t, err)
//verify
assert.Equal(t, "false", annotations["prometheus.io/scrape"])
assert.Equal(t, "1234", annotations["prometheus.io/port"])
assert.Equal(t, "/test", annotations["prometheus.io/path"])
assert.Equal(t, "29cb15a4b87f8c6284e7c3377f6b6c5c74519f5aee8ca39a90b3cf3ca2043c4d", annotations["opentelemetry-operator-config/sha256"])
assert.Equal(t, "29cb15a4b87f8c6284e7c3377f6b6c5c74519f5aee8ca39a90b3cf3ca2043c4d", podAnnotations["opentelemetry-operator-config/sha256"])
}
func TestAnnotationsPropagateDown(t *testing.T) {
// prepare
otelcol := v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"myapp": "mycomponent"},
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
PodAnnotations: map[string]string{"pod_annotation": "pod_annotation_value"},
},
},
}
// test
annotations, err := Annotations(otelcol)
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol)
require.NoError(t, err)
// verify
assert.Len(t, annotations, 5)
assert.Equal(t, "mycomponent", annotations["myapp"])
assert.Equal(t, "mycomponent", podAnnotations["myapp"])
assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"])
}