-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrollout_test.go
More file actions
265 lines (243 loc) · 8.2 KB
/
Copy pathrollout_test.go
File metadata and controls
265 lines (243 loc) · 8.2 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// SPDX-License-Identifier: Apache-2.0
package controller
import (
"testing"
"github.com/go-logr/logr"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
bootcv1alpha1 "github.com/jlebon/bootc-operator/api/v1alpha1"
testutil "github.com/jlebon/bootc-operator/test/util"
)
func TestBuildRolloutState(t *testing.T) {
const (
desiredImage = testImageDigestRefA
otherDigest = testDigestB
)
g := NewWithT(t)
// Classification of each state is tested in TestClassifyNode. This
// test focuses more on aggregation: bucketing, slot counting, and
// nodeCount.
nodes := map[string]*bootcv1alpha1.BootcNode{
"uptodate": testutil.NewNode("uptodate", desiredImage,
testutil.WithBootedDigest(testDigestA),
testutil.WithNodeCondition(bootcv1alpha1.NodeIdle, metav1.ConditionTrue, bootcv1alpha1.NodeReasonIdle)),
"pending": testutil.NewNode("pending", desiredImage,
testutil.WithBootedDigest(otherDigest),
testutil.WithNodeCondition(bootcv1alpha1.NodeIdle, metav1.ConditionTrue, bootcv1alpha1.NodeReasonIdle)),
"staged": testutil.NewNode("staged", desiredImage,
testutil.WithBootedDigest(otherDigest),
testutil.WithNodeCondition(bootcv1alpha1.NodeIdle, metav1.ConditionFalse, bootcv1alpha1.NodeReasonStaged)),
"rebooting-1": testutil.NewNode("rebooting-1", desiredImage,
testutil.WithBootedDigest(otherDigest),
testutil.WithNodeCondition(bootcv1alpha1.NodeIdle, metav1.ConditionFalse, bootcv1alpha1.NodeReasonRebooting),
testutil.WithNodeAnnotation(bootcv1alpha1.AnnotationInRebootSlot, "")),
"rebooting-2": testutil.NewNode("rebooting-2", desiredImage,
testutil.WithBootedDigest(otherDigest),
testutil.WithNodeCondition(bootcv1alpha1.NodeIdle, metav1.ConditionFalse, bootcv1alpha1.NodeReasonRebooting),
testutil.WithNodeAnnotation(bootcv1alpha1.AnnotationInRebootSlot, "")),
}
rs := buildRolloutState(logr.Discard(), nodes)
g.Expect(rs.upToDate).To(HaveLen(1))
g.Expect(rs.pending).To(HaveLen(1))
g.Expect(rs.staged).To(HaveLen(1))
g.Expect(rs.rebooting).To(HaveLen(2))
g.Expect(rs.occupiedSlots).To(Equal(2))
g.Expect(rs.unclassified).To(BeEmpty())
g.Expect(rs.nodeCount()).To(Equal(5))
}
func TestResolveMaxUnavailable(t *testing.T) {
intstrPtr := func(v intstr.IntOrString) *intstr.IntOrString { return &v }
tests := []struct {
name string
maxUnavailable *intstr.IntOrString
paused bool
nodeCount int
want int
wantErr bool
}{
{
name: "nil maxUnavailable defaults to 1",
maxUnavailable: nil,
nodeCount: 10,
want: 1,
},
{
name: "int value",
maxUnavailable: intstrPtr(intstr.FromInt32(3)),
nodeCount: 10,
want: 3,
},
{
name: "percentage rounds up",
maxUnavailable: intstrPtr(intstr.FromString("25%")),
nodeCount: 10,
want: 3,
},
{
name: "paused returns 0 regardless of maxUnavailable",
maxUnavailable: intstrPtr(intstr.FromInt32(3)),
paused: true,
nodeCount: 10,
want: 0,
},
{
name: "invalid string returns error",
maxUnavailable: intstrPtr(intstr.FromString("banana")),
nodeCount: 10,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
var opts []testutil.PoolOption
if tt.maxUnavailable != nil {
opts = append(opts, testutil.WithMaxUnavailable(*tt.maxUnavailable))
}
if tt.paused {
opts = append(opts, testutil.WithPaused(true))
}
pool := testutil.NewPool("p", testImageDigestRefA, opts...)
got, err := resolveMaxUnavailable(pool, tt.nodeCount)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
g.Expect(isInvalidSpecError(err)).To(BeTrue())
return
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(got).To(Equal(tt.want))
})
}
}
func TestSelectDrainCandidates(t *testing.T) {
g := NewWithT(t)
makeBN := func(name string, slotted bool) *bootcv1alpha1.BootcNode {
bn := &bootcv1alpha1.BootcNode{}
bn.Name = name
if slotted {
bn.Annotations = map[string]string{bootcv1alpha1.AnnotationInRebootSlot: ""}
}
return bn
}
// Core behavior: slotted nodes always included and come first
// (alphabetical), then unslotted up to availableSlots (alphabetical).
staged := []*bootcv1alpha1.BootcNode{
makeBN("node-c", false),
makeBN("node-b", true),
makeBN("node-a", true),
makeBN("node-d", false),
}
candidates := selectDrainCandidates(staged, 1)
g.Expect(candidates).To(HaveLen(3))
g.Expect(candidates[0].Name).To(Equal("node-a")) // slotted, alphabetical
g.Expect(candidates[1].Name).To(Equal("node-b")) // slotted, alphabetical
g.Expect(candidates[2].Name).To(Equal("node-c")) // first unslotted by name
// Zero available slots with no slotted nodes returns nil.
g.Expect(selectDrainCandidates([]*bootcv1alpha1.BootcNode{makeBN("x", false)}, 0)).To(BeNil())
}
func TestClassifyNode(t *testing.T) {
const (
desiredImage = testImageDigestRefA
desiredDigest = testDigestA
otherDigest = testDigestB
nodeName = "test-node"
)
idleCond := func(status metav1.ConditionStatus, reason string) metav1.Condition {
return metav1.Condition{
Type: bootcv1alpha1.NodeIdle,
Status: status,
Reason: reason,
}
}
degradedCond := func(status metav1.ConditionStatus, reason string) metav1.Condition {
return metav1.Condition{
Type: bootcv1alpha1.NodeDegraded,
Status: status,
Reason: reason,
}
}
tests := []struct {
name string
bootedDigest string
conditions []metav1.Condition
want nodeState
}{
{
name: "UpToDate: image matches, Idle=True",
bootedDigest: desiredDigest,
conditions: []metav1.Condition{idleCond(metav1.ConditionTrue, bootcv1alpha1.NodeReasonIdle)},
want: nodeStateUpToDate,
},
{
name: "Pending: image differs, Idle=True (daemon hasn't reacted)",
bootedDigest: otherDigest,
conditions: []metav1.Condition{idleCond(metav1.ConditionTrue, bootcv1alpha1.NodeReasonIdle)},
want: nodeStatePending,
},
{
name: "Pending: no booted status yet (daemon starting)",
bootedDigest: "",
conditions: nil,
want: nodeStatePending,
},
{
name: "Pending: image differs, no conditions",
bootedDigest: otherDigest,
conditions: nil,
want: nodeStatePending,
},
{
name: "Staging: image differs, Idle=False reason=Staging",
bootedDigest: otherDigest,
conditions: []metav1.Condition{idleCond(metav1.ConditionFalse, bootcv1alpha1.NodeReasonStaging)},
want: nodeStateStaging,
},
{
name: "Staged: image differs, Idle=False reason=Staged",
bootedDigest: otherDigest,
conditions: []metav1.Condition{idleCond(metav1.ConditionFalse, bootcv1alpha1.NodeReasonStaged)},
want: nodeStateStaged,
},
{
name: "Rebooting: image differs, Idle=False reason=Rebooting",
bootedDigest: otherDigest,
conditions: []metav1.Condition{idleCond(metav1.ConditionFalse, bootcv1alpha1.NodeReasonRebooting)},
want: nodeStateRebooting,
},
{
name: "Staging: Degraded=False does not affect classification",
bootedDigest: otherDigest,
conditions: []metav1.Condition{
idleCond(metav1.ConditionFalse, bootcv1alpha1.NodeReasonStaging),
degradedCond(metav1.ConditionFalse, bootcv1alpha1.NodeReasonHealthy),
},
want: nodeStateStaging,
},
{
name: "Degraded: Degraded=True takes priority over Idle",
bootedDigest: desiredDigest,
conditions: []metav1.Condition{
idleCond(metav1.ConditionTrue, bootcv1alpha1.NodeReasonIdle),
degradedCond(metav1.ConditionTrue, bootcv1alpha1.NodeReasonError),
},
want: nodeStateDegraded,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
var opts []testutil.NodeOption
if tt.bootedDigest != "" {
opts = append(opts, testutil.WithBootedDigest(tt.bootedDigest))
}
for _, c := range tt.conditions {
opts = append(opts, testutil.WithNodeCondition(c.Type, c.Status, c.Reason))
}
bn := testutil.NewNode(nodeName, desiredImage, opts...)
got, err := classifyNode(bn)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(got).To(Equal(tt.want))
})
}
}