Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copy files to the container and create directories as needed upon request #2049

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
117 changes: 77 additions & 40 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1335,27 +1335,45 @@
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) {

Check failure on line 1354 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest) / ./ubuntu-latest/1.20.x

File is not `gofumpt`-ed (gofumpt)
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)
nginxC, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
},
Started: true,
})

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

Expand Down Expand Up @@ -1532,32 +1550,51 @@
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)
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down