forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (postgres): support for creating and restoring Snapshots (testco…
…ntainers#2199) * Add tips and examples to the documentation I recently went through building a complex testing setup for an entire webservice using testcontainers-go, which taught me some important tips and tricks about the library. I have added these in the documentation wherever I thought they could be useful. I also added a new example for using the postgres module and making each test use a clean database. It's a pretty useful tool, and it's available as an example in the Java version, so I've added it here as well. * Added the example to the sidebar * Move everything to the module * Remove testing stuff I added to debug the integration * Apply review feedback and rename reset to restore since it makes more sense in context * Update modules/postgres/postgres.go Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com> * Add test examples for file copy and update the docs * Add the wait for hello file to help understanding the example * chore: use pinned version of the image * chore: simplify waiting for an expected log * chore: check for errors in tests * docs: use dot * docs: simplify tabs * chore: simplify variables and paths * docs: remove misleading comment * docs: move customise requests to each module * docs: swap places * chore: less verbose * chore: use testify assertions * Revert "chore: use testify assertions" This reverts commit b7d6e90. --------- Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com> Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
- Loading branch information
1 parent
54b1d8a
commit 48fc228
Showing
12 changed files
with
528 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
package testcontainers | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
func TestCopyFileToContainer(t *testing.T) { | ||
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cnl() | ||
|
||
// copyFileOnCreate { | ||
absPath, err := filepath.Abs(filepath.Join(".", "testdata", "hello.sh")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
container, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "docker.io/bash", | ||
Files: []ContainerFile{ | ||
{ | ||
HostFilePath: absPath, | ||
ContainerFilePath: "/hello.sh", | ||
FileMode: 0o700, | ||
}, | ||
}, | ||
Cmd: []string{"bash", "/hello.sh"}, | ||
WaitingFor: wait.ForLog("done"), | ||
}, | ||
Started: true, | ||
}) | ||
// } | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, container.Terminate(ctx)) | ||
} | ||
|
||
func TestCopyFileToRunningContainer(t *testing.T) { | ||
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cnl() | ||
|
||
// Not using the assertations here to avoid leaking the library into the example | ||
// copyFileAfterCreate { | ||
waitForPath, err := filepath.Abs(filepath.Join(".", "testdata", "waitForHello.sh")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
helloPath, err := filepath.Abs(filepath.Join(".", "testdata", "hello.sh")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
container, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "docker.io/bash:5.2.26", | ||
Files: []ContainerFile{ | ||
{ | ||
HostFilePath: waitForPath, | ||
ContainerFilePath: "/waitForHello.sh", | ||
FileMode: 0o700, | ||
}, | ||
}, | ||
Cmd: []string{"bash", "/waitForHello.sh"}, | ||
}, | ||
Started: true, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = container.CopyFileToContainer(ctx, helloPath, "/scripts/hello.sh", 0o700) | ||
// } | ||
|
||
require.NoError(t, err) | ||
|
||
// Give some time to the wait script to catch the hello script being created | ||
err = wait.ForLog("done").WithStartupTimeout(200*time.Millisecond).WaitUntilReady(ctx, container) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, container.Terminate(ctx)) | ||
} | ||
|
||
func TestCopyDirectoryToContainer(t *testing.T) { | ||
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cnl() | ||
|
||
// Not using the assertations here to avoid leaking the library into the example | ||
// copyDirectoryToContainer { | ||
dataDirectory, err := filepath.Abs(filepath.Join(".", "testdata")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
container, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "docker.io/bash", | ||
Files: []ContainerFile{ | ||
{ | ||
HostFilePath: dataDirectory, | ||
// ContainerFile cannot create the parent directory, so we copy the scripts | ||
// to the root of the container instead. Make sure to create the container directory | ||
// before you copy a host directory on create. | ||
ContainerFilePath: "/", | ||
FileMode: 0o700, | ||
}, | ||
}, | ||
Cmd: []string{"bash", "/testdata/hello.sh"}, | ||
WaitingFor: wait.ForLog("done"), | ||
}, | ||
Started: true, | ||
}) | ||
// } | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, container.Terminate(ctx)) | ||
} | ||
|
||
func TestCopyDirectoryToRunningContainerAsFile(t *testing.T) { | ||
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cnl() | ||
|
||
// copyDirectoryToRunningContainerAsFile { | ||
dataDirectory, err := filepath.Abs(filepath.Join(".", "testdata")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
waitForPath, err := filepath.Abs(filepath.Join(dataDirectory, "waitForHello.sh")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
container, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "docker.io/bash", | ||
Files: []ContainerFile{ | ||
{ | ||
HostFilePath: waitForPath, | ||
ContainerFilePath: "/waitForHello.sh", | ||
FileMode: 0o700, | ||
}, | ||
}, | ||
Cmd: []string{"bash", "/waitForHello.sh"}, | ||
}, | ||
Started: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// as the container is started, we can create the directory first | ||
_, _, err = container.Exec(ctx, []string{"mkdir", "-p", "/scripts"}) | ||
require.NoError(t, err) | ||
|
||
// because the container path is a directory, it will use the copy dir method as fallback | ||
err = container.CopyFileToContainer(ctx, dataDirectory, "/scripts", 0o700) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// } | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, container.Terminate(ctx)) | ||
} | ||
|
||
func TestCopyDirectoryToRunningContainerAsDir(t *testing.T) { | ||
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cnl() | ||
|
||
// Not using the assertations here to avoid leaking the library into the example | ||
// copyDirectoryToRunningContainerAsDir { | ||
waitForPath, err := filepath.Abs(filepath.Join(".", "testdata", "waitForHello.sh")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dataDirectory, err := filepath.Abs(filepath.Join(".", "testdata")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
container, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "docker.io/bash", | ||
Files: []ContainerFile{ | ||
{ | ||
HostFilePath: waitForPath, | ||
ContainerFilePath: "/waitForHello.sh", | ||
FileMode: 0o700, | ||
}, | ||
}, | ||
Cmd: []string{"bash", "/waitForHello.sh"}, | ||
}, | ||
Started: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// as the container is started, we can create the directory first | ||
_, _, err = container.Exec(ctx, []string{"mkdir", "-p", "/scripts"}) | ||
require.NoError(t, err) | ||
|
||
err = container.CopyDirToContainer(ctx, dataDirectory, "/scripts", 0o700) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// } | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, container.Terminate(ctx)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.