Skip to content

Commit

Permalink
chore: support returning the dynamic port in the helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Apr 19, 2024
1 parent 7aad6e1 commit 05e78f6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
31 changes: 16 additions & 15 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func TestDockerComposeAPI(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -35,7 +35,7 @@ func TestDockerComposeAPI(t *testing.T) {
}

func TestDockerComposeAPIStrategyForInvalidService(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -61,7 +61,7 @@ func TestDockerComposeAPIStrategyForInvalidService(t *testing.T) {
}

func TestDockerComposeAPIWithWaitLogStrategy(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -86,7 +86,7 @@ func TestDockerComposeAPIWithWaitLogStrategy(t *testing.T) {
}

func TestDockerComposeAPIWithRunServices(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -113,7 +113,7 @@ func TestDockerComposeAPIWithRunServices(t *testing.T) {
}

func TestDockerComposeAPI_TestcontainersLabelsArePresent(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand Down Expand Up @@ -158,7 +158,7 @@ func TestDockerComposeAPI_WithReaper(t *testing.T) {
t.Skip("Ryuk is disabled, skipping test")
}

path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand Down Expand Up @@ -187,7 +187,7 @@ func TestDockerComposeAPI_WithoutReaper(t *testing.T) {
t.Skip("Ryuk is enabled, skipping test")
}

path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")
t.Cleanup(func() {
Expand All @@ -212,7 +212,7 @@ func TestDockerComposeAPI_WithoutReaper(t *testing.T) {
}

func TestDockerComposeAPIWithStopServices(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerComposeWith(
WithStackFiles(path),
WithLogger(testcontainers.TestLogger(t)))
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestDockerComposeAPIWithStopServices(t *testing.T) {
}

func TestDockerComposeAPIWithWaitForService(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -276,7 +276,7 @@ func TestDockerComposeAPIWithWaitForService(t *testing.T) {
}

func TestDockerComposeAPIWithWaitHTTPStrategy(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand Down Expand Up @@ -354,7 +354,7 @@ func TestDockerComposeAPIWithWaitStrategy_NoExposedPorts(t *testing.T) {
}

func TestDockerComposeAPIWithMultipleWaitStrategies(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -380,7 +380,7 @@ func TestDockerComposeAPIWithMultipleWaitStrategies(t *testing.T) {
}

func TestDockerComposeAPIWithFailedStrategy(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand Down Expand Up @@ -409,7 +409,7 @@ func TestDockerComposeAPIWithFailedStrategy(t *testing.T) {
}

func TestDockerComposeAPIComplex(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
require.NoError(t, err, "NewDockerCompose()")

Expand All @@ -432,7 +432,7 @@ func TestDockerComposeAPIComplex(t *testing.T) {
func TestDockerComposeAPIWithEnvironment(t *testing.T) {
identifier := testNameHash(t.Name())

path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)

compose, err := NewDockerComposeWith(WithStackFiles(path), identifier)
require.NoError(t, err, "NewDockerCompose()")
Expand Down Expand Up @@ -464,8 +464,9 @@ func TestDockerComposeAPIWithEnvironment(t *testing.T) {
}

func TestDockerComposeAPIWithMultipleComposeFiles(t *testing.T) {
simple, _ := RenderComposeSimple(t)
composeFiles := ComposeStackFiles{
RenderComposeSimple(t),
simple,
RenderComposePostgres(t),
RenderComposeOverride(t),
}
Expand Down
11 changes: 7 additions & 4 deletions modules/compose/compose_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ const (
testdataPackage = "testdata"
)

func RenderComposeComplex(t *testing.T) string {
func RenderComposeComplex(t *testing.T) (string, []int) {
t.Helper()

return writeTemplate(t, "docker-compose-complex.yml", getFreePort(t), getFreePort(t))
ports := []int{getFreePort(t), getFreePort(t)}

return writeTemplate(t, "docker-compose-complex.yml", ports...), ports
}

func RenderComposeOverride(t *testing.T) string {
Expand All @@ -32,10 +34,11 @@ func RenderComposePostgres(t *testing.T) string {
return writeTemplate(t, "docker-compose-postgres.yml", getFreePort(t))
}

func RenderComposeSimple(t *testing.T) string {
func RenderComposeSimple(t *testing.T) (string, []int) {
t.Helper()

return writeTemplate(t, "docker-compose-simple.yml", getFreePort(t))
ports := []int{getFreePort(t)}
return writeTemplate(t, "docker-compose-simple.yml", ports...), ports
}

func RenderComposeWithBuild(t *testing.T) string {
Expand Down
27 changes: 14 additions & 13 deletions modules/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func ExampleLocalDockerCompose_WithEnv() {
}

func TestLocalDockerCompose(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -116,7 +116,7 @@ func TestLocalDockerCompose(t *testing.T) {
}

func TestLocalDockerComposeStrategyForInvalidService(t *testing.T) {
path := RenderComposeSimple(t)
path, ports := RenderComposeSimple(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -130,7 +130,7 @@ func TestLocalDockerComposeStrategyForInvalidService(t *testing.T) {
err := compose.
WithCommand([]string{"up", "-d"}).
// Appending with _1 as given in the Java Test-Containers Example
WithExposedService(compose.Format("mysql", "1"), 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second).WithOccurrence(1)).
WithExposedService(compose.Format("mysql", "1"), ports[0], wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second).WithOccurrence(1)).
Invoke()
require.Error(t, err.Error, "Expected error to be thrown because service with wait strategy is not running")

Expand All @@ -139,7 +139,7 @@ func TestLocalDockerComposeStrategyForInvalidService(t *testing.T) {
}

func TestLocalDockerComposeWithWaitLogStrategy(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -163,7 +163,7 @@ func TestLocalDockerComposeWithWaitLogStrategy(t *testing.T) {
}

func TestLocalDockerComposeWithWaitForService(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)

identifier := strings.ToLower(uuid.New().String())

Expand Down Expand Up @@ -213,7 +213,7 @@ func TestLocalDockerComposeWithWaitForShortLifespanService(t *testing.T) {
}

func TestLocalDockerComposeWithWaitHTTPStrategy(t *testing.T) {
path := RenderComposeSimple(t)
path, ports := RenderComposeSimple(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -229,7 +229,7 @@ func TestLocalDockerComposeWithWaitHTTPStrategy(t *testing.T) {
WithEnv(map[string]string{
"bar": "BAR",
}).
WithExposedService(compose.Format("nginx", "1"), 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).
WithExposedService(compose.Format("nginx", "1"), ports[0], wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).
Invoke()
checkIfError(t, err)

Expand Down Expand Up @@ -285,7 +285,7 @@ func TestLocalDockerComposeWithWaitStrategy_NoExposedPorts(t *testing.T) {
}

func TestLocalDockerComposeWithMultipleWaitStrategies(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -309,7 +309,7 @@ func TestLocalDockerComposeWithMultipleWaitStrategies(t *testing.T) {
}

func TestLocalDockerComposeWithFailedStrategy(t *testing.T) {
path := RenderComposeSimple(t)
path, ports := RenderComposeSimple(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -325,7 +325,7 @@ func TestLocalDockerComposeWithFailedStrategy(t *testing.T) {
WithEnv(map[string]string{
"bar": "BAR",
}).
WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("8080/tcp").WithStartupTimeout(5*time.Second)).
WithExposedService("nginx_1", ports[0], wait.NewHTTPStrategy("/").WithPort("8080/tcp").WithStartupTimeout(5*time.Second)).
Invoke()
// Verify that an error is thrown and not nil
// A specific error message matcher is not asserted since the docker library can change the return message, breaking this test
Expand All @@ -336,7 +336,7 @@ func TestLocalDockerComposeWithFailedStrategy(t *testing.T) {
}

func TestLocalDockerComposeComplex(t *testing.T) {
path := RenderComposeComplex(t)
path, _ := RenderComposeComplex(t)

identifier := strings.ToLower(uuid.New().String())

Expand All @@ -358,7 +358,7 @@ func TestLocalDockerComposeComplex(t *testing.T) {
}

func TestLocalDockerComposeWithEnvironment(t *testing.T) {
path := RenderComposeSimple(t)
path, _ := RenderComposeSimple(t)

identifier := strings.ToLower(uuid.New().String())

Expand Down Expand Up @@ -388,8 +388,9 @@ func TestLocalDockerComposeWithEnvironment(t *testing.T) {
}

func TestLocalDockerComposeWithMultipleComposeFiles(t *testing.T) {
simple, _ := RenderComposeSimple(t)
composeFiles := []string{
RenderComposeSimple(t),
simple,
RenderComposePostgres(t),
RenderComposeOverride(t),
}
Expand Down

0 comments on commit 05e78f6

Please sign in to comment.