|
| 1 | +package kiali |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/containers/kubernetes-mcp-server/internal/test" |
| 9 | + "github.com/containers/kubernetes-mcp-server/pkg/config" |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | +) |
| 12 | + |
| 13 | +type ConfigSuite struct { |
| 14 | + suite.Suite |
| 15 | + tempDir string |
| 16 | + caFile string |
| 17 | +} |
| 18 | + |
| 19 | +func (s *ConfigSuite) SetupTest() { |
| 20 | + // Create a temporary directory for test files |
| 21 | + tempDir, err := os.MkdirTemp("", "kiali-config-test-*") |
| 22 | + s.Require().NoError(err, "Failed to create temp directory") |
| 23 | + s.tempDir = tempDir |
| 24 | + |
| 25 | + // Create a test CA certificate file |
| 26 | + s.caFile = filepath.Join(s.tempDir, "ca.crt") |
| 27 | + err = os.WriteFile(s.caFile, []byte("test ca content"), 0644) |
| 28 | + s.Require().NoError(err, "Failed to write CA file") |
| 29 | +} |
| 30 | + |
| 31 | +func (s *ConfigSuite) TestConfigParser_ResolvesRelativePath() { |
| 32 | + // Create CA file in temp directory |
| 33 | + caFile := filepath.Join(s.tempDir, "ca.crt") |
| 34 | + err := os.WriteFile(caFile, []byte("test ca content"), 0644) |
| 35 | + s.Require().NoError(err, "Failed to write CA file") |
| 36 | + |
| 37 | + // Read config with configDirPath set to tempDir to resolve relative paths |
| 38 | + cfg := test.Must(config.ReadToml([]byte(` |
| 39 | + [toolset_configs.kiali] |
| 40 | + url = "https://kiali.example/" |
| 41 | + certificate_authority = "ca.crt" |
| 42 | + `), config.WithDirPath(s.tempDir))) |
| 43 | + |
| 44 | + // Get Kiali config |
| 45 | + kialiCfg, ok := cfg.GetToolsetConfig("kiali") |
| 46 | + s.Require().True(ok, "Kiali config should be present") |
| 47 | + kcfg, ok := kialiCfg.(*Config) |
| 48 | + s.Require().True(ok, "Kiali config should be of type *Config") |
| 49 | + |
| 50 | + // Verify the path was resolved to absolute |
| 51 | + expectedPath := caFile |
| 52 | + s.Equal(expectedPath, kcfg.CertificateAuthority, "Relative path should be resolved to absolute path") |
| 53 | +} |
| 54 | + |
| 55 | +func (s *ConfigSuite) TestConfigParser_PreservesAbsolutePath() { |
| 56 | + // Convert backslashes to forward slashes for TOML compatibility on Windows |
| 57 | + caFileForTOML := filepath.ToSlash(s.caFile) |
| 58 | + |
| 59 | + cfg := test.Must(config.ReadToml([]byte(` |
| 60 | + [toolset_configs.kiali] |
| 61 | + url = "https://kiali.example/" |
| 62 | + certificate_authority = "` + caFileForTOML + `" |
| 63 | + `))) |
| 64 | + |
| 65 | + kialiCfg, ok := cfg.GetToolsetConfig("kiali") |
| 66 | + s.Require().True(ok, "Kiali config should be present") |
| 67 | + kcfg, ok := kialiCfg.(*Config) |
| 68 | + s.Require().True(ok, "Kiali config should be of type *Config") |
| 69 | + |
| 70 | + // Absolute path should be preserved |
| 71 | + actualPath := filepath.Clean(filepath.FromSlash(kcfg.CertificateAuthority)) |
| 72 | + expectedPath := filepath.Clean(s.caFile) |
| 73 | + s.Equal(expectedPath, actualPath, "Absolute path should be preserved") |
| 74 | +} |
| 75 | + |
| 76 | +func (s *ConfigSuite) TestConfigParser_RejectsInvalidFile() { |
| 77 | + // Use a non-existent file path |
| 78 | + nonExistentFile := filepath.Join(s.tempDir, "non-existent.crt") |
| 79 | + // Convert backslashes to forward slashes for TOML compatibility on Windows |
| 80 | + nonExistentFileForTOML := filepath.ToSlash(nonExistentFile) |
| 81 | + |
| 82 | + cfg, err := config.ReadToml([]byte(` |
| 83 | + [toolset_configs.kiali] |
| 84 | + url = "https://kiali.example/" |
| 85 | + certificate_authority = "` + nonExistentFileForTOML + `" |
| 86 | + `)) |
| 87 | + |
| 88 | + // Validate should reject invalid file path |
| 89 | + s.Require().Error(err, "Validate should reject invalid file path") |
| 90 | + s.Contains(err.Error(), "certificate_authority must be a valid file path", "Error message should indicate file path is invalid") |
| 91 | + s.Nil(cfg, "Config should be nil when validation fails") |
| 92 | +} |
| 93 | + |
| 94 | +func TestConfig(t *testing.T) { |
| 95 | + suite.Run(t, new(ConfigSuite)) |
| 96 | +} |
0 commit comments