-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathworkspace_instance.go
122 lines (101 loc) · 3.11 KB
/
workspace_instance.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package dbtest
import (
"database/sql"
"testing"
"time"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
var (
workspaceInstanceStatus = `{"phase": "stopped", "conditions": {"deployed": false, "pullingImages": false, "serviceExists": false}}`
)
func NewWorkspaceInstance(t *testing.T, instance db.WorkspaceInstance) db.WorkspaceInstance {
t.Helper()
id := uuid.New()
if instance.ID.ID() != 0 { // empty value
id = instance.ID
}
workspaceID := GenerateWorkspaceID()
if instance.WorkspaceID != "" {
workspaceID = instance.WorkspaceID
}
creationTime := db.VarcharTime{}
if instance.CreationTime.IsSet() {
creationTime = instance.CreationTime
} else if instance.StartedTime.IsSet() {
creationTime = instance.StartedTime
}
startedTime := db.VarcharTime{}
if instance.StartedTime.IsSet() {
startedTime = instance.StartedTime
}
deployedTime := db.VarcharTime{}
if instance.DeployedTime.IsSet() {
deployedTime = instance.DeployedTime
}
stoppedTime := db.VarcharTime{}
if instance.StoppedTime.IsSet() {
stoppedTime = instance.StoppedTime
} else if instance.StoppingTime.IsSet() {
creationTime = instance.StoppingTime
}
stoppingTime := db.VarcharTime{}
if instance.StoppingTime.IsSet() {
stoppingTime = instance.StoppingTime
}
status := []byte(workspaceInstanceStatus)
if instance.Status.String() != "" {
status = instance.Status
}
attributionID := db.NewUserAttributionID(uuid.New().String())
if instance.UsageAttributionID != "" {
attributionID = instance.UsageAttributionID
}
workspaceClass := db.WorkspaceClass_Default
if instance.WorkspaceClass != "" {
workspaceClass = instance.WorkspaceClass
}
return db.WorkspaceInstance{
ID: id,
WorkspaceID: workspaceID,
UsageAttributionID: attributionID,
WorkspaceClass: workspaceClass,
Configuration: nil,
Region: "",
ImageBuildInfo: sql.NullString{},
IdeURL: "",
WorkspaceBaseImage: "",
WorkspaceImage: "",
CreationTime: creationTime,
StartedTime: startedTime,
DeployedTime: deployedTime,
StoppedTime: stoppedTime,
LastModified: time.Time{},
StoppingTime: stoppingTime,
LastHeartbeat: "",
StatusOld: sql.NullString{},
Status: status,
Phase: sql.NullString{},
PhasePersisted: "",
}
}
func CreateWorkspaceInstances(t *testing.T, conn *gorm.DB, instances ...db.WorkspaceInstance) []db.WorkspaceInstance {
t.Helper()
var records []db.WorkspaceInstance
var ids []string
for _, instance := range instances {
record := NewWorkspaceInstance(t, instance)
records = append(records, record)
ids = append(ids, record.ID.String())
}
require.NoError(t, conn.CreateInBatches(&records, 1000).Error)
t.Cleanup(func() {
require.NoError(t, conn.Where(ids).Delete(&db.WorkspaceInstance{}).Error)
})
return records
}