@@ -8,12 +8,16 @@ import (
8
8
9
9
"go.uber.org/goleak"
10
10
11
+ "github.com/google/uuid"
12
+
11
13
"github.com/coder/coder/coderd/autobuild/executor"
12
14
"github.com/coder/coder/coderd/autobuild/schedule"
13
15
"github.com/coder/coder/coderd/coderdtest"
14
16
"github.com/coder/coder/coderd/database"
15
17
"github.com/coder/coder/coderd/util/ptr"
16
18
"github.com/coder/coder/codersdk"
19
+ "github.com/coder/coder/provisioner/echo"
20
+ "github.com/coder/coder/provisionersdk/proto"
17
21
18
22
"github.com/stretchr/testify/assert"
19
23
"github.com/stretchr/testify/require"
@@ -540,6 +544,67 @@ func TestExecutorAutostartMultipleOK(t *testing.T) {
540
544
assert .Len (t , stats2 .Transitions , 0 )
541
545
}
542
546
547
+ func TestExecutorAutostartWithParameters (t * testing.T ) {
548
+ t .Parallel ()
549
+
550
+ const (
551
+ stringParameterName = "string_parameter"
552
+ stringParameterValue = "abc"
553
+
554
+ numberParameterName = "number_parameter"
555
+ numberParameterValue = "7"
556
+ )
557
+
558
+ var (
559
+ sched = mustSchedule (t , "CRON_TZ=UTC 0 * * * *" )
560
+ tickCh = make (chan time.Time )
561
+ statsCh = make (chan executor.Stats )
562
+ client = coderdtest .New (t , & coderdtest.Options {
563
+ AutobuildTicker : tickCh ,
564
+ IncludeProvisionerDaemon : true ,
565
+ AutobuildStats : statsCh ,
566
+ })
567
+
568
+ richParameters = []* proto.RichParameter {
569
+ {Name : stringParameterName , Type : "string" , Mutable : true },
570
+ {Name : numberParameterName , Type : "number" , Mutable : true },
571
+ }
572
+
573
+ // Given: we have a user with a workspace that has autostart enabled
574
+ workspace = mustProvisionWorkspaceWithParameters (t , client , richParameters , func (cwr * codersdk.CreateWorkspaceRequest ) {
575
+ cwr .AutostartSchedule = ptr .Ref (sched .String ())
576
+ cwr .RichParameterValues = []codersdk.WorkspaceBuildParameter {
577
+ {
578
+ Name : stringParameterName ,
579
+ Value : stringParameterValue ,
580
+ },
581
+ {
582
+ Name : numberParameterName ,
583
+ Value : numberParameterValue ,
584
+ },
585
+ }
586
+ })
587
+ )
588
+ // Given: workspace is stopped
589
+ workspace = coderdtest .MustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
590
+
591
+ // When: the autobuild executor ticks after the scheduled time
592
+ go func () {
593
+ tickCh <- sched .Next (workspace .LatestBuild .CreatedAt )
594
+ close (tickCh )
595
+ }()
596
+
597
+ // Then: the workspace with parameters should eventually be started
598
+ stats := <- statsCh
599
+ assert .NoError (t , stats .Error )
600
+ assert .Len (t , stats .Transitions , 1 )
601
+ assert .Contains (t , stats .Transitions , workspace .ID )
602
+ assert .Equal (t , database .WorkspaceTransitionStart , stats .Transitions [workspace .ID ])
603
+
604
+ workspace = coderdtest .MustWorkspace (t , client , workspace .ID )
605
+ mustWorkspaceParameters (t , client , workspace .LatestBuild .ID )
606
+ }
607
+
543
608
func mustProvisionWorkspace (t * testing.T , client * codersdk.Client , mut ... func (* codersdk.CreateWorkspaceRequest )) codersdk.Workspace {
544
609
t .Helper ()
545
610
user := coderdtest .CreateFirstUser (t , client )
@@ -551,13 +616,48 @@ func mustProvisionWorkspace(t *testing.T, client *codersdk.Client, mut ...func(*
551
616
return coderdtest .MustWorkspace (t , client , ws .ID )
552
617
}
553
618
619
+ func mustProvisionWorkspaceWithParameters (t * testing.T , client * codersdk.Client , richParameters []* proto.RichParameter , mut ... func (* codersdk.CreateWorkspaceRequest )) codersdk.Workspace {
620
+ t .Helper ()
621
+ user := coderdtest .CreateFirstUser (t , client )
622
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
623
+ Parse : echo .ParseComplete ,
624
+ ProvisionPlan : []* proto.Provision_Response {
625
+ {
626
+ Type : & proto.Provision_Response_Complete {
627
+ Complete : & proto.Provision_Complete {
628
+ Parameters : richParameters ,
629
+ },
630
+ },
631
+ }},
632
+ ProvisionApply : []* proto.Provision_Response {
633
+ {
634
+ Type : & proto.Provision_Response_Complete {
635
+ Complete : & proto.Provision_Complete {},
636
+ },
637
+ },
638
+ },
639
+ })
640
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
641
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
642
+ ws := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID , mut ... )
643
+ coderdtest .AwaitWorkspaceBuildJob (t , client , ws .LatestBuild .ID )
644
+ return coderdtest .MustWorkspace (t , client , ws .ID )
645
+ }
646
+
554
647
func mustSchedule (t * testing.T , s string ) * schedule.Schedule {
555
648
t .Helper ()
556
649
sched , err := schedule .Weekly (s )
557
650
require .NoError (t , err )
558
651
return sched
559
652
}
560
653
654
+ func mustWorkspaceParameters (t * testing.T , client * codersdk.Client , workspaceID uuid.UUID ) {
655
+ ctx := context .Background ()
656
+ buildParameters , err := client .WorkspaceBuildParameters (ctx , workspaceID )
657
+ require .NoError (t , err )
658
+ require .NotEmpty (t , buildParameters )
659
+ }
660
+
561
661
func TestMain (m * testing.M ) {
562
662
goleak .VerifyTestMain (m )
563
663
}
0 commit comments