func TestOccurences(t *testing.T) { c := testCondition(test_f(0), nil, nil, 2) pipe := testPolicy(c, nil, map[string]string{"host": "test"}, nil) p, ta := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() e := event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = 1.0 p.Pass(e) e.Wait() ta.Do(func(ta *test.TestAlert) { if len(ta.Events) != 0 { t.Error("occrences hit too early") } }) e = event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = 1.0 p.Pass(e) e.Wait() ta.Do(func(ta *test.TestAlert) { if len(ta.Events) != 1 { t.Error("occrences not hit", ta.Events) } }) }
func TestProcess(t *testing.T) { c := testCondition(test_f(0), nil, nil, 0) pipe := testPolicy(c, nil, map[string]string{"host": "test"}, nil) p, ta := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() e := event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = 1.0 p.Process(e) e.Wait() if len(ta.Events) != 1 { t.Fatal(ta.Events) } e = event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = -1.0 p.Process(e) e.Wait() if ta.Events[e] != event.OK { t.Fatal(ta.Events) } }
func TestRefresh(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { p.PassEvent(event.NewEvent()) p.Refresh(config.NewDefaultConfig()) p.PassEvent(event.NewEvent()) }) }
func TestKeepAlive(t *testing.T) { logrus.SetLevel(logrus.DebugLevel) c := testCondition(test_f(0), nil, nil, 1) pipe := testPolicy(c, nil, map[string]string{"service": "KeepAlive"}, nil) p, ta := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() e := event.NewEvent() e.Host = "one one" e.Service = "exit" e.Metric = -1 p.Pass(e) e.Wait() p.keepAliveAge = time.Millisecond * 15 p.keepAliveCheckTime = time.Millisecond * 50 go p.checkExpired() time.Sleep(100 * time.Millisecond) ta.Do(func(ta *test.TestAlert) { if len(ta.Events) != 1 { t.Fatal(ta.Events) } }) }
func TestProcessDedupe(t *testing.T) { c := testCondition(test_f(0), nil, nil, 0) pipe := testPolicy(c, nil, map[string]string{"host": "test"}, nil) p, ta := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() events := make([]*event.Event, 100) for i := 0; i < len(events); i++ { e := event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = 1.0 events[i] = e } p.Process(events[0]) for i := 1; i < len(events); i++ { p.Process(events[i]) events[i].Wait() } if len(ta.Events) != 1 { log.Println(ta.Events) t.Fail() } }
func newTestEvent(h, s string, m float64) *event.Event { e := event.NewEvent() e.Tags.Set("host", h) e.Tags.Set("service", s) e.Metric = m return e }
func TestKeepAlive(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { u := &config.User{} u.Permissions = config.WRITE ta := test.NewTestAlert() // add a escalation policy that will catch a keep alive p.UpdateConfig(func(c *config.AppConfig) error { esc := &escalation.EscalationPolicy{} esc.Crit = true esc.Escalations = []escalation.Escalation{ta} esc.Match = event.NewTagset(0) esc.Match.Set(INTERNAL_TAG_NAME, ".*") c.Escalations["test"] = esc esc.Compile() // create a condition for the KeepAlive pol := &escalation.Policy{} pol.Match = event.NewTagset(0) pol.Match.Set(INTERNAL_TAG_NAME, KEEP_ALIVE_INTERNAL_TAG) cond := &escalation.Condition{} cond.Simple = true cond.Greater = test_f(0) cond.WindowSize = 2 cond.Occurences = 1 pol.Crit = cond c.Policies["test"] = pol return nil }, u) // create an event e := event.NewEvent() e.Tags.Set("host", "test") p.PassEvent(e) e.WaitForState(event.StateComplete, time.Millisecond) time.Sleep(20 * time.Millisecond) p.checkExpired() time.Sleep(50 * time.Millisecond) if len(ta.Incidents) != 1 { t.Error(ta.Incidents) } ka := ta.Incidents[0].GetEvent() if ka.Get("host") != "test" { t.Fail() } if ka.Get(INTERNAL_TAG_NAME) != KEEP_ALIVE_INTERNAL_TAG { t.Fail() } }) }
func TestPutIncident(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { in := event.NewIncident("test", event.OK, event.NewEvent()) p.PutIncident(in) }) }
func genEventSlice(size int) []*event.Event { e := make([]*event.Event, size) for i := range e { e[i] = event.NewEvent() } return e }
func TestPauseUnpause(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { // make sure events can go in and out e := event.NewEvent() p.PassEvent(e) p.Pause() for i := 0; i < 1000; i++ { p.PassEvent(event.NewEvent()) } p.Unpause() // pass another event p.PassEvent(event.NewEvent()) }) }
func newTestEvent() *event.Event { e := event.NewEvent() e.Tags.Set("host", randomString(rand.Int()%50)) e.Tags.Set("service", randomString(rand.Int()%50)) e.Tags.Set("sub_service", randomString(rand.Int()%50)) e.Time = time.Now() e.Metric = rand.Float64() * 100 return e }
func TestDedupe(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { in := event.NewIncident("test", event.OK, event.NewEvent()) p.PutIncident(in) if p.Dedupe(in) { t.Fatal("Incident was not deduped") } }) }
func TestListIncidents(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { in := event.NewIncident("test", event.OK, event.NewEvent()) p.PutIncident(in) if l := p.ListIncidents(); l[0].Policy != in.Policy { t.Fatal("Incident was not added to the index") } }) }
func TestPausePipelineCache(t *testing.T) { logrus.SetLevel(logrus.DebugLevel) c := testCondition(test_f(0), nil, nil, 1) pipe := testPolicy(c, nil, map[string]string{"service": "wwoo"}, nil) p, _ := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() p.Start() p.Pause() for i := 0; i < 100; i++ { p.Pass(event.NewEvent()) } p.Unpause() }
func TestPassEvent(t *testing.T) { x := baseTestContext() x.runTest(func(p *Pipeline) { e := event.NewEvent() go func() { p.PassEvent(e) }() ne := <-p.in if ne != e { t.Fatal() } }) }
// create keep alive events for each hostname -> time pair func createKeepAliveEvents(times map[string]time.Time) []*event.Event { n := time.Now() events := make([]*event.Event, len(times)) x := 0 for host, t := range times { e := event.NewEvent() e.Host = host e.Metric = n.Sub(t).Seconds() e.Service = KEEP_ALIVE_SERVICE_NAME events[x] = e x += 1 } return events }
func genEvents(hosts, services []string, next chan *event.Event) { gen := func() *event.Event { e := event.NewEvent() e.Metric = rand.Float64() * 100 e.Time = time.Now() e.Tags.Set("host", hosts[rand.Intn(len(hosts)-1)]) e.Tags.Set("service", services[rand.Intn(len(services)-1)]) return e } go func() { for { next <- gen() } }() }
// create keep alive events for each hostname -> time pair func createKeepAliveEvents(times map[string]time.Time, tag string) []*event.Event { events := make([]*event.Event, len(times)) i := 0 now := time.Now() // fill out events for k, v := range times { e := event.NewEvent() e.Tags.Set(tag, k) e.Tags.Set(INTERNAL_TAG_NAME, KEEP_ALIVE_INTERNAL_TAG) e.Metric = now.Sub(v).Seconds() events[i] = e i++ } return events }
func BenchmarkIndex(b *testing.B) { c := testCondition(test_f(0), nil, nil, 0) pipe := testPolicy(c, nil, map[string]string{"host": "test"}, nil) p, _ := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() e := event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = -1.0 b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { e.Service = fmt.Sprintf("%d", i%1000) p.Process(e) } e.Wait() }
func (t *TCPProvider) consume(c *net.TCPConn, p event.EventPasser) { // create a newman connection conn := newman.NewConn(c) conn.SetWaiter(&newman.Backoff{}) // drain the connection for ever in, _ := conn.Generate(func() newman.Message { return event.NewEvent() }) for raw := range in { // convert it to an event because we know that is what we are getting e := raw.(*event.Event) // pass it on to the next step p.PassEvent(e) } // when it is done, close the connection c.Close() }
func TestMatchPolicy(t *testing.T) { c := testCondition(test_f(0), nil, nil, 1) pipe := testPolicy(c, nil, map[string]string{"host": "test"}, nil) p, ta := testPipeline(map[string]*alarm.Policy{"test": pipe}) defer p.index.Delete() e := event.NewEvent() e.Host = "test" e.Service = "test" e.Metric = 1.0 p.Process(e) e.Wait() if len(ta.Events) == 0 { t.Fatal() } ta.Do(func(ta *test.TestAlert) { for k, _ := range ta.Events { if k.IndexName() != e.IndexName() { t.Fatal(k.IndexName(), e.IndexName()) } } }) }
func TestResolve(t *testing.T) { x := runningTestContext() x.runTest(func(p *Pipeline) { u := &config.User{} u.Permissions = config.WRITE ta := test.NewTestAlert() // add a escalation policy that will catch our metrics p.UpdateConfig(func(c *config.AppConfig) error { esc := &escalation.EscalationPolicy{} esc.Crit = true esc.Ok = true esc.Escalations = []escalation.Escalation{ta} esc.Match = event.NewTagset(0) esc.Match.Set("host", ".*") c.Escalations["test"] = esc esc.Compile() // create a policy that will match against this host pol := &escalation.Policy{} pol.Match = event.NewTagset(0) pol.Match.Set("host", ".*") cond := &escalation.Condition{} cond.Simple = true cond.Greater = test_f(1) cond.WindowSize = 2 cond.Occurences = 1 pol.Crit = cond c.Policies["test"] = pol return nil }, u) // create an event e := event.NewEvent() e.Metric = 4 e.Tags.Set("host", "test") p.PassEvent(e) e.WaitForState(event.StateComplete, time.Millisecond) // TODO get rid of waiting for things to pass through the pipeline time.Sleep(20 * time.Millisecond) if len(ta.Incidents) != 1 { t.Error(ta.Incidents) } time.Sleep(20 * time.Millisecond) e.Metric = 0 p.PassEvent(e) e.WaitForState(event.StateComplete, time.Millisecond) time.Sleep(10 * time.Millisecond) if len(ta.Incidents) != 2 { t.Error(ta.Incidents[0]) } }) }