Skip to content

Commit

Permalink
fixing more tests
Browse files Browse the repository at this point in the history
Signed-off-by: alanprot <alanprot@gmail.com>
  • Loading branch information
alanprot committed Apr 2, 2024
1 parent d1a2139 commit 67f67eb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 61 deletions.
4 changes: 0 additions & 4 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4079,10 +4079,6 @@ ruler_client:
# CLI flag: -ruler.alertmanager-refresh-interval
[alertmanager_refresh_interval: <duration> | default = 1m]
# If enabled requests to Alertmanager will utilize the V2 API.
# CLI flag: -ruler.alertmanager-use-v2
[enable_alertmanager_v2: <boolean> | default = false]
# Capacity of the queue for notifications to be sent to the Alertmanager.
# CLI flag: -ruler.notification-queue-capacity
[notification_queue_capacity: <int> | default = 10000]
Expand Down
20 changes: 0 additions & 20 deletions integration/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -68,25 +67,6 @@ func TestAlertmanager(t *testing.T) {

// Ensure no service-specific metrics prefix is used by the wrong service.
assertServiceMetricsPrefixes(t, AlertManager, alertmanager)

// Test compression by inspecting the response Headers
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/v2/alerts", alertmanager.HTTPEndpoint()), nil)
require.NoError(t, err)

req.Header.Set("X-Scope-OrgID", "user-1")
req.Header.Set("Accept-Encoding", "gzip")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// Execute HTTP request
res, err := http.DefaultClient.Do(req.WithContext(ctx))
require.NoError(t, err)

defer res.Body.Close()
// We assert on the Vary header as the minimum response size for enabling compression is 1500 bytes.
// This is enough to know whenever the handler for compression is enabled or not.
require.Equal(t, "Accept-Encoding", res.Header.Get("Vary"))
}

