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 support for copy file to container #226

Merged
merged 9 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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 .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, containerPath string, fileName string, fileContent []byte, fileMode int64) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about not exposing the fileContent in method header, and obtain it internally? That would simplify clients code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds great, do you think a single parameter like filePath, meaning the filePath on the client side, and from it we can extract the filename to save in the container?

or maybe have a containerFilePath (instead of containerPath) and from it extract the name to save in the container?

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the first one more: containerFilePath and hostFilePath. I'd get the file name from the host file, using it in the container too. Wdyt?

Besides that, what are the Java folks doing? (I'm currently on the phone)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In java the method receives a Transferable (I imagine that this is the content in bytes) and the filepath that we want in the container (path + filename). https:/testcontainers/testcontainers-java/blob/6bdbc8ffe257fc879791eb7fddcf3b5ecccdd52d/core/src/main/java/org/testcontainers/containers/ContainerState.java#L262

Regarding the suggested solution, I think it would be better to use the filename from the container one, this way users will be able to copy the file to the container and at the same time rename it, its more flexible. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the interface, let me know if it sounds good to you

}

// ImageBuildInfo defines what is needed to build an image
Expand Down
22 changes: 22 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 Down Expand Up @@ -306,6 +307,27 @@ func (c *DockerContainer) Exec(ctx context.Context, cmd []string) (int, error) {
return exitCode, nil
}

func (c *DockerContainer) CopyFileToContainer(ctx context.Context, containerPath string, fileName string, fileContent []byte, fileMode int64) error {
buffer := &bytes.Buffer{}

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

hdr := &tar.Header{
Name: fileName,
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, containerPath, 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
29 changes: 29 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"testing"
Expand Down Expand Up @@ -1267,3 +1268,31 @@ func TestContainerNonExistentImage(t *testing.T) {
})

}

func TestDockerContainerCopyFileToContainer(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a single test using CopyFileToContainer since it uses already CopyToContainer underneath

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)

fileContent, err := ioutil.ReadFile("./testresources/hello.sh")
if err != nil {
t.Fatal(err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With above explanation, this would be a matter of the internals of the copy method, simplifying client code's life

copiedFilePath := "hello_copy.sh"
nginxC.CopyFileToContainer(ctx, "/", copiedFilePath, fileContent, 700)
c, err := nginxC.Exec(ctx, []string{"bash", copiedFilePath})
if err != nil {
t.Fatal(err)
}
if c != 0 {
t.Fatalf("File %s should exist, expected return code 0, got %v", copiedFilePath, c)
}
}
23 changes: 23 additions & 0 deletions docs/features/copy_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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,
})

fileContent, err := ioutil.ReadFile("./testresources/hello.sh")
if err != nil {
t.Fatal(err)
}
nginxC.CopyFileToContainer(ctx, "/", "hello_copy.sh", fileContent, 700)
```