8000 fix: fix workspace status filter returning more statuses that requested by Emyrk · Pull Request #7732 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: fix workspace status filter returning more statuses that requested #7732

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 14 commits into from
Jun 5, 2023
Prev Previous commit
Next Next commit
Add unit test to check for filter status
  • Loading branch information
Emyrk committed May 31, 2023
commit a4ba58d3e22b71295f6d11543dacc5769825f4d0
2 changes: 1 addition & 1 deletion coderd/database/dbgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func GroupMember(t testing.TB, db database.Store, orig database.GroupMember) dat
func ProvisionerJob(t testing.TB, db database.Store, orig database.ProvisionerJob) database.ProvisionerJob {
id := takeFirst(orig.ID, uuid.New())
// Always set some tags to prevent Acquire from grabbing jobs it should not.
if !orig.StartedAt.Time.IsZero() || orig.Tags == nil {
if !orig.StartedAt.Time.IsZero() {
if orig.Tags == nil {
orig.Tags = make(dbtype.StringMap)
}
Expand Down
27 changes: 25 additions & 2 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/database/dbgen"
"github.com/coder/coder/coderd/database/dbtestutil"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/coder/coder/coderd/parameter"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/schedule"
Expand Down Expand Up @@ -583,6 +584,9 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
InitiatorID: owner.UserID,
WorkerID: uuid.NullUUID{},
FileID: file.ID,
Tags: dbtype.StringMap{
"custom": "true",
},
})
version := dbgen.TemplateVersion(t, db, database.TemplateVersion{
OrganizationID: owner.OrganizationID,
Expand Down Expand Up @@ -701,11 +705,30 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
}, database.WorkspaceTransitionDelete)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are great and all, but they are not combinatorially exhaustive on null vs non-null for all fields. This is another reason I think a stored procedure for the status is better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is definitely not exhaustive. The annoying thing too is when we prepulate this much data (which isn't even that much), the postgres test gets so slow. So I skipped it in CI, meaning this bug could easily pop up again silently 😢.

I was debating about putting in a batch insert SQL query outside our typical db interface and see if it is faster. If we go the stored procedure route, might be worth investigating how to make the postgres test run in a reasonable amount of time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the slowness is related to how we have to create the job, then acqui 8000 re it, then fail/complete it. Acquiring in particular might be slow because of the locking we do.

In testing we could have some queries that directly insert the job in the state we want it. Not exposed on the main Store interface, but available if you type assert to a test type.


workspaces, err := client.Workspaces(context.Background(), codersdk.WorkspaceFilter{})
apiCtx, cancel := context.WithTimeout(ctx, testutil.WaitShort)
defer cancel()
workspaces, err := client.Workspaces(apiCtx, codersdk.WorkspaceFilter{})
require.NoError(t, err)

// Make sure all workspaces have the correct status
var statuses []codersdk.WorkspaceStatus
for _, apiWorkspace := range workspaces.Workspaces {
require.Equal(t, apiWorkspace.Name, string(apiWorkspace.LatestBuild.Status))
assert.Equal(t, apiWorkspace.Name, string(apiWorkspace.LatestBuild.Status), "workspace has incorrect status")
statuses = append(statuses, apiWorkspace.LatestBuild.Status)
}

// Now test the filter
for _, status := range statuses {
ctx, cancel := context.WithTimeout(ctx, testutil.WaitShort)
defer cancel()

workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{
Status: string(status),
})
require.NoErrorf(t, err, "fetch with status: %s", status)
for _, workspace := range workspaces.Workspaces {
assert.Equal(t, status, workspace.LatestBuild.Status, "expect matching status to filter")
}
}
}

Expand Down
< 1637 ghcc-consent id="ghcc" class="position-fixed bottom-0 left-0" style="z-index: 999999" data-locale="en" data-initial-cookie-consent-allowed="" data-cookie-consent-required="false" >
0