From 5e09139a2076a119db88302822e35ce104760743 Mon Sep 17 00:00:00 2001 From: Karel Bilek Date: Fri, 17 May 2024 22:37:30 +0200 Subject: [PATCH] Fix golangci-lint Update golangci-lint (which fixes the weird timeout), also use the action (which caches the results for faster CI), also fixes all the issues reported. --- .github/workflows/test.yaml | 19 ++++++++---------- docker.go | 15 +++++++------- internal/cleaner/cleaner_test.go | 2 +- internal/gnomockd/gnomockd_test.go | 1 + internal/health/health_test.go | 4 ++-- preset/azurite/preset.go | 4 ++-- preset/azurite/preset_test.go | 3 +++ preset/cockroachdb/preset.go | 2 +- preset/k3s/options_test.go | 1 + preset/kafka/preset.go | 32 +++++++++++++++++++----------- preset/localstack/preset.go | 2 +- preset/mariadb/preset.go | 2 +- preset/memcached/preset.go | 2 +- preset/mssql/preset.go | 2 +- preset/mysql/preset.go | 2 +- preset/postgres/preset.go | 2 +- preset/redis/preset.go | 2 +- preset/vault/preset.go | 2 +- 18 files changed, 55 insertions(+), 44 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 409a85bb..43b653f3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -9,18 +9,15 @@ jobs: name: "[core] lint" runs-on: ubuntu-latest steps: - - name: Set up Go 1.21 - uses: actions/setup-go@v1 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: - go-version: 1.21 - - name: Check out code into the Go module directory - uses: actions/checkout@v1 - - name: Get dependencies - run: go get -v -t -d ./... - - name: Get golangci-lint - run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.51.2 - - name: Lint - run: $(go env GOPATH)/bin/golangci-lint run --timeout 10m0s ./... + go-version: '1.21' + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.58.1 + args: --timeout=10m test-core: name: "[core] gnomock, gnomockd" diff --git a/docker.go b/docker.go index 8b8104ff..6c6a4869 100644 --- a/docker.go +++ b/docker.go @@ -16,6 +16,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/mount" "github.com/docker/docker/client" "github.com/docker/docker/errdefs" @@ -63,15 +64,15 @@ func (g *g) dockerConnect() (*docker, error) { return &docker{client: cli, log: g.log}, nil } -func (d *docker) isExistingLocalImage(ctx context.Context, image string) (bool, error) { - images, err := d.client.ImageList(ctx, types.ImageListOptions{All: true}) +func (d *docker) isExistingLocalImage(ctx context.Context, imagen string) (bool, error) { + images, err := d.client.ImageList(ctx, image.ListOptions{All: true}) if err != nil { return false, fmt.Errorf("can't list image: %w", err) } for _, img := range images { for _, repoTag := range img.RepoTags { - if image == repoTag { + if imagen == repoTag { return true, nil } @@ -79,7 +80,7 @@ func (d *docker) isExistingLocalImage(ctx context.Context, image string) (bool, repoTag = "library/" + repoTag } - if strings.HasSuffix(image, repoTag) { + if strings.HasSuffix(imagen, repoTag) { return true, nil } } @@ -88,10 +89,10 @@ func (d *docker) isExistingLocalImage(ctx context.Context, image string) (bool, return false, nil } -func (d *docker) pullImage(ctx context.Context, image string, cfg *Options) error { +func (d *docker) pullImage(ctx context.Context, imagen string, cfg *Options) error { d.log.Info("pulling image") - reader, err := d.client.ImagePull(ctx, image, types.ImagePullOptions{ + reader, err := d.client.ImagePull(ctx, imagen, image.PullOptions{ RegistryAuth: cfg.Auth, }) if err != nil { @@ -173,7 +174,7 @@ func (d *docker) setupContainerCleanup(id string, cfg *Options) chan string { WithHealthCheck(func(ctx context.Context, c *Container) error { return health.HTTPGet(ctx, c.DefaultAddress()) }), - WithInit(func(ctx context.Context, c *Container) error { + WithInit(func(_ context.Context, c *Container) error { return cleaner.Notify(context.Background(), c.DefaultAddress(), id) }), } diff --git a/internal/cleaner/cleaner_test.go b/internal/cleaner/cleaner_test.go index c5d5eaf1..c1cf2e1b 100644 --- a/internal/cleaner/cleaner_test.go +++ b/internal/cleaner/cleaner_test.go @@ -32,7 +32,7 @@ func TestCleaner(t *testing.T) { gnomock.WithHealthCheck(func(ctx context.Context, c *gnomock.Container) error { return health.HTTPGet(ctx, c.DefaultAddress()) }), - gnomock.WithInit(func(ctx context.Context, c *gnomock.Container) error { + gnomock.WithInit(func(_ context.Context, c *gnomock.Container) error { return cleaner.Notify(rootCtx, c.DefaultAddress(), targetContainer.ID) }), ) diff --git a/internal/gnomockd/gnomockd_test.go b/internal/gnomockd/gnomockd_test.go index a1b07dd8..f86f5eb1 100644 --- a/internal/gnomockd/gnomockd_test.go +++ b/internal/gnomockd/gnomockd_test.go @@ -109,6 +109,7 @@ func TestGnomockd(t *testing.T) { h.ServeHTTP(w, r) res := w.Result() + t.Cleanup(func() { require.NoError(t, res.Body.Close()) }) require.Equal(t, http.StatusOK, res.StatusCode) diff --git a/internal/health/health_test.go b/internal/health/health_test.go index 440a1fb8..e4553af0 100644 --- a/internal/health/health_test.go +++ b/internal/health/health_test.go @@ -22,7 +22,7 @@ func TestHTTPGet(t *testing.T) { }) t.Run("invalid status code", func(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) defer s.Close() @@ -32,7 +32,7 @@ func TestHTTPGet(t *testing.T) { }) t.Run("success", func(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })) defer s.Close() diff --git a/preset/azurite/preset.go b/preset/azurite/preset.go index 01d19bc8..958eea9b 100644 --- a/preset/azurite/preset.go +++ b/preset/azurite/preset.go @@ -80,7 +80,7 @@ func (p *P) setDefaults() { } func (p *P) healthcheck() gnomock.HealthcheckFunc { - return func(ctx context.Context, c *gnomock.Container) (err error) { + return func(_ context.Context, c *gnomock.Container) (err error) { // needs implementation. unfortunately azurite does not offer a health endpoint // a netcat p.healtCheckAddress(c) -z could help alternatively _ = p.healthCheckAddress(c) @@ -97,7 +97,7 @@ func (p *P) healthCheckAddress(c *gnomock.Container) string { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { err := p.initBlobstorage(c) if err != nil { return fmt.Errorf("can't init blob storage: %w", err) diff --git a/preset/azurite/preset_test.go b/preset/azurite/preset_test.go index c4c9a47f..c297e403 100644 --- a/preset/azurite/preset_test.go +++ b/preset/azurite/preset_test.go @@ -54,6 +54,7 @@ func testBlobStorage(version string) func(*testing.T) { resp, err := pager.NextPage(context.Background()) require.NoError(t, err) require.Equal(t, 0, len(resp.Segment.BlobItems)) + pages++ } require.Equal(t, 1, pages) @@ -130,6 +131,7 @@ func ExamplePreset() { for pager.More() { resp, _ := pager.NextPage(context.Background()) fmt.Println("keys before:", len(resp.Segment.BlobItems)) + pages++ } fmt.Println("pages before:", pages) @@ -148,6 +150,7 @@ func ExamplePreset() { for _, v := range resp.Segment.BlobItems { fmt.Println("filename:", *v.Name) } + pages++ } fmt.Println("pages after:", 1) diff --git a/preset/cockroachdb/preset.go b/preset/cockroachdb/preset.go index 87673de0..95852a08 100644 --- a/preset/cockroachdb/preset.go +++ b/preset/cockroachdb/preset.go @@ -100,7 +100,7 @@ func healthcheck(_ context.Context, c *gnomock.Container) error { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { db, err := connect(c, "") if err != nil { return err diff --git a/preset/k3s/options_test.go b/preset/k3s/options_test.go index b7d3c5ee..0627b576 100644 --- a/preset/k3s/options_test.go +++ b/preset/k3s/options_test.go @@ -59,6 +59,7 @@ func TestWithVersion(t *testing.T) { p := k3s.Preset(k3s.WithVersion(tt.inVersion)) assert.IsType(t, &k3s.P{}, p) + if k3sp, ok := p.(*k3s.P); ok { assert.Equal(t, tt.expectedSetVersion, k3sp.Version) } diff --git a/preset/kafka/preset.go b/preset/kafka/preset.go index 1aa9c808..889d495d 100644 --- a/preset/kafka/preset.go +++ b/preset/kafka/preset.go @@ -153,21 +153,29 @@ func (p *P) healthcheck(ctx context.Context, c *gnomock.Container) (err error) { } if p.UseSchemaRegistry { - url := "http://" + c.Address(SchemaRegistryPort) - - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) - if err != nil { - return fmt.Errorf("invalid request: %w", err) + if err := p.healthcheckRegistry(ctx, c); err != nil { + return err } + } - res, err := http.DefaultClient.Do(req) - if err != nil { - return fmt.Errorf("schema registry is not available: %w", err) - } + return nil +} - if err := res.Body.Close(); err != nil { - return fmt.Errorf("error closing schema registry response body: %w", err) - } +func (p *P) healthcheckRegistry(ctx context.Context, c *gnomock.Container) error { + url := "http://" + c.Address(SchemaRegistryPort) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return fmt.Errorf("invalid request: %w", err) + } + + res, err := http.DefaultClient.Do(req) + if err != nil { + return fmt.Errorf("schema registry is not available: %w", err) + } + + if err := res.Body.Close(); err != nil { + return fmt.Errorf("error closing schema registry response body: %w", err) } return nil diff --git a/preset/localstack/preset.go b/preset/localstack/preset.go index 06854e11..9e49e457 100644 --- a/preset/localstack/preset.go +++ b/preset/localstack/preset.go @@ -190,7 +190,7 @@ type healthResponse struct { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { for _, s := range p.Services { if s == S3 { err := p.initS3(c) diff --git a/preset/mariadb/preset.go b/preset/mariadb/preset.go index 51ed942f..517edf65 100644 --- a/preset/mariadb/preset.go +++ b/preset/mariadb/preset.go @@ -104,7 +104,7 @@ func (p *P) healthcheck(_ context.Context, c *gnomock.Container) error { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { addr := c.Address(gnomock.DefaultPort) db, err := p.connect(addr) diff --git a/preset/memcached/preset.go b/preset/memcached/preset.go index 9717191a..7c0bf2d3 100644 --- a/preset/memcached/preset.go +++ b/preset/memcached/preset.go @@ -57,7 +57,7 @@ func (p *P) Options() []gnomock.Option { } if p.ByteValues != nil || p.Values != nil { - initf := func(ctx context.Context, c *gnomock.Container) error { + initf := func(_ context.Context, c *gnomock.Container) error { addr := c.Address(gnomock.DefaultPort) client := memcache.New(addr) diff --git a/preset/mssql/preset.go b/preset/mssql/preset.go index e1b60708..1be3bc5d 100644 --- a/preset/mssql/preset.go +++ b/preset/mssql/preset.go @@ -96,7 +96,7 @@ func (p *P) healthcheck(_ context.Context, c *gnomock.Container) error { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { addr := c.Address(gnomock.DefaultPort) db, err := p.connect(addr, masterDB) diff --git a/preset/mysql/preset.go b/preset/mysql/preset.go index 0e29d157..043aaead 100644 --- a/preset/mysql/preset.go +++ b/preset/mysql/preset.go @@ -106,7 +106,7 @@ func (p *P) healthcheck(_ context.Context, c *gnomock.Container) error { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { addr := c.Address(gnomock.DefaultPort) db, err := p.connect(addr) diff --git a/preset/postgres/preset.go b/preset/postgres/preset.go index 35128d6d..f56bb494 100644 --- a/preset/postgres/preset.go +++ b/preset/postgres/preset.go @@ -106,7 +106,7 @@ func (p *P) healthcheck(_ context.Context, c *gnomock.Container) error { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { if p.DB != defaultDatabase { db, err := connect(c, defaultDatabase) if err != nil { diff --git a/preset/redis/preset.go b/preset/redis/preset.go index 3b2a2b7e..241c4451 100644 --- a/preset/redis/preset.go +++ b/preset/redis/preset.go @@ -56,7 +56,7 @@ func (p *P) Options() []gnomock.Option { } if p.Values != nil { - initf := func(ctx context.Context, c *gnomock.Container) error { + initf := func(_ context.Context, c *gnomock.Container) error { addr := c.Address(gnomock.DefaultPort) client := redisclient.NewClient(&redisclient.Options{Addr: addr}) diff --git a/preset/vault/preset.go b/preset/vault/preset.go index be78efc9..b1bd5334 100644 --- a/preset/vault/preset.go +++ b/preset/vault/preset.go @@ -88,7 +88,7 @@ func (p *P) setDefaults() { } func (p *P) initf() gnomock.InitFunc { - return func(ctx context.Context, c *gnomock.Container) error { + return func(_ context.Context, c *gnomock.Container) error { cli, err := Client(c, p.AuthToken) if err != nil { return err