-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
49 lines (43 loc) · 1.09 KB
/
main_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
package main
import (
"fmt"
"os"
"testing"
"time"
)
func TestWork(t *testing.T) {
// Setup
sentinelDir := t.TempDir()
sentinelPath = sentinelDir + "/reboot-required"
promDir := t.TempDir()
promFile = promDir + "/reboot.prom"
metric = "my_metric_name{foo=\"bar\"}"
// Start and settle
go work()
time.Sleep(time.Millisecond * 100)
t.Run("initalAbsent", func(t *testing.T) {
// value should be zero
f, _ := os.ReadFile(promFile)
if string(f) != fmt.Sprintf("%s 0\n", metric) {
t.Error("metric is not zero")
}
})
t.Run("switchHigh", func(t *testing.T) {
// value should switch to one
os.WriteFile(sentinelPath, []byte(""), 0666)
time.Sleep(time.Millisecond * 100)
f, _ := os.ReadFile(promFile)
if string(f) != fmt.Sprintf("%s 1\n", metric) {
t.Error("metric is not one")
}
})
t.Run("switchLow", func(t *testing.T) {
// value should switch to zero when file deleted
os.Remove(sentinelPath)
time.Sleep(time.Millisecond * 100)
f, _ := os.ReadFile(promFile)
if string(f) != fmt.Sprintf("%s 0\n", metric) {
t.Error("metric did not switch back to zero")
}
})
}