diff --git a/cmd/karma/views_test.go b/cmd/karma/views_test.go index 1860cc6d4..a38a134ec 100644 --- a/cmd/karma/views_test.go +++ b/cmd/karma/views_test.go @@ -769,3 +769,52 @@ func TestCORS(t *testing.T) { t.Errorf("Invalid Access-Control-Allow-Origin value %q, expected 'foo.example.com'", resp.Header().Get("Access-Control-Allow-Origin")) } } + +func TestEmptySettings(t *testing.T) { + mockConfig() + r := ginTestEngine() + req := httptest.NewRequest("GET", "/alerts.json", nil) + + resp := httptest.NewRecorder() + r.ServeHTTP(resp, req) + if resp.Code != http.StatusOK { + t.Errorf("GET /alerts.json returned status %d", resp.Code) + } + ur := models.AlertsResponse{} + body := resp.Body.Bytes() + err := json.Unmarshal(body, &ur) + if err != nil { + t.Errorf("Failed to unmarshal response: %s", err) + } + + expectedSettings := models.Settings{ + StaticColorLabels: []string{}, + AnnotationsDefaultHidden: false, + AnnotationsHidden: []string{}, + AnnotationsVisible: []string{}, + Sorting: models.SortSettings{ + Grid: models.GridSettings{ + Order: "startsAt", + Reverse: true, + Label: "alertname", + }, + ValueMapping: map[string]map[string]string{}, + }, + SilenceForm: models.SilenceFormSettings{ + Strip: models.SilenceFormStripSettings{ + Labels: []string{}, + }, + Author: "", + }, + AlertAcknowledgement: models.AlertAcknowledgementSettings{ + Enabled: false, + DurationSeconds: 900, + Author: "karma", + CommentPrefix: "ACK!", + }, + } + + if diff := cmp.Diff(expectedSettings, ur.Settings); diff != "" { + t.Errorf("Wrong settings returned (-want +got):\n%s", diff) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index f3e9b48bc..c069fab1d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -26,9 +26,13 @@ import ( var ( // Config will hold final configuration read from the file and flags - Config configSchema + Config *configSchema ) +func init() { + Config = &configSchema{} +} + // SetupFlags is used to attach configuration flags to the main flag set func SetupFlags(f *pflag.FlagSet) { f.Duration("alertmanager.interval", time.Minute, @@ -225,6 +229,41 @@ func (config *configSchema) Read(flags *pflag.FlagSet) string { log.Fatalf("Failed to unmarshal configuration: %v", err) } + // FIXME workaround for https://github.com/mitchellh/mapstructure/issues/146 + if config.Annotations.Hidden == nil { + config.Annotations.Hidden = []string{} + } + if config.Annotations.Visible == nil { + config.Annotations.Visible = []string{} + } + if config.Annotations.Keep == nil { + config.Annotations.Keep = []string{} + } + if config.Annotations.Strip == nil { + config.Annotations.Strip = []string{} + } + if config.Labels.Keep == nil { + config.Labels.Keep = []string{} + } + if config.Labels.Strip == nil { + config.Labels.Strip = []string{} + } + if config.Labels.Color.Static == nil { + config.Labels.Color.Static = []string{} + } + if config.Labels.Color.Unique == nil { + config.Labels.Color.Unique = []string{} + } + if config.Receivers.Keep == nil { + config.Receivers.Keep = []string{} + } + if config.Receivers.Strip == nil { + config.Receivers.Strip = []string{} + } + if config.SilenceForm.Strip.Labels == nil { + config.SilenceForm.Strip.Labels = []string{} + } + if config.SilenceForm.Author.PopulateFromHeader.ValueRegex != "" { _, err = regexp.Compile(config.SilenceForm.Author.PopulateFromHeader.ValueRegex) if err != nil { @@ -284,7 +323,7 @@ func (config *configSchema) Read(flags *pflag.FlagSet) string { } } - Config = *config + Config = config return configFileUsed } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index b58aa8f8b..68691ea78 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "github.com/prymitive/karma/internal/uri" "github.com/spf13/pflag" @@ -14,45 +15,8 @@ import ( yaml "gopkg.in/yaml.v2" ) -// unset all karma supported env variables before tests so we start with no -// config from previous test run func resetEnv() { - karmaEnvVariables := []string{ - "ALERTMANAGER_INTERVAL", - "ALERTMANAGER_URI", - "ALERTMANAGER_EXTERNAL_URI", - "ALERTMANAGER_NAME", - "ALERTMANAGET_TIMEOUT", - "ANNOTATIONS_DEFAULT_HIDDEN", - "ANNOTATIONS_HIDDEN", - "ANNOTATIONS_VISIBLE", - "CONFIG_FILE", - "CUSTOM_CSS", - "CUSTOM_JS", - "DEBUG", - "FILTERS_DEFAULT", - "KARMA_NAME", - "LABELS_COLOR_STATIC", - "LABELS_COLOR_UNIQUE", - "LABELS_KEEP", - "LABELS_STRIP", - "LISTEN_ADDRESS", - "LISTEN_PORT", - "LISTEN_PREFIX", - "LOG_CONFIG", - "LOG_LEVEL", - "RECEIVERS_KEEP", - "RECEIVERS_STRIP", - "SENTRY_PRIVATE", - "SENTRY_PUBLIC", - - "HOST", - "PORT", - "SENTRY_DSN", - } - for _, env := range karmaEnvVariables { - os.Unsetenv(env) - } + os.Clearenv() } func testReadConfig(t *testing.T) { @@ -349,3 +313,35 @@ func TestInvalidUITheme(t *testing.T) { t.Error("Invalid ui.theme value didn't cause log.Fatal()") } } + +func TestDefaultConfig(t *testing.T) { + resetEnv() + log.SetLevel(log.ErrorLevel) + mockConfigRead() + + expectedConfig := configSchema{} + expectedConfig.Annotations.Hidden = []string{} + expectedConfig.Annotations.Visible = []string{} + expectedConfig.Annotations.Keep = []string{} + expectedConfig.Annotations.Strip = []string{} + expectedConfig.Labels.Keep = []string{} + expectedConfig.Labels.Strip = []string{} + expectedConfig.Labels.Color.Static = []string{} + expectedConfig.Labels.Color.Unique = []string{} + expectedConfig.Receivers.Keep = []string{} + expectedConfig.Receivers.Strip = []string{} + expectedConfig.SilenceForm.Strip.Labels = []string{} + + if diff := cmp.Diff(expectedConfig.Annotations, Config.Annotations); diff != "" { + t.Errorf("Wrong annotations config returned (-want +got):\n%s", diff) + } + if diff := cmp.Diff(expectedConfig.Labels, Config.Labels); diff != "" { + t.Errorf("Wrong labels config returned (-want +got):\n%s", diff) + } + if diff := cmp.Diff(expectedConfig.Receivers, Config.Receivers); diff != "" { + t.Errorf("Wrong receivers config returned (-want +got):\n%s", diff) + } + if diff := cmp.Diff(expectedConfig.SilenceForm.Strip, Config.SilenceForm.Strip); diff != "" { + t.Errorf("Wrong silence form config returned (-want +got):\n%s", diff) + } +}