mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 11:48:53 +00:00
test/e2e: add basic alertmanager cluster test
This commit is contained in:
parent
e90829d612
commit
ac84dd6cc0
5 changed files with 138 additions and 7 deletions
9
Makefile
9
Makefile
|
@ -2,6 +2,7 @@ all: build
|
|||
|
||||
REPO?=quay.io/coreos/prometheus-operator
|
||||
TAG?=$(shell git rev-parse --short HEAD)
|
||||
NAMESPACE?=prometheus-operator-e2e-tests-$(shell head /dev/urandom | tr -dc a-z0-9 | head -c 13 ; echo '')
|
||||
|
||||
build:
|
||||
./scripts/check_license.sh
|
||||
|
@ -11,8 +12,12 @@ container:
|
|||
GOOS=linux $(MAKE) build
|
||||
docker build -t $(REPO):$(TAG) .
|
||||
|
||||
e2e: container
|
||||
go test -v ./test/e2e/ --kubeconfig "$(HOME)/.kube/config" --operator-image=quay.io/coreos/prometheus-operator:$(TAG) --namespace=prometheus-operator-e2e-tests-$(TAG)
|
||||
e2e-test:
|
||||
go test -v ./test/e2e/ --kubeconfig "$(HOME)/.kube/config" --operator-image=quay.io/coreos/prometheus-operator:$(TAG) --namespace=$(NAMESPACE)
|
||||
|
||||
e2e:
|
||||
$(MAKE) container
|
||||
$(MAKE) e2e-test
|
||||
|
||||
clean-e2e:
|
||||
kubectl delete namespace prometheus-operator-e2e-tests
|
||||
|
|
|
@ -349,7 +349,7 @@ func (c *Operator) sync(key string) error {
|
|||
return c.syncVersion(am)
|
||||
}
|
||||
|
||||
func listOptions(name string) v1.ListOptions {
|
||||
func ListOptions(name string) v1.ListOptions {
|
||||
return v1.ListOptions{
|
||||
LabelSelector: fields.SelectorFromSet(fields.Set(map[string]string{
|
||||
"app": "alertmanager",
|
||||
|
@ -366,7 +366,7 @@ func listOptions(name string) v1.ListOptions {
|
|||
func (c *Operator) syncVersion(am *spec.Alertmanager) error {
|
||||
podClient := c.kclient.Core().Pods(am.Namespace)
|
||||
|
||||
pods, err := podClient.List(listOptions(am.Name))
|
||||
pods, err := podClient.List(ListOptions(am.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -439,7 +439,7 @@ func (c *Operator) destroyAlertmanager(key string) error {
|
|||
// TODO(fabxc): temporary solution until StatefulSet status provides necessary info to know
|
||||
// whether scale-down completed.
|
||||
for {
|
||||
pods, err := podClient.List(listOptions(sset.Name))
|
||||
pods, err := podClient.List(ListOptions(sset.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
82
test/e2e/alertmanager_e2e_test.go
Normal file
82
test/e2e/alertmanager_e2e_test.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
// Copyright 2016 The prometheus-operator Authors
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/client-go/pkg/api/v1"
|
||||
|
||||
"github.com/coreos/prometheus-operator/pkg/alertmanager"
|
||||
"github.com/coreos/prometheus-operator/pkg/spec"
|
||||
)
|
||||
|
||||
var validAlertmanagerConfig = `global:
|
||||
resolve_timeout: 5m
|
||||
route:
|
||||
group_by: ['job']
|
||||
group_wait: 30s
|
||||
group_interval: 5m
|
||||
repeat_interval: 12h
|
||||
receiver: 'webhook'
|
||||
receivers:
|
||||
- name: 'webhook'
|
||||
webhook_configs:
|
||||
- url: 'http://alertmanagerwh:30500/'
|
||||
`
|
||||
|
||||
func TestAlertmanagerCreateCluster(t *testing.T) {
|
||||
name := "alertmanager-test"
|
||||
|
||||
framework.KubeClient.CoreV1().ConfigMaps(framework.Namespace.Name).Create(
|
||||
&v1.ConfigMap{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Data: map[string]string{
|
||||
"alertmanager.yaml": validAlertmanagerConfig,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
spec := &spec.Alertmanager{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: spec.AlertmanagerSpec{
|
||||
Replicas: 3,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := framework.CreateAlertmanager(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := framework.DeleteAlertmanager(name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := framework.WaitForPodsReady(time.Minute*2, 0, alertmanager.ListOptions(name)); err != nil {
|
||||
t.Fatalf("failed to teardown Alertmanager instances: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := framework.WaitForPodsReady(time.Minute*2, 3, alertmanager.ListOptions(name)); err != nil {
|
||||
t.Fatalf("failed to create an Alertmanager cluster with 3 instances: %v", err)
|
||||
}
|
||||
}
|
|
@ -165,7 +165,7 @@ func waitForPodsReady(client v1client.CoreV1Interface, timeout time.Duration, ex
|
|||
}
|
||||
|
||||
runningAndReady := 0
|
||||
if len(pl.Items) > 0 {
|
||||
if len(pl.Items) >= 0 {
|
||||
for _, p := range pl.Items {
|
||||
isRunningAndReady, err := k8sutil.PodRunningAndReady(p)
|
||||
if err != nil {
|
||||
|
@ -231,6 +231,46 @@ func (f *Framework) DeletePrometheus(name string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (f *Framework) CreateAlertmanager(e *spec.Alertmanager) (*spec.Alertmanager, error) {
|
||||
b, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := f.HTTPClient.Post(
|
||||
fmt.Sprintf("%s/apis/monitoring.coreos.com/v1alpha1/namespaces/%s/alertmanagers", f.MasterHost, f.Namespace.Name),
|
||||
"application/json", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("unexpected status: %v", resp.Status)
|
||||
}
|
||||
decoder := yaml.NewYAMLOrJSONDecoder(resp.Body, 100)
|
||||
res := &spec.Alertmanager{}
|
||||
if err := decoder.Decode(res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (f *Framework) DeleteAlertmanager(name string) error {
|
||||
req, err := http.NewRequest("DELETE",
|
||||
fmt.Sprintf("%s/apis/monitoring.coreos.com/v1alpha1/namespaces/%s/alertmanagers/%s", f.MasterHost, f.Namespace.Name, name), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := f.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status: %v", resp.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Framework) createDeployment(deploy *v1beta1.Deployment) error {
|
||||
if _, err := f.KubeClient.Extensions().Deployments(f.Namespace.Name).Create(deploy); err != nil {
|
||||
return err
|
||||
|
|
|
@ -24,7 +24,7 @@ import (
|
|||
"github.com/coreos/prometheus-operator/pkg/spec"
|
||||
)
|
||||
|
||||
func TestCreateCluster(t *testing.T) {
|
||||
func TestPrometheusCreateCluster(t *testing.T) {
|
||||
spec := &spec.Prometheus{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "prometheus-test",
|
||||
|
@ -43,6 +43,10 @@ func TestCreateCluster(t *testing.T) {
|
|||
if err := framework.DeletePrometheus("prometheus-test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := framework.WaitForPodsReady(time.Minute*2, 0, prometheus.ListOptions("prometheus-test")); err != nil {
|
||||
t.Fatalf("failed to teardown Prometheus instances: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := framework.WaitForPodsReady(time.Minute*2, 1, prometheus.ListOptions("prometheus-test")); err != nil {
|
Loading…
Add table
Add a link
Reference in a new issue