-
Notifications
You must be signed in to change notification settings - Fork 12
/
app-checker_test.go
76 lines (64 loc) · 2.03 KB
/
app-checker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"net/url"
"testing"
"time"
"github.com/ashwanthkumar/marathon-alerts/checks"
marathon "github.com/gambol99/go-marathon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestProcessCheckForAllSubscribers(t *testing.T) {
appLabels := make(map[string]string)
client := new(MockMarathon)
apps := marathon.Applications{
Apps: []marathon.Application{marathon.Application{Labels: appLabels}},
}
var urlValues url.Values
client.On("Applications", urlValues).Return(&apps, nil)
alertChan := make(chan checks.AppCheck, 1)
check, now := CreateMockChecker(appLabels)
appChecker := AppChecker{
Client: client,
AlertsChannel: alertChan,
Checks: []checks.Checker{check},
}
expectedCheck := checks.AppCheck{Result: checks.Critical, Timestamp: now, App: "/foo-app", Labels: appLabels}
err := appChecker.processChecks()
assert.Nil(t, err)
assert.Len(t, alertChan, 1)
actualCheck := <-alertChan
assert.Equal(t, actualCheck, expectedCheck)
}
func TestProcessCheckForWithNoSubscribers(t *testing.T) {
appLabels := make(map[string]string)
appLabels["alerts.checks.subscribe"] = "check-that-does-not-exist"
client := new(MockMarathon)
apps := marathon.Applications{
Apps: []marathon.Application{marathon.Application{Labels: appLabels}},
}
var urlValues url.Values
client.On("Applications", urlValues).Return(&apps, nil)
alertChan := make(chan checks.AppCheck, 1)
check, _ := CreateMockChecker(appLabels)
appChecker := AppChecker{
Client: client,
AlertsChannel: alertChan,
Checks: []checks.Checker{check},
}
err := appChecker.processChecks()
assert.Nil(t, err)
assert.Len(t, alertChan, 0)
}
func CreateMockChecker(appLabels map[string]string) (checks.Checker, time.Time) {
now := time.Now()
check := new(MockChecker)
check.On("Name").Return("mock-check")
check.On("Check", mock.AnythingOfType("Application")).Return(checks.AppCheck{
Result: checks.Critical,
Timestamp: now,
App: "/foo-app",
Labels: appLabels,
})
return check, now
}