Skip to content

feat(workspaces): change sorting order of the workspace list #7594

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 6 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor tests into table tests
  • Loading branch information
rodrimaia committed May 19, 2023
commit 3033bdd1d2067fef64a5bfc707b974926455b334
168 changes: 80 additions & 88 deletions coderd/workspaces_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coderd

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -84,101 +83,94 @@ func Test_calculateDeletingAt(t *testing.T) {
}

func TestSortWorkspaces(t *testing.T) {
// the correct sorting order is:
// 1. first show workspaces that are currently running,
// 2. then sort by user_name,
// 3. then sort by last_used_at (descending),
t.Parallel()

t.Run("Running First", func(t *testing.T) {
t.Parallel()
workspaces := []codersdk.Workspace{
WorkspaceFactory(t, "test-workspace-sort-1", uuid.New(), "user1", codersdk.WorkspaceStatusPending),
WorkspaceFactory(t, "test-workspace-sort-2", uuid.New(), "user2", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-3", uuid.New(), "user2", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-4", uuid.New(), "user1", codersdk.WorkspaceStatusPending),
}

sortWorkspaces(workspaces)

require.Equal(t, workspaces[0].LatestBuild.Status, codersdk.WorkspaceStatusRunning)
require.Equal(t, workspaces[1].LatestBuild.Status, codersdk.WorkspaceStatusRunning)
require.Equal(t, workspaces[2].LatestBuild.Status, codersdk.WorkspaceStatusPending)
require.Equal(t, workspaces[3].LatestBuild.Status, codersdk.WorkspaceStatusPending)
})

t.Run("Then sort by owner Name", func(t *testing.T) {
t.Parallel()
workspaces := []codersdk.Workspace{
WorkspaceFactory(t, "test-workspace-sort-1", uuid.New(), "userZ", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-2", uuid.New(), "userA", codersdk.WorkspaceStatusRunning),
WorkspaceFactory(t, "test-workspace-sort-3", uuid.New(), "userB", codersdk.WorkspaceStatusRunning),
workspaceFactory := func(t *testing.T, name string, ownerID uuid.UUID, ownerName string, status codersdk.WorkspaceStatus, lastUsedAt time.Time) codersdk.Workspace {
t.Helper()
return codersdk.Workspace{
ID: uuid.New(),
OwnerID: ownerID,
OwnerName: ownerName,
LatestBuild: codersdk.WorkspaceBuild{
Status: status,
},
Name: name,
LastUsedAt: lastUsedAt,
}
}

sortWorkspaces(workspaces)

require.Equal(t, "userA", workspaces[0].OwnerName)
require.Equal(t, "userB", workspaces[1].OwnerName)
require.Equal(t, "userZ", workspaces[2].OwnerName)
})

t.Run("Then sort by last used at (recent first)", func(t *testing.T) {
t.Parallel()
var workspaces []codersdk.Workspace
userAuuid := uuid.New()

useruuid := uuid.New()
workspaceRunningUserA := workspaceFactory(t, "running-userA", userAuuid, "userA", codersdk.WorkspaceStatusRunning, time.Now())
workspaceRunningUserB := workspaceFactory(t, "running-userB", uuid.New(), "userB", codersdk.WorkspaceStatusRunning, time.Now())
workspacePendingUserC := workspaceFactory(t, "pending-userC", uuid.New(), "userC", codersdk.WorkspaceStatusPending, time.Now())
workspaceRunningUserA2 := workspaceFactory(t, "running-userA2", userAuuid, "userA", codersdk.WorkspaceStatusRunning, time.Now())
workspaceRunningUserZ := workspaceFactory(t, "running-userZ", uuid.New(), "userZ", codersdk.WorkspaceStatusRunning, time.Now().Add(time.Minute))
workspaceRunningUserA3 := workspaceFactory(t, "running-userA3", userAuuid, "userA", codersdk.WorkspaceStatusRunning, time.Now().Add(time.Hour))

for i := 0; i < 4; i++ {
workspaces = append(workspaces, WorkspaceFactory(t, fmt.Sprintf("test-workspace-sort-%d", i+1), useruuid, "user2", codersdk.WorkspaceStatusRunning))
}
testCases := []struct {
name string
input []codersdk.Workspace
expectedOrder []string
}{
{
name: "Running workspaces should be first",
input: []codersdk.Workspace{
workspaceRunningUserB,
workspacePendingUserC,
workspaceRunningUserA,
},
expectedOrder: []string{
"running-userA",
"running-userB",
"pending-userC",
},
},
{
name: "then sort by owner name",
input: []codersdk.Workspace{
workspaceRunningUserZ,
workspaceRunningUserA,
workspaceRunningUserB,
},
expectedOrder: []string{
"running-userA",
"running-userB",
"running-userZ",
},
},
{
name: "then sort by last used at (recent first)",
input: []codersdk.Workspace{
workspaceRunningUserA,
workspaceRunningUserA2,
workspaceRunningUserA3,
},
expectedOrder: []string{
"running-userA3",
"running-userA2",
"running-userA",
},
},
}

sortWorkspaces(workspaces)
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
workspaces := tc.input
sortWorkspaces(workspaces)

// in this case, the last used at is the creation time
require.Equal(t, workspaces[0].Name, "test-workspace-sort-4")
require.Equal(t, workspaces[1].Name, "test-workspace-sort-3")
require.Equal(t, workspaces[2].Name, "test-workspace-sort-2")
require.Equal(t, workspaces[3].Name, "test-workspace-sort-1")
})
}
var resultNames []string
for _, workspace := range workspaces {
resultNames = append(resultNames, workspace.Name)
}

func WorkspaceFactory(t *testing.T, name string, ownerID uuid.UUID, ownerName string, status codersdk.WorkspaceStatus) codersdk.Workspace {
t.Helper()
return codersdk.Workspace{
ID: uuid.New(),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
OwnerID: ownerID,
OwnerName: ownerName,
OrganizationID: [16]byte{},
TemplateID: [16]byte{},
TemplateName: name,
TemplateDisplayName: name,
TemplateIcon: "",
TemplateAllowUserCancelWorkspaceJobs: false,
LatestBuild: codersdk.WorkspaceBuild{
ID: uuid.New(),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
WorkspaceID: [16]byte{},
WorkspaceName: name,
WorkspaceOwnerID: [16]byte{},
WorkspaceOwnerName: ownerName,
TemplateVersionID: [16]byte{},
TemplateVersionName: name,
BuildNumber: 0,
Transition: "",
InitiatorID: [16]byte{},
InitiatorUsername: name,
Job: codersdk.ProvisionerJob{},
Reason: "",
Resources: []codersdk.WorkspaceResource{},
Deadline: codersdk.NullTime{},
MaxDeadline: codersdk.NullTime{},
Status: status,
DailyCost: 0,
},
Outdated: false,
Name: name,
AutostartSchedule: new(string),
TTLMillis: new(int64),
LastUsedAt: time.Now(),
DeletingAt: &time.Time{},
require.Equal(t, tc.expectedOrder, resultNames, tc.name)
})
}
}
44 changes: 0 additions & 44 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,50 +198,6 @@ func TestAdminViewAllWorkspaces(t *testing.T) {
require.Equal(t, 0, len(memberViewWorkspaces.Workspaces), "member in other org should see 0 workspaces")
}

