-
Notifications
You must be signed in to change notification settings - Fork 15
/
repair_test.go
214 lines (197 loc) · 6 KB
/
repair_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cke
import (
"slices"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRepairQueueEntry(t *testing.T) {
cluster := &Cluster{
Nodes: []*Node{
{
Address: "1.1.1.1",
Hostname: "node1",
},
},
Repair: Repair{
RepairProcedures: []RepairProcedure{
{
MachineTypes: []string{"type1", "type2"},
RepairOperations: []RepairOperation{
{
Operation: "unreachable",
RepairSteps: []RepairStep{
{RepairCommand: []string{"type12", "unreachable0"}},
{RepairCommand: []string{"type12", "unreachable1"}},
},
HealthCheckCommand: []string{"check12-unreachable"},
},
{
Operation: "unhealthy",
RepairSteps: []RepairStep{
{RepairCommand: []string{"type12", "unhealthy0"}},
},
HealthCheckCommand: []string{"check12-unhealthy"},
},
},
},
{
MachineTypes: []string{"type3"},
RepairOperations: []RepairOperation{
{
RepairSteps: []RepairStep{
{RepairCommand: []string{"type3", "unreachable0"}},
},
HealthCheckCommand: []string{"check3"},
},
},
},
},
},
}
// for in-cluster machine
entry := NewRepairQueueEntry("unreachable", "type2", "1.1.1.1")
entry.FillNodename(cluster)
if entry.Nodename != "node1" {
t.Error("FillNodename() failed to fill Nodename:", entry.Nodename)
}
if !entry.IsInCluster() {
t.Error("IsInCluster() returned false incorrectly")
}
// for out-of-cluster machine
// GetCorrespondingNode should fail for bad address
entry = NewRepairQueueEntry("unreachable", "type2", "2.2.2.2")
entry.FillNodename(cluster)
if entry.Nodename != "" {
t.Error("FillNodename() filled wrong Nodename:", entry.Nodename)
}
if entry.IsInCluster() {
t.Error("IsInCluster() returned true incorrectly")
}
// HaveFinished should return true iff entry has succeeded or failed
entry = NewRepairQueueEntry("unreachable", "type2", "1.1.1.1")
for _, testCase := range []struct {
status RepairStatus
finished bool
}{
{RepairStatusQueued, false},
{RepairStatusProcessing, false},
{RepairStatusSucceeded, true},
{RepairStatusFailed, true},
} {
entry.Status = testCase.status
if entry.HasFinished() != testCase.finished {
t.Errorf("HaveFinished() returned %v incorrectly for %q", testCase.finished, testCase.status)
}
}
// GetMatchingRepairOperation should succeed
entry = NewRepairQueueEntry("unreachable", "type2", "1.1.1.1")
op, err := entry.GetMatchingRepairOperation(cluster)
if err != nil {
t.Fatal("GetMatchingRepairOperation() failed:", err)
}
if !slices.Equal(op.HealthCheckCommand, []string{"check12-unreachable"}) {
t.Error("GetMatchingRepairOperation() returned wrong repair operation:", op)
}
// GetMatchingRepairOperation should fail for bad machine type
entry = NewRepairQueueEntry("unreachable", "type4", "1.1.1.1")
_, err = entry.GetMatchingRepairOperation(cluster)
if err != ErrRepairProcedureNotFound {
t.Error("GetMatchingRepairOperation() returned wrong error:", err)
}
// GetMatchingRepairOperation should fail for bad operation
entry = NewRepairQueueEntry("noop", "type2", "1.1.1.1")
_, err = entry.GetMatchingRepairOperation(cluster)
if err != ErrRepairOperationNotFound {
t.Error("GetMatchingRepairOperation() returned wrong error:", err)
}
// GetCurrentRepairStep should succeed
entry = NewRepairQueueEntry("unreachable", "type2", "1.1.1.1")
entry.Status = RepairStatusProcessing
entry.Step = 1
entry.StepStatus = RepairStepStatusWatching
step, err := entry.GetCurrentRepairStep(cluster)
if err != nil {
t.Fatal("GetCurrentRepairStep() failed:", err)
}
if !slices.Equal(step.RepairCommand, []string{"type12", "unreachable1"}) {
t.Error("GetCurrentRepairStep() returned wrong repair step:", step)
}
// GetCurrentRepairStep should fail for end of steps
entry.Step++
_, err = entry.GetCurrentRepairStep(cluster)
if err != ErrRepairStepOutOfRange {
t.Error("GetCurrentRepairStep() returned wrong error:", err)
}
// GetCurrentRepairStep should fail for bad machine type
entry = NewRepairQueueEntry("unreachable", "type4", "1.1.1.1")
entry.Status = RepairStatusProcessing
entry.Step = 1
entry.StepStatus = RepairStepStatusWatching
_, err = entry.GetCurrentRepairStep(cluster)
if err != ErrRepairProcedureNotFound {
t.Error("GetCurrentRepairStep() returned wrong error:", err)
}
// GetCurrentRepairStep should fail for bad operation
entry = NewRepairQueueEntry("noop", "type2", "1.1.1.1")
entry.Status = RepairStatusProcessing
entry.Step = 1
entry.StepStatus = RepairStepStatusWatching
_, err = entry.GetCurrentRepairStep(cluster)
if err != ErrRepairOperationNotFound {
t.Error("GetCurrentRepairStep() returned wrong error:", err)
}
}
func TestCountRepairQueueEntries(t *testing.T) {
input := []*RepairQueueEntry{
{Status: RepairStatusQueued},
{Status: RepairStatusProcessing},
{Status: RepairStatusSucceeded},
{Status: RepairStatusProcessing},
{Status: RepairStatusSucceeded},
{Status: RepairStatusSucceeded},
}
expected := map[string]int{
"queued": 1,
"processing": 2,
"succeeded": 3,
"failed": 0,
}
actual := CountRepairQueueEntries(input)
if !cmp.Equal(actual, expected) {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
}
func TestBuildMachineRepairStatus(t *testing.T) {
inputNodes := []*Node{
{Address: "1.1.1.1"},
{Address: "2.2.2.2"},
}
inputEntries := []*RepairQueueEntry{
{Address: "1.1.1.1", Status: RepairStatusQueued},
{Address: "10.10.10.10", Status: RepairStatusFailed},
}
expected := map[string]map[string]bool{
"1.1.1.1": {
"queued": true,
"processing": false,
"succeeded": false,
"failed": false,
},
"2.2.2.2": {
"queued": false,
"processing": false,
"succeeded": false,
"failed": false,
},
"10.10.10.10": {
"queued": false,
"processing": false,
"succeeded": false,
"failed": true,
},
}
actual := BuildMachineRepairStatus(inputNodes, inputEntries)
if !cmp.Equal(actual, expected) {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
}