Skip to content

Commit

Permalink
Fix golangci-lint
Browse files Browse the repository at this point in the history
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
  • Loading branch information
karelbilek committed May 17, 2024
1 parent cf7544d commit 4e97e55
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 44 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 8 additions & 7 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -63,23 +64,23 @@ 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, imageName 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 imageName == repoTag {
return true, nil
}

if !strings.Contains(repoTag, "/") {
repoTag = "library/" + repoTag
}

if strings.HasSuffix(image, repoTag) {
if strings.HasSuffix(imageName, repoTag) {
return true, nil
}
}
Expand All @@ -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, imageName string, cfg *Options) error {
d.log.Info("pulling image")

reader, err := d.client.ImagePull(ctx, image, types.ImagePullOptions{
reader, err := d.client.ImagePull(ctx, imageName, image.PullOptions{
RegistryAuth: cfg.Auth,
})
if err != nil {
Expand Down Expand Up @@ -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)
}),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cleaner/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
)
Expand Down
1 change: 1 addition & 0 deletions internal/gnomockd/gnomockd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions internal/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions preset/azurite/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions preset/azurite/preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -148,6 +150,7 @@ func ExamplePreset() {
for _, v := range resp.Segment.BlobItems {
fmt.Println("filename:", *v.Name)
}

pages++
}
fmt.Println("pages after:", 1)
Expand Down
2 changes: 1 addition & 1 deletion preset/cockroachdb/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions preset/k3s/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
32 changes: 20 additions & 12 deletions preset/kafka/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion preset/localstack/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion preset/mariadb/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion preset/memcached/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion preset/mssql/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion preset/mysql/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion preset/postgres/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion preset/redis/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
2 changes: 1 addition & 1 deletion preset/vault/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e97e55

Please sign in to comment.