Skip to content

Commit

Permalink
make tests more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 5, 2024
1 parent b2ba769 commit 2c789be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
13 changes: 8 additions & 5 deletions pkg/nagflux/collector/livestatus/CacheBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,33 @@ type CacheBuilder struct {
}

const (
// Updateinterval on livestatus data.
intervalToCheckLivestatusCache = time.Duration(30) * time.Second
// QueryForServicesInDowntime livestatusquery for services in downtime.
// default update interval on livestatus data.
defaultIntervalToCheckLivestatusCache = time.Duration(30) * time.Second

// QueryForServicesInDowntime livestatus query for services in downtime.
QueryForServicesInDowntime = `GET services
Columns: downtimes host_name display_name
Filter: scheduled_downtime_depth > 0
OutputFormat: csv
`
// QueryForHostsInDowntime livestatusquery for hosts in downtime
// QueryForHostsInDowntime livestatus query for hosts in downtime
QueryForHostsInDowntime = `GET hosts
Columns: downtimes name
Filter: scheduled_downtime_depth > 0
OutputFormat: csv
`
// QueryForDowntimeid livestatusquery for downtime start/end
// QueryForDowntimeid livestatus query for downtime start/end
QueryForDowntimeid = `GET downtimes
Columns: id start_time entry_time
OutputFormat: csv
`
)

var intervalToCheckLivestatusCache = defaultIntervalToCheckLivestatusCache

// NewLivestatusCacheBuilder constructor, which also starts it immediately.
func NewLivestatusCacheBuilder(livestatusConnector *Connector) *CacheBuilder {
cache := &CacheBuilder{livestatusConnector, make(chan bool, 2), logging.GetLogger(), Cache{make(map[string]map[string]string)}, &sync.Mutex{}}
Expand Down
44 changes: 21 additions & 23 deletions pkg/nagflux/collector/livestatus/CacheBuilder_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package livestatus

import (
"reflect"
"testing"
"time"

"pkg/nagflux/logging"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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")
}
require.NotNilf(t, builder, "Constructor returned pointer")
}

func TestDisabledServiceInDowntime(t *testing.T) {
Expand All @@ -27,31 +27,29 @@ func TestDisabledServiceInDowntime(t *testing.T) {
go livestatus.StartMockLivestatus()
connector := &Connector{logging.GetLogger(), livestatus.LivestatusAddress, livestatus.ConnectionType}

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

// wait 10 seconds till cache matches
waitUntil := time.Now().Add(10 * time.Second)
for time.Now().Before(waitUntil) {
if cacheBuilder.IsServiceInDowntime("host1", "service1", "1") {
break
}
time.Sleep(100 * time.Millisecond)
}

cacheBuilder.Stop()
livestatus.StopMockLivestatus()

intern := map[string]map[string]string{"host1": {"": "1", "service1": "1"}, "host2": {"": "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)
}
assert.Equalf(t, intern, cacheBuilder.downtimeCache.downtime, "internal cache does not fit.")
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`)
}

assert.Truef(t, cacheBuilder.IsServiceInDowntime("host1", "service1", "1"), `"host1","service1","1" should be in downtime`)
assert.Truef(t, cacheBuilder.IsServiceInDowntime("host1", "service1", "2"), `"host1","service1","2" should be in downtime`)
assert.Falsef(t, cacheBuilder.IsServiceInDowntime("host1", "service1", "0"), `"host1","service1","0" should not be in downtime`)
assert.Falsef(t, cacheBuilder.IsServiceInDowntime("host1", "", "0"), `"host1","","0" should not be in downtime`)
assert.Truef(t, cacheBuilder.IsServiceInDowntime("host1", "", "2"), `"host1","","2" should not be in downtime`)
}

0 comments on commit 2c789be

Please sign in to comment.