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.
Merge pull request #464 from VladimirStepanov/issue-439-use-existing-…
…container Issue 439: use an existing container
- Loading branch information
Showing
5 changed files
with
232 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package testcontainers | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
reusableContainerName = "my_test_reusable_container" | ||
) | ||
|
||
func TestGenericReusableContainer(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
n1, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "nginx:1.17.6", | ||
ExposedPorts: []string{"80/tcp"}, | ||
WaitingFor: wait.ForListeningPort("80/tcp"), | ||
Name: reusableContainerName, | ||
}, | ||
Started: true, | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, n1.IsRunning()) | ||
defer n1.Terminate(ctx) | ||
|
||
copiedFileName := "hello_copy.sh" | ||
err = n1.CopyFileToContainer(ctx, "./testresources/hello.sh", "/"+copiedFileName, 700) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
containerName string | ||
errMsg string | ||
reuseOption bool | ||
}{ | ||
{ | ||
name: "reuse option with empty name", | ||
errMsg: ErrReuseEmptyName.Error(), | ||
reuseOption: true, | ||
}, | ||
{ | ||
name: "container already exists (reuse=false)", | ||
containerName: reusableContainerName, | ||
errMsg: "is already in use by container", | ||
reuseOption: false, | ||
}, | ||
{ | ||
name: "success reusing", | ||
containerName: reusableContainerName, | ||
reuseOption: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
n2, err := GenericContainer(ctx, GenericContainerRequest{ | ||
ContainerRequest: ContainerRequest{ | ||
Image: "nginx:1.17.6", | ||
ExposedPorts: []string{"80/tcp"}, | ||
WaitingFor: wait.ForListeningPort("80/tcp"), | ||
Name: tc.containerName, | ||
}, | ||
Started: true, | ||
Reuse: tc.reuseOption, | ||
}) | ||
if tc.errMsg == "" { | ||
c, _, err := n2.Exec(ctx, []string{"bash", copiedFileName}) | ||
require.NoError(t, err) | ||
require.Zero(t, c) | ||
} else { | ||
require.Error(t, err) | ||
require.True(t, strings.Contains(err.Error(), tc.errMsg)) | ||
} | ||
}) | ||
} | ||
|
||
} |