Skip to content

Commit

Permalink
Merge pull request #226 from franklinlindemberg/master
Browse files Browse the repository at this point in the history
Added support for copy file to container
  • Loading branch information
gianarb authored Aug 21, 2020
2 parents 3502019 + e3a7202 commit 8d2b4d5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ site/
.direnv/
src/mkdocs-codeinclude-plugin
src/pip-delete-this-directory.txt
.idea/
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Container interface {
NetworkAliases(context.Context) (map[string][]string, error) // get container network aliases for a network
Exec(ctx context.Context, cmd []string) (int, error)
ContainerIP(context.Context) (string, error) // get container ip
CopyFileToContainer(ctx context.Context, hostFilePath string, containerFilePath string, fileMode int64) error
}

// ImageBuildInfo defines what is needed to build an image
Expand Down
28 changes: 28 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testcontainers

import (
"archive/tar"
"bytes"
"context"
"encoding/binary"
Expand All @@ -11,6 +12,7 @@ import (
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -306,6 +308,32 @@ func (c *DockerContainer) Exec(ctx context.Context, cmd []string) (int, error) {
return exitCode, nil
}

func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath string, containerFilePath string, fileMode int64) error {
fileContent, err := ioutil.ReadFile(hostFilePath)
if err != nil {
return err
}

buffer := &bytes.Buffer{}

tw := tar.NewWriter(buffer)
defer tw.Close()

hdr := &tar.Header{
Name: filepath.Base(containerFilePath),
Mode: fileMode,
Size: int64(len(fileContent)),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(fileContent); err != nil {
return err
}

return c.provider.client.CopyToContainer(ctx, c.ID, filepath.Dir(containerFilePath), buffer, types.CopyToContainerOptions{})
}

// StartLogProducer will start a concurrent process that will continuously read logs
// from the container and will send them to each added LogConsumer
func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
Expand Down
24 changes: 24 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,3 +1318,27 @@ func readHostname(t *testing.T, containerId string) string {
config := data["Config"].(map[string]interface{})
return config["Hostname"].(string)
}

func TestDockerContainerCopyFileToContainer(t *testing.T) {
ctx := context.Background()

nginxC, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: "nginx:1.17.6",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
},
Started: true,
})
defer nginxC.Terminate(ctx)

copiedFileName := "hello_copy.sh"
nginxC.CopyFileToContainer(ctx, "./testresources/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)
}
}
19 changes: 19 additions & 0 deletions docs/features/copy_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copy Files To Container

If you would like to copy a file to a container, you can do it using the `CopyFileToContainer` method...

```go
ctx := context.Background()

nginxC, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: "nginx:1.17.6",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
},
Started: true,
})

nginxC.CopyFileToContainer(ctx, "./testresources/hello.sh", "/hello_copy.sh", fileContent, 700)
```

0 comments on commit 8d2b4d5

Please sign in to comment.