Skip to content

feat: add deleted_at field to workspace model #7475

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 5 commits into from
May 11, 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 test
  • Loading branch information
Kira-Pilot committed May 10, 2023
commit 3886a4f4432c7f41068e850d4024a989c4b3e0ac
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"codersdk",
"cronstrue",
"databasefake",
"dbfake",
"dbgen",
"dbtype",
"DERP",
"derphttp",
Expand Down
76 changes: 76 additions & 0 deletions coderd/workspaces_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package coderd

import (
"testing"
"time"

"github.com/stretchr/testify/require"

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

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

testCases := []struct {
name string
workspace database.Workspace
template database.Template
expected time.Time
}{
{
name: "ImpendingDeletion",
workspace: database.Workspace{
Deleted: false,
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24), // 10 days ago
},
template: database.Template{
InactivityTTL: int64(9 * 24 * time.Hour), // 9 days
},
expected: time.Now().Add(time.Duration(-1) * time.Hour * 24), // yesterday
},
{
name: "InactivityTTLUnset",
workspace: database.Workspace{
Deleted: false,
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24),
},
template: database.Template{
InactivityTTL: 0,
},
expected: time.Time{},
},
{
name: "DeletedWorkspace",
workspace: database.Workspace{
Deleted: true,
LastUsedAt: time.Now().Add(time.Duration(-10) * time.Hour * 24),
},
template: database.Template{
InactivityTTL: int64(9 * 24 * time.Hour),
},
expected: time.Time{},
},
{
name: "ActiveWorkspace",
workspace: database.Workspace{
Deleted: true,
LastUsedAt: time.Now().Add(time.Duration(-5) * time.Hour), // 5 hours ago
},
template: database.Template{
InactivityTTL: int64(1 * 24 * time.Hour), // 1 day
},
expected: time.Time{},
},
}

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

found := calculateImpendingDeletion(tc.workspace, tc.template)
require.WithinDuration(t, tc.expected, found, time.Second, "incorrect impending deletion")
})
}
}