コード例 #1
0
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()
		}
	})
}
コード例 #2
0
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])
		}
	})
}