Skip to content

Commit

Permalink
[ws-manager] PodRejected: Add test for phase transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl committed Oct 1, 2024
1 parent c895e84 commit 2f09f8c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
16 changes: 13 additions & 3 deletions components/ws-manager-mk2/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func TestAPIs(t *testing.T) {
}

var (
ctx context.Context
cancel context.CancelFunc
wsMetrics *controllerMetrics
ctx context.Context
cancel context.CancelFunc
wsMetrics *controllerMetrics
RegisterSubscriber func(func(*workspacev1.Workspace))
)

var _ = BeforeSuite(func() {
Expand Down Expand Up @@ -116,6 +117,15 @@ var _ = BeforeSuite(func() {
Expect(timeoutReconciler.SetupWithManager(k8sManager)).To(Succeed())

ctx, cancel = context.WithCancel(context.Background())
subscriberReconciler, err := NewSubscriberReconciler(k8sManager.GetClient(), &conf)
Expect(err).ToNot(HaveOccurred())
Expect(subscriberReconciler.SetupWithManager(ctx, k8sManager)).To(Succeed())
RegisterSubscriber = func(onReconcile func(*workspacev1.Workspace)) {
subscriberReconciler.OnReconcile = func(ctx context.Context, ws *workspacev1.Workspace) {
onReconcile(ws)
}
}

_ = createNamespace(secretsNamespace)

go func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (r *WorkspaceReconciler) actOnStatus(ctx context.Context, workspace *worksp
// Reset status
sc := workspace.Status.DeepCopy()
workspace.Status = workspacev1.WorkspaceStatus{}
workspace.Status.Phase = workspacev1.WorkspacePhasePending
workspace.Status.OwnerToken = sc.OwnerToken
workspace.Status.PodStarts = sc.PodStarts
workspace.Status.PodRecreated = sc.PodRecreated + 1
Expand Down
35 changes: 35 additions & 0 deletions components/ws-manager-mk2/controllers/workspace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ var _ = Describe("WorkspaceController", func() {
It("pod rejection should result in a retry", func() {
ws := newWorkspace(uuid.NewString(), "default")
m := collectMetricCounts(wsMetrics, ws)
su := collectSubscriberUpdates()

// ### prepare block start
By("creating workspace")
Expand Down Expand Up @@ -530,6 +531,8 @@ var _ = Describe("WorkspaceController", func() {
starts: 1, // this is NOT PodStarts, but merely an artifact of how we count it in the tests
recreations: map[int]int{1: 1},
})

expectPhaseTransitions(su, []workspacev1.WorkspacePhase{workspacev1.WorkspacePhasePending, workspacev1.WorkspacePhaseCreating, workspacev1.WorkspacePhaseInitializing, workspacev1.WorkspacePhaseRunning})
// ### validate end
})
})
Expand Down Expand Up @@ -1042,3 +1045,35 @@ func expectMetricsDelta(initial metricCounts, cur metricCounts, expectedDelta me
Expect(cur.restores-initial.restores).To(Equal(expectedDelta.restores), "expected metric count delta for restores")
Expect(cur.restoreFailures-initial.restoreFailures).To(Equal(expectedDelta.restoreFailures), "expected metric count delta for restoreFailures")
}

type subscriberUpdates struct {
phaseTransitions []workspacev1.WorkspacePhase
}

func collectSubscriberUpdates() *subscriberUpdates {
su := subscriberUpdates{}
recordPhaseTransition := func(su *subscriberUpdates, ws *workspacev1.Workspace) {
phase := ws.Status.Phase

var lastPhase workspacev1.WorkspacePhase
lenPhases := len(su.phaseTransitions)
if lenPhases > 0 {
lastPhase = su.phaseTransitions[lenPhases-1]
}

if lastPhase != phase {
su.phaseTransitions = append(su.phaseTransitions, phase)
}
}

RegisterSubscriber(func(ws *workspacev1.Workspace) {
recordPhaseTransition(&su, ws)
})
return &su
}

func expectPhaseTransitions(su *subscriberUpdates, expectation []workspacev1.WorkspacePhase) {
GinkgoHelper()
By("checking recorded phase transitions")
Expect(su.phaseTransitions).To(HaveExactElements(expectation), "expected list of recorded phase transitions")
}

0 comments on commit 2f09f8c

Please sign in to comment.