Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/workspace_instance_test.go
2497 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package db_test
6
7
import (
8
"context"
9
"fmt"
10
"testing"
11
"time"
12
13
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
14
15
"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"
16
"github.com/google/uuid"
17
"github.com/stretchr/testify/require"
18
)
19
20
var (
21
startOfMay = time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)
22
startOfJune = time.Date(2022, 06, 1, 0, 00, 00, 00, time.UTC)
23
)
24
25
func TestFindStoppedWorkspaceInstancesInRange(t *testing.T) {
26
conn := dbtest.ConnectForTests(t)
27
28
workspace := dbtest.CreateWorkspaces(t, conn, dbtest.NewWorkspace(t, db.Workspace{}))[0]
29
30
valid := []db.WorkspaceInstance{
31
// In the middle of May
32
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
33
WorkspaceID: workspace.ID,
34
StartedTime: db.NewVarCharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)),
35
StoppingTime: db.NewVarCharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)),
36
}),
37
// Start of May
38
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
39
ID: uuid.New(),
40
WorkspaceID: workspace.ID,
41
StartedTime: db.NewVarCharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)),
42
StoppingTime: db.NewVarCharTime(time.Date(2022, 05, 1, 1, 00, 00, 00, time.UTC)),
43
}),
44
// End of May
45
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
46
ID: uuid.New(),
47
WorkspaceID: workspace.ID,
48
StartedTime: db.NewVarCharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)),
49
StoppingTime: db.NewVarCharTime(time.Date(2022, 05, 31, 23, 59, 59, 999999, time.UTC)),
50
}),
51
// Started in April, but continued into May
52
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
53
ID: uuid.New(),
54
WorkspaceID: workspace.ID,
55
StartedTime: db.NewVarCharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)),
56
StoppingTime: db.NewVarCharTime(time.Date(2022, 05, 1, 0, 0, 0, 0, time.UTC)),
57
}),
58
}
59
invalid := []db.WorkspaceInstance{
60
// Started in April, no stop time, still running
61
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
62
ID: uuid.New(),
63
WorkspaceID: workspace.ID,
64
StartedTime: db.NewVarCharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)),
65
}),
66
// Started in May, but continued into June
67
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
68
ID: uuid.New(),
69
WorkspaceID: workspace.ID,
70
StartedTime: db.NewVarCharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)),
71
StoppingTime: db.NewVarCharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
72
}),
73
// Started in April, but continued into June (ran for all of May)
74
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
75
ID: uuid.New(),
76
WorkspaceID: workspace.ID,
77
StartedTime: db.NewVarCharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)),
78
StoppingTime: db.NewVarCharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
79
}),
80
// Start of June
81
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
82
ID: uuid.New(),
83
WorkspaceID: workspace.ID,
84
StartedTime: db.NewVarCharTime(time.Date(2022, 06, 1, 00, 00, 00, 00, time.UTC)),
85
StoppingTime: db.NewVarCharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
86
}),
87
}
88
89
var all []db.WorkspaceInstance
90
all = append(all, valid...)
91
all = append(all, invalid...)
92
93
dbtest.CreateWorkspaceInstances(t, conn, all...)
94
95
retrieved := dbtest.FindStoppedWorkspaceInstancesInRange(t, conn, startOfMay, startOfJune, workspace.ID)
96
97
require.Len(t, retrieved, len(valid))
98
}
99
100
func TestAttributionID_Values(t *testing.T) {
101
scenarios := []struct {
102
Input string
103
ExpectedEntity string
104
ExpectedID string
105
}{
106
{Input: "team:123", ExpectedEntity: "team", ExpectedID: "123"},
107
{Input: "user:123", ExpectedEntity: "", ExpectedID: ""},
108
{Input: "foo:123", ExpectedEntity: "", ExpectedID: ""},
109
{Input: "user:123:invalid", ExpectedEntity: "", ExpectedID: ""},
110
{Input: "invalid:123:", ExpectedEntity: "", ExpectedID: ""},
111
{Input: "", ExpectedEntity: "", ExpectedID: ""},
112
}
113
114
for _, s := range scenarios {
115
t.Run(fmt.Sprintf("attribution in: %s, expecting: %s %s", s.Input, s.ExpectedEntity, s.ExpectedID), func(t *testing.T) {
116
entity, id := db.AttributionID(s.Input).Values()
117
require.Equal(t, s.ExpectedEntity, entity)
118
require.Equal(t, s.ExpectedID, id)
119
})
120
}
121
}
122
123
func TestFindRunningWorkspace(t *testing.T) {
124
conn := dbtest.ConnectForTests(t)
125
126
workspace := dbtest.CreateWorkspaces(t, conn, dbtest.NewWorkspace(t, db.Workspace{}))[0]
127
now := time.Now()
128
fiveMinAgo := now.Add(-5 * time.Minute)
129
tenMinAgo := now.Add(-10 * time.Minute)
130
moreThan10DaysAgo := now.Add(-11 * 24 * time.Hour)
131
132
all := []db.WorkspaceInstance{
133
// one stopped instance
134
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
135
WorkspaceID: workspace.ID,
136
StartedTime: db.NewVarCharTime(tenMinAgo),
137
StoppingTime: db.NewVarCharTime(fiveMinAgo),
138
}),
139
// one unstopped before 10 days ago
140
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
141
WorkspaceID: workspace.ID,
142
StartedTime: db.NewVarCharTime(moreThan10DaysAgo),
143
PhasePersisted: "running",
144
}),
145
// Two running instances
146
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
147
ID: uuid.New(),
148
WorkspaceID: workspace.ID,
149
StartedTime: db.NewVarCharTime(tenMinAgo),
150
PhasePersisted: "running",
151
}),
152
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
153
ID: uuid.New(),
154
WorkspaceID: workspace.ID,
155
StartedTime: db.NewVarCharTime(tenMinAgo),
156
PhasePersisted: "running",
157
}),
158
}
159
160
dbtest.CreateWorkspaceInstances(t, conn, all...)
161
162
retrieved := dbtest.FindRunningWorkspaceInstances(t, conn, workspace.ID)
163
164
require.Equal(t, 2, len(retrieved))
165
for _, ws := range retrieved {
166
require.False(t, ws.StoppingTime.IsSet())
167
}
168
169
}
170
171
func TestFindWorkspacesByInstanceId(t *testing.T) {
172
conn := dbtest.ConnectForTests(t)
173
174
workspace := dbtest.CreateWorkspaces(t, conn, dbtest.NewWorkspace(t, db.Workspace{}))[0]
175
176
all := []db.WorkspaceInstance{
177
// one stopped instance
178
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
179
WorkspaceID: workspace.ID,
180
StartedTime: db.NewVarCharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)),
181
StoppingTime: db.NewVarCharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)),
182
}),
183
// Two running instances
184
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
185
ID: uuid.New(),
186
WorkspaceID: workspace.ID,
187
StartedTime: db.NewVarCharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)),
188
}),
189
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
190
ID: uuid.New(),
191
WorkspaceID: workspace.ID,
192
StartedTime: db.NewVarCharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)),
193
}),
194
}
195
196
twoIds := []uuid.UUID{all[0].ID, all[1].ID}
197
198
dbtest.CreateWorkspaceInstances(t, conn, all...)
199
200
retrieved, err := db.FindWorkspaceInstancesByIds(context.Background(), conn, twoIds)
201
require.NoError(t, err)
202
203
require.Equal(t, 2, len(retrieved))
204
for _, ws := range retrieved {
205
require.NotEqual(t, all[2].ID, ws.ID)
206
}
207
208
}
209
210
func TestListWorkspaceInstanceIDsWithPhaseStoppedButNoStoppingTime(t *testing.T) {
211
dbconn := dbtest.ConnectForTests(t)
212
instances := dbtest.CreateWorkspaceInstances(t, dbconn,
213
// started but not stopped, should be ignored
214
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
215
StartedTime: db.NewVarCharTime(time.Now()),
216
}),
217
// stopped, but no stopping time, should be detected
218
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
219
StartedTime: db.NewVarCharTime(time.Now()),
220
PhasePersisted: "stopped",
221
}),
222
)
223
224
dbtest.CreateUsageRecords(t, dbconn,
225
dbtest.NewUsage(t, db.Usage{
226
ID: instances[0].ID,
227
}),
228
dbtest.NewUsage(t, db.Usage{
229
ID: instances[1].ID,
230
}),
231
)
232
233
detectedIDs, err := db.ListWorkspaceInstanceIDsWithPhaseStoppedButNoStoppingTime(context.Background(), dbconn)
234
require.NoError(t, err)
235
require.Equal(t, []uuid.UUID{instances[1].ID}, detectedIDs)
236
}
237
238