2013-07-22 16:32:45 +00:00
|
|
|
// Copyright 2013 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
|
|
|
|
//
|
|
|
|
// 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 manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
type testSilencerScenario struct {
|
2013-08-27 13:32:08 +00:00
|
|
|
silences Silences
|
|
|
|
silenced Alerts
|
|
|
|
unsilenced Alerts
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
func (scenario *testSilencerScenario) test(i int, t *testing.T) {
|
|
|
|
s := NewSilencer()
|
2013-07-22 16:32:45 +00:00
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
for j, sc := range scenario.silences {
|
|
|
|
id := s.AddSilence(sc)
|
|
|
|
retrievedSilence, err := s.GetSilence(id)
|
2013-07-22 16:32:45 +00:00
|
|
|
if err != nil {
|
2013-07-31 12:39:01 +00:00
|
|
|
t.Fatalf("%d.%d. Error getting silence: %s", i, j, err)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
2014-10-27 13:53:31 +00:00
|
|
|
if retrievedSilence.ID != id {
|
|
|
|
t.Fatalf("%d.%d. Expected ID %d, got %d", i, j, id, retrievedSilence.ID)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
2014-10-27 13:53:31 +00:00
|
|
|
sc.ID = id
|
2013-07-31 12:39:01 +00:00
|
|
|
if sc != retrievedSilence {
|
|
|
|
t.Fatalf("%d.%d. Expected silence %v, got %v", i, j, sc, retrievedSilence)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
for j, a := range scenario.silenced {
|
|
|
|
silenced, sc := s.IsSilenced(a.Labels)
|
|
|
|
if !silenced {
|
|
|
|
t.Fatalf("%d.%d. Expected %v to be silenced", i, j, a)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
2013-07-31 12:39:01 +00:00
|
|
|
if sc == nil {
|
2013-08-27 13:32:08 +00:00
|
|
|
t.Fatalf("%d.%d. Expected non-nil Silence for silenced event %v", i, j, a)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
for j, a := range scenario.unsilenced {
|
|
|
|
silenced, sc := s.IsSilenced(a.Labels)
|
|
|
|
if silenced {
|
|
|
|
t.Fatalf("%d.%d. Expected %v to not be silenced, was silenced by %v", i, j, a, sc)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
l := AlertLabelSets{}
|
|
|
|
for _, a := range append(scenario.silenced, scenario.unsilenced...) {
|
|
|
|
l = append(l, a.Labels)
|
|
|
|
}
|
|
|
|
unsilenced := AlertLabelSets{}
|
|
|
|
for _, a := range scenario.unsilenced {
|
|
|
|
unsilenced = append(unsilenced, a.Labels)
|
|
|
|
}
|
|
|
|
filtered := s.Filter(l)
|
|
|
|
labelSetsMustBeEqual(i, t, filtered, unsilenced)
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
silences := s.SilenceSummary()
|
|
|
|
if len(silences) != len(scenario.silences) {
|
|
|
|
t.Fatalf("%d. Expected %d silences, got %d", i, len(scenario.silences), len(silences))
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
for j, sc := range silences {
|
2014-10-27 13:53:31 +00:00
|
|
|
if err := s.DelSilence(sc.ID); err != nil {
|
2013-07-31 12:39:01 +00:00
|
|
|
t.Fatalf("%d.%d. Got error while deleting silence: %s", i, j, err)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
newSilences := s.SilenceSummary()
|
|
|
|
if len(newSilences) != len(silences)-j-1 {
|
|
|
|
t.Fatalf("%d. Expected %d silences, got %d", i, len(silences), len(newSilences))
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
func TestSilencer(t *testing.T) {
|
|
|
|
scenarios := []testSilencerScenario{
|
2013-07-22 16:32:45 +00:00
|
|
|
{
|
2013-07-31 12:39:01 +00:00
|
|
|
// No silences, one event.
|
2013-08-27 13:32:08 +00:00
|
|
|
unsilenced: Alerts{
|
|
|
|
&Alert{
|
2013-07-22 16:32:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// One rule, two matching events, one non-matching.
|
2013-07-31 12:39:01 +00:00
|
|
|
silences: Silences{
|
|
|
|
&Silence{
|
2013-07-22 16:32:45 +00:00
|
|
|
Filters: Filters{NewFilter("service", "test(-)?service")},
|
|
|
|
EndsAt: time.Now().Add(time.Hour),
|
|
|
|
},
|
2013-07-31 12:39:01 +00:00
|
|
|
&Silence{
|
2013-07-22 16:32:45 +00:00
|
|
|
Filters: Filters{NewFilter("testlabel", ".*")},
|
|
|
|
EndsAt: time.Now().Add(time.Hour),
|
|
|
|
},
|
|
|
|
},
|
2013-08-27 13:32:08 +00:00
|
|
|
silenced: Alerts{
|
|
|
|
&Alert{
|
2013-07-22 16:32:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "testservice",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
2013-08-27 13:32:08 +00:00
|
|
|
&Alert{
|
2013-07-22 16:32:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "test-service",
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
2013-08-27 13:32:08 +00:00
|
|
|
&Alert{
|
2013-07-22 16:32:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "bar-service",
|
|
|
|
"testlabel": "testvalue",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-08-27 13:32:08 +00:00
|
|
|
unsilenced: Alerts{
|
|
|
|
&Alert{
|
2013-07-22 16:32:45 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "testservice2",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
scenario.test(i, t)
|
|
|
|
}
|
|
|
|
}
|