-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_test.go
124 lines (110 loc) · 2.81 KB
/
status_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"os"
"reflect"
"testing"
)
const testStatusFilename string = "test-status.yaml"
func TestServiceStatus(t *testing.T) {
subtests := []struct {
name string
initial Status
expected Status
}{
{
name: "WriteAndRead",
initial: Status{
DownCount: 40,
DownServices: []string{"service1", "service2"},
},
expected: Status{
DownCount: 0,
DownServices: []string{"service1", "service2"},
},
},
{
name: "Increment",
initial: Status{
DownCount: 1,
DownServices: []string{"service1", "service2"},
},
expected: Status{
DownCount: 2,
DownServices: []string{"service1", "service2"},
},
},
{
name: "Decrement",
initial: Status{
DownCount: 6,
DownServices: []string{"service1", "service2"},
},
expected: Status{
DownCount: 5,
DownServices: []string{"service1", "service2"},
},
},
{
name: "IsAffected",
initial: Status{
DownCount: 0,
DownServices: []string{"service1", "service2"},
},
expected: Status{
DownCount: 0,
DownServices: []string{"service1", "service2"},
},
},
{
name: "IsNotAffected",
initial: Status{
DownCount: 40,
DownServices: []string{"service1", "service2"},
},
expected: Status{
DownCount: 5,
DownServices: []string{"service3", "service1"},
},
},
}
var readServiceStatus Status
defer os.Remove(testStatusFilename)
for _, subtest := range subtests {
t.Run(subtest.name, func(t *testing.T) {
switch subtest.name {
case "WriteAndRead":
err := subtest.initial.Write(testStatusFilename)
if err != nil {
t.Errorf("Error writing ServiceStatus: %v", err)
}
err = readServiceStatus.Read(testStatusFilename)
if err != nil {
t.Errorf("Error reading ServiceStatus: %v", err)
}
if !reflect.DeepEqual(readServiceStatus, subtest.expected) {
t.Errorf("Expected: %v, got: %v", subtest.expected, readServiceStatus)
}
case "Increment":
readServiceStatus.DownCount = subtest.initial.DownCount
readServiceStatus.incrementDownCount()
if !reflect.DeepEqual(readServiceStatus, subtest.expected) {
t.Errorf("Expected: %v, got: %v", subtest.expected, readServiceStatus)
}
case "Decrement":
readServiceStatus.DownCount = subtest.initial.DownCount
readServiceStatus.decrementDownCount()
if !reflect.DeepEqual(readServiceStatus, subtest.expected) {
t.Errorf("Expected: %v, got: %v", subtest.expected, readServiceStatus)
}
case "IsAffected":
if !readServiceStatus.isAffected(subtest.expected.DownServices[0]) {
t.Errorf("Expected: %v, got: %v", true, false)
}
case "IsNotAffected":
if readServiceStatus.isAffected(subtest.expected.DownServices[0]) {
t.Errorf("Expected: %v, got: %v", false, true)
}
}
})
}
}