From 5432154afbbea2ec6ed6af308594e9adc7bfaf90 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 9 Nov 2022 11:09:34 -0600 Subject: [PATCH] feat: add golangci-lint (#569) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add golangci-lint * chore: go fmt -s . * chore: remove deprecated functions/methods * chore: add missing error handling and remove unused variables * remove: gotest.tools/v3 usage * review: path -> fileppath * review: rename golangci -> static-analysis * fix: static-analysis job * review: remove leading slash in filepath.Join Co-authored-by: Manuel de la Peña Co-authored-by: Manuel de la Peña --- .github/workflows/ci.yml | 15 ---- .github/workflows/golangci-lint.yml | 47 ++++++++++++ .golangci.yml | 3 + README.md | 6 +- compose_api.go | 3 + compose_local.go | 3 +- container_test.go | 17 ++--- docker.go | 15 ++-- docker_test.go | 109 +++++++++++++++++----------- docs/examples/cockroachdb.md | 6 +- docs/examples/nginx.md | 7 +- docs/examples/pulsar.md | 10 ++- docs/examples/redis.md | 8 +- docs/features/creating_container.md | 14 +++- docs/features/wait/http.md | 2 +- docs/quickstart/gotest.md | 6 +- e2e/container_test.go | 18 ++++- file.go | 5 +- file_test.go | 11 ++- generic_test.go | 1 - go.mod | 1 - go.sum | 1 - logconsumer_test.go | 22 +++--- network_test.go | 26 +++++-- parallel_test.go | 10 +-- reaper.go | 11 ++- scripts/checks.sh | 22 ------ testresources/echoserver.go | 4 +- wait/exec_test.go | 8 +- wait/exit.go | 9 ++- wait/exit_test.go | 1 - wait/health.go | 7 +- wait/http.go | 3 +- wait/http_test.go | 34 +++++---- wait/log.go | 16 ++-- wait/log_test.go | 9 +-- wait/sql.go | 10 +-- wait/testdata/Dockerfile | 2 +- wait/testdata/go.mod | 2 +- wait/testdata/main.go | 4 +- 40 files changed, 311 insertions(+), 197 deletions(-) create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .golangci.yml delete mode 100755 scripts/checks.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54f728c9dd..8943b6bffb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,18 +3,6 @@ name: Main pipeline on: [push, pull_request] jobs: - static-analysis: - runs-on: ubuntu-latest - steps: - - - uses: actions/checkout@v3 - - name: Run ShellCheck - run: | - shellcheck scripts/*.sh - - - name: Run gofmt - run: | - ./scripts/checks.sh test: strategy: matrix: @@ -38,9 +26,6 @@ jobs: - name: modTidy run: go mod tidy - - name: vet - run: go vet ./... - - name: ensure compilation env: GOOS: linux diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000000..65fd121871 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,47 @@ +name: golangci-lint / static-analysis +on: + push: + tags: + - v* + branches: + - main + pull_request: + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + static-analysis: + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v3 + with: + go-version: 1.19 + - uses: actions/checkout@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@07db5389c99593f11ad7b44463c2d4233066a9b1 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: v1.50 + + # Optional: working directory, useful for monorepos + # working-directory: somedir + + # Optional: golangci-lint command line arguments. + # args: --issues-exit-code=0 + args: --timeout=3m + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true then the all caching functionality will be complete disabled, + # takes precedence over all other caching options. + # skip-cache: true + + # Optional: if set to true then the action don't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. + # skip-build-cache: true diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000000..4ad9dfc3cc --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,3 @@ +linters: + enable: + - gofmt diff --git a/README.md b/README.md index 30b4347d80..5ee79ed3e8 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,11 @@ func TestIntegrationNginxLatestReturn(t *testing.T) { } // Clean up the container after the test is complete - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %w", err) + } + }() resp, err := http.Get(nginxC.URI) if resp.StatusCode != http.StatusOK { diff --git a/compose_api.go b/compose_api.go index 17e38fec90..c75453091c 100644 --- a/compose_api.go +++ b/compose_api.go @@ -25,8 +25,10 @@ func (f stackUpOptionFunc) applyToStackUp(o *stackUpOptions) { f(o) } +//nolint:unused type stackDownOptionFunc func(do *api.DownOptions) +//nolint:unused func (f stackDownOptionFunc) applyToStackDown(do *api.DownOptions) { f(do) } @@ -42,6 +44,7 @@ func RunServices(serviceNames ...string) StackUpOption { // IgnoreOrphans - Ignore legacy containers for services that are not defined in the project type IgnoreOrphans bool +//nolint:unused func (io IgnoreOrphans) applyToStackUp(co *api.CreateOptions, _ *api.StartOptions) { co.IgnoreOrphans = bool(io) } diff --git a/compose_local.go b/compose_local.go index 48cc3f52f2..29c3c1bc43 100644 --- a/compose_local.go +++ b/compose_local.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -220,7 +219,7 @@ func (dc *LocalDockerCompose) validate() error { for _, abs := range dc.absComposeFilePaths { c := compose{} - yamlFile, err := ioutil.ReadFile(abs) + yamlFile, err := os.ReadFile(abs) if err != nil { return err } diff --git a/container_test.go b/container_test.go index b8cc1329e8..a63c54c17c 100644 --- a/container_test.go +++ b/container_test.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "strings" "testing" "time" @@ -95,19 +94,19 @@ func Test_GetDockerfile(t *testing.T) { } testTable := []TestCase{ - TestCase{ + { name: "defaults to \"Dockerfile\" 1", ExpectedDockerfileName: "Dockerfile", ContainerRequest: ContainerRequest{}, }, - TestCase{ + { name: "defaults to \"Dockerfile\" 2", ExpectedDockerfileName: "Dockerfile", ContainerRequest: ContainerRequest{ FromDockerfile: FromDockerfile{}, }, }, - TestCase{ + { name: "will override name", ExpectedDockerfileName: "CustomDockerfile", ContainerRequest: ContainerRequest{ @@ -325,7 +324,7 @@ func Test_BuildImageWithContexts(t *testing.T) { } else if err != nil { t.Fatal(err) } else { - c.Terminate(ctx) + terminateContainerOnEnd(t, ctx, c) } }) } @@ -347,7 +346,7 @@ func Test_GetLogsFromFailedContainer(t *testing.T) { if err != nil && !errors.Is(err, context.DeadlineExceeded) { t.Fatal(err) } else if err == nil { - c.Terminate(ctx) + terminateContainerOnEnd(t, ctx, c) t.Fatal("was expecting error starting container") } @@ -356,7 +355,7 @@ func Test_GetLogsFromFailedContainer(t *testing.T) { t.Fatal(logErr) } - b, err := ioutil.ReadAll(logs) + b, err := io.ReadAll(logs) if err != nil { t.Fatal(err) } @@ -397,9 +396,7 @@ func createTestContainer(t *testing.T, ctx context.Context) int { t.Fatalf("could not get mapped port: %v", err) } - t.Cleanup(func() { - container.Terminate(context.Background()) - }) + terminateContainerOnEnd(t, ctx, container) return port.Int() } diff --git a/docker.go b/docker.go index 1e1e5ba697..04c70a6487 100644 --- a/docker.go +++ b/docker.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/url" "os" "os/exec" @@ -296,13 +295,9 @@ func (c *DockerContainer) Logs(ctx context.Context) (io.ReadCloser, error) { r := bufio.NewReader(rc) go func() { - var ( - isPrefix = true - lineStarted = true - line []byte - ) + var lineStarted = true for err == nil { - line, isPrefix, err = r.ReadLine() + line, isPrefix, err := r.ReadLine() if lineStarted && len(line) >= streamHeaderSize { line = line[streamHeaderSize:] // trim stream header @@ -542,7 +537,7 @@ func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath return c.CopyDirToContainer(ctx, hostFilePath, containerFilePath, fileMode) } - fileContent, err := ioutil.ReadFile(hostFilePath) + fileContent, err := os.ReadFile(hostFilePath) if err != nil { return err } @@ -1036,7 +1031,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque if err != nil { return nil, err } - for p, _ := range image.ContainerConfig.ExposedPorts { + for p := range image.ContainerConfig.ExposedPorts { exposedPorts = append(exposedPorts, string(p)) } } @@ -1225,7 +1220,7 @@ func (p *DockerProvider) attemptToPullImage(ctx context.Context, tag string, pul defer pull.Close() // download of docker image finishes at EOF of the pull request - _, err = ioutil.ReadAll(pull) + _, err = io.ReadAll(pull) return err } diff --git a/docker_test.go b/docker_test.go index f051c36eb5..eb4bc5609e 100644 --- a/docker_test.go +++ b/docker_test.go @@ -5,10 +5,10 @@ import ( "database/sql" "errors" "fmt" + "log" // Import mysql into the scope of this package (required) "io" - "io/ioutil" "math/rand" "net/http" "os" @@ -26,8 +26,6 @@ import ( "github.com/go-redis/redis/v8" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gotest.tools/v3/env" - "gotest.tools/v3/fs" "github.com/docker/docker/errdefs" @@ -196,7 +194,7 @@ func TestContainerWithHostNetworkOptions_UseExposePortsFromImageConfigs(t *testi t.Fatal(err) } - defer nginxC.Terminate(ctx) + terminateContainerOnEnd(t, ctx, nginxC) endpoint, err := nginxC.Endpoint(ctx, "http") if err != nil { @@ -227,7 +225,7 @@ func TestContainerWithNetworkModeAndNetworkTogether(t *testing.T) { // Error when NetworkMode = host and Network = []string{"bridge"} t.Logf("Can't use Network and NetworkMode together, %s", err) } - defer nginx.Terminate(ctx) + terminateContainerOnEnd(t, ctx, nginx) } func TestContainerWithHostNetworkOptionsAndWaitStrategy(t *testing.T) { @@ -638,9 +636,10 @@ func TestContainerTerminationRemovesDockerImage(t *testing.T) { if err != nil { t.Fatal(err) } + _, _, err = client.ImageInspectWithRaw(ctx, imageID) if err == nil { - t.Fatal("custom built image should have been removed") + t.Fatal("custom built image should have been removed", err) } }) } @@ -688,6 +687,9 @@ func TestTwoContainersExposingTheSamePort(t *testing.T) { } endpointB, err := nginxB.PortEndpoint(ctx, nginxDefaultPort, "http") + if err != nil { + t.Fatal(err) + } resp, err = http.Get(endpointB) if err != nil { @@ -1018,6 +1020,9 @@ func TestContainerCreationWaitsForLog(t *testing.T) { "root", "password", host, port, "database") db, err := sql.Open("mysql", connectionString) + if err != nil { + t.Fatal(err) + } defer db.Close() if err = db.Ping(); err != nil { @@ -1163,7 +1168,7 @@ func prepareLocalRegistryWithAuth(t *testing.T) { assert.NoError(t, registryC.Terminate(context.Background())) }) - ctx, cancel := context.WithCancel(context.Background()) + _, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) } @@ -1249,7 +1254,7 @@ func Test_BuildContainerFromDockerfileWithBuildArgs(t *testing.T) { t.Fatal(err) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -1287,7 +1292,7 @@ func Test_BuildContainerFromDockerfileWithBuildLog(t *testing.T) { terminateContainerOnEnd(t, ctx, c) _ = w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = rescueStdout temp := strings.Split(string(out), "\n") @@ -1455,7 +1460,7 @@ func TestEntrypoint(t *testing.T) { func TestReadTCPropsFile(t *testing.T) { t.Run("HOME is not set", func(t *testing.T) { - env.Patch(t, "HOME", "") + t.Setenv("HOME", "") config := configureTC() @@ -1463,8 +1468,8 @@ func TestReadTCPropsFile(t *testing.T) { }) t.Run("HOME is not set - TESTCONTAINERS_ env is set", func(t *testing.T) { - env.Patch(t, "HOME", "") - env.Patch(t, "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true") + t.Setenv("HOME", "") + t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true") config := configureTC() @@ -1475,8 +1480,8 @@ func TestReadTCPropsFile(t *testing.T) { }) t.Run("HOME does not contain TC props file", func(t *testing.T) { - tmpDir := fs.NewDir(t, os.TempDir()) - env.Patch(t, "HOME", tmpDir.Path()) + tmpDir := t.TempDir() + t.Setenv("HOME", tmpDir) config := configureTC() @@ -1484,9 +1489,9 @@ func TestReadTCPropsFile(t *testing.T) { }) t.Run("HOME does not contain TC props file - TESTCONTAINERS_ env is set", func(t *testing.T) { - tmpDir := fs.NewDir(t, os.TempDir()) - env.Patch(t, "HOME", tmpDir.Path()) - env.Patch(t, "TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true") + tmpDir := t.TempDir() + t.Setenv("HOME", tmpDir) + t.Setenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED", "true") config := configureTC() expected := TestContainersConfig{} @@ -1691,12 +1696,12 @@ func TestReadTCPropsFile(t *testing.T) { } for i, tt := range tests { t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { - tmpDir := fs.NewDir(t, os.TempDir()) - env.Patch(t, "HOME", tmpDir.Path()) + tmpDir := t.TempDir() + t.Setenv("HOME", tmpDir) for k, v := range tt.env { - env.Patch(t, k, v) + t.Setenv(k, v) } - if err := ioutil.WriteFile(tmpDir.Join(".testcontainers.properties"), []byte(tt.content), 0o600); err != nil { + if err := os.WriteFile(filepath.Join(tmpDir, ".testcontainers.properties"), []byte(tt.content), 0o600); err != nil { t.Errorf("Failed to create the file: %v", err) return } @@ -1721,7 +1726,11 @@ func ExampleDockerProvider_CreateContainer() { ContainerRequest: req, Started: true, }) - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() } func ExampleContainer_Host() { @@ -1735,7 +1744,11 @@ func ExampleContainer_Host() { ContainerRequest: req, Started: true, }) - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() ip, _ := nginxC.Host(ctx) println(ip) } @@ -1750,7 +1763,11 @@ func ExampleContainer_Start() { nginxC, _ := GenericContainer(ctx, GenericContainerRequest{ ContainerRequest: req, }) - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() _ = nginxC.Start(ctx) } @@ -1764,7 +1781,11 @@ func ExampleContainer_Stop() { nginxC, _ := GenericContainer(ctx, GenericContainerRequest{ ContainerRequest: req, }) - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() timeout := 10 * time.Second _ = nginxC.Stop(ctx, &timeout) } @@ -1780,7 +1801,11 @@ func ExampleContainer_MappedPort() { ContainerRequest: req, Started: true, }) - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() ip, _ := nginxC.Host(ctx) port, _ := nginxC.MappedPort(ctx, "80") _, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port())) @@ -1800,7 +1825,7 @@ func TestContainerCreationWithBindAndVolume(t *testing.T) { } // Create the volume. - vol, err := dockerCli.VolumeCreate(ctx, volume.VolumeCreateBody{ + vol, err := dockerCli.VolumeCreate(ctx, volume.CreateOptions{ Driver: "local", }) if err != nil { @@ -1928,11 +1953,7 @@ func TestContainerCustomPlatformImage(t *testing.T) { Started: false, }) - t.Cleanup(func() { - if c != nil { - c.Terminate(ctx) - } - }) + terminateContainerOnEnd(t, ctx, c) assert.Error(t, err) }) @@ -2112,13 +2133,13 @@ func TestDockerCreateContainerWithFiles(t *testing.T) { for _, f := range tc.files { require.NoError(t, err) - hostFileData, err := ioutil.ReadFile(f.HostFilePath) + hostFileData, err := os.ReadFile(f.HostFilePath) require.NoError(t, err) fd, err := nginxC.CopyFileFromContainer(ctx, f.ContainerFilePath) require.NoError(t, err) defer fd.Close() - containerFileData, err := ioutil.ReadAll(fd) + containerFileData, err := io.ReadAll(fd) require.NoError(t, err) require.Equal(t, hostFileData, containerFileData) @@ -2206,7 +2227,7 @@ func TestDockerContainerCopyToContainer(t *testing.T) { copiedFileName := "hello_copy.sh" - fileContent, err := ioutil.ReadFile("./testresources/hello.sh") + fileContent, err := os.ReadFile("./testresources/hello.sh") if err != nil { t.Fatal(err) } @@ -2221,7 +2242,7 @@ func TestDockerContainerCopyToContainer(t *testing.T) { } func TestDockerContainerCopyFileFromContainer(t *testing.T) { - fileContent, err := ioutil.ReadFile("./testresources/hello.sh") + fileContent, err := os.ReadFile("./testresources/hello.sh") if err != nil { t.Fatal(err) } @@ -2255,7 +2276,7 @@ func TestDockerContainerCopyFileFromContainer(t *testing.T) { t.Fatal(err) } - fileContentFromContainer, err := ioutil.ReadAll(reader) + fileContentFromContainer, err := io.ReadAll(reader) if err != nil { t.Fatal(err) } @@ -2293,7 +2314,7 @@ func TestDockerContainerCopyEmptyFileFromContainer(t *testing.T) { t.Fatal(err) } - fileContentFromContainer, err := ioutil.ReadAll(reader) + fileContentFromContainer, err := io.ReadAll(reader) if err != nil { t.Fatal(err) } @@ -2452,7 +2473,7 @@ func TestContainerRunningCheckingStatusCode(t *testing.T) { t.Fatal(err) } - defer influx.Terminate(ctx) + terminateContainerOnEnd(t, ctx, influx) } func TestContainerWithUserID(t *testing.T) { @@ -2477,7 +2498,7 @@ func TestContainerWithUserID(t *testing.T) { t.Fatal(err) } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { t.Fatal(err) } @@ -2506,7 +2527,7 @@ func TestContainerWithNoUserID(t *testing.T) { t.Fatal(err) } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { t.Fatal(err) } @@ -2572,14 +2593,14 @@ func assertExtractedFiles(t *testing.T, ctx context.Context, container Container tmpDir := filepath.Join(t.TempDir()) // compare the bytes of each file in the source with the bytes from the copied-from-container file - srcFiles, err := ioutil.ReadDir(hostFilePath) + srcFiles, err := os.ReadDir(hostFilePath) require.NoError(t, err) for _, srcFile := range srcFiles { if srcFile.IsDir() { continue } - srcBytes, err := ioutil.ReadFile(filepath.Join(hostFilePath, srcFile.Name())) + srcBytes, err := os.ReadFile(filepath.Join(hostFilePath, srcFile.Name())) if err != nil { require.NoError(t, err) } @@ -2602,7 +2623,7 @@ func assertExtractedFiles(t *testing.T, ctx context.Context, container Container require.NoError(t, err) } - untarBytes, err := ioutil.ReadFile(targetPath) + untarBytes, err := os.ReadFile(targetPath) if err != nil { require.NoError(t, err) } diff --git a/docs/examples/cockroachdb.md b/docs/examples/cockroachdb.md index 7700bbd75a..4eb66a2f18 100644 --- a/docs/examples/cockroachdb.md +++ b/docs/examples/cockroachdb.md @@ -94,7 +94,11 @@ func TestIntegrationDBInsertSelect(t *testing.T) { if err != nil { t.Fatal(err) } - defer cdbContainer.Terminate(ctx) + t.Cleanup(func() { + if err := cdbContainer.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) db, err := sql.Open("pgx", cdbContainer.URI+"/projectmanagement") if err != nil { diff --git a/docs/examples/nginx.md b/docs/examples/nginx.md index 59c2cd8985..401edcfdda 100644 --- a/docs/examples/nginx.md +++ b/docs/examples/nginx.md @@ -60,7 +60,12 @@ func TestIntegrationNginxLatestReturn(t *testing.T) { } // Clean up the container after the test is complete - defer nginxC.Terminate(ctx) + t.Cleanup(func() { + t.Log("terminating container") + if err := ctr.Terminate(ctx)); err != nil { + t.Errorf("failed to terminate container: :%w", err) + } + }) resp, err := http.Get(nginxC.URI) if resp.StatusCode != http.StatusOK { diff --git a/docs/examples/pulsar.md b/docs/examples/pulsar.md index 6aec7a3bde..a1b372b5d1 100644 --- a/docs/examples/pulsar.md +++ b/docs/examples/pulsar.md @@ -71,7 +71,13 @@ func TestPulsar(t *testing.T) { if err != nil { t.Fatal(err) } - t.Cleanup(func() { c.Container.Terminate(ctx) }) + // Cleanup the container after the test is complete + t.Cleanup(func() { + t.Log("terminating container") + if err := ctr.Terminate(ctx)); err != nil { + t.Errorf("failed to terminate container: :%w", err) + } + }) pc, err := pulsar.NewClient(pulsar.ClientOptions{ URL: c.URI, @@ -131,4 +137,4 @@ type logConsumer struct{} func (lc *logConsumer) Accept(l testcontainers.Log) { fmt.Print(string(l.Content)) } -``` \ No newline at end of file +``` diff --git a/docs/examples/redis.md b/docs/examples/redis.md index 39eaa729ed..2ae4940250 100644 --- a/docs/examples/redis.md +++ b/docs/examples/redis.md @@ -64,7 +64,13 @@ func TestIntegrationSetGet(t *testing.T) { if err != nil { t.Fatal(err) } - defer redisContainer.Terminate(ctx) + // Clean up the container after the test is complete + t.Cleanup(func() { + t.Log("terminating container") + if err := redisContainer.Terminate(ctx)); err != nil { + t.Errorf("failed to terminate container: :%w", err) + } + }) // You will likely want to wrap your Redis package of choice in an // interface to aid in unit testing and limit lock-in throughtout your diff --git a/docs/features/creating_container.md b/docs/features/creating_container.md index ae1fea8d58..5035198775 100644 --- a/docs/features/creating_container.md +++ b/docs/features/creating_container.md @@ -74,7 +74,11 @@ func TestIntegrationNginxLatestReturn(t *testing.T) { } // Clean up the container after the test is complete - defer nginxC.Terminate(ctx) + t.Cleanup(func() { + if err := nginxC.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) resp, err := http.Get(nginxC.URI) if resp.StatusCode != http.StatusOK { @@ -193,7 +197,6 @@ func main() { } res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{}) - if err != nil { e, ok := err.(testcontainers.ParallelContainersError) if !ok { @@ -207,7 +210,12 @@ func main() { } for _, c := range res { - defer c.Terminate(ctx) + c := c + defer func() { + if err := c.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", c) + } + }() } } ``` diff --git a/docs/features/wait/http.md b/docs/features/wait/http.md index 91d5aefd3e..19800149cc 100644 --- a/docs/features/wait/http.md +++ b/docs/features/wait/http.md @@ -49,7 +49,7 @@ req := testcontainers.ContainerRequest{ WaitingFor: wait.NewHTTPStrategy("/ping"). WithStartupTimeout(time.Second * 10).WithPort("80/tcp"). WithResponseMatcher(func(body io.Reader) bool { - data, _ := ioutil.ReadAll(body) + data, _ := io.ReadAll(body) return bytes.Equal(data, []byte("pong")) }). WithStatusCodeMatcher(func(status int) bool { diff --git a/docs/quickstart/gotest.md b/docs/quickstart/gotest.md index 675fa03255..d5c0451423 100644 --- a/docs/quickstart/gotest.md +++ b/docs/quickstart/gotest.md @@ -35,7 +35,11 @@ func TestWithRedis(t *testing.T) { if err != nil { t.Error(err) } - defer redisC.Terminate(ctx) + defer func() { + if err := redisC.terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %w", err) + } + }() } ``` diff --git a/e2e/container_test.go b/e2e/container_test.go index f9dc377e04..3be69f315d 100644 --- a/e2e/container_test.go +++ b/e2e/container_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/docker/go-connections/nat" + "github.com/stretchr/testify/require" . "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" @@ -45,7 +46,7 @@ func TestContainerWithWaitForSQL(t *testing.T) { t.Fatal(err) } - defer container.Terminate(ctx) + terminateContainerOnEnd(t, ctx, container) }) t.Run("custom query", func(t *testing.T) { req := ContainerRequest{ @@ -65,7 +66,7 @@ func TestContainerWithWaitForSQL(t *testing.T) { t.Fatal(err) } - defer container.Terminate(ctx) + terminateContainerOnEnd(t, ctx, container) }) t.Run("custom bad query", func(t *testing.T) { req := ContainerRequest{ @@ -85,6 +86,17 @@ func TestContainerWithWaitForSQL(t *testing.T) { t.Fatal("expected error, but got a nil") } - defer container.Terminate(ctx) + terminateContainerOnEnd(t, ctx, container) + }) +} + +func terminateContainerOnEnd(tb testing.TB, ctx context.Context, ctr Container) { + tb.Helper() + if ctr == nil { + return + } + tb.Cleanup(func() { + tb.Log("terminating container") + require.NoError(tb, ctr.Terminate(ctx)) }) } diff --git a/file.go b/file.go index 8e33a6ce75..1f402f3842 100644 --- a/file.go +++ b/file.go @@ -40,7 +40,7 @@ func tarDir(src string, fileMode int64) (*bytes.Buffer, error) { tw := tar.NewWriter(zr) // walk through every file in the folder - filepath.Walk(src, func(file string, fi os.FileInfo, errFn error) error { + err := filepath.Walk(src, func(file string, fi os.FileInfo, errFn error) error { if errFn != nil { return fmt.Errorf("error traversing the file system: %w", errFn) } @@ -80,6 +80,9 @@ func tarDir(src string, fileMode int64) (*bytes.Buffer, error) { } return nil }) + if err != nil { + return buffer, err + } // produce tar if err := tw.Close(); err != nil { diff --git a/file_test.go b/file_test.go index 6c176c110a..c34acf7060 100644 --- a/file_test.go +++ b/file_test.go @@ -6,7 +6,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -67,7 +66,7 @@ func Test_TarDir(t *testing.T) { t.Fatal(err) } - srcFiles, err := ioutil.ReadDir(src) + srcFiles, err := os.ReadDir(src) if err != nil { log.Fatal(err) } @@ -76,12 +75,12 @@ func Test_TarDir(t *testing.T) { if srcFile.IsDir() { continue } - srcBytes, err := ioutil.ReadFile(filepath.Join(src, srcFile.Name())) + srcBytes, err := os.ReadFile(filepath.Join(src, srcFile.Name())) if err != nil { t.Fatal(err) } - untarBytes, err := ioutil.ReadFile(filepath.Join(tmpDir, "testresources", srcFile.Name())) + untarBytes, err := os.ReadFile(filepath.Join(tmpDir, "testresources", srcFile.Name())) if err != nil { t.Fatal(err) } @@ -90,7 +89,7 @@ func Test_TarDir(t *testing.T) { } func Test_TarFile(t *testing.T) { - b, err := ioutil.ReadFile(filepath.Join(".", "testresources", "Dockerfile")) + b, err := os.ReadFile(filepath.Join(".", "testresources", "Dockerfile")) if err != nil { t.Fatal(err) } @@ -106,7 +105,7 @@ func Test_TarFile(t *testing.T) { t.Fatal(err) } - untarBytes, err := ioutil.ReadFile(filepath.Join(tmpDir, "Docker.file")) + untarBytes, err := os.ReadFile(filepath.Join(tmpDir, "Docker.file")) if err != nil { t.Fatal(err) } diff --git a/generic_test.go b/generic_test.go index ee58667efb..4762e0c726 100644 --- a/generic_test.go +++ b/generic_test.go @@ -29,7 +29,6 @@ func TestGenericReusableContainer(t *testing.T) { }) require.NoError(t, err) require.True(t, n1.IsRunning()) - terminateContainerOnEnd(t, ctx, n1) copiedFileName := "hello_copy.sh" diff --git a/go.mod b/go.mod index 02b321bb7a..1b47dedeb5 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( golang.org/x/sys v0.1.0 gopkg.in/yaml.v3 v3.0.1 gotest.tools/gotestsum v1.8.2 - gotest.tools/v3 v3.4.0 ) replace ( diff --git a/go.sum b/go.sum index 337d999c2d..60dba0c740 100644 --- a/go.sum +++ b/go.sum @@ -1068,7 +1068,6 @@ gotest.tools/gotestsum v1.8.2/go.mod h1:6JHCiN6TEjA7Kaz23q1bH0e2Dc3YJjDUZ0DmctFZ gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= -gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/logconsumer_test.go b/logconsumer_test.go index 34060f46c6..cfddbdd847 100644 --- a/logconsumer_test.go +++ b/logconsumer_test.go @@ -4,14 +4,14 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gotest.tools/v3/assert" "github.com/docker/docker/client" @@ -106,8 +106,8 @@ func Test_LogConsumerGetsCalled(t *testing.T) { // get rid of the server "ready" log g.Msgs = g.Msgs[1:] - assert.DeepEqual(t, []string{"echo hello\n", "echo there\n"}, g.Msgs) - _ = c.Terminate(ctx) + assert.Equal(t, []string{"echo hello\n", "echo there\n"}, g.Msgs) + terminateContainerOnEnd(t, ctx, c) } type TestLogTypeConsumer struct { @@ -144,7 +144,7 @@ func Test_ShouldRecognizeLogTypes(t *testing.T) { if err != nil { t.Fatal(err) } - defer func() { _ = c.Terminate(ctx) }() + terminateContainerOnEnd(t, ctx, c) ep, err := c.Endpoint(ctx, "http") if err != nil { @@ -181,7 +181,7 @@ func Test_ShouldRecognizeLogTypes(t *testing.T) { <-g.Ack _ = c.StopLogProducer() - assert.DeepEqual(t, map[string]string{ + assert.Equal(t, map[string]string{ StdoutLog: "echo this-is-stdout\n", StderrLog: "echo this-is-stderr\n", }, g.LogTypes) @@ -248,7 +248,7 @@ func TestContainerLogWithErrClosed(t *testing.T) { if err := nginx.Start(ctx); err != nil { t.Fatal(err) } - defer nginx.Terminate(ctx) + terminateContainerOnEnd(t, ctx, nginx) port, err := nginx.MappedPort(ctx, "80/tcp") if err != nil { @@ -259,7 +259,9 @@ func TestContainerLogWithErrClosed(t *testing.T) { if err = nginx.StartLogProducer(ctx); err != nil { t.Fatal(err) } - defer nginx.StopLogProducer() + defer func() { + _ = nginx.StopLogProducer() + }() nginx.FollowOutput(&consumer) // Gather the initial container logs @@ -320,13 +322,13 @@ func TestContainerLogsShouldBeWithoutStreamHeader(t *testing.T) { if err != nil { t.Fatal(err) } - defer container.Terminate(ctx) + terminateContainerOnEnd(t, ctx, container) r, err := container.Logs(ctx) if err != nil { t.Fatal(err) } defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { t.Fatal(err) } diff --git a/network_test.go b/network_test.go index 39888e51b5..23d41656cb 100644 --- a/network_test.go +++ b/network_test.go @@ -3,6 +3,7 @@ package testcontainers import ( "context" "fmt" + "log" "testing" "time" @@ -22,7 +23,9 @@ func ExampleNetworkProvider_CreateNetwork() { CheckDuplicate: true, }, }) - defer net.Remove(ctx) + defer func() { + _ = net.Remove(ctx) + }() nginxC, _ := GenericContainer(ctx, GenericContainerRequest{ ContainerRequest: ContainerRequest{ @@ -35,7 +38,12 @@ func ExampleNetworkProvider_CreateNetwork() { }, }, }) - defer nginxC.Terminate(ctx) + defer func() { + if err := nginxC.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() + nginxC.GetContainerID() } @@ -66,7 +74,9 @@ func Test_NetworkWithIPAM(t *testing.T) { t.Fatal("cannot create network: ", err) } - defer net.Remove(ctx) + defer func() { + _ = net.Remove(ctx) + }() nginxC, _ := GenericContainer(ctx, GenericContainerRequest{ ContainerRequest: ContainerRequest{ @@ -79,7 +89,7 @@ func Test_NetworkWithIPAM(t *testing.T) { }, }, }) - defer nginxC.Terminate(ctx) + terminateContainerOnEnd(t, ctx, nginxC) nginxC.GetContainerID() provider, err := ProviderDocker.GetProvider() @@ -123,7 +133,9 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) { t.Fatal("cannot create network") } - defer net.Remove(ctx) + defer func() { + _ = net.Remove(ctx) + }() postgres, err := GenericContainer(ctx, GenericContainerRequest{ ContainerRequest: dbContainerRequest, @@ -133,7 +145,7 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) { t.Fatal(err) } - defer postgres.Terminate(ctx) + terminateContainerOnEnd(t, ctx, postgres) env = make(map[string]string) env["RABBITMQ_ERLANG_COOKIE"] = "f2a2d3d27c75" @@ -158,7 +170,7 @@ func Test_MultipleContainersInTheNewNetwork(t *testing.T) { return } - defer rabbitmq.Terminate(ctx) + terminateContainerOnEnd(t, ctx, rabbitmq) fmt.Println(postgres.GetContainerID()) fmt.Println(rabbitmq.GetContainerID()) } diff --git a/parallel_test.go b/parallel_test.go index 84d86552be..3a23c47e57 100644 --- a/parallel_test.go +++ b/parallel_test.go @@ -103,7 +103,6 @@ func TestParallelContainers(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { res, err := ParallelContainers(context.Background(), tc.reqs, ParallelContainersOptions{}) - if err != nil { require.NotZero(t, tc.expErrors) e, _ := err.(ParallelContainersError) @@ -114,7 +113,8 @@ func TestParallelContainers(t *testing.T) { } for _, c := range res { - defer c.Terminate(context.Background()) + c := c + terminateContainerOnEnd(t, context.Background(), c) } if len(res) != tc.resLen { @@ -165,8 +165,6 @@ func TestParallelContainersWithReuse(t *testing.T) { e, _ := err.(ParallelContainersError) t.Fatalf("expected errors: %d, got: %d\n", 0, len(e.Errors)) } - - for _, c := range res { - defer c.Terminate(ctx) - } + // Container is reused, only terminate first container + terminateContainerOnEnd(t, ctx, res[0]) } diff --git a/reaper.go b/reaper.go index 5a81336597..a82a8ba54f 100644 --- a/reaper.go +++ b/reaper.go @@ -126,8 +126,14 @@ func (r *Reaper) Connect() (chan bool, error) { for retryLimit > 0 { retryLimit-- - sock.WriteString(strings.Join(labelFilters, "&")) - sock.WriteString("\n") + if _, err := sock.WriteString(strings.Join(labelFilters, "&")); err != nil { + continue + } + + if _, err := sock.WriteString("\n"); err != nil { + continue + } + if err := sock.Flush(); err != nil { continue } @@ -136,6 +142,7 @@ func (r *Reaper) Connect() (chan bool, error) { if err != nil { continue } + if resp == "ACK\n" { break } diff --git a/scripts/checks.sh b/scripts/checks.sh deleted file mode 100755 index 61cc2c10d8..0000000000 --- a/scripts/checks.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -BASEDIR=$(dirname "$0") - -go_fmt() { - format=$(go fmt ./...) - if [[ ${format} ]]; then - echo "go fmt failed:" - echo "${format}" - exit 1 - else - echo "go fmt passed" - fi -} - -main() { - cd "${BASEDIR}"/.. || return - - go_fmt -} - -main "$@" \ No newline at end of file diff --git a/testresources/echoserver.go b/testresources/echoserver.go index 0ab8d5c31f..1b331e8890 100644 --- a/testresources/echoserver.go +++ b/testresources/echoserver.go @@ -11,7 +11,7 @@ import ( func envHandler() http.HandlerFunc { return func(rw http.ResponseWriter, req *http.Request) { - rw.Write([]byte(os.Getenv("FOO"))) + _, _ = rw.Write([]byte(os.Getenv("FOO"))) rw.WriteHeader(http.StatusAccepted) } @@ -44,5 +44,5 @@ func main() { fmt.Println("ready") - http.Serve(ln, mux) + _ = http.Serve(ln, mux) } diff --git a/wait/exec_test.go b/wait/exec_test.go index f97d799295..f796c8893a 100644 --- a/wait/exec_test.go +++ b/wait/exec_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "io" + "log" "testing" "time" @@ -29,7 +30,12 @@ func ExampleExecStrategy() { panic(err) } - defer localstack.Terminate(ctx) // nolint: errcheck + defer func() { + if err := localstack.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() + // Here you have a running container } diff --git a/wait/exit.go b/wait/exit.go index eace6702ac..528a2306be 100644 --- a/wait/exit.go +++ b/wait/exit.go @@ -18,7 +18,7 @@ type ExitStrategy struct { PollInterval time.Duration } -//NewExitStrategy constructs with polling interval of 100 milliseconds without timeout by default +// NewExitStrategy constructs with polling interval of 100 milliseconds without timeout by default func NewExitStrategy() *ExitStrategy { return &ExitStrategy{ PollInterval: defaultPollInterval(), @@ -45,9 +45,10 @@ func (ws *ExitStrategy) WithPollInterval(pollInterval time.Duration) *ExitStrate // ForExit is the default construction for the fluid interface. // // For Example: -// wait. -// ForExit(). -// WithPollInterval(1 * time.Second) +// +// wait. +// ForExit(). +// WithPollInterval(1 * time.Second) func ForExit() *ExitStrategy { return NewExitStrategy() } diff --git a/wait/exit_test.go b/wait/exit_test.go index d7a6b476af..7e00136e90 100644 --- a/wait/exit_test.go +++ b/wait/exit_test.go @@ -12,7 +12,6 @@ import ( type exitStrategyTarget struct { isRunning bool - err error } func (st exitStrategyTarget) Host(ctx context.Context) (string, error) { diff --git a/wait/health.go b/wait/health.go index 73885c2e81..9ff6635867 100644 --- a/wait/health.go +++ b/wait/health.go @@ -45,9 +45,10 @@ func (ws *HealthStrategy) WithPollInterval(pollInterval time.Duration) *HealthSt // ForHealthCheck is the default construction for the fluid interface. // // For Example: -// wait. -// ForHealthCheck(). -// WithPollInterval(1 * time.Second) +// +// wait. +// ForHealthCheck(). +// WithPollInterval(1 * time.Second) func ForHealthCheck() *HealthStrategy { return NewHealthStrategy() } diff --git a/wait/http.go b/wait/http.go index 9315991c51..206f51afb3 100644 --- a/wait/http.go +++ b/wait/http.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "strconv" @@ -189,7 +188,7 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge // cache the body into a byte-slice so that it can be iterated over multiple times var body []byte if ws.Body != nil { - body, err = ioutil.ReadAll(ws.Body) + body, err = io.ReadAll(ws.Body) if err != nil { return } diff --git a/wait/http_test.go b/wait/http_test.go index 9e0e3aa8b4..cbfac78674 100644 --- a/wait/http_test.go +++ b/wait/http_test.go @@ -7,10 +7,11 @@ import ( "crypto/x509" "fmt" "io" - "io/ioutil" + "log" "net" "net/http" "os" + "path/filepath" "testing" "time" @@ -18,7 +19,6 @@ import ( "github.com/testcontainers/testcontainers-go/wait" ) -// // https://github.com/testcontainers/testcontainers-go/issues/183 func ExampleHTTPStrategy() { ctx := context.Background() @@ -36,9 +36,13 @@ func ExampleHTTPStrategy() { panic(err) } - defer gogs.Terminate(ctx) // nolint: errcheck - // Here you have a running container + defer func() { + if err := gogs.Terminate(ctx); err != nil { + log.Fatalf("failed to terminate container: %s", err) + } + }() + // Here you have a running container } func TestHTTPStrategyWaitUntilReady(t *testing.T) { @@ -48,8 +52,8 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) { return } - capath := workdir + "/testdata/root.pem" - cafile, err := ioutil.ReadFile(capath) + capath := filepath.Join(workdir, "testdata", "root.pem") + cafile, err := os.ReadFile(capath) if err != nil { t.Errorf("can't load ca file: %v", err) return @@ -65,13 +69,13 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) { var i int dockerReq := testcontainers.ContainerRequest{ FromDockerfile: testcontainers.FromDockerfile{ - Context: workdir + "/testdata", + Context: filepath.Join(workdir, "testdata"), }, ExposedPorts: []string{"6443/tcp"}, WaitingFor: wait.NewHTTPStrategy("/ping").WithTLS(true, tlsconfig). WithStartupTimeout(time.Second * 10).WithPort("6443/tcp"). WithResponseMatcher(func(body io.Reader) bool { - data, _ := ioutil.ReadAll(body) + data, _ := io.ReadAll(body) return bytes.Equal(data, []byte("pong")) }). WithStatusCodeMatcher(func(status int) bool { @@ -81,20 +85,24 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) { WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))), } - container, err := testcontainers.GenericContainer(context.Background(), - testcontainers.GenericContainerRequest{ContainerRequest: dockerReq, Started: true}) + ctx := context.Background() + container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ContainerRequest: dockerReq, Started: true}) if err != nil { t.Error(err) return } - defer container.Terminate(context.Background()) // nolint: errcheck + t.Cleanup(func() { + if err := container.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) - host, err := container.Host(context.Background()) + host, err := container.Host(ctx) if err != nil { t.Error(err) return } - port, err := container.MappedPort(context.Background(), "6443/tcp") + port, err := container.MappedPort(ctx, "6443/tcp") if err != nil { t.Error(err) return diff --git a/wait/log.go b/wait/log.go index d16b3d1b82..aa3d711475 100644 --- a/wait/log.go +++ b/wait/log.go @@ -2,7 +2,7 @@ package wait import ( "context" - "io/ioutil" + "io" "strings" "time" ) @@ -60,9 +60,10 @@ func (ws *LogStrategy) WithOccurrence(o int) *LogStrategy { // ForLog is the default construction for the fluid interface. // // For Example: -// wait. -// ForLog("some text"). -// WithPollInterval(1 * time.Second) +// +// wait. +// ForLog("some text"). +// WithPollInterval(1 * time.Second) func ForLog(log string) *LogStrategy { return NewLogStrategy(log) } @@ -80,12 +81,17 @@ LOOP: return ctx.Err() default: reader, err := target.Logs(ctx) + if err != nil { + time.Sleep(ws.PollInterval) + continue + } + b, err := io.ReadAll(reader) if err != nil { time.Sleep(ws.PollInterval) continue } - b, err := ioutil.ReadAll(reader) + logs := string(b) if strings.Count(logs, ws.Log) >= ws.Occurrence { break LOOP diff --git a/wait/log_test.go b/wait/log_test.go index fdd9db9fc3..c209cba0d6 100644 --- a/wait/log_test.go +++ b/wait/log_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "io/ioutil" "testing" "time" @@ -41,7 +40,7 @@ func (st noopStrategyTarget) State(ctx context.Context) (*types.ContainerState, func TestWaitForLog(t *testing.T) { target := noopStrategyTarget{ - ioReaderCloser: ioutil.NopCloser(bytes.NewReader([]byte("docker"))), + ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("docker"))), } wg := NewLogStrategy("docker").WithStartupTimeout(100 * time.Microsecond) err := wg.WaitUntilReady(context.Background(), target) @@ -52,7 +51,7 @@ func TestWaitForLog(t *testing.T) { func TestWaitWithExactNumberOfOccurrences(t *testing.T) { target := noopStrategyTarget{ - ioReaderCloser: ioutil.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker\n\rdocker"))), + ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker\n\rdocker"))), } wg := NewLogStrategy("docker"). WithStartupTimeout(100 * time.Microsecond). @@ -65,7 +64,7 @@ func TestWaitWithExactNumberOfOccurrences(t *testing.T) { func TestWaitWithExactNumberOfOccurrencesButItWillNeverHappen(t *testing.T) { target := noopStrategyTarget{ - ioReaderCloser: ioutil.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker"))), + ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker"))), } wg := NewLogStrategy("containerd"). WithStartupTimeout(100 * time.Microsecond). @@ -78,7 +77,7 @@ func TestWaitWithExactNumberOfOccurrencesButItWillNeverHappen(t *testing.T) { func TestWaitShouldFailWithExactNumberOfOccurrences(t *testing.T) { target := noopStrategyTarget{ - ioReaderCloser: ioutil.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker"))), + ioReaderCloser: io.NopCloser(bytes.NewReader([]byte("kubernetes\r\ndocker"))), } wg := NewLogStrategy("docker"). WithStartupTimeout(100 * time.Microsecond). diff --git a/wait/sql.go b/wait/sql.go index 4ef6da25ea..c18203ab0a 100644 --- a/wait/sql.go +++ b/wait/sql.go @@ -11,7 +11,7 @@ import ( const defaultForSqlQuery = "SELECT 1" -//ForSQL constructs a new waitForSql strategy for the given driver +// ForSQL constructs a new waitForSql strategy for the given driver func ForSQL(port nat.Port, driver string, url func(host string, port nat.Port) string) *waitForSql { return &waitForSql{ Port: port, @@ -32,7 +32,7 @@ type waitForSql struct { query string } -//Timeout sets the maximum waiting time for the strategy after which it'll give up and return an error +// Timeout sets the maximum waiting time for the strategy after which it'll give up and return an error // Deprecated: Use WithStartupTimeout func (w *waitForSql) Timeout(duration time.Duration) *waitForSql { return w.WithStartupTimeout(duration) @@ -44,19 +44,19 @@ func (w *waitForSql) WithStartupTimeout(startupTimeout time.Duration) *waitForSq return w } -//WithPollInterval can be used to override the default polling interval of 100 milliseconds +// WithPollInterval can be used to override the default polling interval of 100 milliseconds func (w *waitForSql) WithPollInterval(pollInterval time.Duration) *waitForSql { w.PollInterval = pollInterval return w } -//WithQuery can be used to override the default query used in the strategy. +// WithQuery can be used to override the default query used in the strategy. func (w *waitForSql) WithQuery(query string) *waitForSql { w.query = query return w } -//WaitUntilReady repeatedly tries to run "SELECT 1" or user defined query on the given port using sql and driver. +// WaitUntilReady repeatedly tries to run "SELECT 1" or user defined query on the given port using sql and driver. // // If it doesn't succeed until the timeout value which defaults to 60 seconds, it will return an error. func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) (err error) { diff --git a/wait/testdata/Dockerfile b/wait/testdata/Dockerfile index d89165a2e0..512f09c8fc 100644 --- a/wait/testdata/Dockerfile +++ b/wait/testdata/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.15-alpine as builder +FROM golang:1.18-alpine as builder WORKDIR /app COPY . . RUN mkdir -p dist diff --git a/wait/testdata/go.mod b/wait/testdata/go.mod index 9557d2288d..64fef1d966 100644 --- a/wait/testdata/go.mod +++ b/wait/testdata/go.mod @@ -1,3 +1,3 @@ module httptest -go 1.15 +go 1.18 diff --git a/wait/testdata/main.go b/wait/testdata/main.go index a8bdfc2ee4..ee8f642789 100644 --- a/wait/testdata/main.go +++ b/wait/testdata/main.go @@ -3,7 +3,7 @@ package main import ( "bytes" "context" - "io/ioutil" + "io" "log" "net/http" "os" @@ -19,7 +19,7 @@ func main() { }) mux.HandleFunc("/ping", func(w http.ResponseWriter, req *http.Request) { - data, _ := ioutil.ReadAll(req.Body) + data, _ := io.ReadAll(req.Body) if bytes.Equal(data, []byte("ping")) { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("pong"))