@@ -3,46 +3,91 @@ package config
33import (
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]]
1525group = "apps"
1626version = "v1"
1727kind = "Deployment"
18-
1928[[denied_resources]]
2029group = "rbac.authorization.k8s.io"
2130version = "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+ }
0 commit comments