test(config): additional test cases for config errors

Relates to #131
This commit is contained in:
Marc Nuri
2025-06-30 15:05:52 +02:00
committed by GitHub
parent cd1cb1a630
commit b777972c14
2 changed files with 72 additions and 14 deletions

View File

@@ -3,14 +3,53 @@ package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestReadConfig(t *testing.T) {
tempDir := t.TempDir()
func TestReadConfigMissingFile(t *testing.T) {
config, err := ReadConfig("non-existent-config.toml")
t.Run("returns error for missing file", func(t *testing.T) {
if err == nil {
t.Fatal("Expected error for missing file, got nil")
}
if config != nil {
t.Fatalf("Expected nil config for missing file, got %v", config)
}
})
}
t.Run("ValidConfigFileWithDeniedResources", func(t *testing.T) {
validConfigContent := `
func TestReadConfigInvalid(t *testing.T) {
invalidConfigPath := writeConfig(t, `
[[denied_resources]]
group = "apps"
version = "v1"
kind = "Deployment"
[[denied_resources]]
group = "rbac.authorization.k8s.io"
version = "v1"
kind = "Role
`)
config, err := ReadConfig(invalidConfigPath)
t.Run("returns error for invalid file", func(t *testing.T) {
if err == nil {
t.Fatal("Expected error for invalid file, got nil")
}
if config != nil {
t.Fatalf("Expected nil config for invalid file, got %v", config)
}
})
t.Run("error message contains toml error with line number", func(t *testing.T) {
expectedError := "toml: line 9"
if err != nil && !strings.HasPrefix(err.Error(), expectedError) {
t.Fatalf("Expected error message '%s' to contain line number, got %v", expectedError, err)
}
})
}
func TestReadConfigValid(t *testing.T) {
validConfigPath := writeConfig(t, `
[[denied_resources]]
group = "apps"
version = "v1"
@@ -19,26 +58,21 @@ kind = "Deployment"
[[denied_resources]]
group = "rbac.authorization.k8s.io"
version = "v1"
`
validConfigPath := filepath.Join(tempDir, "valid_denied_config.toml")
err := os.WriteFile(validConfigPath, []byte(validConfigContent), 0644)
if err != nil {
t.Fatalf("Failed to write valid config file: %v", err)
}
`)
config, err := ReadConfig(validConfigPath)
config, err := ReadConfig(validConfigPath)
t.Run("reads and unmarshalls file", func(t *testing.T) {
if err != nil {
t.Fatalf("ReadConfig returned an error for a valid file: %v", err)
}
if config == nil {
t.Fatal("ReadConfig returned a nil config for a valid file")
}
})
t.Run("denied resources are parsed correctly", func(t *testing.T) {
if len(config.DeniedResources) != 2 {
t.Fatalf("Expected 2 denied resources, got %d", len(config.DeniedResources))
}
if config.DeniedResources[0].Group != "apps" ||
config.DeniedResources[0].Version != "v1" ||
config.DeniedResources[0].Kind != "Deployment" {
@@ -46,3 +80,14 @@ version = "v1"
}
})
}
func writeConfig(t *testing.T, content string) string {
t.Helper()
tempDir := t.TempDir()
path := filepath.Join(tempDir, "config.toml")
err := os.WriteFile(path, []byte(content), 0644)
if err != nil {
t.Fatalf("Failed to write config file %s: %v", path, err)
}
return path
}

View File

@@ -66,6 +66,19 @@ func TestConfig(t *testing.T) {
t.Fatalf("Expected config to be %s, got %s %v", expected, out.String(), err)
}
})
t.Run("invalid path throws error", func(t *testing.T) {
ioStreams, _ := testStream()
rootCmd := NewMCPServer(ioStreams)
rootCmd.SetArgs([]string{"--version", "--log-level=1", "--config", "invalid-path-to-config.toml"})
err := rootCmd.Execute()
if err == nil {
t.Fatal("Expected error for invalid config path, got nil")
}
expected := "open invalid-path-to-config.toml: "
if !strings.HasPrefix(err.Error(), expected) {
t.Fatalf("Expected error to be %s, got %s", expected, err.Error())
}
})
}
func TestProfile(t *testing.T) {