Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8b9e30d
feat: implement claiming of prebuilt workspaces
evgeniy-scherbina Apr 17, 2025
d9497df
Merge branch 'main' of github.com:/coder/coder into yevhenii/512-clai…
dannykopping Apr 22, 2025
217e46f
Reverting unncessary changes
dannykopping Apr 21, 2025
c459533
Refactoring
dannykopping Apr 22, 2025
f905219
Removing unnecessary field
dannykopping Apr 22, 2025
8266338
make fmt
dannykopping Apr 22, 2025
4098ed7
CR's fixes
evgeniy-scherbina Apr 22, 2025
50d23e4
CR's fixes
evgeniy-scherbina Apr 22, 2025
546b7ae
Update coderd/workspaces.go
evgeniy-scherbina Apr 22, 2025
85f7926
CR's fixes
evgeniy-scherbina Apr 22, 2025
ee9908c
CR's fixes
evgeniy-scherbina Apr 22, 2025
70bf179
CR's fixes
evgeniy-scherbina Apr 22, 2025
7a72d03
CR's fixes
evgeniy-scherbina Apr 22, 2025
b246589
fix tests by updating noop.go
evgeniy-scherbina Apr 22, 2025
ff8d3de
Merge branch 'main' of github.com:/coder/coder into yevhenii/512-clai…
dannykopping Apr 23, 2025
fcdbba8
test: add failure testcase scenario
evgeniy-scherbina Apr 23, 2025
e7b7f80
refactor: revert interface changes
evgeniy-scherbina Apr 23, 2025
e7455db
refactor: simplify signature of claim method
evgeniy-scherbina Apr 23, 2025
077d7f0
refactor: CR's fixes
evgeniy-scherbina Apr 23, 2025
0d426c7
Merge remote-tracking branch 'origin/main' into yevhenii/512-claim-pr…
evgeniy-scherbina Apr 23, 2025
663a8c0
refactor: CR's fixes
evgeniy-scherbina Apr 23, 2025
bc31fac
refactor: make lint
evgeniy-scherbina Apr 23, 2025
087bd20
refactor: fix linter
evgeniy-scherbina Apr 23, 2025
9f66169
refactor: remove PrebuildsOrchestrator context from claim tests
evgeniy-scherbina Apr 23, 2025
7f25f24
refactor: don't fail test in a goroutine
evgeniy-scherbina Apr 23, 2025
ff9eeb4
docs: fix ssh coder example in testing-templates doc (#17550)
EdwardAngert Apr 23, 2025
31b873f
fix: add websocket close handling (#17548)
jaaydenh Apr 23, 2025
4fe770d
ci: move go install tools to separate action (#17552)
ethanndickson Apr 24, 2025
8d8fc99
chore(dogfood): allow provider minor version updates (#17554)
matifali Apr 24, 2025
af32325
fix(examples/templates/docker-devcontainer): update folder path and p…
Aericio Apr 24, 2025
0826e8a
feat: enable masking password inputs instead of blocking echo (#17469)
ibetitsmike Apr 24, 2025
cbb6c12
fix(examples/templates/kubernetes-devcontainer): update coder provide…
matifali Apr 24, 2025
e466844
docs: add automatic release calendar updates in docs (#17531)
matifali Apr 24, 2025
f24557b
chore(dogfood): add windsurf module (#17558)
matifali Apr 24, 2025
fa65564
Merge branch 'main' of github.com:/coder/coder into yevhenii/512-clai…
dannykopping Apr 24, 2025
53b38d7
fix: use userCtx instead of prebuildCtx for claiming prebuild
evgeniy-scherbina Apr 24, 2025
6c41e03
refactor: CR's fixes
evgeniy-scherbina Apr 24, 2025
9173e9a
CR's fixes
evgeniy-scherbina Apr 24, 2025
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
Next Next commit
Refactoring
Signed-off-by: Danny Kopping <[email protected]>
  • Loading branch information
dannykopping committed Apr 22, 2025
commit c4595336d23e59f2de7d3d475ce67d257251dabf
2 changes: 1 addition & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func createWorkspace(
provisionerDaemons []database.GetEligibleProvisionerDaemonsByProvisionerJobIDsRow
)

prebuilds := (*api.PrebuildsClaimer.Load()).(prebuilds.Claimer)
prebuilds := *api.PrebuildsClaimer.Load()

err = api.Database.InTx(func(db database.Store) error {
var (
Expand Down
99 changes: 20 additions & 79 deletions enterprise/coderd/prebuilds/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

"github.com/coder/quartz"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
agplprebuilds "github.com/coder/coder/v2/coderd/prebuilds"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
Expand Down Expand Up @@ -78,40 +78,20 @@ func TestClaimPrebuild(t *testing.T) {
)

cases := map[string]struct {
entitlementEnabled bool
experimentEnabled bool
attemptPrebuildClaim bool
expectPrebuildClaimed bool
markPrebuildsClaimable bool
expectedPrebuildsCount int
}{
"without the experiment enabled, prebuilds will not provisioned": {
experimentEnabled: false,
entitlementEnabled: true,
attemptPrebuildClaim: false,
expectedPrebuildsCount: 0,
},
"without the entitlement, prebuilds will not provisioned": {
experimentEnabled: true,
entitlementEnabled: false,
attemptPrebuildClaim: false,
expectedPrebuildsCount: 0,
},
"with everything enabled, but no eligible prebuilds to claim": {
entitlementEnabled: true,
experimentEnabled: true,
attemptPrebuildClaim: true,
"no eligible prebuilds to claim": {
expectPrebuildClaimed: false,
markPrebuildsClaimable: false,
expectedPrebuildsCount: desiredInstances * presetCount,
},
"with everything enabled, claiming an eligible prebuild should succeed": {
entitlementEnabled: true,
experimentEnabled: true,
attemptPrebuildClaim: true,
"claiming an eligible prebuild should succeed": {
expectPrebuildClaimed: true,
markPrebuildsClaimable: true,
},
"claiming an eligible prebuild results in error": {
expectPrebuildClaimed: true,
markPrebuildsClaimable: true,
expectedPrebuildsCount: desiredInstances * presetCount,
},
Comment thread
evgeniy-scherbina marked this conversation as resolved.
Outdated
}

Expand All @@ -121,49 +101,26 @@ func TestClaimPrebuild(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

// Setup. // TODO: abstract?

// Setup.
ctx := testutil.Context(t, testutil.WaitMedium)
Comment thread
evgeniy-scherbina marked this conversation as resolved.
Outdated
db, pubsub := dbtestutil.NewDB(t)
spy := newStoreSpy(db)

//var prebuildsEntitled int64
//if tc.entitlementEnabled {
// prebuildsEntitled = 1
//}
expectedPrebuildsCount := desiredInstances * presetCount

logger := testutil.Logger(t)
client, _, _, owner := coderdenttest.NewWithAPI(t, &coderdenttest.Options{
client, _, api, owner := coderdenttest.NewWithAPI(t, &coderdenttest.Options{
Options: &coderdtest.Options{
IncludeProvisionerDaemon: true,
Database: spy,
Pubsub: pubsub,
DeploymentValues: coderdtest.DeploymentValues(t, func(values *codersdk.DeploymentValues) {
//values.Prebuilds.ReconciliationInterval = serpent.Duration(time.Hour) // We will kick off a reconciliation manually.
//
//if tc.experimentEnabled {
// values.Experiments = serpent.StringArray{string(codersdk.ExperimentWorkspacePrebuilds)}
//}
}),
},

EntitlementsUpdateInterval: time.Second,
//LicenseOptions: &coderdenttest.LicenseOptions{
// Features: license.Features{
// codersdk.FeatureWorkspacePrebuilds: prebuildsEntitled,
// },
//},
})
reconciler := prebuilds.NewStoreReconciler(spy, pubsub, codersdk.PrebuildsConfig{}, logger, quartz.NewMock(t))

// The entitlements will need to refresh before the reconciler is set.
require.Eventually(t, func() bool {
if tc.entitlementEnabled && tc.experimentEnabled {
assert.IsType(t, &prebuilds.StoreReconciler{}, reconciler)
}

return reconciler != nil
}, testutil.WaitSuperLong, testutil.IntervalFast)
reconciler := prebuilds.NewStoreReconciler(spy, pubsub, codersdk.PrebuildsConfig{}, logger, quartz.NewMock(t))
var claimer agplprebuilds.Claimer = &prebuilds.EnterpriseClaimer{}
api.AGPL.PrebuildsClaimer.Store(&claimer)

version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, templateWithAgentAndPresetsWithPrebuilds(desiredInstances))
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
Expand All @@ -179,14 +136,8 @@ func TestClaimPrebuild(t *testing.T) {
// Given: the reconciliation state is snapshot.
state, err := reconciler.SnapshotState(ctx, spy)
require.NoError(t, err)

// When: the experiment or entitlement is not preset, there should be nothing to reconcile.
if !tc.entitlementEnabled || !tc.experimentEnabled {
require.Len(t, state.Presets, 0)
return
}

require.Len(t, state.Presets, presetCount)

// When: a reconciliation is setup for each preset.
for _, preset := range presets {
ps, err := state.FilterByPreset(preset.ID)
Expand Down Expand Up @@ -215,6 +166,7 @@ func TestClaimPrebuild(t *testing.T) {
agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, row.ID)
require.NoError(t, err)

// Workspaces are eligible once its agent is marked "ready".
for _, agent := range agents {
require.NoError(t, db.UpdateWorkspaceAgentLifecycleStateByID(ctx, database.UpdateWorkspaceAgentLifecycleStateByIDParams{
ID: agent.ID,
Expand All @@ -225,9 +177,9 @@ func TestClaimPrebuild(t *testing.T) {
}
}

t.Logf("found %d running prebuilds so far, want %d", len(runningPrebuilds), tc.expectedPrebuildsCount)
t.Logf("found %d running prebuilds so far, want %d", len(runningPrebuilds), expectedPrebuildsCount)

return len(runningPrebuilds) == tc.expectedPrebuildsCount
return len(runningPrebuilds) == expectedPrebuildsCount
}, testutil.WaitSuperLong, testutil.IntervalSlow)
Comment thread
evgeniy-scherbina marked this conversation as resolved.

// When: a user creates a new workspace with a preset for which prebuilds are configured.
Expand All @@ -243,21 +195,10 @@ func TestClaimPrebuild(t *testing.T) {
TemplateVersionPresetID: presets[0].ID,
ClaimPrebuildIfAvailable: true, // TODO: doesn't do anything yet; it probably should though.
})

require.NoError(t, err)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, userWorkspace.LatestBuild.ID)

// Then: if we're not expecting any prebuild claims to succeed, handle this specifically.
if !tc.attemptPrebuildClaim {
require.EqualValues(t, spy.claims.Load(), 0)
require.Nil(t, spy.claimedWorkspace.Load())

currentPrebuilds, err := spy.GetRunningPrebuiltWorkspaces(ctx)
require.NoError(t, err)
// The number of prebuilds should NOT change.
require.Equal(t, len(currentPrebuilds), len(runningPrebuilds))
return
}

// Then: a prebuild should have been claimed.
require.EqualValues(t, spy.claims.Load(), 1)
require.NotNil(t, spy.claims.Load())
Comment thread
evgeniy-scherbina marked this conversation as resolved.
Outdated
Expand Down Expand Up @@ -315,9 +256,9 @@ func TestClaimPrebuild(t *testing.T) {
rows, err := spy.GetRunningPrebuiltWorkspaces(ctx)
require.NoError(t, err)

t.Logf("found %d running prebuilds so far, want %d", len(rows), tc.expectedPrebuildsCount)
t.Logf("found %d running prebuilds so far, want %d", len(rows), expectedPrebuildsCount)

return len(runningPrebuilds) == tc.expectedPrebuildsCount
return len(runningPrebuilds) == expectedPrebuildsCount
}, testutil.WaitSuperLong, testutil.IntervalSlow)

// Then: when restarting the created workspace (which claimed a prebuild), it should not try and claim a new prebuild.
Expand Down