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
Fix test
  • Loading branch information
Emyrk committed May 31, 2023
commit 01e9fb0e66e62d4bc4dbf73ef3b3e1a8eb14e9f9
33 changes: 28 additions & 5 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package coderd_test

import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -564,7 +566,7 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
func TestWorkspaceFilterAllStatus(t *testing.T) {
t.Parallel()
if os.Getenv("DB") != "" {
t.Skip(`This test takes too long with an actual database`)
t.Skip(`This test takes too long with an actual database. Takes 10s on local machine`)
}

ctx := dbauthz.AsSystemRestricted(context.Background())
Expand Down Expand Up @@ -607,8 +609,14 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
workspace.TemplateID = template.ID
workspace = dbgen.Workspace(t, db, workspace)

jobID := uuid.New()
job.ID = jobID
job.Type = database.ProvisionerJobTypeWorkspaceBuild
job.OrganizationID = owner.OrganizationID
// Need to prevent acquire from getting this job.
job.Tags = dbtype.StringMap{
jobID.String(): "true",
}
job = dbgen.ProvisionerJob(t, db, job)

build := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{
Expand Down Expand Up @@ -664,14 +672,23 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
}, database.WorkspaceTransitionStop)

// failed
// failed -- delete
_, _, _ = makeWorkspace(database.Workspace{
Name: string(database.WorkspaceStatusFailed),
Name: string(database.WorkspaceStatusFailed) + "-deleted",
}, database.ProvisionerJob{
StartedAt: sql.NullTime{Time: time.Now().Add(time.Second * -2), Valid: true},
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
Error: sql.NullString{String: "Some error", Valid: true},
}, database.WorkspaceTransitionStart)
}, database.WorkspaceTransitionDelete)

// failed -- stop
_, _, _ = makeWorkspace(database.Workspace{
Name: string(database.WorkspaceStatusFailed) + "-stopped",
}, database.ProvisionerJob{
StartedAt: sql.NullTime{Time: time.Now().Add(time.Second * -2), Valid: true},
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
Error: sql.NullString{String: "Some error", Valid: true},
}, database.WorkspaceTransitionStop)

// canceling
_, _, _ = makeWorkspace(database.Workspace{
Expand Down Expand Up @@ -713,7 +730,13 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
// Make sure all workspaces have the correct status
var statuses []codersdk.WorkspaceStatus
for _, apiWorkspace := range workspaces.Workspaces {
assert.Equal(t, apiWorkspace.Name, string(apiWorkspace.LatestBuild.Status), "workspace has incorrect status")
expStatus := strings.Split(apiWorkspace.Name, "-")
if !assert.Equal(t, expStatus[0], string(apiWorkspace.LatestBuild.Status), "workspace has incorrect status") {
d, _ := json.Marshal(apiWorkspace)
var buf bytes.Buffer
_ = json.Indent(&buf, d, "", "\t")
t.Logf("Incorrect workspace: %s", buf.String())
}
statuses = append(statuses, apiWorkspace.LatestBuild.Status)
}

Expand Down