Skip to content

Commit b777972

Browse files
authored
test(config): additional test cases for config errors
Relates to #131
1 parent cd1cb1a commit b777972

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

pkg/config/config_test.go

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,91 @@ package config
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78
)
89

9-
func TestReadConfig(t *testing.T) {
10-
tempDir := t.TempDir()
10+
func TestReadConfigMissingFile(t *testing.T) {
11+
config, err := ReadConfig("non-existent-config.toml")
12+
t.Run("returns error for missing file", func(t *testing.T) {
13+
if err == nil {
14+
t.Fatal("Expected error for missing file, got nil")
15+
}
16+
if config != nil {
17+
t.Fatalf("Expected nil config for missing file, got %v", config)
18+
}
19+
})
20+
}
1121

12-
t.Run("ValidConfigFileWithDeniedResources", func(t *testing.T) {
13-
validConfigContent := `
22+
func TestReadConfigInvalid(t *testing.T) {
23+
invalidConfigPath := writeConfig(t, `
1424
[[denied_resources]]
1525
group = "apps"
1626
version = "v1"
1727
kind = "Deployment"
18-
1928
[[denied_resources]]
2029
group = "rbac.authorization.k8s.io"
2130
version = "v1"
22-
`
23-
validConfigPath := filepath.Join(tempDir, "valid_denied_config.toml")
24-
err := os.WriteFile(validConfigPath, []byte(validConfigContent), 0644)
25-
if err != nil {
26-
t.Fatalf("Failed to write valid config file: %v", err)
31+
kind = "Role
32+
`)
33+
34+
config, err := ReadConfig(invalidConfigPath)
35+
t.Run("returns error for invalid file", func(t *testing.T) {
36+
if err == nil {
37+
t.Fatal("Expected error for invalid file, got nil")
2738
}
39+
if config != nil {
40+
t.Fatalf("Expected nil config for invalid file, got %v", config)
41+
}
42+
})
43+
t.Run("error message contains toml error with line number", func(t *testing.T) {
44+
expectedError := "toml: line 9"
45+
if err != nil && !strings.HasPrefix(err.Error(), expectedError) {
46+
t.Fatalf("Expected error message '%s' to contain line number, got %v", expectedError, err)
47+
}
48+
})
49+
}
2850

29-
config, err := ReadConfig(validConfigPath)
51+
func TestReadConfigValid(t *testing.T) {
52+
validConfigPath := writeConfig(t, `
53+
[[denied_resources]]
54+
group = "apps"
55+
version = "v1"
56+
kind = "Deployment"
57+
58+
[[denied_resources]]
59+
group = "rbac.authorization.k8s.io"
60+
version = "v1"
61+
`)
62+
63+
config, err := ReadConfig(validConfigPath)
64+
t.Run("reads and unmarshalls file", func(t *testing.T) {
3065
if err != nil {
3166
t.Fatalf("ReadConfig returned an error for a valid file: %v", err)
3267
}
33-
3468
if config == nil {
3569
t.Fatal("ReadConfig returned a nil config for a valid file")
3670
}
37-
71+
})
72+
t.Run("denied resources are parsed correctly", func(t *testing.T) {
3873
if len(config.DeniedResources) != 2 {
3974
t.Fatalf("Expected 2 denied resources, got %d", len(config.DeniedResources))
4075
}
41-
4276
if config.DeniedResources[0].Group != "apps" ||
4377
config.DeniedResources[0].Version != "v1" ||
4478
config.DeniedResources[0].Kind != "Deployment" {
4579
t.Errorf("Unexpected denied resources: %v", config.DeniedResources[0])
4680
}
4781
})
4882
}
83+
84+
func writeConfig(t *testing.T, content string) string {
85+
t.Helper()
86+
tempDir := t.TempDir()
87+
path := filepath.Join(tempDir, "config.toml")
88+
err := os.WriteFile(path, []byte(content), 0644)
89+
if err != nil {
90+
t.Fatalf("Failed to write config file %s: %v", path, err)
91+
}
92+
return path
93+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ func TestConfig(t *testing.T) {
6666
t.Fatalf("Expected config to be %s, got %s %v", expected, out.String(), err)
6767
}
6868
})
69+
t.Run("invalid path throws error", func(t *testing.T) {
70+
ioStreams, _ := testStream()
71+
rootCmd := NewMCPServer(ioStreams)
72+
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--config", "invalid-path-to-config.toml"})
73+
err := rootCmd.Execute()
74+
if err == nil {
75+
t.Fatal("Expected error for invalid config path, got nil")
76+
}
77+
expected := "open invalid-path-to-config.toml: "
78+
if !strings.HasPrefix(err.Error(), expected) {
79+
t.Fatalf("Expected error to be %s, got %s", expected, err.Error())
80+
}
81+
})
6982
}
7083

7184
func TestProfile(t *testing.T) {

0 commit comments

Comments
 (0)