Skip to content

Commit f668658

Browse files
authored
test(cmd): additional test cases for config flags
Relates to #131
1 parent 754da19 commit f668658

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

pkg/kubernetes-mcp-server/cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ func NewMCPServer(streams genericiooptions.IOStreams) *cobra.Command {
9090
},
9191
}
9292

93-
cmd.Flags().StringVar(&o.ConfigPath, "config", o.ConfigPath, "Path of the config file. Each profile has its set of defaults.")
9493
cmd.Flags().BoolVar(&o.Version, "version", o.Version, "Print version information and quit")
9594
cmd.Flags().IntVar(&o.LogLevel, "log-level", o.LogLevel, "Set the log level (from 0 to 9)")
95+
cmd.Flags().StringVar(&o.ConfigPath, "config", o.ConfigPath, "Path of the config file. Each profile has its set of defaults.")
9696
cmd.Flags().IntVar(&o.SSEPort, "sse-port", o.SSEPort, "Start a SSE server on the specified port")
9797
cmd.Flags().IntVar(&o.HttpPort, "http-port", o.HttpPort, "Start a streamable HTTP server on the specified port")
9898
cmd.Flags().StringVar(&o.SSEBaseUrl, "sse-base-url", o.SSEBaseUrl, "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
@@ -145,13 +145,14 @@ func (m *MCPServerOptions) Run() error {
145145
return fmt.Errorf("Invalid output name: %s, valid names are: %s\n", m.ListOutput, strings.Join(output.Names, ", "))
146146
}
147147
klog.V(1).Info("Starting kubernetes-mcp-server")
148+
klog.V(1).Infof(" - Config: %s", m.ConfigPath)
148149
klog.V(1).Infof(" - Profile: %s", profile.GetName())
149150
klog.V(1).Infof(" - ListOutput: %s", listOutput.GetName())
150151
klog.V(1).Infof(" - Read-only mode: %t", m.ReadOnly)
151152
klog.V(1).Infof(" - Disable destructive tools: %t", m.DisableDestructive)
152153

153154
if m.Version {
154-
fmt.Fprintf(m.Out, "%s\n", version.Version)
155+
_, _ = fmt.Fprintf(m.Out, "%s\n", version.Version)
155156
return nil
156157
}
157158
mcpServer, err := mcp.NewServer(mcp.Configuration{

pkg/kubernetes-mcp-server/cmd/root_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"bytes"
55
"io"
66
"os"
7+
"path/filepath"
8+
"regexp"
9+
"runtime"
710
"strings"
811
"testing"
912

@@ -41,6 +44,30 @@ func TestVersion(t *testing.T) {
4144
}
4245
}
4346

47+
func TestConfig(t *testing.T) {
48+
t.Run("defaults to none", func(t *testing.T) {
49+
ioStreams, out := testStream()
50+
rootCmd := NewMCPServer(ioStreams)
51+
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
52+
expectedConfig := `" - Config: "`
53+
if err := rootCmd.Execute(); !strings.Contains(out.String(), expectedConfig) {
54+
t.Fatalf("Expected config to be %s, got %s %v", expectedConfig, out.String(), err)
55+
}
56+
})
57+
t.Run("set with --config", func(t *testing.T) {
58+
ioStreams, out := testStream()
59+
rootCmd := NewMCPServer(ioStreams)
60+
_, file, _, _ := runtime.Caller(0)
61+
emptyConfigPath := filepath.Join(filepath.Dir(file), "testdata", "empty-config.toml")
62+
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--config", emptyConfigPath})
63+
_ = rootCmd.Execute()
64+
expected := `(?m)\" - Config\:[^\"]+empty-config\.toml\"`
65+
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
66+
t.Fatalf("Expected config to be %s, got %s %v", expected, out.String(), err)
67+
}
68+
})
69+
}
70+
4471
func TestProfile(t *testing.T) {
4572
t.Run("available", func(t *testing.T) {
4673
ioStreams, _ := testStream()
@@ -59,6 +86,16 @@ func TestProfile(t *testing.T) {
5986
t.Fatalf("Expected profile 'full', got %s %v", out, err)
6087
}
6188
})
89+
t.Run("set with --profile", func(t *testing.T) {
90+
ioStreams, out := testStream()
91+
rootCmd := NewMCPServer(ioStreams)
92+
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--profile", "full"}) // TODO: change by some non-default profile
93+
_ = rootCmd.Execute()
94+
expected := `(?m)\" - Profile\: full\"`
95+
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
96+
t.Fatalf("Expected profile to be %s, got %s %v", expected, out.String(), err)
97+
}
98+
})
6299
}
63100

64101
func TestListOutput(t *testing.T) {
@@ -79,6 +116,16 @@ func TestListOutput(t *testing.T) {
79116
t.Fatalf("Expected list-output 'table', got %s %v", out, err)
80117
}
81118
})
119+
t.Run("set with --list-output", func(t *testing.T) {
120+
ioStreams, out := testStream()
121+
rootCmd := NewMCPServer(ioStreams)
122+
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--list-output", "yaml"})
123+
_ = rootCmd.Execute()
124+
expected := `(?m)\" - ListOutput\: yaml\"`
125+
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
126+
t.Fatalf("Expected list-output to be %s, got %s %v", expected, out.String(), err)
127+
}
128+
})
82129
}
83130

84131
func TestReadOnly(t *testing.T) {
@@ -90,6 +137,16 @@ func TestReadOnly(t *testing.T) {
90137
t.Fatalf("Expected read-only mode false, got %s %v", out, err)
91138
}
92139
})
140+
t.Run("set with --read-only", func(t *testing.T) {
141+
ioStreams, out := testStream()
142+
rootCmd := NewMCPServer(ioStreams)
143+
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--read-only"})
144+
_ = rootCmd.Execute()
145+
expected := `(?m)\" - Read-only mode\: true\"`
146+
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
147+
t.Fatalf("Expected read-only mode to be %s, got %s %v", expected, out.String(), err)
148+
}
149+
})
93150
}
94151

95152
func TestDisableDestructive(t *testing.T) {
@@ -101,4 +158,14 @@ func TestDisableDestructive(t *testing.T) {
101158
t.Fatalf("Expected disable destructive false, got %s %v", out, err)
102159
}
103160
})
161+
t.Run("set with --disable-destructive", func(t *testing.T) {
162+
ioStreams, out := testStream()
163+
rootCmd := NewMCPServer(ioStreams)
164+
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--disable-destructive"})
165+
_ = rootCmd.Execute()
166+
expected := `(?m)\" - Disable destructive tools\: true\"`
167+
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
168+
t.Fatalf("Expected disable-destructive mode to be %s, got %s %v", expected, out.String(), err)
169+
}
170+
})
104171
}

pkg/kubernetes-mcp-server/cmd/testdata/empty-config.toml

Whitespace-only changes.

0 commit comments

Comments
 (0)