test(cmd): additional test cases for config flags

Relates to #131
This commit is contained in:
Marc Nuri
2025-06-19 16:26:53 +02:00
committed by GitHub
parent 754da19d81
commit f668658217
3 changed files with 70 additions and 2 deletions

View File

@@ -90,9 +90,9 @@ func NewMCPServer(streams genericiooptions.IOStreams) *cobra.Command {
},
}
cmd.Flags().StringVar(&o.ConfigPath, "config", o.ConfigPath, "Path of the config file. Each profile has its set of defaults.")
cmd.Flags().BoolVar(&o.Version, "version", o.Version, "Print version information and quit")
cmd.Flags().IntVar(&o.LogLevel, "log-level", o.LogLevel, "Set the log level (from 0 to 9)")
cmd.Flags().StringVar(&o.ConfigPath, "config", o.ConfigPath, "Path of the config file. Each profile has its set of defaults.")
cmd.Flags().IntVar(&o.SSEPort, "sse-port", o.SSEPort, "Start a SSE server on the specified port")
cmd.Flags().IntVar(&o.HttpPort, "http-port", o.HttpPort, "Start a streamable HTTP server on the specified port")
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 {
return fmt.Errorf("Invalid output name: %s, valid names are: %s\n", m.ListOutput, strings.Join(output.Names, ", "))
}
klog.V(1).Info("Starting kubernetes-mcp-server")
klog.V(1).Infof(" - Config: %s", m.ConfigPath)
klog.V(1).Infof(" - Profile: %s", profile.GetName())
klog.V(1).Infof(" - ListOutput: %s", listOutput.GetName())
klog.V(1).Infof(" - Read-only mode: %t", m.ReadOnly)
klog.V(1).Infof(" - Disable destructive tools: %t", m.DisableDestructive)
if m.Version {
fmt.Fprintf(m.Out, "%s\n", version.Version)
_, _ = fmt.Fprintf(m.Out, "%s\n", version.Version)
return nil
}
mcpServer, err := mcp.NewServer(mcp.Configuration{

View File

@@ -4,6 +4,9 @@ import (
"bytes"
"io"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
@@ -41,6 +44,30 @@ func TestVersion(t *testing.T) {
}
}
func TestConfig(t *testing.T) {
t.Run("defaults to none", func(t *testing.T) {
ioStreams, out := testStream()
rootCmd := NewMCPServer(ioStreams)
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
expectedConfig := `" - Config: "`
if err := rootCmd.Execute(); !strings.Contains(out.String(), expectedConfig) {
t.Fatalf("Expected config to be %s, got %s %v", expectedConfig, out.String(), err)
}
})
t.Run("set with --config", func(t *testing.T) {
ioStreams, out := testStream()
rootCmd := NewMCPServer(ioStreams)
_, file, _, _ := runtime.Caller(0)
emptyConfigPath := filepath.Join(filepath.Dir(file), "testdata", "empty-config.toml")
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--config", emptyConfigPath})
_ = rootCmd.Execute()
expected := `(?m)\" - Config\:[^\"]+empty-config\.toml\"`
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
t.Fatalf("Expected config to be %s, got %s %v", expected, out.String(), err)
}
})
}
func TestProfile(t *testing.T) {
t.Run("available", func(t *testing.T) {
ioStreams, _ := testStream()
@@ -59,6 +86,16 @@ func TestProfile(t *testing.T) {
t.Fatalf("Expected profile 'full', got %s %v", out, err)
}
})
t.Run("set with --profile", func(t *testing.T) {
ioStreams, out := testStream()
rootCmd := NewMCPServer(ioStreams)
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--profile", "full"}) // TODO: change by some non-default profile
_ = rootCmd.Execute()
expected := `(?m)\" - Profile\: full\"`
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
t.Fatalf("Expected profile to be %s, got %s %v", expected, out.String(), err)
}
})
}
func TestListOutput(t *testing.T) {
@@ -79,6 +116,16 @@ func TestListOutput(t *testing.T) {
t.Fatalf("Expected list-output 'table', got %s %v", out, err)
}
})
t.Run("set with --list-output", func(t *testing.T) {
ioStreams, out := testStream()
rootCmd := NewMCPServer(ioStreams)
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--list-output", "yaml"})
_ = rootCmd.Execute()
expected := `(?m)\" - ListOutput\: yaml\"`
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
t.Fatalf("Expected list-output to be %s, got %s %v", expected, out.String(), err)
}
})
}
func TestReadOnly(t *testing.T) {
@@ -90,6 +137,16 @@ func TestReadOnly(t *testing.T) {
t.Fatalf("Expected read-only mode false, got %s %v", out, err)
}
})
t.Run("set with --read-only", func(t *testing.T) {
ioStreams, out := testStream()
rootCmd := NewMCPServer(ioStreams)
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--read-only"})
_ = rootCmd.Execute()
expected := `(?m)\" - Read-only mode\: true\"`
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
t.Fatalf("Expected read-only mode to be %s, got %s %v", expected, out.String(), err)
}
})
}
func TestDisableDestructive(t *testing.T) {
@@ -101,4 +158,14 @@ func TestDisableDestructive(t *testing.T) {
t.Fatalf("Expected disable destructive false, got %s %v", out, err)
}
})
t.Run("set with --disable-destructive", func(t *testing.T) {
ioStreams, out := testStream()
rootCmd := NewMCPServer(ioStreams)
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--disable-destructive"})
_ = rootCmd.Execute()
expected := `(?m)\" - Disable destructive tools\: true\"`
if m, err := regexp.MatchString(expected, out.String()); !m || err != nil {
t.Fatalf("Expected disable-destructive mode to be %s, got %s %v", expected, out.String(), err)
}
})
}