@@ -3827,16 +3827,12 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
3827
3827
}
3828
3828
3829
3829
var (
3830
- ctx = testutil .Context (t , testutil .WaitShort )
3831
- logger = testutil .Logger (t )
3832
- mClock = quartz .NewMock (t )
3833
- tickerTrap = mClock .Trap ().TickerFunc ("updaterLoop" )
3830
+ ctx = testutil .Context (t , testutil .WaitShort )
3831
+ logger = testutil .Logger (t )
3834
3832
3835
- mCtrl = gomock .NewController (t )
3836
- mCCLI = acmock .NewMockContainerCLI (mCtrl )
3837
- mDCCLI = acmock .NewMockDevcontainerCLI (mCtrl )
3838
-
3839
- fSAC = & fakeSubAgentClient {}
3833
+ fDCCLI = & fakeDevcontainerCLI {readConfigErrC : make (chan func (envs []string ) error , 1 )}
3834
+ fCCLI = & fakeContainerCLI {arch : runtime .GOARCH }
3835
+ fSAC = & fakeSubAgentClient {}
3840
3836
3841
3837
testDC = codersdk.WorkspaceAgentDevcontainer {
3842
3838
ID : uuid .New (),
@@ -3855,17 +3851,12 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
3855
3851
userAppURL = "user.zed"
3856
3852
)
3857
3853
3858
- coderBin , err := os .Executable ()
3859
- require .NoError (t , err )
3860
-
3861
3854
// ==================================================
3862
3855
// PHASE 1: Prebuild workspace creates devcontainer
3863
3856
// ==================================================
3864
3857
3865
3858
// Given: There are no containers initially.
3866
- mCCLI .EXPECT ().List (gomock .Any ()).Return (codersdk.WorkspaceAgentListContainersResponse {
3867
- Containers : []codersdk.WorkspaceAgentContainer {},
3868
- }, nil )
3859
+ fCCLI .containers = codersdk.WorkspaceAgentListContainersResponse {}
3869
3860
3870
3861
api := agentcontainers .NewAPI (logger ,
3871
3862
// We want this first `agentcontainers.API` to have a manifest info
@@ -3877,72 +3868,43 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
3877
3868
[]codersdk.WorkspaceAgentScript {{ID : testDC .ID , LogSourceID : uuid .New ()}},
3878
3869
),
3879
3870
agentcontainers .WithSubAgentClient (fSAC ),
3880
- agentcontainers .WithContainerCLI (mCCLI ),
3881
- agentcontainers .WithDevcontainerCLI (mDCCLI ),
3882
- agentcontainers .WithClock (mClock ),
3871
+ agentcontainers .WithContainerCLI (fCCLI ),
3872
+ agentcontainers .WithDevcontainerCLI (fDCCLI ),
3883
3873
agentcontainers .WithWatcher (watcher .NewNoop ()),
3884
3874
)
3885
3875
api .Start ()
3886
3876
3887
- tickerTrap .MustWait (ctx ).MustRelease (ctx )
3888
- tickerTrap .Close ()
3877
+ fCCLI .containers = codersdk.WorkspaceAgentListContainersResponse {
3878
+ Containers : []codersdk.WorkspaceAgentContainer {testContainer },
3879
+ }
3889
3880
3890
3881
// Given: We allow the dev container to be created.
3891
- mDCCLI .EXPECT ().Up (gomock .Any (), testDC .WorkspaceFolder , testDC .ConfigPath , gomock .Any ()).
3892
- Return ("test-container-id" , nil )
3893
-
3894
- mCCLI .EXPECT ().List (gomock .Any ()).Return (codersdk.WorkspaceAgentListContainersResponse {
3895
- Containers : []codersdk.WorkspaceAgentContainer {testContainer },
3896
- }, nil )
3897
-
3898
- gomock .InOrder (
3899
- mCCLI .EXPECT ().DetectArchitecture (gomock .Any (), "test-container-id" ).Return (runtime .GOARCH , nil ),
3900
-
3901
- // Verify prebuild environment variables are passed to devcontainer
3902
- mDCCLI .EXPECT ().ReadConfig (gomock .Any (),
3903
- testDC .WorkspaceFolder ,
3904
- testDC .ConfigPath ,
3905
- gomock .Cond (func (envs []string ) bool {
3906
- return slices .Contains (envs , "CODER_WORKSPACE_OWNER_NAME=" + prebuildOwner ) &&
3907
- slices .Contains (envs , "CODER_WORKSPACE_NAME=" + prebuildWorkspace )
3908
- }),
3909
- ).Return (agentcontainers.DevcontainerConfig {
3910
- MergedConfiguration : agentcontainers.DevcontainerMergedConfiguration {
3911
- Customizations : agentcontainers.DevcontainerMergedCustomizations {
3912
- Coder : []agentcontainers.CoderCustomization {
3913
- {
3914
- Apps : []agentcontainers.SubAgentApp {
3915
- {
3916
- Slug : "zed" ,
3917
- URL : prebuildAppURL ,
3918
- },
3919
- },
3920
- },
3882
+ fDCCLI .upID = testContainer .ID
3883
+ fDCCLI .readConfig = agentcontainers.DevcontainerConfig {
3884
+ MergedConfiguration : agentcontainers.DevcontainerMergedConfiguration {
3885
+ Customizations : agentcontainers.DevcontainerMergedCustomizations {
3886
+ Coder : []agentcontainers.CoderCustomization {{
3887
+ Apps : []agentcontainers.SubAgentApp {
3888
+ {Slug : "zed" , URL : prebuildAppURL },
3921
3889
},
3922
- },
3890
+ }} ,
3923
3891
},
3924
- }, nil ),
3925
-
3926
- mCCLI .EXPECT ().ExecAs (gomock .Any (), testContainer .ID , "root" , "mkdir" , "-p" , "/.coder-agent" ).Return (nil , nil ),
3927
- mCCLI .EXPECT ().Copy (gomock .Any (), testContainer .ID , coderBin , "/.coder-agent/coder" ).Return (nil ),
3928
- mCCLI .EXPECT ().ExecAs (gomock .Any (), testContainer .ID , "root" , "chmod" , "0755" , "/.coder-agent" , "/.coder-agent/coder" ).Return (nil , nil ),
3929
- mCCLI .EXPECT ().ExecAs (gomock .Any (), testContainer .ID , "root" , "/bin/sh" , "-c" , "chown $(id -u):$(id -g) /.coder-agent/coder" ).Return (nil , nil ),
3892
+ },
3893
+ }
3930
3894
3931
- // We want to mock how the `Exec` function works when starting an agent. This should
3932
- // run until the given `ctx` is done.
3933
- mDCCLI .EXPECT ().Exec (gomock .Any (),
3934
- testDC .WorkspaceFolder , testDC .ConfigPath ,
3935
- "/.coder-agent/coder" , []string {"agent" }, gomock .Any (), gomock .Any (),
3936
- ).Do (func (ctx context.Context , _ , _ , _ string , _ []string , _ ... agentcontainers.DevcontainerCLIExecOptions ) error {
3937
- <- ctx .Done ()
3938
- return nil
3939
- }),
3940
- )
3895
+ var readConfigEnvVars []string
3896
+ testutil .RequireSend (ctx , t , fDCCLI .readConfigErrC , func (env []string ) error {
3897
+ readConfigEnvVars = env
3898
+ return nil
3899
+ })
3941
3900
3942
3901
// When: We create the dev container resource
3943
- err = api .CreateDevcontainer (testDC .WorkspaceFolder , testDC .ConfigPath )
3902
+ err : = api .CreateDevcontainer (testDC .WorkspaceFolder , testDC .ConfigPath )
3944
3903
require .NoError (t , err )
3945
3904
3905
+ require .Contains (t , readConfigEnvVars , "CODER_WORKSPACE_OWNER_NAME=" + prebuildOwner )
3906
+ require .Contains (t , readConfigEnvVars , "CODER_WORKSPACE_NAME=" + prebuildWorkspace )
3907
+
3946
3908
// Then: We there to be only 1 agent.
3947
3909
require .Len (t , fSAC .agents , 1 )
3948
3910
@@ -3968,14 +3930,6 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
3968
3930
// PHASE 2: User claims workspace, devcontainer should be reused
3969
3931
// =============================================================
3970
3932
3971
- // Given: We have a running container.
3972
- mCCLI .EXPECT ().List (gomock .Any ()).Return (codersdk.WorkspaceAgentListContainersResponse {
3973
- Containers : []codersdk.WorkspaceAgentContainer {testContainer },
3974
- }, nil )
3975
-
3976
- mClock = quartz .NewMock (t )
3977
- tickerTrap = mClock .Trap ().TickerFunc ("updaterLoop" )
3978
-
3979
3933
// Given: We create a new claimed API
3980
3934
api = agentcontainers .NewAPI (logger ,
3981
3935
// We want this second `agentcontainers.API` to have a manifest info
@@ -3987,74 +3941,45 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
3987
3941
[]codersdk.WorkspaceAgentScript {{ID : testDC .ID , LogSourceID : uuid .New ()}},
3988
3942
),
3989
3943
agentcontainers .WithSubAgentClient (fSAC ),
3990
- agentcontainers .WithContainerCLI (mCCLI ),
3991
- agentcontainers .WithDevcontainerCLI (mDCCLI ),
3992
- agentcontainers .WithClock (mClock ),
3944
+ agentcontainers .WithContainerCLI (fCCLI ),
3945
+ agentcontainers .WithDevcontainerCLI (fDCCLI ),
3993
3946
agentcontainers .WithWatcher (watcher .NewNoop ()),
3994
3947
)
3995
3948
api .Start ()
3996
- defer api .Close ()
3949
+ defer func () {
3950
+ close (fDCCLI .readConfigErrC )
3997
3951
3998
- tickerTrap . MustWait ( ctx ). MustRelease ( ctx )
3999
- tickerTrap . Close ()
3952
+ api . Close ( )
3953
+ } ()
4000
3954
4001
3955
// Given: We allow the dev container to be created.
4002
- mDCCLI .EXPECT ().Up (gomock .Any (), testDC .WorkspaceFolder , testDC .ConfigPath , gomock .Any ()).
4003
- Return ("test-container-id" , nil )
4004
-
4005
- mCCLI .EXPECT ().List (gomock .Any ()).Return (codersdk.WorkspaceAgentListContainersResponse {
4006
- Containers : []codersdk.WorkspaceAgentContainer {testContainer },
4007
- }, nil )
4008
-
4009
- gomock .InOrder (
4010
- mCCLI .EXPECT ().DetectArchitecture (gomock .Any (), "test-container-id" ).Return (runtime .GOARCH , nil ),
4011
-
4012
- // Verify claimed workspace environment variables are passed to devcontainer
4013
- mDCCLI .EXPECT ().ReadConfig (gomock .Any (),
4014
- testDC .WorkspaceFolder ,
4015
- testDC .ConfigPath ,
4016
- gomock .Cond (func (envs []string ) bool {
4017
- return slices .Contains (envs , "CODER_WORKSPACE_OWNER_NAME=" + userOwner ) &&
4018
- slices .Contains (envs , "CODER_WORKSPACE_NAME=" + userWorkspace )
4019
- }),
4020
- ).Return (agentcontainers.DevcontainerConfig {
4021
- MergedConfiguration : agentcontainers.DevcontainerMergedConfiguration {
4022
- Customizations : agentcontainers.DevcontainerMergedCustomizations {
4023
- Coder : []agentcontainers.CoderCustomization {
4024
- {
4025
- Apps : []agentcontainers.SubAgentApp {
4026
- {
4027
- Slug : "zed" ,
4028
- URL : userAppURL ,
4029
- },
4030
- },
4031
- },
3956
+ fDCCLI .upID = testContainer .ID
3957
+ fDCCLI .readConfig = agentcontainers.DevcontainerConfig {
3958
+ MergedConfiguration : agentcontainers.DevcontainerMergedConfiguration {
3959
+ Customizations : agentcontainers.DevcontainerMergedCustomizations {
3960
+ Coder : []agentcontainers.CoderCustomization {{
3961
+ Apps : []agentcontainers.SubAgentApp {
3962
+ {Slug : "zed" , URL : userAppURL },
4032
3963
},
4033
- },
3964
+ }} ,
4034
3965
},
4035
- }, nil ),
4036
-
4037
- mCCLI .EXPECT ().ExecAs (gomock .Any (), testContainer .ID , "root" , "mkdir" , "-p" , "/.coder-agent" ).Return (nil , nil ),
4038
- mCCLI .EXPECT ().Copy (gomock .Any (), testContainer .ID , coderBin , "/.coder-agent/coder" ).Return (nil ),
4039
- mCCLI .EXPECT ().ExecAs (gomock .Any (), testContainer .ID , "root" , "chmod" , "0755" , "/.coder-agent" , "/.coder-agent/coder" ).Return (nil , nil ),
4040
- mCCLI .EXPECT ().ExecAs (gomock .Any (), testContainer .ID , "root" , "/bin/sh" , "-c" , "chown $(id -u):$(id -g) /.coder-agent/coder" ).Return (nil , nil ),
3966
+ },
3967
+ }
4041
3968
4042
- // We want to mock how the `Exec` function works when starting an agent. This should
4043
- // run until the given `ctx` is done.
4044
- mDCCLI .EXPECT ().Exec (gomock .Any (),
4045
- testDC .WorkspaceFolder , testDC .ConfigPath ,
4046
- "/.coder-agent/coder" , []string {"agent" }, gomock .Any (), gomock .Any (),
4047
- ).Do (func (ctx context.Context , _ , _ , _ string , _ []string , _ ... agentcontainers.DevcontainerCLIExecOptions ) error {
4048
- <- ctx .Done ()
4049
- return nil
4050
- }),
4051
- )
3969
+ testutil .RequireSend (ctx , t , fDCCLI .readConfigErrC , func (env []string ) error {
3970
+ readConfigEnvVars = env
3971
+ return nil
3972
+ })
4052
3973
4053
3974
// When: We create the dev container resource.
4054
3975
err = api .CreateDevcontainer (testDC .WorkspaceFolder , testDC .ConfigPath )
4055
3976
require .NoError (t , err )
4056
3977
4057
- // Then: We expect there to be only 1 agent.
3978
+ // Then: We expect the environment variables were passed correctly.
3979
+ require .Contains (t , readConfigEnvVars , "CODER_WORKSPACE_OWNER_NAME=" + userOwner )
3980
+ require .Contains (t , readConfigEnvVars , "CODER_WORKSPACE_NAME=" + userWorkspace )
3981
+
3982
+ // And: We expect there to be only 1 agent.
4058
3983
require .Len (t , fSAC .agents , 1 )
4059
3984
4060
3985
// And: We expect _a separate agent_ to have been created.
0 commit comments