@@ -3,46 +3,91 @@ package config
3
3
import (
4
4
"os"
5
5
"path/filepath"
6
+ "strings"
6
7
"testing"
7
8
)
8
9
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
+ }
11
21
12
- t . Run ( "ValidConfigFileWithDeniedResources" , func (t * testing.T ) {
13
- validConfigContent := `
22
+ func TestReadConfigInvalid (t * testing.T ) {
23
+ invalidConfigPath := writeConfig ( t , `
14
24
[[denied_resources]]
15
25
group = "apps"
16
26
version = "v1"
17
27
kind = "Deployment"
18
-
19
28
[[denied_resources]]
20
29
group = "rbac.authorization.k8s.io"
21
30
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" )
27
38
}
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
+ }
28
50
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 ) {
30
65
if err != nil {
31
66
t .Fatalf ("ReadConfig returned an error for a valid file: %v" , err )
32
67
}
33
-
34
68
if config == nil {
35
69
t .Fatal ("ReadConfig returned a nil config for a valid file" )
36
70
}
37
-
71
+ })
72
+ t .Run ("denied resources are parsed correctly" , func (t * testing.T ) {
38
73
if len (config .DeniedResources ) != 2 {
39
74
t .Fatalf ("Expected 2 denied resources, got %d" , len (config .DeniedResources ))
40
75
}
41
-
42
76
if config .DeniedResources [0 ].Group != "apps" ||
43
77
config .DeniedResources [0 ].Version != "v1" ||
44
78
config .DeniedResources [0 ].Kind != "Deployment" {
45
79
t .Errorf ("Unexpected denied resources: %v" , config .DeniedResources [0 ])
46
80
}
47
81
})
48
82
}
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
+ }
0 commit comments