From 3a87c214541c89a56f8293f45628c4b7bd3507f7 Mon Sep 17 00:00:00 2001 From: Fergal Kearns Date: Sat, 4 Nov 2023 13:37:49 +0000 Subject: [PATCH 1/2] feat: Allow the container working directory to be specified in ContainerRequest --- container.go | 1 + docker.go | 1 + docker_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/container.go b/container.go index ad524f94d0..22c73beb35 100644 --- a/container.go +++ b/container.go @@ -136,6 +136,7 @@ type ContainerRequest struct { HostConfigModifier func(*container.HostConfig) // Modifier for the host config before container creation EnpointSettingsModifier func(map[string]*network.EndpointSettings) // Modifier for the network settings before container creation LifecycleHooks []ContainerLifecycleHooks // define hooks to be executed during container lifecycle + WorkingDir string // specify the working directory of the container } // containerOptions functional options for a container diff --git a/docker.go b/docker.go index 4527067753..cc84b31fc3 100644 --- a/docker.go +++ b/docker.go @@ -999,6 +999,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque Cmd: req.Cmd, Hostname: req.Hostname, User: req.User, + WorkingDir: req.WorkingDir, } hostConfig := &container.HostConfig{ diff --git a/docker_test.go b/docker_test.go index b1ef1c2c81..e66dfdaf76 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1059,6 +1059,34 @@ func TestEntrypoint(t *testing.T) { terminateContainerOnEnd(t, ctx, c) } +func TestWorkingDir(t *testing.T) { + /* + print the current working directory to ensure that + we can specify working directory in the + ContainerRequest and it will be used for the container + */ + + ctx := context.Background() + + req := ContainerRequest{ + Image: "docker.io/alpine", + WaitingFor: wait.ForAll( + wait.ForLog("/var/tmp/test"), + ), + Entrypoint: []string{"pwd"}, + WorkingDir: "/var/tmp/test", + } + + c, err := GenericContainer(ctx, GenericContainerRequest{ + ProviderType: providerType, + ContainerRequest: req, + Started: true, + }) + + require.NoError(t, err) + terminateContainerOnEnd(t, ctx, c) +} + func ExampleDockerProvider_CreateContainer() { ctx := context.Background() req := ContainerRequest{ From 154b7208a51e8fbdde66e22a930216aa23100b6a Mon Sep 17 00:00:00 2001 From: Fergal Kearns Date: Mon, 6 Nov 2023 12:51:30 +0000 Subject: [PATCH 2/2] move WorkingDir field position in ContainerRequest struct --- container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container.go b/container.go index 22c73beb35..df05d1d246 100644 --- a/container.go +++ b/container.go @@ -114,6 +114,7 @@ type ContainerRequest struct { WaitingFor wait.Strategy Name string // for specifying container name Hostname string + WorkingDir string // specify the working directory of the container ExtraHosts []string // Deprecated: Use HostConfigModifier instead Privileged bool // For starting privileged container Networks []string // for specifying network names @@ -136,7 +137,6 @@ type ContainerRequest struct { HostConfigModifier func(*container.HostConfig) // Modifier for the host config before container creation EnpointSettingsModifier func(map[string]*network.EndpointSettings) // Modifier for the network settings before container creation LifecycleHooks []ContainerLifecycleHooks // define hooks to be executed during container lifecycle - WorkingDir string // specify the working directory of the container } // containerOptions functional options for a container