Skip to content

feat: add impending deletion filter to workspaces page #7860

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 18 commits into from
Jun 12, 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
added enterprise test
  • Loading branch information
Kira-Pilot committed Jun 9, 2023
commit a08a0c92c58589cb5180a22ca4b75b4f25b1ee0e
8 changes: 5 additions & 3 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,8 @@ func TestWorkspaceFilterManual(t *testing.T) {
}, testutil.IntervalMedium, "agent status timeout")
})

t.Run("FilterQueryHasDeletingBy", func(t *testing.T) {
t.Run("FilterQueryHasDeletingByAndUnlicensed", func(t *testing.T) {
// this test has a licensed counterpart in enterprise/coderd/workspaces_test.go: FilterQueryHasDeletingByAndLicensed
t.Parallel()
inactivityTTL := 1 * 24 * time.Hour
var setCalled int64
Expand Down Expand Up @@ -1059,8 +1060,9 @@ func TestWorkspaceFilterManual(t *testing.T) {
})

assert.NoError(t, err)
assert.Len(t, res.Workspaces, 1)
assert.Equal(t, workspace.ID, res.Workspaces[0].ID)
// we are expecting that no workspaces are returned as user is unlicensed
// and template.InactivityTTL should be 0
assert.Len(t, res.Workspaces, 0)
})
}

Expand Down
56 changes: 56 additions & 0 deletions enterprise/coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package coderd_test

import (
"context"
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
Expand Down Expand Up @@ -70,3 +73,56 @@ func TestCreateWorkspace(t *testing.T) {
require.Error(t, err)
})
}

func TestWorkspacesFiltering(t *testing.T) {
t.Parallel()

t.Run("FilterQueryHasDeletingByAndLicensed", func(t *testing.T) {
t.Parallel()

inactivityTTL := 1 * 24 * time.Hour

client := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
IncludeProvisionerDaemon: true,
},
})
user := coderdtest.CreateFirstUser(t, client)
_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAdvancedTemplateScheduling: 1,
},
})

version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

// update template with inactivity ttl
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

template, err := client.UpdateTemplateMeta(ctx, template.ID, codersdk.UpdateTemplateMeta{
InactivityTTLMillis: inactivityTTL.Milliseconds(),
})

assert.NoError(t, err)
assert.Equal(t, inactivityTTL.Milliseconds(), template.InactivityTTLMillis)

workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

// stop build so workspace is inactive
stopBuild := coderdtest.CreateWorkspaceBuild(t, client, workspace, database.WorkspaceTransitionStop)
coderdtest.AwaitWorkspaceBuildJob(t, client, stopBuild.ID)

res, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{
// adding a second to time.Now() to give some buffer in case test runs quickly
FilterQuery: fmt.Sprintf("deleting_by:%s", time.Now().Add(time.Second).Add(inactivityTTL).Format("2006-01-02")),
})
assert.NoError(t, err)
assert.Len(t, res.Workspaces, 1)
assert.Equal(t, workspace.ID, res.Workspaces[0].ID)
})
}