|
| 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 collector |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + appsv1 "k8s.io/api/apps/v1" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 27 | + |
| 28 | + "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" |
| 29 | +) |
| 30 | + |
| 31 | +func TestUpdateCollectorStatusUnsupported(t *testing.T) { |
| 32 | + ctx := context.TODO() |
| 33 | + cli := client.Client(fake.NewFakeClient()) |
| 34 | + |
| 35 | + changed := &v1alpha1.OpenTelemetryCollector{ |
| 36 | + ObjectMeta: metav1.ObjectMeta{ |
| 37 | + Name: "test-sidecar", |
| 38 | + Namespace: "default", |
| 39 | + }, |
| 40 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 41 | + Mode: v1alpha1.ModeSidecar, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + err := UpdateCollectorStatus(ctx, cli, changed) |
| 46 | + assert.NoError(t, err) |
| 47 | + |
| 48 | + assert.Equal(t, int32(0), changed.Status.Scale.Replicas, "expected replicas to be 0") |
| 49 | + assert.Equal(t, "", changed.Status.Scale.Selector, "expected selector to be empty") |
| 50 | +} |
| 51 | + |
| 52 | +func createMockKubernetesClientDeployment() client.Client { |
| 53 | + deployment := &appsv1.Deployment{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Name: "test-deployment-collector", |
| 56 | + Namespace: "default", |
| 57 | + }, |
| 58 | + Status: appsv1.DeploymentStatus{ |
| 59 | + Replicas: 1, |
| 60 | + ReadyReplicas: 1, |
| 61 | + }, |
| 62 | + Spec: appsv1.DeploymentSpec{ |
| 63 | + Template: corev1.PodTemplateSpec{ |
| 64 | + Spec: corev1.PodSpec{ |
| 65 | + Containers: []corev1.Container{ |
| 66 | + { |
| 67 | + Name: "app", |
| 68 | + Image: "app:latest", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + return fake.NewClientBuilder().WithObjects(deployment).Build() |
| 76 | +} |
| 77 | + |
| 78 | +func TestUpdateCollectorStatusDeploymentMode(t *testing.T) { |
| 79 | + ctx := context.TODO() |
| 80 | + cli := createMockKubernetesClientDeployment() |
| 81 | + |
| 82 | + changed := &v1alpha1.OpenTelemetryCollector{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: "test-deployment", |
| 85 | + Namespace: "default", |
| 86 | + }, |
| 87 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 88 | + Mode: v1alpha1.ModeDeployment, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + err := UpdateCollectorStatus(ctx, cli, changed) |
| 93 | + assert.NoError(t, err) |
| 94 | + |
| 95 | + assert.Equal(t, int32(1), changed.Status.Scale.Replicas, "expected replicas to be 1") |
| 96 | + assert.Equal(t, "1/1", changed.Status.Scale.StatusReplicas, "expected status replicas to be 1/1") |
| 97 | + assert.Equal(t, "app:latest", changed.Status.Image, "expected image to be app:latest") |
| 98 | +} |
| 99 | + |
| 100 | +func createMockKubernetesClientStatefulset() client.Client { |
| 101 | + statefulset := &appsv1.StatefulSet{ |
| 102 | + ObjectMeta: metav1.ObjectMeta{ |
| 103 | + Name: "test-statefulset-collector", |
| 104 | + Namespace: "default", |
| 105 | + }, |
| 106 | + Status: appsv1.StatefulSetStatus{ |
| 107 | + Replicas: 1, |
| 108 | + ReadyReplicas: 1, |
| 109 | + }, |
| 110 | + Spec: appsv1.StatefulSetSpec{ |
| 111 | + Template: corev1.PodTemplateSpec{ |
| 112 | + Spec: corev1.PodSpec{ |
| 113 | + Containers: []corev1.Container{ |
| 114 | + { |
| 115 | + Name: "app", |
| 116 | + Image: "app:latest", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + return fake.NewClientBuilder().WithObjects(statefulset).Build() |
| 124 | +} |
| 125 | + |
| 126 | +func TestUpdateCollectorStatusStatefulset(t *testing.T) { |
| 127 | + ctx := context.TODO() |
| 128 | + cli := createMockKubernetesClientStatefulset() |
| 129 | + |
| 130 | + changed := &v1alpha1.OpenTelemetryCollector{ |
| 131 | + ObjectMeta: metav1.ObjectMeta{ |
| 132 | + Name: "test-statefulset", |
| 133 | + Namespace: "default", |
| 134 | + }, |
| 135 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 136 | + Mode: v1alpha1.ModeStatefulSet, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + err := UpdateCollectorStatus(ctx, cli, changed) |
| 141 | + assert.NoError(t, err) |
| 142 | + |
| 143 | + assert.Equal(t, int32(1), changed.Status.Scale.Replicas, "expected replicas to be 1") |
| 144 | + assert.Equal(t, "1/1", changed.Status.Scale.StatusReplicas, "expected status replicas to be 1/1") |
| 145 | + assert.Equal(t, "app:latest", changed.Status.Image, "expected image to be app:latest") |
| 146 | +} |
| 147 | + |
| 148 | +func createMockKubernetesClientDaemonset() client.Client { |
| 149 | + daemonset := &appsv1.DaemonSet{ |
| 150 | + ObjectMeta: metav1.ObjectMeta{ |
| 151 | + Name: "test-daemonset-collector", |
| 152 | + Namespace: "default", |
| 153 | + }, |
| 154 | + Spec: appsv1.DaemonSetSpec{ |
| 155 | + Template: corev1.PodTemplateSpec{ |
| 156 | + Spec: corev1.PodSpec{ |
| 157 | + Containers: []corev1.Container{ |
| 158 | + { |
| 159 | + Name: "app", |
| 160 | + Image: "app:latest", |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + return fake.NewClientBuilder().WithObjects(daemonset).Build() |
| 168 | +} |
| 169 | + |
| 170 | +func TestUpdateCollectorStatusDaemonsetMode(t *testing.T) { |
| 171 | + ctx := context.TODO() |
| 172 | + cli := createMockKubernetesClientDaemonset() |
| 173 | + |
| 174 | + changed := &v1alpha1.OpenTelemetryCollector{ |
| 175 | + ObjectMeta: metav1.ObjectMeta{ |
| 176 | + Name: "test-daemonset", |
| 177 | + Namespace: "default", |
| 178 | + Labels: map[string]string{ |
| 179 | + "customLabel": "customValue", |
| 180 | + }, |
| 181 | + }, |
| 182 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 183 | + Mode: v1alpha1.ModeDaemonSet, |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + err := UpdateCollectorStatus(ctx, cli, changed) |
| 188 | + assert.NoError(t, err) |
| 189 | + |
| 190 | + assert.Contains(t, changed.Status.Scale.Selector, "customLabel=customValue", "expected selector to contain customlabel=customValue") |
| 191 | + assert.Equal(t, "app:latest", changed.Status.Image, "expected image to be app:latest") |
| 192 | +} |
0 commit comments