Skip to content

Commit

Permalink
Move api v1 client into v1 tests
Browse files Browse the repository at this point in the history
This commit moves the stuff formerly in /client into /test/with_api_v1
so that we can discourage use of the v1 client without breaking things

Signed-off-by: sinkingpoint <colin@quirl.co.nz>
  • Loading branch information
sinkingpoint committed Nov 11, 2021
1 parent 5b825e2 commit 96e4575
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
16 changes: 7 additions & 9 deletions test/with_api_v1/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (

"github.com/prometheus/client_golang/api"
"github.com/prometheus/common/model"

"github.com/prometheus/alertmanager/client"
)

// AcceptanceTest provides declarative definition of given inputs and expected
Expand Down Expand Up @@ -335,26 +333,26 @@ func (am *Alertmanager) cleanup() {
// Push declares alerts that are to be pushed to the Alertmanager
// server at a relative point in time.
func (am *Alertmanager) Push(at float64, alerts ...*TestAlert) {
var cas []client.Alert
var cas []APIV1Alert
for i := range alerts {
a := alerts[i].nativeAlert(am.opts)
al := client.Alert{
Labels: client.LabelSet{},
Annotations: client.LabelSet{},
al := APIV1Alert{
Labels: LabelSet{},
Annotations: LabelSet{},
StartsAt: a.StartsAt,
EndsAt: a.EndsAt,
GeneratorURL: a.GeneratorURL,
}
for n, v := range a.Labels {
al.Labels[client.LabelName(n)] = client.LabelValue(v)
al.Labels[LabelName(n)] = LabelValue(v)
}
for n, v := range a.Annotations {
al.Annotations[client.LabelName(n)] = client.LabelValue(v)
al.Annotations[LabelName(n)] = LabelValue(v)
}
cas = append(cas, al)
}

alertAPI := client.NewAlertAPI(am.client)
alertAPI := NewAlertAPI(am.client)

am.t.Do(at, func() {
if err := alertAPI.Push(context.Background(), cas...); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions client/client.go → test/with_api_v1/helper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Prometheus Authors
// Copyright 2015 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package client
package test

import (
"bytes"
Expand Down Expand Up @@ -158,11 +158,11 @@ type AlertAPI interface {
// List returns all the active alerts.
List(ctx context.Context, filter, receiver string, silenced, inhibited, active, unprocessed bool) ([]*ExtendedAlert, error)
// Push sends a list of alerts to the Alertmanager.
Push(ctx context.Context, alerts ...Alert) error
Push(ctx context.Context, alerts ...APIV1Alert) error
}

// Alert represents an alert as expected by the AlertManager's push alert API.
type Alert struct {
// APIV1Alert represents an alert as expected by the AlertManager's push alert API.
type APIV1Alert struct {
Labels LabelSet `json:"labels"`
Annotations LabelSet `json:"annotations"`
StartsAt time.Time `json:"startsAt,omitempty"`
Expand All @@ -172,7 +172,7 @@ type Alert struct {

// ExtendedAlert represents an alert as returned by the AlertManager's list alert API.
type ExtendedAlert struct {
Alert
APIV1Alert
Status types.AlertStatus `json:"status"`
Receivers []string `json:"receivers"`
Fingerprint string `json:"fingerprint"`
Expand Down Expand Up @@ -225,7 +225,7 @@ func (h *httpAlertAPI) List(ctx context.Context, filter, receiver string, silenc
return alts, err
}

func (h *httpAlertAPI) Push(ctx context.Context, alerts ...Alert) error {
func (h *httpAlertAPI) Push(ctx context.Context, alerts ...APIV1Alert) error {
u := h.client.URL(epAlerts, nil)

var buf bytes.Buffer
Expand Down
8 changes: 4 additions & 4 deletions client/client_test.go → test/with_api_v1/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package client
package test

import (
"bytes"
Expand Down Expand Up @@ -111,15 +111,15 @@ func TestAPI(t *testing.T) {
return api.Get(context.Background())
}

alertOne := Alert{
alertOne := APIV1Alert{
StartsAt: now,
EndsAt: now.Add(time.Duration(5 * time.Minute)),
Labels: LabelSet{"label1": "test1"},
Annotations: LabelSet{"annotation1": "some text"},
}
alerts := []*ExtendedAlert{
{
Alert: alertOne,
APIV1Alert: alertOne,
Fingerprint: "1c93eec3511dc156",
Status: types.AlertStatus{
State: types.AlertStateActive,
Expand All @@ -132,7 +132,7 @@ func TestAPI(t *testing.T) {
}
doAlertPush := func() (interface{}, error) {
api := httpAlertAPI{client: client}
return nil, api.Push(context.Background(), []Alert{alertOne}...)
return nil, api.Push(context.Background(), []APIV1Alert{alertOne}...)
}

silOne := &types.Silence{
Expand Down

0 comments on commit 96e4575

Please sign in to comment.