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