From e92a249ce2eb75a296e9759c2abeadaced6edd37 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Wed, 18 Mar 2020 14:30:30 +0530 Subject: [PATCH] e2e prometheus: Add metallb test to check alerts Signed-off-by: Suraj Deshmukh --- test/monitoring/components_alerts_test.go | 109 ++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 test/monitoring/components_alerts_test.go diff --git a/test/monitoring/components_alerts_test.go b/test/monitoring/components_alerts_test.go new file mode 100644 index 000000000..bd8c5600a --- /dev/null +++ b/test/monitoring/components_alerts_test.go @@ -0,0 +1,109 @@ +// Copyright 2020 The Lokomotive 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. + +// +build aws packet +// +build poste2e + +package monitoring + +import ( + "context" + "fmt" + "reflect" + "testing" + "time" + + v1 "github.com/prometheus/client_golang/api/prometheus/v1" + "k8s.io/apimachinery/pkg/util/wait" + + testutil "github.com/kinvolk/lokomotive/test/components/util" +) + +const ( + retryInterval = time.Second * 5 + timeout = time.Minute * 9 + contextTimeout = 10 +) + +//nolint:funlen +func testComponentAlerts(t *testing.T, v1api v1.API) { + alertTestCases := []struct { + ComponentName string + RuleGroup string + platforms []testutil.Platform + Alerts []string + }{ + { + ComponentName: "metallb", + RuleGroup: "metallb-rules", + platforms: []testutil.Platform{testutil.PlatformPacket}, + Alerts: []string{ + "MetalLBNoBGPSession", "MetalLBConfigStale", "MetalLBControllerPodsAvailability", + "MetalLBSpeakerPodsAvailability", + }, + }, + } + + for _, tc := range alertTestCases { + tc := tc + t.Run(tc.ComponentName, func(t *testing.T) { + t.Parallel() + + if !testutil.IsPlatformSupported(t, tc.platforms) { + t.Skip() + } + + if err := wait.PollImmediate(retryInterval, timeout, func() (done bool, err error) { + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout*time.Second) + defer cancel() + + result, err := v1api.Rules(ctx) + if err != nil { + return false, fmt.Errorf("error listing rules: %v", err) + } + + // This map will store information from cluster so that it is easier to search it against + // the test cases + ruleGroups := make(map[string][]string, len(result.Groups)) + + for _, ruleGroup := range result.Groups { + rules := make([]string, 0) + + for _, rule := range ruleGroup.Rules { + switch v := rule.(type) { + case v1.AlertingRule: + rules = append(rules, v.Name) + default: + } + } + + ruleGroups[ruleGroup.Name] = rules + } + + rules, ok := ruleGroups[tc.RuleGroup] + if !ok { + return false, fmt.Errorf("RuleGroup %q not found", tc.RuleGroup) + } + + if !reflect.DeepEqual(rules, tc.Alerts) { + return false, fmt.Errorf("Rules don't match. Expected: %#v and \ngot %#v", tc.Alerts, rules) + } + + return true, nil + }); err != nil { + t.Fatalf("%v", err) + } + }) + } +}