forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspaces_internal_test.go
More file actions
82 lines (75 loc) · 1.91 KB
/
Copy pathworkspaces_internal_test.go
File metadata and controls
82 lines (75 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package coderd
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/codersdk"
)
func Test_calculateDeletingAt(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
workspace database.Workspace
template database.Template
build codersdk.WorkspaceBuild
expected *time.Time
}{
{
name: "InactiveWorkspace",
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
},
build: codersdk.WorkspaceBuild{
Status: codersdk.WorkspaceStatusStopped,
},
expected: ptr.Ref(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,
},
build: codersdk.WorkspaceBuild{
Status: codersdk.WorkspaceStatusStopped,
},
expected: nil,
},
{
name: "ActiveWorkspace",
workspace: database.Workspace{
Deleted: false,
LastUsedAt: time.Now(),
},
template: database.Template{
InactivityTTL: int64(1 * 24 * time.Hour),
},
build: codersdk.WorkspaceBuild{
Status: codersdk.WorkspaceStatusRunning,
},
expected: nil,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
found := calculateDeletingAt(tc.workspace, tc.template, tc.build)
if tc.expected == nil {
require.Nil(t, found, "impending deletion should be nil")
} else {
require.NotNil(t, found)
require.WithinDuration(t, *tc.expected, *found, time.Second, "incorrect impending deletion")
}
})
}
}