diff --git a/docker.go b/docker.go index 252d94ec7c..1aa41e5a30 100644 --- a/docker.go +++ b/docker.go @@ -603,7 +603,7 @@ func (c *DockerContainer) CopyToContainer(ctx context.Context, fileContent []byt return err } - err = c.provider.client.CopyToContainer(ctx, c.ID, filepath.Dir(containerFilePath), buffer, types.CopyToContainerOptions{}) + err = c.provider.client.CopyToContainer(ctx, c.ID, "/", buffer, types.CopyToContainerOptions{}) if err != nil { return err } diff --git a/docker_test.go b/docker_test.go index 80fb02e3df..fccac32612 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1335,27 +1335,44 @@ func readHostname(tb testing.TB, containerId string) string { func TestDockerContainerCopyFileToContainer(t *testing.T) { ctx := context.Background() - nginxC, err := GenericContainer(ctx, GenericContainerRequest{ - ProviderType: providerType, - ContainerRequest: ContainerRequest{ - Image: nginxImage, - ExposedPorts: []string{nginxDefaultPort}, - WaitingFor: wait.ForListeningPort(nginxDefaultPort), + tests := []struct { + name string + copiedFileName string + }{ + { + name: "success copy", + copiedFileName: "/hello_copy.sh", }, - Started: true, - }) + { + name: "success copy with dir", + copiedFileName: "/test/hello_copy.sh", + }, + } - require.NoError(t, err) - terminateContainerOnEnd(t, ctx, nginxC) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + nginxC, err := GenericContainer(ctx, GenericContainerRequest{ + ProviderType: providerType, + ContainerRequest: ContainerRequest{ + Image: nginxImage, + ExposedPorts: []string{nginxDefaultPort}, + WaitingFor: wait.ForListeningPort(nginxDefaultPort), + }, + Started: true, + }) - copiedFileName := "hello_copy.sh" - _ = nginxC.CopyFileToContainer(ctx, filepath.Join(".", "testdata", "hello.sh"), "/"+copiedFileName, 700) - c, _, err := nginxC.Exec(ctx, []string{"bash", copiedFileName}) - if err != nil { - t.Fatal(err) - } - if c != 0 { - t.Fatalf("File %s should exist, expected return code 0, got %v", copiedFileName, c) + require.NoError(t, err) + terminateContainerOnEnd(t, ctx, nginxC) + + _ = nginxC.CopyFileToContainer(ctx, filepath.Join(".", "testdata", "hello.sh"), tc.copiedFileName, 700) + c, _, err := nginxC.Exec(ctx, []string{"bash", tc.copiedFileName}) + if err != nil { + t.Fatal(err) + } + if c != 0 { + t.Fatalf("File %s should exist, expected return code 0, got %v", tc.copiedFileName, c) + } + }) } } @@ -1532,32 +1549,51 @@ func TestDockerCreateContainerWithDirs(t *testing.T) { func TestDockerContainerCopyToContainer(t *testing.T) { ctx := context.Background() - nginxC, err := GenericContainer(ctx, GenericContainerRequest{ - ProviderType: providerType, - ContainerRequest: ContainerRequest{ - Image: nginxImage, - ExposedPorts: []string{nginxDefaultPort}, - WaitingFor: wait.ForListeningPort(nginxDefaultPort), + tests := []struct { + name string + copiedFileName string + }{ + { + name: "success copy", + copiedFileName: "hello_copy.sh", }, - Started: true, - }) + { + name: "success copy with dir", + copiedFileName: "/test/hello_copy.sh", + }, + } - require.NoError(t, err) - terminateContainerOnEnd(t, ctx, nginxC) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + nginxC, err := GenericContainer(ctx, GenericContainerRequest{ + ProviderType: providerType, + ContainerRequest: ContainerRequest{ + Image: nginxImage, + ExposedPorts: []string{nginxDefaultPort}, + WaitingFor: wait.ForListeningPort(nginxDefaultPort), + }, + Started: true, + }) - copiedFileName := "hello_copy.sh" + require.NoError(t, err) + terminateContainerOnEnd(t, ctx, nginxC) - fileContent, err := os.ReadFile(filepath.Join(".", "testdata", "hello.sh")) - if err != nil { - t.Fatal(err) - } - _ = nginxC.CopyToContainer(ctx, fileContent, "/"+copiedFileName, 700) - c, _, err := nginxC.Exec(ctx, []string{"bash", copiedFileName}) - if err != nil { - t.Fatal(err) - } - if c != 0 { - t.Fatalf("File %s should exist, expected return code 0, got %v", copiedFileName, c) + fileContent, err := os.ReadFile(filepath.Join(".", "testdata", "hello.sh")) + if err != nil { + t.Fatal(err) + } + err = nginxC.CopyToContainer(ctx, fileContent, tc.copiedFileName, 700) + if err != nil { + t.Fatal(err) + } + c, _, err := nginxC.Exec(ctx, []string{"bash", tc.copiedFileName}) + if err != nil { + t.Fatal(err) + } + if c != 0 { + t.Fatalf("File %s should exist, expected return code 0, got %v", tc.copiedFileName, c) + } + }) } } diff --git a/file.go b/file.go index 509da643d4..76844bd6d7 100644 --- a/file.go +++ b/file.go @@ -117,7 +117,7 @@ func tarFile(fileContent []byte, basePath string, fileMode int64) (*bytes.Buffer tw := tar.NewWriter(zr) hdr := &tar.Header{ - Name: filepath.Base(basePath), + Name: basePath, Mode: fileMode, Size: int64(len(fileContent)), }