func TestAlertmanagerStoreAPI(t *testing.T) {
Expand Down
48 changes: 21 additions & 27 deletions integration/e2ecortex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
alertConfig "github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/types"
promapi "github.com/prometheus/client_golang/api"
Expand Down Expand Up @@ -614,7 +615,7 @@ func (c *Client) getRawPage(ctx context.Context, url string) ([]byte, error) {

// GetAlertmanagerConfig gets the status of an alertmanager instance
func (c *Client) GetAlertmanagerConfig(ctx context.Context) (*alertConfig.Config, error) {
u := c.alertmanagerClient.URL("/api/prom/api/v1/status", nil)
u := c.alertmanagerClient.URL("/api/prom/api/v2/status", nil)

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
Expand All @@ -634,16 +635,17 @@ func (c *Client) GetAlertmanagerConfig(ctx context.Context) (*alertConfig.Config
return nil, fmt.Errorf("getting config failed with status %d and error %v", resp.StatusCode, string(body))
}

var ss *ServerStatus
err = json.Unmarshal(body, &ss)
cfg := &open_api_models.AlertmanagerStatus{}
err = yaml.Unmarshal(body, cfg)

if err != nil {
return nil, err
}

cfg := &alertConfig.Config{}
err = yaml.Unmarshal([]byte(ss.Data.ConfigYaml), cfg)
original := &alertConfig.Config{}
err = yaml.Unmarshal([]byte(*cfg.Config.Original), original)

return cfg, err
return original, err
}

// SetAlertmanagerConfig gets the status of an alertmanager instance
Expand Down Expand Up @@ -705,7 +707,7 @@ func (c *Client) DeleteAlertmanagerConfig(ctx context.Context) error {

// SendAlertToAlermanager sends alerts to the Alertmanager API
func (c *Client) SendAlertToAlermanager(ctx context.Context, alert *model.Alert) error {
u := c.alertmanagerClient.URL("/api/prom/api/v1/alerts", nil)
u := c.alertmanagerClient.URL("/api/prom/api/v2/alerts", nil)

data, err := json.Marshal([]types.Alert{{Alert: *alert}})
if err != nil {
Expand All @@ -716,7 +718,7 @@ func (c *Client) SendAlertToAlermanager(ctx context.Context, alert *model.Alert)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}

req.Header.Set("Content-Type", "application/json")
resp, body, err := c.alertmanagerClient.Do(ctx, req)
if err != nil {
return err
Expand Down Expand Up @@ -817,22 +819,14 @@ func (c *Client) CreateSilence(ctx context.Context, silence types.Silence) (stri
}

type response struct {
Status string `json:"status"`
Data struct {
SilenceID string `json:"silenceID"`
} `json:"data"`
SilenceID string `json:"silenceID"`
}

decoded := &response{}
if err := json.Unmarshal(body, decoded); err != nil {
return "", err
}

if decoded.Status != "success" {
return "", fmt.Errorf("unexpected response status '%s'", decoded.Status)
}

return decoded.Data.SilenceID, nil
return decoded.SilenceID, nil
}

func (c *Client) GetSilencesV2(ctx context.Context) ([]types.Silence, error) {
Expand Down Expand Up @@ -894,7 +888,7 @@ func (c *Client) GetSilenceV2(ctx context.Context, id string) (types.Silence, er
}

func (c *Client) DeleteSilence(ctx context.Context, id string) error {
u := c.alertmanagerClient.URL(fmt.Sprintf("api/prom/api/v1/silence/%s", url.PathEscape(id)), nil)
u := c.alertmanagerClient.URL(fmt.Sprintf("api/prom/api/v2/silence/%s", url.PathEscape(id)), nil)

req, err := http.NewRequest(http.MethodDelete, u.String(), nil)
if err != nil {
Expand All @@ -918,7 +912,7 @@ func (c *Client) DeleteSilence(ctx context.Context, id string) error {
}

func (c *Client) GetReceivers(ctx context.Context) ([]string, error) {
u := c.alertmanagerClient.URL("api/prom/api/v1/receivers", nil)
u := c.alertmanagerClient.URL("api/prom/api/v2/receivers", nil)

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
Expand All @@ -938,21 +932,21 @@ func (c *Client) GetReceivers(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("getting receivers failed with status %d and error %v", resp.StatusCode, string(body))
}

r := []string{}
type response struct {
Status string `json:"status"`
Data []string `json:"data"`
Name string `json:"name"`
}

decoded := &response{}
if err := json.Unmarshal(body, decoded); err != nil {
decoded := []response{}
if err := json.Unmarshal(body, &decoded); err != nil {
return nil, err
}

if decoded.Status != "success" {
return nil, fmt.Errorf("unexpected response status '%s'", decoded.Status)
for _, d := range decoded {
r = append(r, d.Name)
}

return decoded.Data, nil
return r, nil
}

func (c *Client) PostRequest(url string, body io.Reader) (*http.Response, error) {
Expand Down
18 changes: 17 additions & 1 deletion pkg/ingester/client/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,27 @@ func TestQueryRequest(t *testing.T) {
if !reflect.DeepEqual(haveTo, to) {
t.Fatalf("Bad to FromQueryRequest(ToQueryRequest) round trip")
}
if !reflect.DeepEqual(haveMatchers, matchers) {
if !matchersEqual(haveMatchers, matchers) {
t.Fatalf("Bad have FromQueryRequest(ToQueryRequest) round trip - %v != %v", haveMatchers, matchers)
}
}

func matchersEqual(expected, actual []*labels.Matcher) bool {
if len(expected) != len(actual) {
return false
}

for i := 0; i < len(expected); i++ {
a := actual[i]
e := expected[i]
if a.Name != e.Name || a.Value != e.Value || a.Type != e.Type {
return false
}
}

return true
}

func buildTestMatrix(numSeries int, samplesPerSeries int, offset int) model.Matrix {
m := make(model.Matrix, 0, numSeries)
for i := 0; i < numSeries; i++ {
Expand Down
18 changes: 9 additions & 9 deletions pkg/ruler/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestBuildNotifierConfig(t *testing.T) {
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand All @@ -63,7 +63,7 @@ func TestBuildNotifierConfig(t *testing.T) {
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestBuildNotifierConfig(t *testing.T) {
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand All @@ -107,7 +107,7 @@ func TestBuildNotifierConfig(t *testing.T) {
},
},
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand All @@ -132,7 +132,7 @@ func TestBuildNotifierConfig(t *testing.T) {
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand All @@ -145,7 +145,7 @@ func TestBuildNotifierConfig(t *testing.T) {
},
},
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestBuildNotifierConfig(t *testing.T) {
HTTPClientConfig: config_util.HTTPClientConfig{
BasicAuth: &config_util.BasicAuth{Username: "marco", Password: "hunter2"},
},
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestBuildNotifierConfig(t *testing.T) {
HTTPClientConfig: config_util.HTTPClientConfig{
BasicAuth: &config_util.BasicAuth{Username: "jacob", Password: "test"},
},
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand All @@ -233,7 +233,7 @@ func TestBuildNotifierConfig(t *testing.T) {
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{
{
APIVersion: "v1",
APIVersion: "v2",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
Expand Down
1 change: 1 addition & 0 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
flagext.DeprecatedFlag(f, "ruler.group-timeout", "This flag is no longer functional.", util_log.Logger)
//lint:ignore faillint Need to pass the global logger like this for warning on deprecated methods
flagext.DeprecatedFlag(f, "ruler.num-workers", "This flag is no longer functional. For increased concurrency horizontal sharding is recommended", util_log.Logger)
//lint:ignore faillint Need to pass the global logger like this for warning on deprecated methods
flagext.DeprecatedFlag(f, "ruler.alertmanager-use-v2", "This flag is no longer functional. V1 API is deprecated and removed", util_log.Logger)

cfg.ExternalURL.URL, _ = url.Parse("") // Must be non-nil
Expand Down

0 comments on commit 67f67eb

Please sign in to comment.