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 {
|
|
|
|
silences Silences
|
|
|
|
inhibited Events
|
|
|
|
uninhibited Events
|
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
|
|
|
}
|
2013-07-31 12:39:01 +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
|
|
|
}
|
2013-07-31 12:39:01 +00:00
|
|
|
sc.Id = id
|
|
|
|
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-07-31 12:39:01 +00:00
|
|
|
for j, ev := range scenario.inhibited {
|
|
|
|
inhibited, sc := s.IsInhibited(ev)
|
2013-07-22 16:32:45 +00:00
|
|
|
if !inhibited {
|
|
|
|
t.Fatalf("%d.%d. Expected %v to be inhibited", i, j, ev)
|
|
|
|
}
|
2013-07-31 12:39:01 +00:00
|
|
|
if sc == nil {
|
|
|
|
t.Fatalf("%d.%d. Expected non-nil Silence for inhibited event %v", i, j, ev)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 12:39:01 +00:00
|
|
|
for j, ev := range scenario.uninhibited {
|
|
|
|
inhibited, sc := s.IsInhibited(ev)
|
2013-07-22 16:32:45 +00:00
|
|
|
if inhibited {
|
2013-07-31 12:39:01 +00:00
|
|
|
t.Fatalf("%d.%d. Expected %v to not be inhibited, was inhibited by %v", i, j, ev, sc)
|
2013-07-22 16:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if err := s.DelSilence(sc.Id); err != nil {
|
|
|
|
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-07-22 16:32:45 +00:00
|
|
|
uninhibited: Events{
|
|
|
|
&Event{
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inhibited: Events{
|
|
|
|
&Event{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "testservice",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Event{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "test-service",
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Event{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "bar-service",
|
|
|
|
"testlabel": "testvalue",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
uninhibited: Events{
|
|
|
|
&Event{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"service": "testservice2",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
scenario.test(i, t)
|
|
|
|
}
|
|
|
|
}
|