Skip to content

Commit

Permalink
fix: make reusable container tests Podman compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
prskr committed Jul 12, 2022
1 parent 009b4b7 commit b80f3c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
8 changes: 3 additions & 5 deletions docker.go
Expand Up @@ -1042,10 +1042,8 @@ func (p *DockerProvider) findContainerByName(ctx context.Context, name string) (
if name == "" {
return nil, nil
}
filter := filters.NewArgs(filters.KeyValuePair{
Key: "name",
Value: name,
})

filter := filters.NewArgs(filters.Arg("name", name))
containers, err := p.client.ContainerList(ctx, types.ContainerListOptions{Filters: filter})
if err != nil {
return nil, err
Expand Down Expand Up @@ -1089,8 +1087,8 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
logger: p.Logger,
isRunning: c.State == "running",
}
return dc, nil

return dc, nil
}

// attemptToPullImage tries to pull the image while respecting the ctx cancellations.
Expand Down
53 changes: 34 additions & 19 deletions generic_test.go
Expand Up @@ -2,7 +2,7 @@ package testcontainers

import (
"context"
"strings"
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -18,17 +18,19 @@ func TestGenericReusableContainer(t *testing.T) {
ctx := context.Background()

n1, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: "nginx:1.17.6",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
Name: reusableContainerName,
},
Started: true,
})
require.NoError(t, err)
require.True(t, n1.IsRunning())
defer n1.Terminate(ctx)

terminateContainerOnEnd(t, ctx, n1)

copiedFileName := "hello_copy.sh"
err = n1.CopyFileToContainer(ctx, "./testresources/hello.sh", "/"+copiedFileName, 700)
Expand All @@ -37,48 +39,61 @@ func TestGenericReusableContainer(t *testing.T) {
tests := []struct {
name string
containerName string
errMsg string
errorMatcher func(err error) error
reuseOption bool
}{
{
name: "reuse option with empty name",
errMsg: ErrReuseEmptyName.Error(),
name: "reuse option with empty name",
errorMatcher: func(err error) error {
if errors.Is(err, ErrReuseEmptyName) {
return nil
}
return err
},
reuseOption: true,
},
{
name: "container already exists (reuse=false)",
containerName: reusableContainerName,
errMsg: "is already in use by container",
reuseOption: false,
errorMatcher: func(err error) error {
if err == nil {
return errors.New("expected error but got none")
}
return nil
},
reuseOption: false,
},
{
name: "success reusing",
containerName: reusableContainerName,
reuseOption: true,
errorMatcher: func(err error) error {
return err
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
n2, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: "nginx:1.17.6",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
Name: tc.containerName,
},
Started: true,
Reuse: tc.reuseOption,
})
if tc.errMsg == "" {
c, _, err := n2.Exec(ctx, []string{"bash", copiedFileName})

require.NoError(t, tc.errorMatcher(err))

if err == nil {
c, _, err := n2.Exec(ctx, []string{"/bin/ash", copiedFileName})
require.NoError(t, err)
require.Zero(t, c)
} else {
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), tc.errMsg))
}
})
}

}

0 comments on commit b80f3c4

Please sign in to comment.