From 60643e2ef1ef6312ce7ae697939a4249ed216841 Mon Sep 17 00:00:00 2001 From: Itay Kalfon <82172131+Itaykal@users.noreply.github.com> Date: Sat, 18 May 2024 22:49:34 +0000 Subject: [PATCH] initial test structure --- integration/mimir/alertmanager_test.go | 43 +++++++++++++++++++ .../alertmanager/no-template-files.yml | 17 -------- .../alertmanager/test-alertmanager.yml | 2 +- pkg/mimir/client/http_client.go | 6 --- 4 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 integration/mimir/alertmanager_test.go delete mode 100644 integration/mimir/testdata/alertmanager/no-template-files.yml diff --git a/integration/mimir/alertmanager_test.go b/integration/mimir/alertmanager_test.go new file mode 100644 index 00000000..4adc1651 --- /dev/null +++ b/integration/mimir/alertmanager_test.go @@ -0,0 +1,43 @@ +package mimir + +import ( + "os" + "testing" + + "github.com/grafana/grizzly/pkg/grizzly" + "github.com/grafana/grizzly/pkg/mimir" + "github.com/grafana/grizzly/pkg/mimir/client" + "github.com/grafana/grizzly/pkg/testutil" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" +) + +func TestAlertmanager(t *testing.T) { + alertmanagerTestFilePath := "testdata/alertmanager/test-alertmanager.yml" + provider := mimir.NewProvider(&testutil.TestContext().Mimir) + client := client.NewHTTPClient(&testutil.TestContext().Mimir) + handler := mimir.NewAlertmanagerHandler(provider, client) + + t.Run("create prometheus alertmanager config", func(t *testing.T) { + + file, err := os.ReadFile(alertmanagerTestFilePath) + require.NoError(t, err) + + var resource grizzly.Resource + require.NoError(t, yaml.Unmarshal(file, &resource.Body)) + require.NoError(t, handler.Add(resource)) + + t.Run("get remote alertmanager config", func(t *testing.T) { + file, err := os.ReadFile(alertmanagerTestFilePath) + require.NoError(t, err) + + var resource grizzly.Resource + require.NoError(t, yaml.Unmarshal(file, &resource.Body)) + + remoteResource, err := handler.GetRemote(grizzly.Resource{}) + require.NoError(t, err) + + require.Equal(t, resource, *remoteResource) + }) + }) +} diff --git a/integration/mimir/testdata/alertmanager/no-template-files.yml b/integration/mimir/testdata/alertmanager/no-template-files.yml deleted file mode 100644 index bf5b6cad..00000000 --- a/integration/mimir/testdata/alertmanager/no-template-files.yml +++ /dev/null @@ -1,17 +0,0 @@ -apiVersion: grizzly.grafana.com/v1alpha1 -kind: PrometheusAlertmanagerConfig -metadata: - name: no-template-files -spec: - alertmanager_config: - global: - smtp_smarthost: 'localhost:25' - smtp_from: 'youraddress@example.org' - templates: - - 'default_template' - route: - receiver: example-email - receivers: - - name: example-email - email_configs: - - to: 'youraddress@example.org' \ No newline at end of file diff --git a/integration/mimir/testdata/alertmanager/test-alertmanager.yml b/integration/mimir/testdata/alertmanager/test-alertmanager.yml index ef7c136c..3b04762e 100644 --- a/integration/mimir/testdata/alertmanager/test-alertmanager.yml +++ b/integration/mimir/testdata/alertmanager/test-alertmanager.yml @@ -1,7 +1,7 @@ apiVersion: grizzly.grafana.com/v1alpha1 kind: PrometheusAlertmanagerConfig metadata: - name: test-alertmanager + name: global spec: template_files: default_template: | diff --git a/pkg/mimir/client/http_client.go b/pkg/mimir/client/http_client.go index 617b621b..737287b4 100644 --- a/pkg/mimir/client/http_client.go +++ b/pkg/mimir/client/http_client.go @@ -21,11 +21,6 @@ var loadRulesEndpoint = "%s/prometheus/config/v1/rules/%s" var listRulesEndpoint = "%s/prometheus/api/v1/rules" var alertmanagerAPIPath = "%s/api/v1/alerts" -type GetAlertmanagerConfigResponse struct { - TemplateFiles map[string]string `yaml:"template_files"` - AlertmanagerConfig string `yaml:"alertmanager_config"` -} - type ListGroupResponse struct { Status string `yaml:"status"` Data struct { @@ -50,7 +45,6 @@ func NewHTTPClient(config *config.MimirConfig) Mimir { func (c *Client) CreateAlertmangerConfig(resource models.PrometheusAlertmanagerConfig) error { url := fmt.Sprintf(alertmanagerAPIPath, c.config.Address) cfg, err := yaml.Marshal(&resource) - fmt.Println(string(cfg)) if err != nil { return err }