TestSoftDeleteWorkspaceAgentsByWorkspaceID verifies the delete-path invariant: when a workspace is soft-deleted, every one of its agents (across all builds) gets soft-deleted in the same transaction. Agents on *other* workspaces, even ones sharing an auth_instance_id, must be untouched.
(t *testing.T)
| 14805 | // *other* workspaces, even ones sharing an auth_instance_id, must be |
| 14806 | // untouched. |
| 14807 | func TestSoftDeleteWorkspaceAgentsByWorkspaceID(t *testing.T) { |
| 14808 | t.Parallel() |
| 14809 | |
| 14810 | db, _, sqlDB := dbtestutil.NewDBWithSQLDB(t) |
| 14811 | ctx := testutil.Context(t, testutil.WaitShort) |
| 14812 | |
| 14813 | type buildBundle struct { |
| 14814 | workspaceID uuid.UUID |
| 14815 | buildID uuid.UUID |
| 14816 | agentID uuid.UUID |
| 14817 | } |
| 14818 | |
| 14819 | user := dbgen.User(t, db, database.User{}) |
| 14820 | org := dbgen.Organization(t, db, database.Organization{}) |
| 14821 | tpl := dbgen.Template(t, db, database.Template{ |
| 14822 | OrganizationID: org.ID, |
| 14823 | CreatedBy: user.ID, |
| 14824 | }) |
| 14825 | tplVersion := dbgen.TemplateVersion(t, db, database.TemplateVersion{ |
| 14826 | TemplateID: uuid.NullUUID{UUID: tpl.ID, Valid: true}, |
| 14827 | OrganizationID: org.ID, |
| 14828 | CreatedBy: user.ID, |
| 14829 | }) |
| 14830 | |
| 14831 | newBuild := func(t *testing.T, wsID uuid.UUID, buildNumber int32, instanceID string) buildBundle { |
| 14832 | t.Helper() |
| 14833 | job := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{ |
| 14834 | OrganizationID: org.ID, |
| 14835 | Type: database.ProvisionerJobTypeWorkspaceBuild, |
| 14836 | }) |
| 14837 | build := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{ |
| 14838 | WorkspaceID: wsID, |
| 14839 | JobID: job.ID, |
| 14840 | TemplateVersionID: tplVersion.ID, |
| 14841 | BuildNumber: buildNumber, |
| 14842 | Transition: database.WorkspaceTransitionStart, |
| 14843 | }) |
| 14844 | resource := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{JobID: job.ID}) |
| 14845 | agent := dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{ |
| 14846 | ResourceID: resource.ID, |
| 14847 | AuthInstanceID: sql.NullString{String: instanceID, Valid: true}, |
| 14848 | }) |
| 14849 | return buildBundle{workspaceID: wsID, buildID: build.ID, agentID: agent.ID} |
| 14850 | } |
| 14851 | |
| 14852 | agentDeleted := func(id uuid.UUID) bool { |
| 14853 | t.Helper() |
| 14854 | var deleted bool |
| 14855 | err := sqlDB.QueryRowContext(ctx, |
| 14856 | `SELECT deleted FROM workspace_agents WHERE id = $1`, id).Scan(&deleted) |
| 14857 | require.NoError(t, err) |
| 14858 | return deleted |
| 14859 | } |
| 14860 | |
| 14861 | // wsA: 3 builds (so multiple agents to sweep on delete). |
| 14862 | // wsB: 1 build, same auth_instance_id as wsA (proves scoping). |
| 14863 | wsA := dbgen.Workspace(t, db, database.WorkspaceTable{ |
| 14864 | OrganizationID: org.ID, |
nothing calls this directly
no test coverage detected