6
6
"database/sql"
7
7
"encoding/hex"
8
8
"fmt"
9
+ "net"
9
10
"testing"
10
11
"time"
11
12
@@ -21,6 +22,34 @@ import (
21
22
// All methods take in a 'seed' object. Any provided fields in the seed will be
22
23
// maintained. Any fields omitted will have sensible defaults generated.
23
24
25
+ func AuditLog (t * testing.T , db database.Store , seed database.AuditLog ) database.AuditLog {
26
+ log , err := db .InsertAuditLog (context .Background (), database.InsertAuditLogParams {
27
+ ID : takeFirst (seed .ID , uuid .New ()),
28
+ Time : takeFirst (seed .Time , time .Now ()),
29
+ UserID : takeFirst (seed .UserID , uuid .New ()),
30
+ OrganizationID : takeFirst (seed .OrganizationID , uuid .New ()),
31
+ Ip : pqtype.Inet {
32
+ IPNet : takeFirstIP (seed .Ip .IPNet , net.IPNet {}),
33
+ Valid : takeFirst (seed .Ip .Valid , false ),
34
+ },
35
+ UserAgent : sql.NullString {
36
+ String : takeFirst (seed .UserAgent .String , "" ),
37
+ Valid : takeFirst (seed .UserAgent .Valid , false ),
38
+ },
39
+ ResourceType : takeFirst (seed .ResourceType , database .ResourceTypeOrganization ),
40
+ ResourceID : takeFirst (seed .ResourceID , uuid .New ()),
41
+ ResourceTarget : takeFirst (seed .ResourceTarget , uuid .NewString ()),
42
+ Action : takeFirst (seed .Action , database .AuditActionCreate ),
43
+ Diff : takeFirstBytes (seed .Diff , []byte ("{}" )),
44
+ StatusCode : takeFirst (seed .StatusCode , 200 ),
45
+ AdditionalFields : takeFirstBytes (seed .Diff , []byte ("{}" )),
46
+ RequestID : takeFirst (seed .RequestID , uuid .New ()),
47
+ ResourceIcon : takeFirst (seed .ResourceIcon , "" ),
48
+ })
49
+ require .NoError (t , err , "insert audit log" )
50
+ return log
51
+ }
52
+
24
53
func Template (t * testing.T , db database.Store , seed database.Template ) database.Template {
25
54
template , err := db .InsertTemplate (context .Background (), database.InsertTemplateParams {
26
55
ID : takeFirst (seed .ID , uuid .New ()),
@@ -66,6 +95,47 @@ func APIKey(t *testing.T, db database.Store, seed database.APIKey) (key database
66
95
return key , fmt .Sprintf ("%s-%s" , key .ID , secret )
67
96
}
68
97
98
+ func WorkspaceAgent (t * testing.T , db database.Store , orig database.WorkspaceAgent ) database.WorkspaceAgent {
99
+ workspace , err := db .InsertWorkspaceAgent (context .Background (), database.InsertWorkspaceAgentParams {
100
+ ID : takeFirst (orig .ID , uuid .New ()),
101
+ CreatedAt : takeFirst (orig .CreatedAt , time .Now ()),
102
+ UpdatedAt : takeFirst (orig .UpdatedAt , time .Now ()),
103
+ Name : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
104
+ ResourceID : takeFirst (orig .ResourceID , uuid .New ()),
105
+ AuthToken : takeFirst (orig .AuthToken , uuid .New ()),
106
+ AuthInstanceID : sql.NullString {
107
+ String : takeFirst (orig .AuthInstanceID .String , namesgenerator .GetRandomName (1 )),
108
+ Valid : takeFirst (orig .AuthInstanceID .Valid , true ),
109
+ },
110
+ Architecture : takeFirst (orig .Architecture , "amd64" ),
111
+ EnvironmentVariables : pqtype.NullRawMessage {
112
+ RawMessage : takeFirstBytes (orig .EnvironmentVariables .RawMessage , []byte ("{}" )),
113
+ Valid : takeFirst (orig .EnvironmentVariables .Valid , false ),
114
+ },
115
+ OperatingSystem : takeFirst (orig .OperatingSystem , "linux" ),
116
+ StartupScript : sql.NullString {
117
+ String : takeFirst (orig .StartupScript .String , "" ),
118
+ Valid : takeFirst (orig .StartupScript .Valid , false ),
119
+ },
120
+ Directory : takeFirst (orig .Directory , "" ),
121
+ InstanceMetadata : pqtype.NullRawMessage {
122
+ RawMessage : takeFirstBytes (orig .ResourceMetadata .RawMessage , []byte ("{}" )),
123
+ Valid : takeFirst (orig .ResourceMetadata .Valid , false ),
124
+ },
125
+ ResourceMetadata : pqtype.NullRawMessage {
126
+ RawMessage : takeFirstBytes (orig .ResourceMetadata .RawMessage , []byte ("{}" )),
127
+ Valid : takeFirst (orig .ResourceMetadata .Valid , false ),
128
+ },
129
+ ConnectionTimeoutSeconds : takeFirst (orig .ConnectionTimeoutSeconds , 3600 ),
130
+ TroubleshootingURL : takeFirst (orig .TroubleshootingURL , "https://example.com" ),
131
+ MOTDFile : takeFirst (orig .TroubleshootingURL , "" ),
132
+ LoginBeforeReady : takeFirst (orig .LoginBeforeReady , false ),
133
+ StartupScriptTimeoutSeconds : takeFirst (orig .StartupScriptTimeoutSeconds , 3600 ),
134
+ })
135
+ require .NoError (t , err , "insert workspace agent" )
136
+ return workspace
137
+ }
138
+
69
139
func Workspace (t * testing.T , db database.Store , orig database.Workspace ) database.Workspace {
70
140
workspace , err := db .InsertWorkspace (context .Background (), database.InsertWorkspaceParams {
71
141
ID : takeFirst (orig .ID , uuid .New ()),
@@ -89,7 +159,7 @@ func WorkspaceBuild(t *testing.T, db database.Store, orig database.WorkspaceBuil
89
159
UpdatedAt : takeFirst (orig .UpdatedAt , time .Now ()),
90
160
WorkspaceID : takeFirst (orig .WorkspaceID , uuid .New ()),
91
161
TemplateVersionID : takeFirst (orig .TemplateVersionID , uuid .New ()),
92
- BuildNumber : takeFirst (orig .BuildNumber , 0 ),
162
+ BuildNumber : takeFirst (orig .BuildNumber , 1 ),
93
163
Transition : takeFirst (orig .Transition , database .WorkspaceTransitionStart ),
94
164
InitiatorID : takeFirst (orig .InitiatorID , uuid .New ()),
95
165
JobID : takeFirst (orig .JobID , uuid .New ()),
@@ -140,6 +210,20 @@ func Group(t *testing.T, db database.Store, orig database.Group) database.Group
140
210
return group
141
211
}
142
212
213
+ func GroupMember (t * testing.T , db database.Store , orig database.GroupMember ) database.GroupMember {
214
+ member := database.GroupMember {
215
+ UserID : takeFirst (orig .UserID , uuid .New ()),
216
+ GroupID : takeFirst (orig .GroupID , uuid .New ()),
217
+ }
218
+ //nolint:gosimple
219
+ err := db .InsertGroupMember (context .Background (), database.InsertGroupMemberParams {
220
+ UserID : member .UserID ,
221
+ GroupID : member .GroupID ,
222
+ })
223
+ require .NoError (t , err , "insert group member" )
224
+ return member
225
+ }
226
+
143
227
func ProvisionerJob (t * testing.T , db database.Store , orig database.ProvisionerJob ) database.ProvisionerJob {
144
228
job , err := db .InsertProvisionerJob (context .Background (), database.InsertProvisionerJobParams {
145
229
ID : takeFirst (orig .ID , uuid .New ()),
0 commit comments