Skip to content

Commit 2db96b0

Browse files
refactor: use fakes instead of mocks
1 parent 2b97f35 commit 2db96b0

File tree

1 file changed

+55
-130
lines changed

1 file changed

+55
-130
lines changed

agent/agentcontainers/api_test.go

Lines changed: 55 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,16 +3827,12 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
38273827
}
38283828

38293829
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)
38343832

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{}
38403836

38413837
testDC = codersdk.WorkspaceAgentDevcontainer{
38423838
ID: uuid.New(),
@@ -3855,17 +3851,12 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
38553851
userAppURL = "user.zed"
38563852
)
38573853

3858-
coderBin, err := os.Executable()
3859-
require.NoError(t, err)
3860-
38613854
// ==================================================
38623855
// PHASE 1: Prebuild workspace creates devcontainer
38633856
// ==================================================
38643857

38653858
// 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{}
38693860

38703861
api := agentcontainers.NewAPI(logger,
38713862
// We want this first `agentcontainers.API` to have a manifest info
@@ -3877,72 +3868,43 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
38773868
[]codersdk.WorkspaceAgentScript{{ID: testDC.ID, LogSourceID: uuid.New()}},
38783869
),
38793870
agentcontainers.WithSubAgentClient(fSAC),
3880-
agentcontainers.WithContainerCLI(mCCLI),
3881-
agentcontainers.WithDevcontainerCLI(mDCCLI),
3882-
agentcontainers.WithClock(mClock),
3871+
agentcontainers.WithContainerCLI(fCCLI),
3872+
agentcontainers.WithDevcontainerCLI(fDCCLI),
38833873
agentcontainers.WithWatcher(watcher.NewNoop()),
38843874
)
38853875
api.Start()
38863876

3887-
tickerTrap.MustWait(ctx).MustRelease(ctx)
3888-
tickerTrap.Close()
3877+
fCCLI.containers = codersdk.WorkspaceAgentListContainersResponse{
3878+
Containers: []codersdk.WorkspaceAgentContainer{testContainer},
3879+
}
38893880

38903881
// 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},
39213889
},
3922-
},
3890+
}},
39233891
},
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+
}
39303894

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+
})
39413900

39423901
// When: We create the dev container resource
3943-
err = api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath)
3902+
err := api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath)
39443903
require.NoError(t, err)
39453904

3905+
require.Contains(t, readConfigEnvVars, "CODER_WORKSPACE_OWNER_NAME="+prebuildOwner)
3906+
require.Contains(t, readConfigEnvVars, "CODER_WORKSPACE_NAME="+prebuildWorkspace)
3907+
39463908
// Then: We there to be only 1 agent.
39473909
require.Len(t, fSAC.agents, 1)
39483910

@@ -3968,14 +3930,6 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
39683930
// PHASE 2: User claims workspace, devcontainer should be reused
39693931
// =============================================================
39703932

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-
39793933
// Given: We create a new claimed API
39803934
api = agentcontainers.NewAPI(logger,
39813935
// We want this second `agentcontainers.API` to have a manifest info
@@ -3987,74 +3941,45 @@ func TestDevcontainerPrebuildSupport(t *testing.T) {
39873941
[]codersdk.WorkspaceAgentScript{{ID: testDC.ID, LogSourceID: uuid.New()}},
39883942
),
39893943
agentcontainers.WithSubAgentClient(fSAC),
3990-
agentcontainers.WithContainerCLI(mCCLI),
3991-
agentcontainers.WithDevcontainerCLI(mDCCLI),
3992-
agentcontainers.WithClock(mClock),
3944+
agentcontainers.WithContainerCLI(fCCLI),
3945+
agentcontainers.WithDevcontainerCLI(fDCCLI),
39933946
agentcontainers.WithWatcher(watcher.NewNoop()),
39943947
)
39953948
api.Start()
3996-
defer api.Close()
3949+
defer func() {
3950+
close(fDCCLI.readConfigErrC)
39973951

3998-
tickerTrap.MustWait(ctx).MustRelease(ctx)
3999-
tickerTrap.Close()
3952+
api.Close()
3953+
}()
40003954

40013955
// 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},
40323963
},
4033-
},
3964+
}},
40343965
},
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+
}
40413968

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+
})
40523973

40533974
// When: We create the dev container resource.
40543975
err = api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath)
40553976
require.NoError(t, err)
40563977

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.
40583983
require.Len(t, fSAC.agents, 1)
40593984

40603985
// And: We expect _a separate agent_ to have been created.

0 commit comments

Comments
 (0)