|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package pmetricutil_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "go.opentelemetry.io/collector/pdata/pmetric" |
| 13 | + |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/pmetricutil" |
| 15 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden" |
| 16 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest" |
| 17 | +) |
| 18 | + |
| 19 | +func TestMoveResourcesIf(t *testing.T) { |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + condition func(pmetric.ResourceMetrics) bool |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "move_none", |
| 26 | + condition: func(pmetric.ResourceMetrics) bool { |
| 27 | + return false |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "move_all", |
| 32 | + condition: func(pmetric.ResourceMetrics) bool { |
| 33 | + return true |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "move_one", |
| 38 | + condition: func(rl pmetric.ResourceMetrics) bool { |
| 39 | + rname, ok := rl.Resource().Attributes().Get("resourceName") |
| 40 | + return ok && rname.AsString() == "resourceA" |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "move_to_preexisting", |
| 45 | + condition: func(rl pmetric.ResourceMetrics) bool { |
| 46 | + rname, ok := rl.Resource().Attributes().Get("resourceName") |
| 47 | + return ok && rname.AsString() == "resourceB" |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range testCases { |
| 53 | + t.Run(tt.name, func(t *testing.T) { |
| 54 | + // Load up a fresh copy of the input for each test, since it may be modified in place. |
| 55 | + from, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "from.yaml")) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + to, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "to.yaml")) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + fromModifed, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "from_modified.yaml")) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + toModified, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "to_modified.yaml")) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + pmetricutil.MoveResourcesIf(from, to, tt.condition) |
| 68 | + |
| 69 | + assert.NoError(t, pmetrictest.CompareMetrics(fromModifed, from), "from not modified as expected") |
| 70 | + assert.NoError(t, pmetrictest.CompareMetrics(toModified, to), "to not as expected") |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments