Skip to content

Commit

Permalink
[chore][connector/routing] Refactor metric test examples into code (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Nov 3, 2024
1 parent a5807e5 commit 8dd2b7b
Show file tree
Hide file tree
Showing 33 changed files with 329 additions and 5,974 deletions.
61 changes: 35 additions & 26 deletions connector/routingconnector/internal/pmetricutil/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,79 @@
package pmetricutil_test

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/pmetricutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/pmetricutiltest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)

func TestMoveResourcesIf(t *testing.T) {
testCases := []struct {
name string
condition func(pmetric.ResourceMetrics) bool
name string
moveIf func(pmetric.ResourceMetrics) bool
from pmetric.Metrics
to pmetric.Metrics
expectFrom pmetric.Metrics
expectTo pmetric.Metrics
}{
{
name: "move_none",
condition: func(pmetric.ResourceMetrics) bool {
moveIf: func(pmetric.ResourceMetrics) bool {
return false
},
from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "FG"),
to: pmetric.NewMetrics(),
expectFrom: pmetricutiltest.NewMetrics("AB", "CD", "EF", "FG"),
expectTo: pmetric.NewMetrics(),
},
{
name: "move_all",
condition: func(pmetric.ResourceMetrics) bool {
moveIf: func(pmetric.ResourceMetrics) bool {
return true
},
from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "FG"),
to: pmetric.NewMetrics(),
expectFrom: pmetric.NewMetrics(),
expectTo: pmetricutiltest.NewMetrics("AB", "CD", "EF", "FG"),
},
{
name: "move_one",
condition: func(rl pmetric.ResourceMetrics) bool {
moveIf: func(rl pmetric.ResourceMetrics) bool {
rname, ok := rl.Resource().Attributes().Get("resourceName")
return ok && rname.AsString() == "resourceA"
},
from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "FG"),
to: pmetric.NewMetrics(),
expectFrom: pmetricutiltest.NewMetrics("B", "CD", "EF", "FG"),
expectTo: pmetricutiltest.NewMetrics("A", "CD", "EF", "FG"),
},
{
name: "move_to_preexisting",
condition: func(rl pmetric.ResourceMetrics) bool {
moveIf: func(rl pmetric.ResourceMetrics) bool {
rname, ok := rl.Resource().Attributes().Get("resourceName")
return ok && rname.AsString() == "resourceB"
},
from: pmetricutiltest.NewMetrics("AB", "CD", "EF", "FG"),
to: pmetricutiltest.NewMetrics("1", "2", "3", "4"),
expectFrom: pmetricutiltest.NewMetrics("A", "CD", "EF", "FG"),
expectTo: func() pmetric.Metrics {
move := pmetricutiltest.NewMetrics("B", "CD", "EF", "FG")
moveTo := pmetricutiltest.NewMetrics("1", "2", "3", "4")
move.ResourceMetrics().MoveAndAppendTo(moveTo.ResourceMetrics())
return moveTo
}(),
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
// Load up a fresh copy of the input for each test, since it may be modified in place.
from, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "from.yaml"))
require.NoError(t, err)

to, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "to.yaml"))
require.NoError(t, err)

fromModifed, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "from_modified.yaml"))
require.NoError(t, err)

toModified, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "to_modified.yaml"))
require.NoError(t, err)

pmetricutil.MoveResourcesIf(from, to, tt.condition)

assert.NoError(t, pmetrictest.CompareMetrics(fromModifed, from), "from not modified as expected")
assert.NoError(t, pmetrictest.CompareMetrics(toModified, to), "to not as expected")
pmetricutil.MoveResourcesIf(tt.from, tt.to, tt.moveIf)
assert.NoError(t, pmetrictest.CompareMetrics(tt.expectFrom, tt.from), "from not modified as expected")
assert.NoError(t, pmetrictest.CompareMetrics(tt.expectTo, tt.to), "to not as expected")
})
}
}
Loading

0 comments on commit 8dd2b7b

Please sign in to comment.