Skip to content

refactor: claim prebuilt workspace tests #17567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 28, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: minor refactoring
  • Loading branch information
evgeniy-scherbina committed Apr 28, 2025
commit a91b915ff3c66291f9edf72bc903b6ecda33702c
18 changes: 10 additions & 8 deletions enterprise/coderd/prebuilds/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type storeSpy struct {
claimParams *atomic.Pointer[database.ClaimPrebuiltWorkspaceParams]
claimedWorkspace *atomic.Pointer[database.ClaimPrebuiltWorkspaceRow]

// if claimingErr is not nil - error will be returned when ClaimPrebuiltWorkspace is called
claimingErr error
}

Expand Down Expand Up @@ -93,7 +94,8 @@ func TestClaimPrebuild(t *testing.T) {
cases := map[string]struct {
expectPrebuildClaimed bool
markPrebuildsClaimable bool
storeError error // should be set only for errorStoreType
// if claimingErr is not nil - error will be returned when ClaimPrebuiltWorkspace is called
claimingErr error
}{
"no eligible prebuilds to claim": {
expectPrebuildClaimed: false,
Expand All @@ -107,12 +109,12 @@ func TestClaimPrebuild(t *testing.T) {
"no claimable prebuilt workspaces error is returned": {
expectPrebuildClaimed: false,
markPrebuildsClaimable: true,
storeError: agplprebuilds.ErrNoClaimablePrebuiltWorkspaces,
claimingErr: agplprebuilds.ErrNoClaimablePrebuiltWorkspaces,
},
"unexpected claiming error is returned": {
expectPrebuildClaimed: false,
markPrebuildsClaimable: true,
storeError: unexpectedClaimingError,
claimingErr: unexpectedClaimingError,
},
}

Expand All @@ -126,7 +128,7 @@ func TestClaimPrebuild(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitSuperLong)
db, pubsub := dbtestutil.NewDB(t)

spy := newStoreSpy(db, tc.storeError)
spy := newStoreSpy(db, tc.claimingErr)
expectedPrebuildsCount := desiredInstances * presetCount

logger := testutil.Logger(t)
Expand Down Expand Up @@ -223,7 +225,7 @@ func TestClaimPrebuild(t *testing.T) {
})

switch {
case tc.storeError != nil && errors.Is(tc.storeError, agplprebuilds.ErrNoClaimablePrebuiltWorkspaces):
case tc.claimingErr != nil && errors.Is(tc.claimingErr, agplprebuilds.ErrNoClaimablePrebuiltWorkspaces):
require.NoError(t, err)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, userWorkspace.LatestBuild.ID)

Expand All @@ -233,7 +235,7 @@ func TestClaimPrebuild(t *testing.T) {
require.Equal(t, expectedPrebuildsCount, len(currentPrebuilds))
return

case tc.storeError != nil && errors.Is(tc.storeError, unexpectedClaimingError):
case tc.claimingErr != nil && errors.Is(tc.claimingErr, unexpectedClaimingError):
// Then: unexpected error happened and was propagated all the way to the caller
require.Error(t, err)
require.ErrorContains(t, err, unexpectedClaimingError.Error())
Expand All @@ -245,12 +247,12 @@ func TestClaimPrebuild(t *testing.T) {
return

default:
// tc.storeType == spyStoreType scenario
// tc.claimingErr is nil scenario
require.NoError(t, err)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, userWorkspace.LatestBuild.ID)
}

// at this point we know that wrappedStore has *storeSpy type
// at this point we know that tc.claimingErr is nil

// Then: a prebuild should have been claimed.
require.EqualValues(t, spy.claims.Load(), 1)
Expand Down
Loading