diff --git a/.github/workflows/ci-test-go.yml b/.github/workflows/ci-test-go.yml index 6707c871f5..526bb7d3f7 100644 --- a/.github/workflows/ci-test-go.yml +++ b/.github/workflows/ci-test-go.yml @@ -71,7 +71,7 @@ jobs: - name: golangci-lint # TODO: Remove each example/module once it passes the golangci-lint - if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["examples/cockroachdb", "examples/toxiproxy", "modules/compose", "modules/redis"]'), inputs.project-directory) }} + if: ${{ inputs.platform == 'ubuntu-latest' && inputs.go-version == '1.20.x' && !contains(fromJSON('["examples/cockroachdb", "examples/toxiproxy", "modules/redis"]'), inputs.project-directory) }} uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3 with: # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version diff --git a/modules/compose/compose_api_test.go b/modules/compose/compose_api_test.go index b7bc70ebee..6a726b51d2 100644 --- a/modules/compose/compose_api_test.go +++ b/modules/compose/compose_api_test.go @@ -28,10 +28,10 @@ const ( func TestDockerComposeAPI(t *testing.T) { path := filepath.Join(testdataPackage, simpleCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -46,7 +46,7 @@ func TestDockerComposeAPIStrategyForInvalidService(t *testing.T) { require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -58,21 +58,21 @@ func TestDockerComposeAPIStrategyForInvalidService(t *testing.T) { Up(ctx, Wait(true)) require.Error(t, err, "Expected error to be thrown because service with wait strategy is not running") - assert.Equal(t, err.Error(), "no container found for service name mysql-1") + require.Equal(t, "no container found for service name mysql-1", err.Error()) serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } func TestDockerComposeAPIWithWaitLogStrategy(t *testing.T) { path := filepath.Join(testdataPackage, complexCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -86,7 +86,7 @@ func TestDockerComposeAPIWithWaitLogStrategy(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 2, len(serviceNames)) + assert.Len(t, len(serviceNames), 2) assert.Contains(t, serviceNames, "nginx") assert.Contains(t, serviceNames, "mysql") } @@ -94,10 +94,10 @@ func TestDockerComposeAPIWithWaitLogStrategy(t *testing.T) { func TestDockerComposeAPIWithRunServices(t *testing.T) { path := filepath.Join(testdataPackage, complexCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -112,9 +112,9 @@ func TestDockerComposeAPIWithRunServices(t *testing.T) { serviceNames := compose.Services() _, err = compose.ServiceContainer(context.Background(), "mysql") - assert.Error(t, err, "Make sure there is no mysql container") + require.Error(t, err, "Make sure there is no mysql container") - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } @@ -123,10 +123,10 @@ func TestDockerComposeAPIWithStopServices(t *testing.T) { compose, err := NewDockerComposeWith( WithStackFiles(path), WithLogger(testcontainers.TestLogger(t))) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -136,7 +136,7 @@ func TestDockerComposeAPIWithStopServices(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 2, len(serviceNames)) + assert.Len(t, len(serviceNames), 2) assert.Contains(t, serviceNames, "nginx") assert.Contains(t, serviceNames, "mysql") @@ -150,7 +150,7 @@ func TestDockerComposeAPIWithStopServices(t *testing.T) { // check container status state, err := mysqlContainer.State(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.False(t, state.Running) assert.Equal(t, "exited", state.Status) } @@ -158,10 +158,10 @@ func TestDockerComposeAPIWithStopServices(t *testing.T) { func TestDockerComposeAPIWithWaitForService(t *testing.T) { path := filepath.Join(testdataPackage, simpleCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -178,17 +178,17 @@ func TestDockerComposeAPIWithWaitForService(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } func TestDockerComposeAPIWithWaitHTTPStrategy(t *testing.T) { path := filepath.Join(testdataPackage, simpleCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -205,17 +205,17 @@ func TestDockerComposeAPIWithWaitHTTPStrategy(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } func TestDockerComposeAPIWithContainerName(t *testing.T) { path := filepath.Join(testdataPackage, "docker-compose-container-name.yml") compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -232,17 +232,17 @@ func TestDockerComposeAPIWithContainerName(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } func TestDockerComposeAPIWithWaitStrategy_NoExposedPorts(t *testing.T) { path := filepath.Join(testdataPackage, "docker-compose-no-exposed-ports.yml") compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -256,17 +256,17 @@ func TestDockerComposeAPIWithWaitStrategy_NoExposedPorts(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } func TestDockerComposeAPIWithMultipleWaitStrategies(t *testing.T) { path := filepath.Join(testdataPackage, complexCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -281,7 +281,7 @@ func TestDockerComposeAPIWithMultipleWaitStrategies(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 2, len(serviceNames)) + assert.Len(t, len(serviceNames), 2) assert.Contains(t, serviceNames, "nginx") assert.Contains(t, serviceNames, "mysql") } @@ -289,10 +289,10 @@ func TestDockerComposeAPIWithMultipleWaitStrategies(t *testing.T) { func TestDockerComposeAPIWithFailedStrategy(t *testing.T) { path := filepath.Join(testdataPackage, simpleCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -311,17 +311,17 @@ func TestDockerComposeAPIWithFailedStrategy(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") } func TestDockerComposeAPIComplex(t *testing.T) { path := filepath.Join(testdataPackage, complexCompose) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -331,7 +331,7 @@ func TestDockerComposeAPIComplex(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 2, len(serviceNames)) + assert.Len(t, len(serviceNames), 2) assert.Contains(t, serviceNames, "nginx") assert.Contains(t, serviceNames, "mysql") } @@ -342,10 +342,10 @@ func TestDockerComposeAPIWithEnvironment(t *testing.T) { path := filepath.Join(testdataPackage, simpleCompose) compose, err := NewDockerComposeWith(WithStackFiles(path), identifier) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -360,7 +360,7 @@ func TestDockerComposeAPIWithEnvironment(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 1, len(serviceNames)) + assert.Len(t, len(serviceNames), 1) assert.Contains(t, serviceNames, "nginx") present := map[string]string{ @@ -380,10 +380,10 @@ func TestDockerComposeAPIWithMultipleComposeFiles(t *testing.T) { identifier := testNameHash(t.Name()) compose, err := NewDockerComposeWith(composeFiles, identifier) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -399,7 +399,7 @@ func TestDockerComposeAPIWithMultipleComposeFiles(t *testing.T) { serviceNames := compose.Services() - assert.Equal(t, 3, len(serviceNames)) + assert.Len(t, len(serviceNames), 3) assert.Contains(t, serviceNames, "nginx") assert.Contains(t, serviceNames, "mysql") assert.Contains(t, serviceNames, "postgres") @@ -415,10 +415,10 @@ func TestDockerComposeAPIWithMultipleComposeFiles(t *testing.T) { func TestDockerComposeAPIWithVolume(t *testing.T) { path := filepath.Join(testdataPackage, composeWithVolume) compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -433,7 +433,7 @@ func TestDockerComposeAPIVolumesDeletedOnDown(t *testing.T) { identifier := uuid.New().String() stackFiles := WithStackFiles(path) compose, err := NewDockerComposeWith(stackFiles, StackIdentifier(identifier)) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) @@ -442,15 +442,15 @@ func TestDockerComposeAPIVolumesDeletedOnDown(t *testing.T) { require.NoError(t, err, "compose.Up()") err = compose.Down(context.Background(), RemoveOrphans(true), RemoveVolumes(true), RemoveImagesLocal) - assert.NoError(t, err, "compose.Down()") + require.NoError(t, err, "compose.Down()") volumeListFilters := filters.NewArgs() // the "mydata" identifier comes from the "testdata/docker-compose-volume.yml" file volumeListFilters.Add("name", fmt.Sprintf("%s_mydata", identifier)) volumeList, err := compose.dockerClient.VolumeList(ctx, volume.ListOptions{Filters: volumeListFilters}) - assert.NoError(t, err, "compose.dockerClient.VolumeList()") + require.NoError(t, err, "compose.dockerClient.VolumeList()") - assert.Equal(t, 0, len(volumeList.Volumes), "Volumes are not cleaned up") + assert.Empty(t, volumeList.Volumes, "Volumes are not cleaned up") } func TestDockerComposeAPIWithBuild(t *testing.T) { @@ -458,10 +458,10 @@ func TestDockerComposeAPIWithBuild(t *testing.T) { path := filepath.Join(testdataPackage, "docker-compose-build.yml") compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -477,10 +477,10 @@ func TestDockerComposeAPIWithBuild(t *testing.T) { func TestDockerComposeApiWithWaitForShortLifespanService(t *testing.T) { path := filepath.Join(testdataPackage, "docker-compose-short-lifespan.yml") compose, err := NewDockerCompose(path) - assert.NoError(t, err, "NewDockerCompose()") + require.NoError(t, err, "NewDockerCompose()") t.Cleanup(func() { - assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") }) ctx, cancel := context.WithCancel(context.Background()) @@ -496,7 +496,7 @@ func TestDockerComposeApiWithWaitForShortLifespanService(t *testing.T) { services := compose.Services() - assert.Equal(t, 2, len(services)) + assert.Len(t, len(services), 2) assert.Contains(t, services, "falafel") assert.Contains(t, services, "tzatziki") } diff --git a/modules/compose/compose_test.go b/modules/compose/compose_test.go index e34ccb2ef7..465c82dcba 100644 --- a/modules/compose/compose_test.go +++ b/modules/compose/compose_test.go @@ -14,6 +14,7 @@ import ( "github.com/docker/docker/api/types/volume" "github.com/google/uuid" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" @@ -136,9 +137,9 @@ func TestDockerComposeStrategyForInvalidService(t *testing.T) { // 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)). Invoke() - assert.NotEqual(t, err.Error, nil, "Expected error to be thrown because service with wait strategy is not running") + require.NoError(t, err.Error, "Expected error to be thrown because service with wait strategy is not running") - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") } @@ -161,7 +162,7 @@ func TestDockerComposeWithWaitLogStrategy(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 2, len(compose.Services)) + assert.Len(t, len(compose.Services), 2) assert.Contains(t, compose.Services, "nginx") assert.Contains(t, compose.Services, "mysql") } @@ -187,7 +188,7 @@ func TestDockerComposeWithWaitForService(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") } @@ -211,7 +212,7 @@ func TestDockerComposeWithWaitForShortLifespanService(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 2, len(compose.Services)) + assert.Len(t, len(compose.Services), 2) assert.Contains(t, compose.Services, "falafel") assert.Contains(t, compose.Services, "tzatziki") } @@ -237,7 +238,7 @@ func TestDockerComposeWithWaitHTTPStrategy(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") } @@ -262,7 +263,7 @@ func TestDockerComposeWithContainerName(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") } @@ -284,7 +285,7 @@ func TestDockerComposeWithWaitStrategy_NoExposedPorts(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") } @@ -307,7 +308,7 @@ func TestDockerComposeWithMultipleWaitStrategies(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 2, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") assert.Contains(t, compose.Services, "mysql") } @@ -333,9 +334,9 @@ func TestDockerComposeWithFailedStrategy(t *testing.T) { 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 - assert.NotEqual(t, err.Error, nil, "Expected error to be thrown because of a wrong suplied wait strategy") + require.NoError(t, err.Error, "Expected error to be thrown because of a wrong suplied wait strategy") - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") } @@ -356,7 +357,7 @@ func TestLocalDockerComposeComplex(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 2, len(compose.Services)) + assert.Len(t, len(compose.Services), 2) assert.Contains(t, compose.Services, "nginx") assert.Contains(t, compose.Services, "mysql") } @@ -381,7 +382,7 @@ func TestLocalDockerComposeWithEnvironment(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 1, len(compose.Services)) + assert.Len(t, len(compose.Services), 1) assert.Contains(t, compose.Services, "nginx") present := map[string]string{ @@ -416,7 +417,7 @@ func TestLocalDockerComposeWithMultipleComposeFiles(t *testing.T) { Invoke() checkIfError(t, err) - assert.Equal(t, 3, len(compose.Services)) + assert.Len(t, len(compose.Services), 3) assert.Contains(t, compose.Services, "nginx") assert.Contains(t, compose.Services, "mysql") assert.Contains(t, compose.Services, "postgres")