Skip to content

Commit

Permalink
feat: copy files to the container and create directories as needed up…
Browse files Browse the repository at this point in the history
…on request
  • Loading branch information
stillya committed Dec 24, 2023
1 parent 9060ff7 commit f9dfe34
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
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{})
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 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) {

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

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

0 comments on commit f9dfe34

Please sign in to comment.