func TestWorkspacesSortOrder(t *testing.T) {
// the correct sorting order is:
// 1. first show workspaces that are currently running,
// 2. then sort by user_name,
// 3. then sort by last_used_at (descending),
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
firstUser := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, firstUser.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, firstUser.OrganizationID, version.ID)
workspace1 := coderdtest.CreateWorkspace(t, client, firstUser.OrganizationID, template.ID, func(ctr *codersdk.CreateWorkspaceRequest) {
ctr.Name = "test-workspace-sort-1"
})
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace1.LatestBuild.ID)

workspace2 := coderdtest.CreateWorkspace(t, client, firstUser.OrganizationID, template.ID, func(ctr *codersdk.CreateWorkspaceRequest) {
ctr.Name = "test-workspace-sort-2"
})
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace2.LatestBuild.ID)

build2 := coderdtest.CreateWorkspaceBuild(t, client, workspace2, database.WorkspaceTransitionStop)
coderdtest.AwaitWorkspaceBuildJob(t, client, build2.ID)

workspace3 := coderdtest.CreateWorkspace(t, client, firstUser.OrganizationID, template.ID, func(ctr *codersdk.CreateWorkspaceRequest) {
ctr.Name = "test-workspace-sort-3"
})
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace3.LatestBuild.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

workspacesResponse, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
require.NoError(t, err, "(first) fetch workspaces")
require.Equal(t, 3, len(workspacesResponse.Workspaces), "should be 3 workspaces present")

require.Equal(t, codersdk.WorkspaceStatusRunning, workspacesResponse.Workspaces[0].LatestBuild.Status, "should be the first item in the list because it is running")
require.Equal(t, codersdk.WorkspaceStatusRunning, workspacesResponse.Workspaces[1].LatestBuild.Status)
require.Equal(t, codersdk.WorkspaceStatusStopped, workspacesResponse.Workspaces[2].LatestBuild.Status, "The stopped workspace should be last")

require.Equal(t, workspace3.ID, workspacesResponse.Workspaces[0].ID, "If both are running, and have the same owner, sort by last used (in this case, the last one created)")

}

func TestPostWorkspacesByOrganization(t *testing.T) {
t.Parallel()
t.Run("InvalidTemplate", func(t *testing.T) {
Expand Down