TestSoftDeletePriorWorkspaceAgents verifies the invariant maintained by wsbuilder.Builder.Build: when a new build of a workspace is created, all agents belonging to prior builds of that same workspace are soft-deleted, and agents belonging to *other* workspaces are untouched.
(t *testing.T)
| 14684 | // agents belonging to prior builds of that same workspace are soft-deleted, |
| 14685 | // and agents belonging to *other* workspaces are untouched. |
| 14686 | func TestSoftDeletePriorWorkspaceAgents(t *testing.T) { |
| 14687 | t.Parallel() |
| 14688 | |
| 14689 | db, _, sqlDB := dbtestutil.NewDBWithSQLDB(t) |
| 14690 | ctx := testutil.Context(t, testutil.WaitShort) |
| 14691 | |
| 14692 | // Helper: create a workspace + one build + its agent. Returns the IDs we |
| 14693 | // need to assert on. The agent uses the shared EC2-style auth_instance_id |
| 14694 | // so we can prove per-workspace scoping. |
| 14695 | type buildBundle struct { |
| 14696 | workspaceID uuid.UUID |
| 14697 | buildID uuid.UUID |
| 14698 | agentID uuid.UUID |
| 14699 | } |
| 14700 | |
| 14701 | user := dbgen.User(t, db, database.User{}) |
| 14702 | org := dbgen.Organization(t, db, database.Organization{}) |
| 14703 | tpl := dbgen.Template(t, db, database.Template{ |
| 14704 | OrganizationID: org.ID, |
| 14705 | CreatedBy: user.ID, |
| 14706 | }) |
| 14707 | tplVersion := dbgen.TemplateVersion(t, db, database.TemplateVersion{ |
| 14708 | TemplateID: uuid.NullUUID{UUID: tpl.ID, Valid: true}, |
| 14709 | OrganizationID: org.ID, |
| 14710 | CreatedBy: user.ID, |
| 14711 | }) |
| 14712 | |
| 14713 | newBuild := func(t *testing.T, wsID uuid.UUID, buildNumber int32, instanceID string) buildBundle { |
| 14714 | t.Helper() |
| 14715 | job := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{ |
| 14716 | OrganizationID: org.ID, |
| 14717 | Type: database.ProvisionerJobTypeWorkspaceBuild, |
| 14718 | }) |
| 14719 | build := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{ |
| 14720 | WorkspaceID: wsID, |
| 14721 | JobID: job.ID, |
| 14722 | TemplateVersionID: tplVersion.ID, |
| 14723 | BuildNumber: buildNumber, |
| 14724 | Transition: database.WorkspaceTransitionStart, |
| 14725 | }) |
| 14726 | resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{JobID: job.ID}) |
| 14727 | agent := dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{ |
| 14728 | ResourceID: resource.ID, |
| 14729 | AuthInstanceID: sql.NullString{String: instanceID, Valid: true}, |
| 14730 | }) |
| 14731 | return buildBundle{workspaceID: wsID, buildID: build.ID, agentID: agent.ID} |
| 14732 | } |
| 14733 | |
| 14734 | // Read `deleted` via raw SQL. GetWorkspaceAgentByID filters deleted rows |
| 14735 | // out, which is exactly what we want to observe here. |
| 14736 | agentDeleted := func(id uuid.UUID) bool { |
| 14737 | t.Helper() |
| 14738 | var deleted bool |
| 14739 | err := sqlDB.QueryRowContext(ctx, |
| 14740 | `SELECT deleted FROM workspace_agents WHERE id = $1`, id).Scan(&deleted) |
| 14741 | require.NoError(t, err) |
| 14742 | return deleted |
| 14743 | } |
nothing calls this directly
no test coverage detected