Skip to content

Commit

Permalink
Changed siganture to return a ReadCloser
Browse files Browse the repository at this point in the history
  • Loading branch information
codepitbull committed Aug 24, 2021
1 parent 8bc1332 commit 3792295
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +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) ([]byte, error)
CopyFileFromContainer(ctx context.Context, filePath string) (io.ReadCloser, error)
}

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

func (c *DockerContainer) CopyFileFromContainer(ctx context.Context, filePath string) ([]byte, error) {
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
}
defer r.Close()
tarReader := tar.NewReader(r)

tr := tar.NewReader(r)
for {
_, err := tr.Next()
if err == io.EOF {
// end of tar archive
return []byte{}, nil
}
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
buf.ReadFrom(tr)
//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
}

return buf.Bytes(), nil
ret := &FileFromContainer{
underlying: &r,
tarreader: tarReader,
}

return ret, nil
}

func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath string, containerFilePath string, fileMode int64) error {
Expand Down
42 changes: 41 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1482,13 +1482,53 @@ func TestDockerContainerCopyFileFromContainer(t *testing.T) {
t.Fatalf("File %s should exist, expected return code 0, got %v", copiedFileName, c)
}

fileContentFromContainer, err := nginxC.CopyFileFromContainer(ctx, "/"+copiedFileName)
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.

0 comments on commit 3792295

Please sign in to comment.