package config import ( "fmt" "testing" ) func testFactoryFileRelatedError(fileName string, expectedErrorText string) func(t *testing.T) { return func(t *testing.T) { c := Config{} err := c.Load(fmt.Sprintf("./__mocks__/%s", fileName)) want := err != nil && err.Error() == expectedErrorText if !want { t.Fatalf("Expected error message %s, but got %s", expectedErrorText, err.Error()) } } } func TestLoad(t *testing.T) { t.Run("Can find file", testLoadCanFindFile()) t.Run("Cannot find file", testFactoryFileRelatedError( "nonexistent.yaml", "failed to read config file: open ./__mocks__/nonexistent.yaml: no such file or directory", )) t.Run("Missing CheckInterval in Scheduled mode", testFactoryFileRelatedError( "testLoadMissingCheckInterval.yaml", "failed to validate config: check interval must be set when mode is 'Scheduled'", )) t.Run("Invalid mode", testFactoryFileRelatedError( "testLoadInvalidMode.yaml", "failed to validate config: mode must be one of 'RunOnce' or 'Scheduled'", )) t.Run("Invalid YAML", testFactoryFileRelatedError( "testLoadInvalidYAML.yaml", "failed to unmarshal config file: yaml: line 2: did not find expected key", )) } func testLoadCanFindFile() func(t *testing.T) { return func(t *testing.T) { c := Config{} err := c.Load("./__mocks__/testLoadCanFindFile.yaml") want := err == nil && c.DNSProvider.Type == "ionos" && c.ExternalIPProvider.Type == "plain" && c.Mode == "RunOnce" if !want || err != nil { t.Fatalf("Failed to load config file, expected no errors but got: %s", err) } } }