Files
zeekay 2aa66a566e refactor(config): debrand env — O11Y prefix, DATASTORE store name (no SIGNOZ/CLICKHOUSE in env)
Canonical flat operator key O11Y_DATASTORE_DSN aliases into telemetrystore.datastore.dsn; long structured form kept as internal fallback. Store stays ClickHouse wire-protocol (driver/provider/tables unchanged).
2026-07-02 16:50:28 -07:00

109 lines
2.5 KiB
Go

package envprovider
import (
"context"
"os"
"strings"
"testing"
"github.com/hanzoai/o11y/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// clearEnv unsets all existing O11Y_* env vars for the duration of the test.
func clearEnv(t *testing.T) {
t.Helper()
for _, kv := range os.Environ() {
if strings.HasPrefix(kv, prefix) {
key := strings.SplitN(kv, "=", 2)[0]
orig, _ := os.LookupEnv(key)
_ = os.Unsetenv(key)
t.Cleanup(func() { _ = os.Setenv(key, orig) })
}
}
}
func TestGetWithStrings(t *testing.T) {
clearEnv(t)
t.Setenv("O11Y_K1_K2", "string")
t.Setenv("O11Y_K3__K4", "string")
t.Setenv("O11Y_K5__K6_K7__K8", "string")
t.Setenv("O11Y_K9___K10", "string")
t.Setenv("O11Y_K11____K12", "string")
expected := map[string]any{
"k1::k2": "string",
"k3_k4": "string",
"k5_k6::k7_k8": "string",
"k9_::k10": "string",
"k11__k12": "string",
}
provider := New(config.ProviderConfig{})
actual, err := provider.Get(context.Background(), config.MustNewUri("env:"))
require.NoError(t, err)
assert.Equal(t, expected, actual.All())
}
func TestGetWithNoPrefix(t *testing.T) {
clearEnv(t)
t.Setenv("K1_K2", "string")
t.Setenv("K3_K4", "string")
expected := map[string]any{}
provider := New(config.ProviderConfig{})
actual, err := provider.Get(context.Background(), config.MustNewUri("env:"))
require.NoError(t, err)
assert.Equal(t, expected, actual.All())
}
func TestGetWithGoTypes(t *testing.T) {
clearEnv(t)
t.Setenv("O11Y_BOOL", "true")
t.Setenv("O11Y_STRING", "string")
t.Setenv("O11Y_INT", "1")
t.Setenv("O11Y_SLICE", "[1,2]")
expected := map[string]any{
"bool": "true",
"int": "1",
"slice": "[1,2]",
"string": "string",
}
provider := New(config.ProviderConfig{})
actual, err := provider.Get(context.Background(), config.MustNewUri("env:"))
require.NoError(t, err)
assert.Equal(t, expected, actual.All())
}
func TestGetWithGoTypesWithUnmarshal(t *testing.T) {
t.Setenv("O11Y_BOOL", "true")
t.Setenv("O11Y_STRING", "string")
t.Setenv("O11Y_INT", "1")
type test struct {
Bool bool `mapstructure:"bool"`
String string `mapstructure:"string"`
Int int `mapstructure:"int"`
}
expected := test{
Bool: true,
String: "string",
Int: 1,
}
provider := New(config.ProviderConfig{})
conf, err := provider.Get(context.Background(), config.MustNewUri("env:"))
require.NoError(t, err)
actual := test{}
err = conf.Unmarshal("", &actual)
require.NoError(t, err)
assert.Equal(t, expected, actual)
}