Skip to content

Commit

Permalink
Add cumulativetodelta tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madaraszg-tulip committed Feb 14, 2025
1 parent 9454d2c commit 93f2585
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (args *Arguments) Validate() error {
}

if (len(args.Include.MatchType) > 0 && args.Include.MatchType != "strict" && args.Include.MatchType != "regexp") ||
(len(args.Include.MatchType) > 0 && args.Include.MatchType != "strict" && args.Include.MatchType != "regexp") {
(len(args.Exclude.MatchType) > 0 && args.Exclude.MatchType != "strict" && args.Exclude.MatchType != "regexp") {

return fmt.Errorf("match_type must be one of %q and %q", "strict", "regexp")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package cumulativetodelta_test

import (
"testing"

"github.com/grafana/alloy/internal/component/otelcol/processor/cumulativetodelta"
"github.com/grafana/alloy/syntax"
"github.com/mitchellh/mapstructure"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor"
"github.com/stretchr/testify/require"
)

func TestArguments_UnmarshalAlloy(t *testing.T) {
tests := []struct {
testName string
cfg string
expected map[string]interface{}
}{
{
testName: "Defaults",
cfg: `
output {}
`,
expected: map[string]interface{}{
"max_staleness": 0,
"initial_value": 0,
},
},
{
testName: "Defaults Match Upstream",
cfg: `
output {}
`,
expected: map[string]interface{}{},
},
{
testName: "Initial Value Auto",
cfg: `
initial_value = "auto"
output {}
`,
expected: map[string]interface{}{
"initial_value": 0,
},
},
{
testName: "Initial Value Keep",
cfg: `
initial_value = "keep"
output {}
`,
expected: map[string]interface{}{
"initial_value": 1,
},
},
{
testName: "Initial Value Drop",
cfg: `
initial_value = "drop"
output {}
`,
expected: map[string]interface{}{
"initial_value": 2,
},
},
{
testName: "Explicit Values",
cfg: `
max_staleness = "24h"
initial_value = "drop"
include {
metrics = ["metric1", "metric2"]
match_type = "strict"
}
exclude {
metrics = [".*metric.*"]
match_type = "regexp"
}
output {}
`,
expected: map[string]interface{}{
"max_staleness": 86400000000000,
"initial_value": 2,
"include": map[string]interface{}{
"metrics": []string{"metric1", "metric2"},
"match_type": "strict",
},
"exclude": map[string]interface{}{
"metrics": []string{".*metric.*"},
"match_type": "regexp",
},
},
},
}

for _, tc := range tests {
t.Run(tc.testName, func(t *testing.T) {
var args cumulativetodelta.Arguments
err := syntax.Unmarshal([]byte(tc.cfg), &args)
require.NoError(t, err)

actualPtr, err := args.Convert()
require.NoError(t, err)

actual := actualPtr.(*cumulativetodeltaprocessor.Config)

var expected cumulativetodeltaprocessor.Config
err = mapstructure.Decode(tc.expected, &expected)
require.NoError(t, err)

require.Equal(t, expected, *actual)
})
}
}

func TestArguments_Validate(t *testing.T) {
tests := []struct {
testName string
cfg string
expectedError string
}{
{
testName: "Initial Value",
cfg: `
initial_value = "wait"
output {}
`,
expectedError: `initial_value must be one of "auto", "keep", "drop"`,
},
{
testName: "Missing Include Metrics",
cfg: `
include {
match_type = "strict"
}
output {}
`,
expectedError: "metrics must be supplied if match_type is set",
},
{
testName: "Missing Include Match Type",
cfg: `
include {
metrics = ["metric"]
}
output {}
`,
expectedError: "match_type must be set if metrics are supplied",
},
{
testName: "Missing Exclude Metrics",
cfg: `
exclude {
match_type = "strict"
}
output {}
`,
expectedError: "metrics must be supplied if match_type is set",
},
{
testName: "Missing Exclude Match Type",
cfg: `
exclude {
metrics = ["metric"]
}
output {}
`,
expectedError: "match_type must be set if metrics are supplied",
},
{
testName: "Incorrect Include Match Type",
cfg: `
include {
metrics = ["metric"]
match_type = "regex"
}
output {}
`,
expectedError: `match_type must be one of "strict" and "regexp"`,
},
{
testName: "Incorrect Exclude Match Type",
cfg: `
exclude {
metrics = ["metric"]
match_type = "regex"
}
output {}
`,
expectedError: `match_type must be one of "strict" and "regexp"`,
},
}
for _, tc := range tests {
t.Run(tc.testName, func(t *testing.T) {
var args cumulativetodelta.Arguments
require.ErrorContains(t, syntax.Unmarshal([]byte(tc.cfg), &args), tc.expectedError)
})
}
}

0 comments on commit 93f2585

Please sign in to comment.