|
1 | 1 | package config
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "io/fs" |
4 | 6 | "os"
|
5 | 7 | "path/filepath"
|
6 | 8 | "strings"
|
7 | 9 | "testing"
|
| 10 | + |
| 11 | + "github.com/stretchr/testify/suite" |
8 | 12 | )
|
9 | 13 |
|
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 |
| - } |
| 14 | +type ConfigSuite struct { |
| 15 | + suite.Suite |
| 16 | +} |
| 17 | + |
| 18 | +func (s *ConfigSuite) TestReadConfigMissingFile() { |
| 19 | + config, err := Read("non-existent-config.toml") |
| 20 | + s.Run("returns error for missing file", func() { |
| 21 | + s.Require().NotNil(err, "Expected error for missing file, got nil") |
| 22 | + s.True(errors.Is(err, fs.ErrNotExist), "Expected ErrNotExist, got %v", err) |
| 23 | + }) |
| 24 | + s.Run("returns nil config for missing file", func() { |
| 25 | + s.Nil(config, "Expected nil config for missing file") |
19 | 26 | })
|
20 | 27 | }
|
21 | 28 |
|
22 |
| -func TestReadConfigInvalid(t *testing.T) { |
23 |
| - invalidConfigPath := writeConfig(t, ` |
24 |
| -[[denied_resources]] |
25 |
| -group = "apps" |
26 |
| -version = "v1" |
27 |
| -kind = "Deployment" |
28 |
| -[[denied_resources]] |
29 |
| -group = "rbac.authorization.k8s.io" |
30 |
| -version = "v1" |
31 |
| -kind = "Role |
32 |
| -`) |
| 29 | +func (s *ConfigSuite) TestReadConfigInvalid() { |
| 30 | + invalidConfigPath := s.writeConfig(` |
| 31 | + [[denied_resources]] |
| 32 | + group = "apps" |
| 33 | + version = "v1" |
| 34 | + kind = "Deployment" |
| 35 | + [[denied_resources]] |
| 36 | + group = "rbac.authorization.k8s.io" |
| 37 | + version = "v1" |
| 38 | + kind = "Role |
| 39 | + `) |
33 | 40 |
|
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") |
38 |
| - } |
39 |
| - if config != nil { |
40 |
| - t.Fatalf("Expected nil config for invalid file, got %v", config) |
41 |
| - } |
| 41 | + config, err := Read(invalidConfigPath) |
| 42 | + s.Run("returns error for invalid file", func() { |
| 43 | + s.Require().NotNil(err, "Expected error for invalid file, got nil") |
42 | 44 | })
|
43 |
| - t.Run("error message contains toml error with line number", func(t *testing.T) { |
| 45 | + s.Run("error message contains toml error with line number", func() { |
44 | 46 | 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 |
| - } |
| 47 | + s.Truef(strings.HasPrefix(err.Error(), expectedError), "Expected error message to contain line number, got %v", err) |
| 48 | + }) |
| 49 | + s.Run("returns nil config for invalid file", func() { |
| 50 | + s.Nil(config, "Expected nil config for missing file") |
48 | 51 | })
|
49 | 52 | }
|
50 | 53 |
|
51 |
| -func TestReadConfigValid(t *testing.T) { |
52 |
| - validConfigPath := writeConfig(t, ` |
53 |
| -log_level = 1 |
54 |
| -port = "9999" |
55 |
| -sse_base_url = "https://example.com" |
56 |
| -kubeconfig = "./path/to/config" |
57 |
| -list_output = "yaml" |
58 |
| -read_only = true |
59 |
| -disable_destructive = true |
| 54 | +func (s *ConfigSuite) TestReadConfigValid() { |
| 55 | + validConfigPath := s.writeConfig(` |
| 56 | + log_level = 1 |
| 57 | + port = "9999" |
| 58 | + sse_base_url = "https://example.com" |
| 59 | + kubeconfig = "./path/to/config" |
| 60 | + list_output = "yaml" |
| 61 | + read_only = true |
| 62 | + disable_destructive = true |
60 | 63 |
|
61 |
| -denied_resources = [ |
62 |
| - {group = "apps", version = "v1", kind = "Deployment"}, |
63 |
| - {group = "rbac.authorization.k8s.io", version = "v1", kind = "Role"} |
64 |
| -] |
| 64 | + toolsets = ["core", "config", "helm", "metrics"] |
| 65 | + |
| 66 | + enabled_tools = ["configuration_view", "events_list", "namespaces_list", "pods_list", "resources_list", "resources_get", "resources_create_or_update", "resources_delete"] |
| 67 | + disabled_tools = ["pods_delete", "pods_top", "pods_log", "pods_run", "pods_exec"] |
65 | 68 |
|
66 |
| -enabled_tools = ["configuration_view", "events_list", "namespaces_list", "pods_list", "resources_list", "resources_get", "resources_create_or_update", "resources_delete"] |
67 |
| -disabled_tools = ["pods_delete", "pods_top", "pods_log", "pods_run", "pods_exec"] |
68 |
| -`) |
| 69 | + denied_resources = [ |
| 70 | + {group = "apps", version = "v1", kind = "Deployment"}, |
| 71 | + {group = "rbac.authorization.k8s.io", version = "v1", kind = "Role"} |
| 72 | + ] |
| 73 | + |
| 74 | + `) |
69 | 75 |
|
70 |
| - config, err := ReadConfig(validConfigPath) |
71 |
| - t.Run("reads and unmarshalls file", func(t *testing.T) { |
72 |
| - if err != nil { |
73 |
| - t.Fatalf("ReadConfig returned an error for a valid file: %v", err) |
74 |
| - } |
75 |
| - if config == nil { |
76 |
| - t.Fatal("ReadConfig returned a nil config for a valid file") |
77 |
| - } |
| 76 | + config, err := Read(validConfigPath) |
| 77 | + s.Require().NotNil(config) |
| 78 | + s.Run("reads and unmarshalls file", func() { |
| 79 | + s.Nil(err, "Expected nil error for valid file") |
| 80 | + s.Require().NotNil(config, "Expected non-nil config for valid file") |
78 | 81 | })
|
79 |
| - t.Run("denied resources are parsed correctly", func(t *testing.T) { |
80 |
| - if len(config.DeniedResources) != 2 { |
81 |
| - t.Fatalf("Expected 2 denied resources, got %d", len(config.DeniedResources)) |
82 |
| - } |
83 |
| - if config.DeniedResources[0].Group != "apps" || |
84 |
| - config.DeniedResources[0].Version != "v1" || |
85 |
| - config.DeniedResources[0].Kind != "Deployment" { |
86 |
| - t.Errorf("Unexpected denied resources: %v", config.DeniedResources[0]) |
87 |
| - } |
| 82 | + s.Run("log_level parsed correctly", func() { |
| 83 | + s.Equalf(1, config.LogLevel, "Expected LogLevel to be 1, got %d", config.LogLevel) |
88 | 84 | })
|
89 |
| - t.Run("log_level parsed correctly", func(t *testing.T) { |
90 |
| - if config.LogLevel != 1 { |
91 |
| - t.Fatalf("Unexpected log level: %v", config.LogLevel) |
92 |
| - } |
| 85 | + s.Run("port parsed correctly", func() { |
| 86 | + s.Equalf("9999", config.Port, "Expected Port to be 9999, got %s", config.Port) |
93 | 87 | })
|
94 |
| - t.Run("port parsed correctly", func(t *testing.T) { |
95 |
| - if config.Port != "9999" { |
96 |
| - t.Fatalf("Unexpected port value: %v", config.Port) |
97 |
| - } |
| 88 | + s.Run("sse_base_url parsed correctly", func() { |
| 89 | + s.Equalf("https://example.com", config.SSEBaseURL, "Expected SSEBaseURL to be https://example.com, got %s", config.SSEBaseURL) |
98 | 90 | })
|
99 |
| - t.Run("sse_base_url parsed correctly", func(t *testing.T) { |
100 |
| - if config.SSEBaseURL != "https://example.com" { |
101 |
| - t.Fatalf("Unexpected sse_base_url value: %v", config.SSEBaseURL) |
102 |
| - } |
| 91 | + s.Run("kubeconfig parsed correctly", func() { |
| 92 | + s.Equalf("./path/to/config", config.KubeConfig, "Expected KubeConfig to be ./path/to/config, got %s", config.KubeConfig) |
103 | 93 | })
|
104 |
| - t.Run("kubeconfig parsed correctly", func(t *testing.T) { |
105 |
| - if config.KubeConfig != "./path/to/config" { |
106 |
| - t.Fatalf("Unexpected kubeconfig value: %v", config.KubeConfig) |
107 |
| - } |
| 94 | + s.Run("list_output parsed correctly", func() { |
| 95 | + s.Equalf("yaml", config.ListOutput, "Expected ListOutput to be yaml, got %s", config.ListOutput) |
| 96 | + }) |
| 97 | + s.Run("read_only parsed correctly", func() { |
| 98 | + s.Truef(config.ReadOnly, "Expected ReadOnly to be true, got %v", config.ReadOnly) |
| 99 | + }) |
| 100 | + s.Run("disable_destructive parsed correctly", func() { |
| 101 | + s.Truef(config.DisableDestructive, "Expected DisableDestructive to be true, got %v", config.DisableDestructive) |
108 | 102 | })
|
109 |
| - t.Run("list_output parsed correctly", func(t *testing.T) { |
110 |
| - if config.ListOutput != "yaml" { |
111 |
| - t.Fatalf("Unexpected list_output value: %v", config.ListOutput) |
| 103 | + s.Run("toolsets", func() { |
| 104 | + s.Require().Lenf(config.Toolsets, 4, "Expected 4 toolsets, got %d", len(config.Toolsets)) |
| 105 | + for _, toolset := range []string{"core", "config", "helm", "metrics"} { |
| 106 | + s.Containsf(config.Toolsets, toolset, "Expected toolsets to contain %s", toolset) |
112 | 107 | }
|
113 | 108 | })
|
114 |
| - t.Run("read_only parsed correctly", func(t *testing.T) { |
115 |
| - if !config.ReadOnly { |
116 |
| - t.Fatalf("Unexpected read-only mode: %v", config.ReadOnly) |
| 109 | + s.Run("enabled_tools", func() { |
| 110 | + s.Require().Lenf(config.EnabledTools, 8, "Expected 8 enabled tools, got %d", len(config.EnabledTools)) |
| 111 | + for _, tool := range []string{"configuration_view", "events_list", "namespaces_list", "pods_list", "resources_list", "resources_get", "resources_create_or_update", "resources_delete"} { |
| 112 | + s.Containsf(config.EnabledTools, tool, "Expected enabled tools to contain %s", tool) |
117 | 113 | }
|
118 | 114 | })
|
119 |
| - t.Run("disable_destructive parsed correctly", func(t *testing.T) { |
120 |
| - if !config.DisableDestructive { |
121 |
| - t.Fatalf("Unexpected disable destructive: %v", config.DisableDestructive) |
| 115 | + s.Run("disabled_tools", func() { |
| 116 | + s.Require().Lenf(config.DisabledTools, 5, "Expected 5 disabled tools, got %d", len(config.DisabledTools)) |
| 117 | + for _, tool := range []string{"pods_delete", "pods_top", "pods_log", "pods_run", "pods_exec"} { |
| 118 | + s.Containsf(config.DisabledTools, tool, "Expected disabled tools to contain %s", tool) |
122 | 119 | }
|
123 | 120 | })
|
124 |
| - t.Run("enabled_tools parsed correctly", func(t *testing.T) { |
125 |
| - if len(config.EnabledTools) != 8 { |
126 |
| - t.Fatalf("Unexpected enabled tools: %v", config.EnabledTools) |
| 121 | + s.Run("denied_resources", func() { |
| 122 | + s.Require().Lenf(config.DeniedResources, 2, "Expected 2 denied resources, got %d", len(config.DeniedResources)) |
| 123 | + s.Run("contains apps/v1/Deployment", func() { |
| 124 | + s.Contains(config.DeniedResources, GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, |
| 125 | + "Expected denied resources to contain apps/v1/Deployment") |
| 126 | + }) |
| 127 | + s.Run("contains rbac.authorization.k8s.io/v1/Role", func() { |
| 128 | + s.Contains(config.DeniedResources, GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "Role"}, |
| 129 | + "Expected denied resources to contain rbac.authorization.k8s.io/v1/Role") |
| 130 | + }) |
| 131 | + }) |
| 132 | +} |
127 | 133 |
|
128 |
| - } |
129 |
| - for i, tool := range []string{"configuration_view", "events_list", "namespaces_list", "pods_list", "resources_list", "resources_get", "resources_create_or_update", "resources_delete"} { |
130 |
| - if config.EnabledTools[i] != tool { |
131 |
| - t.Errorf("Expected enabled tool %d to be %s, got %s", i, tool, config.EnabledTools[i]) |
132 |
| - } |
133 |
| - } |
| 134 | +func (s *ConfigSuite) TestReadConfigValidPreservesDefaultsForMissingFields() { |
| 135 | + validConfigPath := s.writeConfig(` |
| 136 | + port = "1337" |
| 137 | + `) |
| 138 | + |
| 139 | + config, err := Read(validConfigPath) |
| 140 | + s.Require().NotNil(config) |
| 141 | + s.Run("reads and unmarshalls file", func() { |
| 142 | + s.Nil(err, "Expected nil error for valid file") |
| 143 | + s.Require().NotNil(config, "Expected non-nil config for valid file") |
134 | 144 | })
|
135 |
| - t.Run("disabled_tools parsed correctly", func(t *testing.T) { |
136 |
| - if len(config.DisabledTools) != 5 { |
137 |
| - t.Fatalf("Unexpected disabled tools: %v", config.DisabledTools) |
138 |
| - } |
139 |
| - for i, tool := range []string{"pods_delete", "pods_top", "pods_log", "pods_run", "pods_exec"} { |
140 |
| - if config.DisabledTools[i] != tool { |
141 |
| - t.Errorf("Expected disabled tool %d to be %s, got %s", i, tool, config.DisabledTools[i]) |
142 |
| - } |
| 145 | + s.Run("log_level defaulted correctly", func() { |
| 146 | + s.Equalf(0, config.LogLevel, "Expected LogLevel to be 0, got %d", config.LogLevel) |
| 147 | + }) |
| 148 | + s.Run("port parsed correctly", func() { |
| 149 | + s.Equalf("1337", config.Port, "Expected Port to be 9999, got %s", config.Port) |
| 150 | + }) |
| 151 | + s.Run("list_output defaulted correctly", func() { |
| 152 | + s.Equalf("table", config.ListOutput, "Expected ListOutput to be table, got %s", config.ListOutput) |
| 153 | + }) |
| 154 | + s.Run("toolsets defaulted correctly", func() { |
| 155 | + s.Require().Lenf(config.Toolsets, 3, "Expected 3 toolsets, got %d", len(config.Toolsets)) |
| 156 | + for _, toolset := range []string{"core", "config", "helm"} { |
| 157 | + s.Containsf(config.Toolsets, toolset, "Expected toolsets to contain %s", toolset) |
143 | 158 | }
|
144 | 159 | })
|
145 | 160 | }
|
146 | 161 |
|
147 |
| -func writeConfig(t *testing.T, content string) string { |
148 |
| - t.Helper() |
149 |
| - tempDir := t.TempDir() |
| 162 | +func (s *ConfigSuite) writeConfig(content string) string { |
| 163 | + s.T().Helper() |
| 164 | + tempDir := s.T().TempDir() |
150 | 165 | path := filepath.Join(tempDir, "config.toml")
|
151 | 166 | err := os.WriteFile(path, []byte(content), 0644)
|
152 | 167 | if err != nil {
|
153 |
| - t.Fatalf("Failed to write config file %s: %v", path, err) |
| 168 | + s.T().Fatalf("Failed to write config file %s: %v", path, err) |
154 | 169 | }
|
155 | 170 | return path
|
156 | 171 | }
|
| 172 | + |
| 173 | +func TestConfig(t *testing.T) { |
| 174 | + suite.Run(t, new(ConfigSuite)) |
| 175 | +} |
0 commit comments