-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
Changes from 6 commits
08c0b48
d5ae599
1ca2158
88c7fb9
09b0430
635892e
e46cbf4
51c8e42
e3a7202
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ site/ | |
.direnv/ | ||
src/mkdocs-codeinclude-plugin | ||
src/pip-delete-this-directory.txt | ||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"path/filepath" | ||
"testing" | ||
|
@@ -1267,3 +1268,31 @@ func TestContainerNonExistentImage(t *testing.T) { | |
}) | ||
|
||
} | ||
|
||
func TestDockerContainerCopyFileToContainer(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} |
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) | ||
``` | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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