|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package instrumentation |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + |
| 23 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" |
| 24 | +) |
| 25 | + |
| 26 | +func TestExporter(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + exporter v1alpha1.Exporter |
| 30 | + expected corev1.Pod |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "ca, crt and key from secret", |
| 34 | + exporter: v1alpha1.Exporter{ |
| 35 | + Endpoint: "http://collector:4318", |
| 36 | + TLS: &v1alpha1.TLS{ |
| 37 | + SecretName: "my-certs", |
| 38 | + CA: "ca.crt", |
| 39 | + Cert: "cert.crt", |
| 40 | + Key: "key.key", |
| 41 | + }, |
| 42 | + }, |
| 43 | + expected: corev1.Pod{ |
| 44 | + Spec: corev1.PodSpec{ |
| 45 | + Volumes: []corev1.Volume{ |
| 46 | + { |
| 47 | + Name: "opentelemetry-auto-instrumentation-secret-my-certs", |
| 48 | + VolumeSource: corev1.VolumeSource{ |
| 49 | + Secret: &corev1.SecretVolumeSource{ |
| 50 | + SecretName: "my-certs", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + Containers: []corev1.Container{ |
| 56 | + { |
| 57 | + VolumeMounts: []corev1.VolumeMount{ |
| 58 | + { |
| 59 | + Name: "opentelemetry-auto-instrumentation-secret-my-certs", |
| 60 | + ReadOnly: true, |
| 61 | + MountPath: "/otel-auto-instrumentation-secret-my-certs", |
| 62 | + }, |
| 63 | + }, |
| 64 | + Env: []corev1.EnvVar{ |
| 65 | + { |
| 66 | + Name: "OTEL_EXPORTER_OTLP_ENDPOINT", |
| 67 | + Value: "http://collector:4318", |
| 68 | + }, |
| 69 | + { |
| 70 | + Name: "OTEL_EXPORTER_OTLP_CERTIFICATE", |
| 71 | + Value: "/otel-auto-instrumentation-secret-my-certs/ca.crt", |
| 72 | + }, |
| 73 | + { |
| 74 | + Name: "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE", |
| 75 | + Value: "/otel-auto-instrumentation-secret-my-certs/cert.crt", |
| 76 | + }, |
| 77 | + { |
| 78 | + Name: "OTEL_EXPORTER_OTLP_CLIENT_KEY", |
| 79 | + Value: "/otel-auto-instrumentation-secret-my-certs/key.key", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "crt and key from secret and ca from configmap", |
| 89 | + exporter: v1alpha1.Exporter{ |
| 90 | + Endpoint: "http://collector:4318", |
| 91 | + TLS: &v1alpha1.TLS{ |
| 92 | + SecretName: "my-certs", |
| 93 | + ConfigMapName: "ca-bundle", |
| 94 | + CA: "ca.crt", |
| 95 | + Cert: "cert.crt", |
| 96 | + Key: "key.key", |
| 97 | + }, |
| 98 | + }, |
| 99 | + expected: corev1.Pod{ |
| 100 | + Spec: corev1.PodSpec{ |
| 101 | + Volumes: []corev1.Volume{ |
| 102 | + { |
| 103 | + Name: "opentelemetry-auto-instrumentation-secret-my-certs", |
| 104 | + VolumeSource: corev1.VolumeSource{ |
| 105 | + Secret: &corev1.SecretVolumeSource{ |
| 106 | + SecretName: "my-certs", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + Name: "opentelemetry-auto-instrumentation-configmap-ca-bundle", |
| 112 | + VolumeSource: corev1.VolumeSource{ |
| 113 | + ConfigMap: &corev1.ConfigMapVolumeSource{ |
| 114 | + LocalObjectReference: corev1.LocalObjectReference{ |
| 115 | + Name: "ca-bundle", |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + Containers: []corev1.Container{ |
| 122 | + { |
| 123 | + VolumeMounts: []corev1.VolumeMount{ |
| 124 | + { |
| 125 | + Name: "opentelemetry-auto-instrumentation-secret-my-certs", |
| 126 | + ReadOnly: true, |
| 127 | + MountPath: "/otel-auto-instrumentation-secret-my-certs", |
| 128 | + }, |
| 129 | + { |
| 130 | + Name: "opentelemetry-auto-instrumentation-configmap-ca-bundle", |
| 131 | + ReadOnly: true, |
| 132 | + MountPath: "/otel-auto-instrumentation-configmap-ca-bundle", |
| 133 | + }, |
| 134 | + }, |
| 135 | + Env: []corev1.EnvVar{ |
| 136 | + { |
| 137 | + Name: "OTEL_EXPORTER_OTLP_ENDPOINT", |
| 138 | + Value: "http://collector:4318", |
| 139 | + }, |
| 140 | + { |
| 141 | + Name: "OTEL_EXPORTER_OTLP_CERTIFICATE", |
| 142 | + Value: "/otel-auto-instrumentation-configmap-ca-bundle/ca.crt", |
| 143 | + }, |
| 144 | + { |
| 145 | + Name: "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE", |
| 146 | + Value: "/otel-auto-instrumentation-secret-my-certs/cert.crt", |
| 147 | + }, |
| 148 | + { |
| 149 | + Name: "OTEL_EXPORTER_OTLP_CLIENT_KEY", |
| 150 | + Value: "/otel-auto-instrumentation-secret-my-certs/key.key", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "ca, crt key absolute paths", |
| 160 | + exporter: v1alpha1.Exporter{ |
| 161 | + Endpoint: "http://collector:4318", |
| 162 | + TLS: &v1alpha1.TLS{ |
| 163 | + CA: "/ca.crt", |
| 164 | + Cert: "/cert.crt", |
| 165 | + Key: "/key.key", |
| 166 | + }, |
| 167 | + }, |
| 168 | + expected: corev1.Pod{ |
| 169 | + Spec: corev1.PodSpec{ |
| 170 | + Containers: []corev1.Container{ |
| 171 | + { |
| 172 | + Env: []corev1.EnvVar{ |
| 173 | + { |
| 174 | + Name: "OTEL_EXPORTER_OTLP_ENDPOINT", |
| 175 | + Value: "http://collector:4318", |
| 176 | + }, |
| 177 | + { |
| 178 | + Name: "OTEL_EXPORTER_OTLP_CERTIFICATE", |
| 179 | + Value: "/ca.crt", |
| 180 | + }, |
| 181 | + { |
| 182 | + Name: "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE", |
| 183 | + Value: "/cert.crt", |
| 184 | + }, |
| 185 | + { |
| 186 | + Name: "OTEL_EXPORTER_OTLP_CLIENT_KEY", |
| 187 | + Value: "/key.key", |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + } |
| 196 | + for _, test := range tests { |
| 197 | + t.Run(test.name, func(t *testing.T) { |
| 198 | + pod := corev1.Pod{ |
| 199 | + Spec: corev1.PodSpec{ |
| 200 | + Containers: []corev1.Container{ |
| 201 | + {}, |
| 202 | + }, |
| 203 | + }, |
| 204 | + } |
| 205 | + configureExporter(test.exporter, &pod, &pod.Spec.Containers[0]) |
| 206 | + assert.Equal(t, test.expected, pod) |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
0 commit comments