Skip to content
Prev Previous commit
Next Next commit
Write unit test for workspace status
  • Loading branch information
Emyrk committed May 30, 2023
commit 2cb3ece5a0de5162de238eb2a1b85a49a988f314
41 changes: 41 additions & 0 deletions coderd/database/dbgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

"github.com/coder/coder/coderd/database/dbtype"

"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/rbac"

Expand All @@ -26,6 +28,7 @@ import (
// All methods take in a 'seed' object. Any provided fields in the seed will be
// maintained. Any fields omitted will have sensible defaults generated.

// genCtx is to give all generator functions permission if the db is a dbauthz db.
var genCtx = dbauthz.As(context.Background(), rbac.Subject{
ID: "owner",
Roles: rbac.Roles(must(rbac.RoleNames{rbac.RoleOwner()}.Expand())),
Expand Down Expand Up @@ -271,7 +274,17 @@ func GroupMember(t testing.TB, db database.Store, orig database.GroupMember) dat
return member
}

// ProvisionerJob might not have all the correct values like CompletedAt and CancelledAt. This is because
// the workspaceBuild is required to fetch those,
func ProvisionerJob(t testing.TB, db database.Store, orig database.ProvisionerJob) database.ProvisionerJob {
id := takeFirst(orig.ID, uuid.New())
if !orig.StartedAt.Time.IsZero() {
if orig.Tags == nil {
orig.Tags = make(dbtype.StringMap)
}
// Make sure when we acquire the job, we only get this one.
orig.Tags[id.String()] = "true"
}
job, err := db.InsertProvisionerJob(genCtx, database.InsertProvisionerJobParams{
ID: takeFirst(orig.ID, uuid.New()),
CreatedAt: takeFirst(orig.CreatedAt, database.Now()),
Expand All @@ -286,6 +299,34 @@ func ProvisionerJob(t testing.TB, db database.Store, orig database.ProvisionerJo
Tags: orig.Tags,
})
require.NoError(t, err, "insert job")

if !orig.CompletedAt.Time.IsZero() || orig.Error.String != "" {
err := db.UpdateProvisionerJobWithCompleteByID(genCtx, database.UpdateProvisionerJobWithCompleteByIDParams{
ID: job.ID,
UpdatedAt: orig.UpdatedAt,
CompletedAt: orig.CompletedAt,
Error: orig.Error,
ErrorCode: orig.ErrorCode,
})
require.NoError(t, err)
}
if !orig.CanceledAt.Time.IsZero() {
err := db.UpdateProvisionerJobWithCancelByID(genCtx, database.UpdateProvisionerJobWithCancelByIDParams{
ID: job.ID,
CanceledAt: orig.CanceledAt,
CompletedAt: orig.CompletedAt,
})
require.NoError(t, err)
}
if !orig.StartedAt.Time.IsZero() {
job, err = db.AcquireProvisionerJob(genCtx, database.AcquireProvisionerJobParams{
StartedAt: orig.StartedAt,
Types: []database.ProvisionerType{database.ProvisionerTypeEcho},
Tags: must(json.Marshal(orig.Tags)),
})
require.NoError(t, err)
}

return job
}

Expand Down
47 changes: 41 additions & 6 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package coderd_test

import (
"context"
"database/sql"
"fmt"
"net/http"
"strings"
"testing"
"time"

"github.com/coder/coder/coderd/database/dbauthz"

"github.com/coder/coder/coderd/database/dbgen"

"github.com/google/uuid"
Expand Down Expand Up @@ -558,6 +561,7 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
}

func TestWorkspaceFilterAllStatus(t *testing.T) {
ctx := dbauthz.AsSystemRestricted(context.Background())
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{})

owner := coderdtest.CreateFirstUser(t, client)
Expand All @@ -582,17 +586,48 @@ func TestWorkspaceFilterAllStatus(t *testing.T) {
CreatedBy: owner.UserID,
})

makeWorkspace := func(workspace database.Workspace) database.Workspace {
makeWorkspace := func(workspace database.Workspace, job database.ProvisionerJob, transition database.WorkspaceTransition) (database.Workspace, database.WorkspaceBuild, database.ProvisionerJob) {
db := api.Database

workspace.OwnerID = owner.UserID
workspace.OrganizationID = owner.OrganizationID
workspace.TemplateID = template.ID
workspace = dbgen.Workspace(t, api.Database, workspace)
workspace = dbgen.Workspace(t, db, workspace)

job.Type = database.ProvisionerJobTypeWorkspaceBuild
job.OrganizationID = owner.OrganizationID
job = dbgen.ProvisionerJob(t, db, job)

build := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{
WorkspaceID: workspace.ID,
TemplateVersionID: version.ID,
BuildNumber: 1,
Transition: transition,
InitiatorID: owner.UserID,
JobID: job.ID,
})

var err error
job, err = db.GetProvisionerJobByID(ctx, job.IDtus
)
require.NoError(t, err)

return workspace, build, job
}

_, _, _ = makeWorkspace(database.Workspace{
Name: string(database.WorkspaceStatusRunning),
}, database.ProvisionerJob{
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
StartedAt: sql.NullTime{Time: time.Now().Add(time.Second * -2), Valid: true},
}, database.WorkspaceTransitionStart)

workspaces, err := client.Workspaces(context.Background(), codersdk.WorkspaceFilter{})
require.NoError(t, err)

return workspace
for _, apiWorkspace := range workspaces.Workspaces {
require.Equal(t, apiWorkspace.Name, string(apiWorkspace.LatestBuild.Status))
}
var _ = makeWorkspace
//makeWorkspace(database.Workspace{
//})

// pending
// starting
Expand Down