@@ -974,22 +974,23 @@ func TestWorkspaceUpdateTTL(t *testing.T) {
974
974
func TestWorkspaceExtend (t * testing.T ) {
975
975
t .Parallel ()
976
976
var (
977
+ ttl = 8 * time .Hour
978
+ newDeadline = time .Now ().Add (ttl + time .Hour ).UTC ()
977
979
ctx = context .Background ()
978
980
client = coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
979
981
user = coderdtest .CreateFirstUser (t , client )
980
982
version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
981
983
_ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
982
- project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
983
- workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
984
- extend = 90 * time .Minute
985
- _ = coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
986
- oldDeadline = time .Now ().Add (time .Duration (* workspace .TTLMillis ) * time .Millisecond ).UTC ()
987
- newDeadline = time .Now ().Add (time .Duration (* workspace .TTLMillis )* time .Millisecond + extend ).UTC ()
984
+ template = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
985
+ workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID , func (cwr * codersdk.CreateWorkspaceRequest ) {
986
+ cwr .TTLMillis = ptr .Ref (ttl .Milliseconds ())
987
+ })
988
+ _ = coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
988
989
)
989
990
990
991
workspace , err := client .Workspace (ctx , workspace .ID )
991
992
require .NoError (t , err , "fetch provisioned workspace" )
992
- require . InDelta ( t , oldDeadline . Unix (), workspace .LatestBuild .Deadline . Unix (), 60 )
993
+ oldDeadline := workspace .LatestBuild .Deadline
993
994
994
995
// Updating the deadline should succeed
995
996
req := codersdk.PutExtendWorkspaceRequest {
@@ -1001,30 +1002,45 @@ func TestWorkspaceExtend(t *testing.T) {
1001
1002
// Ensure deadline set correctly
1002
1003
updated , err := client .Workspace (ctx , workspace .ID )
1003
1004
require .NoError (t , err , "failed to fetch updated workspace" )
1004
- require .InDelta (t , newDeadline . Unix () , updated .LatestBuild .Deadline . Unix (), 60 )
1005
+ require .WithinDuration (t , newDeadline , updated .LatestBuild .Deadline , time . Minute )
1005
1006
1006
1007
// Zero time should fail
1007
1008
err = client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
1008
1009
Deadline : time.Time {},
1009
1010
})
1010
1011
require .ErrorContains (t , err , "deadline: Validation failed for tag \" required\" with value: \" 0001-01-01 00:00:00 +0000 UTC\" " , "setting an empty deadline on a workspace should fail" )
1011
1012
1012
- // Updating with an earlier time should also fail
1013
+ // Updating with a deadline 29 minutes in the future should fail
1014
+ deadlineTooSoon := time .Now ().Add (29 * time .Minute )
1015
+ err = client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
1016
+ Deadline : deadlineTooSoon ,
1017
+ })
1018
+ require .ErrorContains (t , err , "new deadline must be at least 30 minutes in the future" , "setting a deadline less than 30 minutes in the future should fail" )
1019
+
1020
+ // And with a deadline greater than the template max_ttl should also fail
1021
+ deadlineExceedsMaxTTL := time .Now ().Add (time .Duration (template .MaxTTLMillis ) * time .Millisecond ).Add (time .Minute )
1022
+ err = client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
1023
+ Deadline : deadlineExceedsMaxTTL ,
1024
+ })
1025
+ require .ErrorContains (t , err , "new deadline is greater than template allows" , "setting a deadline greater than that allowed by the template should fail" )
1026
+
1027
+ // Updating with a deadline 30 minutes in the future should succeed
1028
+ deadlineJustSoonEnough := time .Now ().Add (30 * time .Minute )
1013
1029
err = client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
1014
- Deadline : oldDeadline ,
1030
+ Deadline : deadlineJustSoonEnough ,
1015
1031
})
1016
- require .ErrorContains (t , err , "deadline: minimum extension is one minute" , "setting an earlier deadline should fail " )
1032
+ require .NoError (t , err , "setting a deadline at least 30 minutes in the future should succeed " )
1017
1033
1018
- // Updating with a time far in the future should also fail
1034
+ // Updating with a deadline an hour before the previous deadline should succeed
1019
1035
err = client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
1020
- Deadline : oldDeadline .AddDate ( 1 , 0 , 0 ),
1036
+ Deadline : oldDeadline .Add ( - time . Hour ),
1021
1037
})
1022
- require .ErrorContains (t , err , "deadline: maximum extension is 24 hours" , " setting an earlier deadline should fail" )
1038
+ require .NoError (t , err , "setting an earlier deadline should not fail" )
1023
1039
1024
1040
// Ensure deadline still set correctly
1025
1041
updated , err = client .Workspace (ctx , workspace .ID )
1026
1042
require .NoError (t , err , "failed to fetch updated workspace" )
1027
- require .InDelta (t , newDeadline . Unix ( ), updated .LatestBuild .Deadline . Unix (), 60 )
1043
+ require .WithinDuration (t , oldDeadline . Add ( - time . Hour ), updated .LatestBuild .Deadline , time . Minute )
1028
1044
}
1029
1045
1030
1046
func TestWorkspaceWatcher (t * testing.T ) {
0 commit comments