Example #1
0
func TestNewCacheBuilder(t *testing.T) {
	logging.InitTestLogger()
	connector := &Connector{logging.GetLogger(), "localhost:6558", "tcp"}
	builder := NewLivestatusCacheBuilder(connector)
	if builder == nil {
		t.Error("Constructor returned null pointer")
	}
}
Example #2
0
func TestPrintInfluxdbDowntime(t *testing.T) {
	logging.InitTestLogger()
	down := DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip"}, endTime: "123"}
	if !didThisPanic(down.PrintForInfluxDB, "0.8") {
		t.Errorf("This should panic, due to unsuported influxdb version")
	}

	result := down.PrintForInfluxDB("0.9")
	expected := `messages,host=host\ 1,service=service\ 1,type=downtime,author=philip message="Downtime start: <br>" 000
messages,host=host\ 1,service=service\ 1,type=downtime,author=philip message="Downtime end: <br>" 123000`
	if result != expected {
		t.Errorf("The result did not match the expected. Result:\n%s \nExpected:\n%s", result, expected)
	}
}
Example #3
0
func TestPrintElasticsearchComment(t *testing.T) {
	logging.InitTestLogger()
	config.InitConfigFromString(fmt.Sprintf(Config, "monthly"))
	comment := CommentData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip", comment: "hallo world", entryTime: "1458988932000"}, entryType: "1"}
	if !didThatPanic(comment.PrintForElasticsearch, "1.0", "index") {
		t.Errorf("This should panic, due to unsuported elasticsearch version")
	}
	for _, data := range PrintCommentData {
		actual := data.input.PrintForElasticsearch("2.0", "index")
		if actual != data.outputElastic {
			t.Errorf("Print(%s): expected: %s, actual: %s", data.input, data.outputElastic, actual)
		}
	}
}
Example #4
0
func TestPrintInfluxdbComment(t *testing.T) {
	t.Parallel()
	logging.InitTestLogger()
	comment := CommentData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip", comment: "hallo world"}, entryType: "1"}
	if !didThisPanic(comment.PrintForInfluxDB, "0.8") {
		t.Errorf("This should panic, due to unsuported influxdb version")
	}
	for _, data := range PrintCommentData {
		actual := data.input.PrintForInfluxDB("0.9")
		if actual != data.outputInflux {
			t.Errorf("Print(%s): expected: %s, actual: %s", data.input, data.outputInflux, actual)
		}
	}
}
Example #5
0
func TestPrintElasticsearchDowntime(t *testing.T) {
	logging.InitTestLogger()
	config.InitConfigFromString(fmt.Sprintf(Config, "monthly"))
	down := DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip", entryTime: "1458988932000"}, endTime: "123"}
	if !didThatPanic(down.PrintForElasticsearch, "1.0", "index") {
		t.Errorf("This should panic, due to unsuported elasticsearch version")
	}

	result := down.PrintForElasticsearch("2.0", "index")
	expected := `{"index":{"_index":"index-2016.03","_type":"messages"}}
{"timestamp":1458988932000000,"message":"Downtime start: <br>","author":"philip","host":"host 1","service":"service 1","type":"downtime"}

{"index":{"_index":"index-1970.01","_type":"messages"}}
{"timestamp":123000,"message":"Downtime end: <br>","author":"philip","host":"host 1","service":"service 1","type":"downtime"}
`
	if result != expected {
		t.Errorf("The result did not match the expected. Result: %sExpected: %s", result, expected)
	}
}
Example #6
0
func DisabledTestServiceInDowntime(t *testing.T) {
	logging.InitTestLogger()
	queries := map[string]string{}
	queries[QueryForServicesInDowntime] = "1,2;host1;service1\n"
	queries[QueryForHostsInDowntime] = "3,4;host1\n5;host2\n"
	queries[QueryForDowntimeid] = "1;0;1\n2;2;3\n3;0;1\n4;1;2\n5;2;1\n"
	livestatus := &MockLivestatus{"localhost:6558", "tcp", queries, true}
	go livestatus.StartMockLivestatus()
	connector := &Connector{logging.GetLogger(), livestatus.LivestatusAddress, livestatus.ConnectionType}

	cacheBuilder := NewLivestatusCacheBuilder(connector)
	time.Sleep(time.Duration(2) * time.Second)

	cacheBuilder.Stop()
	livestatus.StopMockLivestatus()

	intern := map[string]map[string]string{"host1": map[string]string{"": "1", "service1": "1"}, "host2": map[string]string{"": "2"}}
	cacheBuilder.mutex.Lock()
	if !reflect.DeepEqual(cacheBuilder.downtimeCache.downtime, intern) {
		t.Errorf("Internall Cache does not fit.\nExpexted:%s\nResult:%s\n", intern, cacheBuilder.downtimeCache.downtime)
	}
	cacheBuilder.mutex.Unlock()
	if !cacheBuilder.IsServiceInDowntime("host1", "service1", "1") {
		t.Errorf(`"host1","service1","1" should be in downtime`)
	}
	if !cacheBuilder.IsServiceInDowntime("host1", "service1", "2") {
		t.Errorf(`"host1","service1","2" should be in downtime`)
	}
	if cacheBuilder.IsServiceInDowntime("host1", "service1", "0") {
		t.Errorf(`"host1","service1","0" should NOT be in downtime`)
	}
	if cacheBuilder.IsServiceInDowntime("host1", "", "0") {
		t.Errorf(`"host1","","0" should NOT be in downtime`)
	}
	if !cacheBuilder.IsServiceInDowntime("host1", "", "2") {
		t.Errorf(`"host1","","2" should be in downtime`)
	}

}