@@ -17,6 +17,47 @@ import (
17
17
func TestBump (t * testing.T ) {
18
18
t .Parallel ()
19
19
20
+ t .Run ("BumpOKDefault" , func (t * testing.T ) {
21
+ t .Parallel ()
22
+
23
+ // Given: we have a workspace
24
+ var (
25
+ err error
26
+ ctx = context .Background ()
27
+ client = coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
28
+ user = coderdtest .CreateFirstUser (t , client )
29
+ version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
30
+ _ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
31
+ project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
32
+ workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
33
+ cmdArgs = []string {"bump" , workspace .Name }
34
+ stdoutBuf = & bytes.Buffer {}
35
+ )
36
+
37
+ // Given: we wait for the workspace to be built
38
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
39
+ workspace , err = client .Workspace (ctx , workspace .ID )
40
+ require .NoError (t , err )
41
+ expectedDeadline := workspace .LatestBuild .Deadline .Add (90 * time .Minute )
42
+
43
+ // Assert test invariant: workspace build has a deadline set equal to now plus ttl
44
+ initDeadline := time .Now ().Add (time .Duration (* workspace .TTLMillis ) * time .Millisecond )
45
+ require .WithinDuration (t , initDeadline , workspace .LatestBuild .Deadline , time .Minute )
46
+
47
+ cmd , root := clitest .New (t , cmdArgs ... )
48
+ clitest .SetupConfig (t , client , root )
49
+ cmd .SetOut (stdoutBuf )
50
+
51
+ // When: we execute `coder bump <workspace>`
52
+ err = cmd .ExecuteContext (ctx )
53
+ require .NoError (t , err , "unexpected error" )
54
+
55
+ // Then: the deadline of the latest build is updated
56
+ updated , err := client .Workspace (ctx , workspace .ID )
57
+ require .NoError (t , err )
58
+ require .WithinDuration (t , expectedDeadline , updated .LatestBuild .Deadline , time .Minute )
59
+ })
60
+
20
61
t .Run ("BumpSpecificDuration" , func (t * testing.T ) {
21
62
t .Parallel ()
22
63
@@ -30,15 +71,15 @@ func TestBump(t *testing.T) {
30
71
_ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
31
72
project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
32
73
workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
33
- cmdArgs = []string {"bump" , workspace .Name , "10h " }
74
+ cmdArgs = []string {"bump" , workspace .Name , "30 " }
34
75
stdoutBuf = & bytes.Buffer {}
35
76
)
36
77
37
78
// Given: we wait for the workspace to be built
38
79
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
39
80
workspace , err = client .Workspace (ctx , workspace .ID )
40
81
require .NoError (t , err )
41
- expectedDeadline := time . Now (). Add (10 * time .Hour )
82
+ expectedDeadline := workspace . LatestBuild . Deadline . Add (30 * time .Minute )
42
83
43
84
// Assert test invariant: workspace build has a deadline set equal to now plus ttl
44
85
initDeadline := time .Now ().Add (time .Duration (* workspace .TTLMillis ) * time .Millisecond )
@@ -109,7 +150,7 @@ func TestBump(t *testing.T) {
109
150
workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID , func (cwr * codersdk.CreateWorkspaceRequest ) {
110
151
cwr .TTLMillis = nil
111
152
})
112
- cmdArgs = []string {"bump" , workspace .Name , "1h" }
153
+ cmdArgs = []string {"bump" , workspace .Name }
113
154
stdoutBuf = & bytes.Buffer {}
114
155
)
115
156
// Unset the workspace TTL
@@ -139,11 +180,51 @@ func TestBump(t *testing.T) {
139
180
140
181
// When: we execute `coder bump workspace``
141
182
err = cmd .ExecuteContext (ctx )
142
- require .Error (t , err )
183
+ require .NoError (t , err )
143
184
144
185
// Then: nothing happens and the deadline remains unset
145
186
updated , err := client .Workspace (ctx , workspace .ID )
146
187
require .NoError (t , err )
147
188
require .Zero (t , updated .LatestBuild .Deadline )
148
189
})
190
+
191
+ t .Run ("BumpMinimumDuration" , func (t * testing.T ) {
192
+ t .Parallel ()
193
+
194
+ // Given: we have a workspace with no deadline set
195
+ var (
196
+ err error
197
+ ctx = context .Background ()
198
+ client = coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
199
+ user = coderdtest .CreateFirstUser (t , client )
200
+ version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
201
+ _ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
202
+ project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
203
+ workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
204
+ cmdArgs = []string {"bump" , workspace .Name , "59s" }
205
+ stdoutBuf = & bytes.Buffer {}
206
+ )
207
+
208
+ // Given: we wait for the workspace to build
209
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
210
+ workspace , err = client .Workspace (ctx , workspace .ID )
211
+ require .NoError (t , err )
212
+
213
+ // Assert test invariant: workspace build has a deadline set equal to now plus ttl
214
+ initDeadline := time .Now ().Add (time .Duration (* workspace .TTLMillis ) * time .Millisecond )
215
+ require .WithinDuration (t , initDeadline , workspace .LatestBuild .Deadline , time .Minute )
216
+
217
+ cmd , root := clitest .New (t , cmdArgs ... )
218
+ clitest .SetupConfig (t , client , root )
219
+ cmd .SetOut (stdoutBuf )
220
+
221
+ // When: we execute `coder bump workspace 59s`
222
+ err = cmd .ExecuteContext (ctx )
223
+ require .ErrorContains (t , err , "minimum bump duration is 1 minute" )
224
+
225
+ // Then: an error is reported and the deadline remains as before
226
+ updated , err := client .Workspace (ctx , workspace .ID )
227
+ require .NoError (t , err )
228
+ require .WithinDuration (t , workspace .LatestBuild .Deadline , updated .LatestBuild .Deadline , time .Minute )
229
+ })
149
230
}
0 commit comments