|
| 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 upgrade_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + |
| 26 | + "github.com/open-telemetry/opentelemetry-operator/api/v1alpha1" |
| 27 | + "github.com/open-telemetry/opentelemetry-operator/internal/version" |
| 28 | + "github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters" |
| 29 | + "github.com/open-telemetry/opentelemetry-operator/pkg/collector/upgrade" |
| 30 | +) |
| 31 | + |
| 32 | +func TestRemoveQueuedRetryProcessor(t *testing.T) { |
| 33 | + // prepare |
| 34 | + nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"} |
| 35 | + existing := v1alpha1.OpenTelemetryCollector{ |
| 36 | + ObjectMeta: metav1.ObjectMeta{ |
| 37 | + Name: nsn.Name, |
| 38 | + Namespace: nsn.Namespace, |
| 39 | + Labels: map[string]string{ |
| 40 | + "app.kubernetes.io/managed-by": "opentelemetry-operator", |
| 41 | + }, |
| 42 | + }, |
| 43 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 44 | + Config: `processors: |
| 45 | + queued_retry: |
| 46 | + otherprocessor: |
| 47 | + queued_retry/second: |
| 48 | + compression: "on" |
| 49 | + reconnection_delay: 15 |
| 50 | + num_workers: 123`, |
| 51 | + }, |
| 52 | + } |
| 53 | + existing.Status.Version = "0.18.0" |
| 54 | + |
| 55 | + // sanity check |
| 56 | + require.Contains(t, existing.Spec.Config, "queued_retry") |
| 57 | + require.Contains(t, existing.Spec.Config, "queued_retry/second") |
| 58 | + require.Contains(t, existing.Spec.Config, "num_workers: 123") // checking one property is sufficient |
| 59 | + |
| 60 | + // test |
| 61 | + res, err := upgrade.ManagedInstance(context.Background(), logger, version.Get(), nil, existing) |
| 62 | + assert.NoError(t, err) |
| 63 | + |
| 64 | + // verify |
| 65 | + assert.NotContains(t, res.Spec.Config, "queued_retry:") |
| 66 | + assert.Contains(t, res.Spec.Config, "otherprocessor:") |
| 67 | + assert.NotContains(t, res.Spec.Config, "queued_retry/second:") |
| 68 | + assert.NotContains(t, res.Spec.Config, "num_workers: 123") // checking one property is sufficient |
| 69 | + assert.Contains(t, res.Status.Messages[0], "upgrade to v0.19.0 removed the processor") |
| 70 | +} |
| 71 | + |
| 72 | +func TestMigrateResourceType(t *testing.T) { |
| 73 | + // prepare |
| 74 | + nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"} |
| 75 | + existing := v1alpha1.OpenTelemetryCollector{ |
| 76 | + ObjectMeta: metav1.ObjectMeta{ |
| 77 | + Name: nsn.Name, |
| 78 | + Namespace: nsn.Namespace, |
| 79 | + Labels: map[string]string{ |
| 80 | + "app.kubernetes.io/managed-by": "opentelemetry-operator", |
| 81 | + }, |
| 82 | + }, |
| 83 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 84 | + Config: `processors: |
| 85 | + resource: |
| 86 | + type: some-type |
| 87 | +`, |
| 88 | + }, |
| 89 | + } |
| 90 | + existing.Status.Version = "0.18.0" |
| 91 | + |
| 92 | + // test |
| 93 | + res, err := upgrade.ManagedInstance(context.Background(), logger, version.Get(), nil, existing) |
| 94 | + assert.NoError(t, err) |
| 95 | + |
| 96 | + // verify |
| 97 | + assert.Equal(t, `processors: |
| 98 | + resource: |
| 99 | + attributes: |
| 100 | + - action: upsert |
| 101 | + key: opencensus.type |
| 102 | + value: some-type |
| 103 | +`, res.Spec.Config) |
| 104 | + assert.Contains(t, res.Status.Messages[0], "upgrade to v0.19.0 migrated the property 'type' for processor") |
| 105 | +} |
| 106 | + |
| 107 | +func TestMigrateLabels(t *testing.T) { |
| 108 | + // prepare |
| 109 | + nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"} |
| 110 | + existing := v1alpha1.OpenTelemetryCollector{ |
| 111 | + ObjectMeta: metav1.ObjectMeta{ |
| 112 | + Name: nsn.Name, |
| 113 | + Namespace: nsn.Namespace, |
| 114 | + Labels: map[string]string{ |
| 115 | + "app.kubernetes.io/managed-by": "opentelemetry-operator", |
| 116 | + }, |
| 117 | + }, |
| 118 | + Spec: v1alpha1.OpenTelemetryCollectorSpec{ |
| 119 | + Config: `processors: |
| 120 | + resource: |
| 121 | + labels: |
| 122 | + cloud.zone: zone-1 |
| 123 | + host.name: k8s-node |
| 124 | +`, |
| 125 | + }, |
| 126 | + } |
| 127 | + existing.Status.Version = "0.18.0" |
| 128 | + |
| 129 | + // test |
| 130 | + res, err := upgrade.ManagedInstance(context.Background(), logger, version.Get(), nil, existing) |
| 131 | + assert.NoError(t, err) |
| 132 | + |
| 133 | + actual, err := adapters.ConfigFromString(res.Spec.Config) |
| 134 | + require.NoError(t, err) |
| 135 | + actualProcessors := actual["processors"].(map[interface{}]interface{}) |
| 136 | + actualProcessor := actualProcessors["resource"].(map[interface{}]interface{}) |
| 137 | + actualAttrs := actualProcessor["attributes"].([]interface{}) |
| 138 | + |
| 139 | + // verify |
| 140 | + assert.Len(t, actualAttrs, 2) |
| 141 | + assert.Nil(t, actualProcessor["labels"]) |
| 142 | + assert.Contains(t, res.Status.Messages[0], "upgrade to v0.19.0 migrated the property 'labels' for processor") |
| 143 | +} |
0 commit comments