1package v2
2
3import (
4	"encoding/json"
5	"testing"
6
7	"github.com/stretchr/testify/assert"
8	"github.com/stretchr/testify/require"
9)
10
11func TestCheckValidate(t *testing.T) {
12	var c Check
13
14	// Invalid interval
15	c.Interval = 0
16	assert.Error(t, c.Validate())
17	c.Interval = 10
18
19	c.Name = "test"
20
21	assert.NoError(t, c.Validate())
22}
23
24func TestScheduleValidation(t *testing.T) {
25	c := FixtureCheck("check")
26
27	// Fixture comes with valid interval-based schedule
28	assert.NoError(t, c.Validate())
29
30	c.Cron = "* * * * *"
31	assert.Error(t, c.Validate())
32
33	c.Interval = 0
34	assert.NoError(t, c.Validate())
35
36	c.Cron = "this is an invalid cron"
37	assert.Error(t, c.Validate())
38}
39
40func TestFixtureCheckIsValid(t *testing.T) {
41	c := FixtureCheck("check")
42
43	assert.Equal(t, "check", c.Name)
44	assert.NoError(t, c.Validate())
45
46	c.RuntimeAssets = []string{"good"}
47	assert.NoError(t, c.Validate())
48
49	c.RuntimeAssets = []string{"BAD--a!!!---ASDFASDF$$$$"}
50	assert.Error(t, c.Validate())
51}
52
53func TestMergeWith(t *testing.T) {
54	originalCheck := FixtureCheck("check")
55	originalCheck.Status = 1
56
57	newCheck := FixtureCheck("check")
58	newCheck.History = []CheckHistory{}
59
60	newCheck.MergeWith(originalCheck)
61
62	assert.NotEmpty(t, newCheck.History)
63	assert.Equal(t, newCheck.Status, newCheck.History[20].Status)
64}
65
66func TestOutputMetricFormatValidate(t *testing.T) {
67	assert.NoError(t, ValidateOutputMetricFormat("nagios_perfdata"))
68	assert.NoError(t, ValidateOutputMetricFormat(NagiosOutputMetricFormat))
69	assert.NoError(t, ValidateOutputMetricFormat(GraphiteOutputMetricFormat))
70	assert.NoError(t, ValidateOutputMetricFormat(InfluxDBOutputMetricFormat))
71	assert.NoError(t, ValidateOutputMetricFormat(OpenTSDBOutputMetricFormat))
72	assert.Error(t, ValidateOutputMetricFormat("anything_else"))
73	assert.Error(t, ValidateOutputMetricFormat("NAGIOS_PERFDATA"))
74}
75
76func TestCheckHasZeroIssuedMarshaled(t *testing.T) {
77	check := FixtureCheck("foo")
78	check.Issued = 0
79	b, err := json.Marshal(check)
80	if err != nil {
81		t.Fatal(err)
82	}
83	var m map[string]interface{}
84	if err := json.Unmarshal(b, &m); err != nil {
85		t.Fatal(err)
86	}
87	if _, ok := m["issued"]; !ok {
88		t.Error("issued not present")
89	}
90}
91
92func TestCheckHasNonNilSubscriptions(t *testing.T) {
93	var c Check
94	b, err := json.Marshal(&c)
95	require.NoError(t, err)
96	require.NoError(t, json.Unmarshal(b, &c))
97	require.NotNil(t, c.Subscriptions)
98}
99
100func TestCheckHasNonNilHandlers(t *testing.T) {
101	var c Check
102	b, err := json.Marshal(&c)
103	require.NoError(t, err)
104	require.NoError(t, json.Unmarshal(b, &c))
105	require.NotNil(t, c.Handlers)
106}
107
108func TestCheckFlapThresholdValidation(t *testing.T) {
109	c := FixtureCheck("foo")
110	// zero-valued flap threshold is valid
111	c.LowFlapThreshold, c.HighFlapThreshold = 0, 0
112	assert.NoError(t, c.Validate())
113
114	// low flap threshold < high flap threshold is valid
115	c.LowFlapThreshold, c.HighFlapThreshold = 5, 10
116	assert.NoError(t, c.Validate())
117
118	// low flap threshold = high flap threshold is invalid
119	c.LowFlapThreshold, c.HighFlapThreshold = 10, 10
120	assert.Error(t, c.Validate())
121
122	// low flap threshold > high flap threshold is invalid
123	c.LowFlapThreshold, c.HighFlapThreshold = 11, 10
124	assert.Error(t, c.Validate())
125}
126
127func TestCheckMerge(t *testing.T) {
128	a := FixtureCheck("check")
129	b := FixtureCheck("check")
130
131	for i := range a.History {
132		if i%2 == 0 {
133			a.History[i].Status = 1
134		}
135	}
136
137	a.Occurrences = 1
138	a.OccurrencesWatermark = 1
139
140	b.History = nil
141	b.TotalStateChange = 0
142	b.State = ""
143	b.Occurrences = 0
144	b.OccurrencesWatermark = 0
145
146	b.MergeWith(a)
147
148	if got, want := b.TotalStateChange, uint32(98); got != want {
149		t.Errorf("bad TotalStateChange: got %d, want %d", got, want)
150	}
151
152	if got, want := b.State, EventFlappingState; got != want {
153		t.Errorf("bad State: got %s, want %s", got, want)
154	}
155
156	if got, want := b.Occurrences, int64(1); got != want {
157		t.Errorf("bad Occurrences: got %d, want %d", got, want)
158	}
159
160	if got, want := b.OccurrencesWatermark, int64(1); got != want {
161		t.Errorf("bad OccurrencesWatermark: got %d, want %d", got, want)
162	}
163
164}
165