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

Added CopyFileFromContainer to DockerContainer #347

Merged
merged 3 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Container interface {
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
CopyFileFromContainer(ctx context.Context, filePath string) (io.ReadCloser, error)
}

// ImageBuildInfo defines what is needed to build an image
Expand Down
35 changes: 35 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,41 @@ func (c *DockerContainer) Exec(ctx context.Context, cmd []string) (int, error) {
return exitCode, nil
}

type FileFromContainer struct {
underlying *io.ReadCloser
tarreader *tar.Reader
}

func (fc *FileFromContainer) Read(b []byte) (int, error) {
return (*fc.tarreader).Read(b)
}

func (fc *FileFromContainer) Close() error {
return (*fc.underlying).Close()
}

func (c *DockerContainer) CopyFileFromContainer(ctx context.Context, filePath string) (io.ReadCloser, error) {
r, _, err := c.provider.client.CopyFromContainer(ctx, c.ID, filePath)
if err != nil {
return nil, err
}
tarReader := tar.NewReader(r)

//if we got here we have exactly one file in the TAR-stream
//so we advance the index by one so the next call to Read will start reading it
_, err = tarReader.Next()
if err != nil {
return nil, err
}

ret := &FileFromContainer{
underlying: &r,
tarreader: tarReader,
}

return ret, nil
}

func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath string, containerFilePath string, fileMode int64) error {
fileContent, err := ioutil.ReadFile(hostFilePath)
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,80 @@ func TestDockerContainerCopyFileToContainer(t *testing.T) {
}
}

func TestDockerContainerCopyFileFromContainer(t *testing.T) {
fileContent, err := ioutil.ReadFile("./testresources/hello.sh")
if err != nil {
t.Fatal(err)
}
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)
}

reader, err := nginxC.CopyFileFromContainer(ctx, "/"+copiedFileName)
if err != nil {
t.Fatal(err)
}

fileContentFromContainer, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, fileContent, fileContentFromContainer)
}

func TestDockerContainerCopyEmptyFileFromContainer(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/empty.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)
}

reader, err := nginxC.CopyFileFromContainer(ctx, "/"+copiedFileName)
if err != nil {
t.Fatal(err)
}

fileContentFromContainer, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
assert.Empty(t, fileContentFromContainer)
}

func TestContainerWithReaperNetwork(t *testing.T) {
ctx := context.Background()
networks := []string{
Expand Down
Empty file added testresources/empty.sh
Empty file.