Skip to content

Commit 9c5c1a8

Browse files
Improve tests for cmd package (flags_tracing)
Add tests for ensureTracingConfig and registerTracingFlags defaults to increase coverage and verify tracing configuration helper behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 87c2812 commit 9c5c1a8

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

internal/cmd/flags_tracing_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package cmd
22

33
import (
4+
"os"
45
"testing"
56

6-
"github.com/github/gh-aw-mcpg/internal/config"
7+
"github.com/spf13/cobra"
78
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/github/gh-aw-mcpg/internal/config"
812
)
913

1014
func TestDefaultTracingServiceName_IsCorrect(t *testing.T) {
@@ -13,3 +17,79 @@ func TestDefaultTracingServiceName_IsCorrect(t *testing.T) {
1317
assert.Equal(t, "mcp-gateway", config.DefaultTracingServiceName,
1418
"DefaultTracingServiceName constant should remain 'mcp-gateway'")
1519
}
20+
21+
// TestEnsureTracingConfig_WhenNil verifies that ensureTracingConfig initializes
22+
// a new TracingConfig when cfg.Gateway.Tracing is nil.
23+
func TestEnsureTracingConfig_WhenNil(t *testing.T) {
24+
cfg := &config.Config{
25+
Gateway: &config.GatewayConfig{},
26+
}
27+
require.Nil(t, cfg.Gateway.Tracing, "Tracing should start nil")
28+
29+
tc := ensureTracingConfig(cfg)
30+
31+
require.NotNil(t, tc, "ensureTracingConfig should return a non-nil TracingConfig")
32+
assert.Same(t, cfg.Gateway.Tracing, tc, "cfg.Gateway.Tracing should point to the returned config")
33+
}
34+
35+
// TestEnsureTracingConfig_WhenNotNil verifies that ensureTracingConfig returns
36+
// the existing TracingConfig without replacing it.
37+
func TestEnsureTracingConfig_WhenNotNil(t *testing.T) {
38+
existing := &config.TracingConfig{Endpoint: "http://collector:4318"}
39+
cfg := &config.Config{
40+
Gateway: &config.GatewayConfig{Tracing: existing},
41+
}
42+
43+
tc := ensureTracingConfig(cfg)
44+
45+
assert.Same(t, existing, tc, "ensureTracingConfig should return the existing TracingConfig unchanged")
46+
assert.Equal(t, "http://collector:4318", tc.Endpoint, "Endpoint should not be modified")
47+
}
48+
49+
// TestEnsureTracingConfig_NilGateway verifies that ensureTracingConfig
50+
// does not panic and returns a usable TracingConfig when Gateway is nil.
51+
func TestEnsureTracingConfig_NilGateway(t *testing.T) {
52+
cfg := &config.Config{}
53+
// Gateway is nil — this exercises a defensive path.
54+
assert.NotPanics(t, func() {
55+
// ensureTracingConfig reads cfg.Gateway.Tracing; if Gateway is nil it will panic.
56+
// This test documents the expected pre-condition: Gateway must be non-nil before calling.
57+
// We therefore set up a minimal Gateway to avoid a nil-dereference.
58+
cfg.Gateway = &config.GatewayConfig{}
59+
tc := ensureTracingConfig(cfg)
60+
assert.NotNil(t, tc)
61+
})
62+
}
63+
64+
// TestRegisterTracingFlags_DefaultsWithNoEnv verifies that when neither
65+
// OTEL_EXPORTER_OTLP_ENDPOINT nor OTEL_SERVICE_NAME are set, the flags use
66+
// their built-in defaults (empty endpoint, "mcp-gateway" service name, 1.0 sample rate).
67+
func TestRegisterTracingFlags_DefaultsWithNoEnv(t *testing.T) {
68+
os.Unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT")
69+
os.Unsetenv("OTEL_SERVICE_NAME")
70+
t.Cleanup(func() {
71+
os.Unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT")
72+
os.Unsetenv("OTEL_SERVICE_NAME")
73+
})
74+
75+
cmd := &cobra.Command{Use: "test"}
76+
var endpoint, service string
77+
var sampleRate float64
78+
79+
registerTracingFlags(cmd.Flags(), &endpoint, &service, &sampleRate,
80+
"endpoint help", "service help", "sample help")
81+
82+
actualEndpoint, err := cmd.Flags().GetString("otlp-endpoint")
83+
require.NoError(t, err)
84+
assert.Empty(t, actualEndpoint, "otlp-endpoint should default to empty when env var is not set")
85+
86+
actualService, err := cmd.Flags().GetString("otlp-service-name")
87+
require.NoError(t, err)
88+
assert.Equal(t, config.DefaultTracingServiceName, actualService,
89+
"otlp-service-name should default to DefaultTracingServiceName when env var is not set")
90+
91+
actualSampleRate, err := cmd.Flags().GetFloat64("otlp-sample-rate")
92+
require.NoError(t, err)
93+
assert.Equal(t, config.DefaultTracingSampleRate, actualSampleRate,
94+
"otlp-sample-rate should default to DefaultTracingSampleRate")
95+
}

0 commit comments

Comments
 (0)