Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/workspace_instance.go
2500 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 dbtest
6
7
import (
8
"context"
9
"database/sql"
10
"testing"
11
"time"
12
13
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
14
"github.com/google/uuid"
15
"github.com/stretchr/testify/require"
16
"gorm.io/gorm"
17
)
18
19
var (
20
workspaceInstanceStatus = `{"phase": "stopped", "conditions": {"deployed": false, "pullingImages": false, "serviceExists": false}}`
21
)
22
23
func NewWorkspaceInstance(t *testing.T, instance db.WorkspaceInstance) db.WorkspaceInstance {
24
t.Helper()
25
26
id := uuid.New()
27
if instance.ID != uuid.Nil {
28
id = instance.ID
29
}
30
31
workspaceID := GenerateWorkspaceID()
32
if instance.WorkspaceID != "" {
33
workspaceID = instance.WorkspaceID
34
}
35
36
creationTime := db.VarcharTime{}
37
if instance.CreationTime.IsSet() {
38
creationTime = instance.CreationTime
39
} else if instance.StartedTime.IsSet() {
40
creationTime = instance.StartedTime
41
}
42
43
startedTime := db.VarcharTime{}
44
if instance.StartedTime.IsSet() {
45
startedTime = instance.StartedTime
46
}
47
48
deployedTime := db.VarcharTime{}
49
if instance.DeployedTime.IsSet() {
50
deployedTime = instance.DeployedTime
51
}
52
53
stoppedTime := db.VarcharTime{}
54
if instance.StoppedTime.IsSet() {
55
stoppedTime = instance.StoppedTime
56
} else if instance.StoppingTime.IsSet() {
57
creationTime = instance.StoppingTime
58
}
59
60
stoppingTime := db.VarcharTime{}
61
if instance.StoppingTime.IsSet() {
62
stoppingTime = instance.StoppingTime
63
}
64
65
status := []byte(workspaceInstanceStatus)
66
if instance.Status.String() != "" {
67
status = instance.Status
68
}
69
70
attributionID := db.NewTeamAttributionID(uuid.New().String())
71
if instance.UsageAttributionID != "" {
72
attributionID = instance.UsageAttributionID
73
}
74
75
workspaceClass := db.WorkspaceClass_Default
76
if instance.WorkspaceClass != "" {
77
workspaceClass = instance.WorkspaceClass
78
}
79
80
phasePersisted := ""
81
if instance.PhasePersisted != "" {
82
phasePersisted = instance.PhasePersisted
83
}
84
85
return db.WorkspaceInstance{
86
ID: id,
87
WorkspaceID: workspaceID,
88
UsageAttributionID: attributionID,
89
WorkspaceClass: workspaceClass,
90
Configuration: nil,
91
Region: "",
92
ImageBuildInfo: sql.NullString{},
93
IdeURL: "",
94
WorkspaceBaseImage: "",
95
WorkspaceImage: "",
96
CreationTime: creationTime,
97
StartedTime: startedTime,
98
DeployedTime: deployedTime,
99
StoppedTime: stoppedTime,
100
LastModified: time.Time{},
101
StoppingTime: stoppingTime,
102
LastHeartbeat: "",
103
StatusOld: sql.NullString{},
104
Status: status,
105
Phase: sql.NullString{},
106
PhasePersisted: phasePersisted,
107
}
108
}
109
110
func CreateWorkspaceInstances(t *testing.T, conn *gorm.DB, instances ...db.WorkspaceInstance) []db.WorkspaceInstance {
111
t.Helper()
112
113
var records []db.WorkspaceInstance
114
var ids []string
115
for _, instance := range instances {
116
record := NewWorkspaceInstance(t, instance)
117
records = append(records, record)
118
ids = append(ids, record.ID.String())
119
}
120
121
require.NoError(t, conn.CreateInBatches(&records, 1000).Error)
122
123
t.Cleanup(func() {
124
require.NoError(t, conn.Where(ids).Delete(&db.WorkspaceInstance{}).Error)
125
})
126
127
return records
128
}
129
130
func FindStoppedWorkspaceInstancesInRange(t *testing.T, conn *gorm.DB, from, to time.Time, workspaceID string) []db.WorkspaceInstanceForUsage {
131
all, err := db.FindStoppedWorkspaceInstancesInRange(context.Background(), conn, from, to)
132
require.NoError(t, err)
133
return filterByWorkspaceId(all, workspaceID)
134
}
135
136
func FindRunningWorkspaceInstances(t *testing.T, conn *gorm.DB, workspaceID string) []db.WorkspaceInstanceForUsage {
137
all, err := db.FindRunningWorkspaceInstances(context.Background(), conn)
138
require.NoError(t, err)
139
return filterByWorkspaceId(all, workspaceID)
140
}
141
142
func filterByWorkspaceId(all []db.WorkspaceInstanceForUsage, workspaceID string) []db.WorkspaceInstanceForUsage {
143
var result []db.WorkspaceInstanceForUsage
144
for _, candidate := range all {
145
if candidate.WorkspaceID == workspaceID {
146
result = append(result, candidate)
147
}
148
}
149
return result
150
}
151
152