-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add generic test-container code for re-use, migrate mysql (#11157)
- Loading branch information
1 parent
a0da3e3
commit f321c7f
Showing
6 changed files
with
154 additions
and
14 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,72 @@ | ||
//go:build !freebsd | ||
// +build !freebsd | ||
|
||
package testutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/docker/go-connections/nat" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
type Container struct { | ||
Image string | ||
Env map[string]string | ||
ExposedPorts []string | ||
WaitingFor wait.Strategy | ||
|
||
Address string | ||
Port string | ||
|
||
container testcontainers.Container | ||
ctx context.Context | ||
} | ||
|
||
func (c *Container) Start() error { | ||
c.ctx = context.Background() | ||
|
||
req := testcontainers.GenericContainerRequest{ | ||
ContainerRequest: testcontainers.ContainerRequest{ | ||
Image: c.Image, | ||
Env: c.Env, | ||
ExposedPorts: c.ExposedPorts, | ||
WaitingFor: c.WaitingFor, | ||
}, | ||
Started: true, | ||
} | ||
|
||
container, err := testcontainers.GenericContainer(c.ctx, req) | ||
if err != nil { | ||
return fmt.Errorf("container failed to start: %s", err) | ||
} | ||
c.container = container | ||
|
||
c.Address, err = c.container.Host(c.ctx) | ||
if err != nil { | ||
return fmt.Errorf("container host address failed: %s", err) | ||
} | ||
|
||
// assume the first port is the one the test will connect to | ||
// additional ports can be used for the waiting for section | ||
if len(c.ExposedPorts) > 0 { | ||
p, err := c.container.MappedPort(c.ctx, nat.Port(c.ExposedPorts[0])) | ||
if err != nil { | ||
return fmt.Errorf("container host port failed: %s", err) | ||
} | ||
c.Port = p.Port() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Container) Terminate() error { | ||
err := c.container.Terminate(c.ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to terminate the container: %s", err) | ||
} | ||
|
||
return nil | ||
} |
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,36 @@ | ||
package testutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEmptyContainer(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping integration test in short mode") | ||
} | ||
|
||
container := Container{ | ||
Image: "docksal/empty", | ||
} | ||
|
||
err := container.Start() | ||
require.NoError(t, err) | ||
|
||
err = container.Terminate() | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestBadImageName(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping integration test in short mode") | ||
} | ||
|
||
container := Container{ | ||
Image: "fAk3-n4mE", | ||
} | ||
|
||
err := container.Start() | ||
require.Error(t, err) | ||
} |