StatefulSet: Cleanup the complex defer function updating the status

In the long term, the complex defer function makes the code harder to
maintain as code after it should take that into account. This removes
the complex defer function updating the status of a statefulset.
This commit is contained in:
Gunju Kim
2022-09-26 22:11:07 +09:00
parent 43a2bb4df4
commit 6559050ee1
2 changed files with 95 additions and 20 deletions

View File

@@ -2106,6 +2106,78 @@ func TestStatefulSetAvailability(t *testing.T) {
}
}
func TestStatefulSetStatusUpdate(t *testing.T) {
var (
syncErr = fmt.Errorf("sync error")
statusErr = fmt.Errorf("status error")
)
testCases := []struct {
desc string
hasSyncErr bool
hasStatusErr bool
expectedErr error
}{
{
desc: "no error",
hasSyncErr: false,
hasStatusErr: false,
expectedErr: nil,
},
{
desc: "sync error",
hasSyncErr: true,
hasStatusErr: false,
expectedErr: syncErr,
},
{
desc: "status error",
hasSyncErr: false,
hasStatusErr: true,
expectedErr: statusErr,
},
{
desc: "sync and status error",
hasSyncErr: true,
hasStatusErr: true,
expectedErr: syncErr,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
set := newStatefulSet(3)
client := fake.NewSimpleClientset(set)
om, ssu, ssc := setupController(client)
if tc.hasSyncErr {
om.SetCreateStatefulPodError(syncErr, 0)
}
if tc.hasStatusErr {
ssu.SetUpdateStatefulSetStatusError(statusErr, 0)
}
selector, err := metav1.LabelSelectorAsSelector(set.Spec.Selector)
if err != nil {
t.Error(err)
}
pods, err := om.podsLister.Pods(set.Namespace).List(selector)
if err != nil {
t.Error(err)
}
_, err = ssc.UpdateStatefulSet(context.TODO(), set, pods)
if ssu.updateStatusTracker.requests != 1 {
t.Errorf("Did not update status")
}
if !errors.Is(err, tc.expectedErr) {
t.Errorf("Expected error: %v, got: %v", tc.expectedErr, err)
}
})
}
}
type requestTracker struct {
requests int
err error