1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 03:38:43 +00:00

test/e2e: refactor e2e tests

This commit is contained in:
Frederic Branczyk 2017-01-05 18:15:21 +01:00
parent ac84dd6cc0
commit 678e7a8ced
No known key found for this signature in database
GPG key ID: CA14788B1E48B256
5 changed files with 248 additions and 163 deletions

View file

@ -16,67 +16,18 @@ 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) {
func TestAlertmanagerCreateDeleteCluster(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 {
if err := framework.DeleteAlertmanagerAndWaitUntilGone(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)
if err := framework.CreateAlertmanagerAndWaitUntilReady(framework.MakeBasicAlertmanager(name, 3)); err != nil {
t.Fatal(err)
}
}

View file

@ -0,0 +1,133 @@
// 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 framework
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/util/yaml"
"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 (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) MakeBasicAlertmanager(name string, replicas int32) *spec.Alertmanager {
return &spec.Alertmanager{
ObjectMeta: v1.ObjectMeta{
Name: name,
},
Spec: spec.AlertmanagerSpec{
Replicas: replicas,
},
}
}
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) CreateAlertmanagerAndWaitUntilReady(a *spec.Alertmanager) error {
_, err := f.KubeClient.CoreV1().ConfigMaps(f.Namespace.Name).Create(
&v1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: a.Name,
},
Data: map[string]string{
"alertmanager.yaml": ValidAlertmanagerConfig,
},
},
)
if err != nil {
return err
}
_, err = f.CreateAlertmanager(a)
if err != nil {
return err
}
_, err = f.WaitForPodsReady(time.Minute*2, int(a.Spec.Replicas), alertmanager.ListOptions(a.Name))
if err != nil {
return fmt.Errorf("failed to create an Alertmanager cluster (%s) with %d instances: %v", a.Name, a.Spec.Replicas, err)
}
return nil
}
func (f *Framework) DeleteAlertmanagerAndWaitUntilGone(name string) error {
if err := f.DeleteAlertmanager(name); err != nil {
return err
}
if _, err := f.WaitForPodsReady(time.Minute*2, 0, alertmanager.ListOptions(name)); err != nil {
return fmt.Errorf("failed to teardown Alertmanager (%s) instances: %v", name, err)
}
return f.KubeClient.CoreV1().ConfigMaps(f.Namespace.Name).Delete(name, nil)
}

View file

@ -15,8 +15,6 @@
package framework
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
@ -34,7 +32,6 @@ import (
"github.com/coreos/prometheus-operator/pkg/k8sutil"
"github.com/coreos/prometheus-operator/pkg/prometheus"
"github.com/coreos/prometheus-operator/pkg/spec"
)
type Framework struct {
@ -191,86 +188,6 @@ func (f *Framework) CreateDeployment(kclient kubernetes.Interface, ns string, de
return nil
}
func (f *Framework) CreatePrometheus(e *spec.Prometheus) (*spec.Prometheus, 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/prometheuses", 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.Prometheus{}
if err := decoder.Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (f *Framework) DeletePrometheus(name string) error {
req, err := http.NewRequest("DELETE",
fmt.Sprintf("%s/apis/monitoring.coreos.com/v1alpha1/namespaces/%s/prometheuses/%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) 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

View file

@ -0,0 +1,106 @@
// 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 framework
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/util/yaml"
"github.com/coreos/prometheus-operator/pkg/prometheus"
"github.com/coreos/prometheus-operator/pkg/spec"
)
func (f *Framework) CreatePrometheus(e *spec.Prometheus) (*spec.Prometheus, 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/prometheuses", 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.Prometheus{}
if err := decoder.Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (f *Framework) DeletePrometheus(name string) error {
req, err := http.NewRequest("DELETE",
fmt.Sprintf("%s/apis/monitoring.coreos.com/v1alpha1/namespaces/%s/prometheuses/%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) MakeBasicPrometheus(name string, replicas int32) *spec.Prometheus {
return &spec.Prometheus{
ObjectMeta: v1.ObjectMeta{
Name: name,
},
Spec: spec.PrometheusSpec{
Replicas: replicas,
},
}
}
func (f *Framework) CreatePrometheusAndWaitUntilReady(p *spec.Prometheus) error {
_, err := f.CreatePrometheus(p)
if err != nil {
return err
}
_, err = f.WaitForPodsReady(time.Minute*2, int(p.Spec.Replicas), prometheus.ListOptions(p.Name))
if err != nil {
return fmt.Errorf("failed to create %d Prometheus instances (%s): %v", p.Spec.Replicas, p.Name, err)
}
return nil
}
func (f *Framework) DeletePrometheusAndWaitUntilGone(name string) error {
if err := f.DeletePrometheus(name); err != nil {
return err
}
if _, err := f.WaitForPodsReady(time.Minute*2, 0, prometheus.ListOptions(name)); err != nil {
return fmt.Errorf("failed to teardown Prometheus instances (%s): %v", name, err)
}
return nil
}

View file

@ -16,40 +16,18 @@ package e2e
import (
"testing"
"time"
"k8s.io/client-go/pkg/api/v1"
"github.com/coreos/prometheus-operator/pkg/prometheus"
"github.com/coreos/prometheus-operator/pkg/spec"
)
func TestPrometheusCreateCluster(t *testing.T) {
spec := &spec.Prometheus{
ObjectMeta: v1.ObjectMeta{
Name: "prometheus-test",
},
Spec: spec.PrometheusSpec{
Replicas: 1,
},
}
_, err := framework.CreatePrometheus(spec)
if err != nil {
t.Fatal(err)
}
func TestPrometheusCreateDeleteCluster(t *testing.T) {
name := "prometheus-test"
defer func() {
if err := framework.DeletePrometheus("prometheus-test"); err != nil {
if err := framework.DeletePrometheusAndWaitUntilGone(name); 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 {
t.Fatalf("failed to create 1 Prometheus instances: %v", err)
if err := framework.CreatePrometheusAndWaitUntilReady(framework.MakeBasicPrometheus(name, 1)); err != nil {
t.Fatal(err)
}
}