Files
telemetry/confmap/internal/e2e/maplist_expanded_test.go
zooqueen a350085259 tests: repair the four suites the module wall was hiding
With every module resolving again, four suites ran for the first time in a
long while and did not compile or pass. Each had drifted from the source it
covers, and nothing caught it because the walk never reached them.

service/telemetry/otelconftelemetry: TestDefaultConfig lost the `gate` field
from its table while the body still read tt.gate, and strconv went with it.
The gate chooses between "localhost" and "", so both cases are back.

confmap/internal/e2e: the tests drove confmap.internal.NewExpandedValueSanitizer,
which no longer exists — the gate moved to internal/metadata and went Stable.
A stable gate cannot be disabled, and both tests flipped it off to assert the
old behaviour, so that half was testing something the code can no longer do.
Dropped the gate plumbing; the behaviour it guarded is now unconditional and
the assertions still hold.

exporter/exporterhelper: newObsReportSender grew an extraAttrs parameter and
four of its six call sites were updated. The other two now pass nil too.

otelcol: TestSortFactoriesByType keyed every factory under a name other than
its own type, and canonicalFactoryKeys keeps a key only when it equals
f.Type(), so every case sorted to empty. Keys now match what MakeFactoryMap
actually produces, including the alias entry filed under both its own type
and its deprecated alias.

Per-module go test is clean for all four.
2026-08-12 21:13:28 -07:00

116 lines
3.0 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package e2etest
import (
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/internal"
)
type testHeadersConfig struct {
Headers configopaque.MapList `mapstructure:"headers"`
}
// TestMapListWithExpandedValue tests that MapList can handle ExpandedValue
// from environment variable expansion
func TestMapListWithExpandedValue(t *testing.T) {
// Simulate what happens when ${env:TOKEN} is expanded
// The confmap will contain an ExpandedValue instead of a plain string
data := map[string]any{
"headers": []any{
map[string]any{
"name": "Authorization",
"value": internal.ExpandedValue{
Value: "Bearer secret-token",
Original: "Bearer secret-token",
},
},
},
}
conf := confmap.NewFromStringMap(data)
var tc testHeadersConfig
err := conf.Unmarshal(&tc)
require.NoError(t, err)
val, ok := tc.Headers.Get("Authorization")
require.True(t, ok)
require.Equal(t, configopaque.String("Bearer secret-token"), val)
}
// TestMapListWithExpandedValueIntValue tests an ExpandedValue with an integer Value
func TestMapListWithExpandedValueIntValue(t *testing.T) {
// Simulate what happens when expanding a value that parses as an int
data := map[string]any{
"headers": []any{
map[string]any{
"name": "X-Port",
"value": internal.ExpandedValue{
Value: 8080, // Value is parsed as int
Original: "8080", // Original is string
},
},
},
}
conf := confmap.NewFromStringMap(data)
var tc testHeadersConfig
err := conf.Unmarshal(&tc)
require.NoError(t, err)
val, ok := tc.Headers.Get("X-Port")
require.True(t, ok)
require.Equal(t, configopaque.String("8080"), val)
}
// TestDirectConfigopaqueStringWithExpandedValueIntValue tests that direct unmarshaling works
func TestDirectConfigopaqueStringWithExpandedValueIntValue(t *testing.T) {
type testConfig struct {
Value configopaque.String `mapstructure:"value"`
}
// Direct configopaque.String field (not in a map/slice structure)
data := map[string]any{
"value": internal.ExpandedValue{
Value: 8080,
Original: "8080",
},
}
conf := confmap.NewFromStringMap(data)
var tc testConfig
err := conf.Unmarshal(&tc)
// This should work because useExpandValue detects the target is a string
require.NoError(t, err)
require.Equal(t, configopaque.String("8080"), tc.Value)
}
// TestStringyStructureWithExpandedValue tests the isStringyStructure path in useExpandValue
func TestStringyStructureWithExpandedValue(t *testing.T) {
type testConfig struct {
Tags []string `mapstructure:"tags"`
}
data := map[string]any{
"tags": []any{
internal.ExpandedValue{
Value: 8080,
Original: "8080",
},
},
}
// useExpandValue should detect []string as stringy
conf := confmap.NewFromStringMap(data)
var tc testConfig
err := conf.Unmarshal(&tc)
require.NoError(t, err)
require.Equal(t, []string{"8080"}, tc.Tags)
}