Skip to content

Commit

Permalink
chore: bring golangci-lint back (#2571)
Browse files Browse the repository at this point in the history
* fix: enable golangci-lint again

It was accidentally dropped when bumping the Go version

* fix: lint

* fix: lint

* fix: lint

* fix: dolt module must raise errors with options

* fix: lint

* fix: return error in compose module

* fix: influxdb module must return error on customise

* chore: use assert

* fix: no named returns in k3s module

* chore: fix lint

* chore: lint

* fix: registry module must return error on customise

* fi: use same types
  • Loading branch information
mdelapenya committed Jun 10, 2024
1 parent 1889099 commit b856769
Show file tree
Hide file tree
Showing 23 changed files with 61 additions and 63 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ jobs:
id: go

- 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' }}
if: ${{ inputs.platform == 'ubuntu-latest' }}
uses: golangci/golangci-lint-action@9d1e0624a798bb64f6c3cea93db47765312263dc # v5
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
Expand Down
2 changes: 1 addition & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ func TestContainerInspect_RawInspectIsCleanedOnStop(t *testing.T) {

assert.NotEmpty(t, inspect.ID)

container.Stop(context.Background(), nil)
require.NoError(t, container.Stop(context.Background(), nil))

// type assertion to ensure that the container is a DockerContainer
dc := container.(*DockerContainer)
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestReadTCConfig(t *testing.T) {
``,
map[string]string{
"TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT": "13s",
"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT": "12s",
"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT": "12s",
},
Config{
RyukReconnectionTimeout: 13 * time.Second,
Expand All @@ -291,7 +291,7 @@ func TestReadTCConfig(t *testing.T) {
ryuk.reconnection.timeout=23s`,
map[string]string{
"TESTCONTAINERS_RYUK_RECONNECTION_TIMEOUT": "13s",
"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT": "12s",
"TESTCONTAINERS_RYUK_CONNECTION_TIMEOUT": "12s",
},
Config{
RyukReconnectionTimeout: 13 * time.Second,
Expand Down
30 changes: 15 additions & 15 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,17 @@ func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, m
title := module.Title()

data := sanitiseContent(content)
assert.Equal(t, data[0], "# "+title)
assert.Equal(t, "# "+title, data[0])
assert.Equal(t, `Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>`, data[2])
assert.Equal(t, "## Introduction", data[4])
assert.Equal(t, data[6], "The Testcontainers module for "+title+".")
assert.Equal(t, "The Testcontainers module for "+title+".", data[6])
assert.Equal(t, "## Adding this module to your project dependencies", data[8])
assert.Equal(t, data[10], "Please run the following command to add the "+title+" module to your Go dependencies:")
assert.Equal(t, data[13], "go get github.com/testcontainers/testcontainers-go/"+module.ParentDir()+"/"+lower)
assert.Equal(t, "Please run the following command to add the "+title+" module to your Go dependencies:", data[10])
assert.Equal(t, "go get github.com/testcontainers/testcontainers-go/"+module.ParentDir()+"/"+lower, data[13])
assert.Equal(t, "<!--codeinclude-->", data[18])
assert.Equal(t, data[19], "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/examples_test.go) inside_block:run"+title+"Container")
assert.Equal(t, "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/examples_test.go) inside_block:run"+title+"Container", data[19])
assert.Equal(t, "<!--/codeinclude-->", data[20])
assert.Equal(t, data[24], "The "+title+" module exposes one entrypoint function to create the "+title+" container, and this function receives two parameters:")
assert.Equal(t, "The "+title+" module exposes one entrypoint function to create the "+title+" container, and this function receives two parameters:", data[24])
assert.True(t, strings.HasSuffix(data[27], "(*"+title+"Container, error)"))
assert.Equal(t, "for "+title+". E.g. `testcontainers.WithImage(\""+module.Image+"\")`.", data[40])
}
Expand Down Expand Up @@ -422,15 +422,15 @@ func assertModuleContent(t *testing.T, module context.TestcontainersModule, exam
entrypoint := module.Entrypoint()

data := sanitiseContent(content)
assert.Equal(t, data[0], "package "+lower)
assert.Equal(t, data[9], "// "+containerName+" represents the "+exampleName+" container type used in the module")
assert.Equal(t, data[10], "type "+containerName+" struct {")
assert.Equal(t, data[14], "// "+entrypoint+" creates an instance of the "+exampleName+" container type")
assert.Equal(t, data[15], "func "+entrypoint+"(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*"+containerName+", error) {")
assert.Equal(t, data[17], "\t\tImage: \""+module.Image+"\",")
assert.Equal(t, data[26], "\t\tif err := opt.Customize(&genericContainerReq); err != nil {")
assert.Equal(t, data[27], "\t\t\treturn nil, fmt.Errorf(\"customize: %w\", err)")
assert.Equal(t, data[36], "\treturn &"+containerName+"{Container: container}, nil")
assert.Equal(t, "package "+lower, data[0])
assert.Equal(t, "// "+containerName+" represents the "+exampleName+" container type used in the module", data[9])
assert.Equal(t, "type "+containerName+" struct {", data[10])
assert.Equal(t, "// "+entrypoint+" creates an instance of the "+exampleName+" container type", data[14])
assert.Equal(t, "func "+entrypoint+"(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*"+containerName+", error) {", data[15])
assert.Equal(t, "\t\tImage: \""+module.Image+"\",", data[17])
assert.Equal(t, "\t\tif err := opt.Customize(&genericContainerReq); err != nil {", data[26])
assert.Equal(t, "\t\t\treturn nil, fmt.Errorf(\"customize: %w\", err)", data[27])
assert.Equal(t, "\treturn &"+containerName+"{Container: container}, nil", data[36])
}

// assert content GitHub workflow for the module
Expand Down
8 changes: 4 additions & 4 deletions modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) error {
for i, reader := range r {
tmp := os.TempDir()
tmp = filepath.Join(tmp, strconv.FormatInt(time.Now().UnixNano(), 10))
err := os.MkdirAll(tmp, 0755)
err := os.MkdirAll(tmp, 0o755)
if err != nil {
return fmt.Errorf("failed to create temporary directory: %w", err)
}
Expand All @@ -135,12 +135,12 @@ func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) error {

bs, err := io.ReadAll(reader)
if err != nil {
fmt.Errorf("failed to read from reader: %w", err)
return fmt.Errorf("failed to read from reader: %w", err)
}

err = os.WriteFile(filepath.Join(tmp, name), bs, 0644)
err = os.WriteFile(filepath.Join(tmp, name), bs, 0o644)
if err != nil {
fmt.Errorf("failed to write to temporary file: %w", err)
return fmt.Errorf("failed to write to temporary file: %w", err)
}

f[i] = filepath.Join(tmp, name)
Expand Down
1 change: 1 addition & 0 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ services:
require.True(t, os.IsNotExist(err), "File should be removed")
require.Nil(t, f, "File should be removed")
}

func TestDockerComposeAPIWithEnvironment(t *testing.T) {
identifier := testNameHash(t.Name())

Expand Down
4 changes: 3 additions & 1 deletion modules/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
opts = append(opts, WithDefaultCredentials())

for _, opt := range opts {
opt.Customize(&genericContainerReq)
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}

createUser := true
Expand Down
1 change: 0 additions & 1 deletion modules/elasticsearch/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ func TestElasticsearch8WithoutSSL(t *testing.T) {
}
})
}

}

func TestElasticsearch8WithoutCredentials(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion modules/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
}

for _, opt := range opts {
opt.Customize(&genericContainerReq)
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}

hasInitDb := false
Expand Down
4 changes: 2 additions & 2 deletions modules/influxdb/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestWithInitDb(t *testing.T) {
ctx := context.Background()
influxDbContainer, err := influxdb.RunContainer(ctx,
testcontainers.WithImage("influxdb:1.8.10"),
influxdb.WithInitDb(filepath.Join("testdata")),
influxdb.WithInitDb("testdata"),
)
require.NoError(t, err)
t.Cleanup(func() {
Expand Down Expand Up @@ -122,5 +122,5 @@ func TestWithConfigFile(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, "1.8.10", version)
assert.True(t, ping > 0)
assert.Greater(t, ping, time.Duration(0))
}
5 changes: 3 additions & 2 deletions modules/k3s/k3s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ func Test_LoadImages(t *testing.T) {
})
}

func getTestPodState(ctx context.Context, k8s *kubernetes.Clientset) (state corev1.ContainerState, err error) {
func getTestPodState(ctx context.Context, k8s *kubernetes.Clientset) (corev1.ContainerState, error) {
var pod *corev1.Pod
var err error
pod, err = k8s.CoreV1().Pods("default").Get(ctx, "test-pod", metav1.GetOptions{})
if err != nil || len(pod.Status.ContainerStatuses) == 0 {
return
return corev1.ContainerState{}, err
}
return pod.Status.ContainerStatuses[0].State, nil
}
Expand Down
4 changes: 0 additions & 4 deletions modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ type DownloadableFile struct {
func (d *DownloadableFile) getDownloadPath() string {
baseName := path.Base(d.Uri.Path)
return path.Join(d.DownloadDir, baseName)

}

func downloadFileFromDescription(d DownloadableFile) error {

client := http.Client{Timeout: time.Second * 60}
req, err := http.NewRequest(http.MethodGet, d.Uri.String(), nil)
if err != nil {
Expand All @@ -62,7 +60,6 @@ func downloadFileFromDescription(d DownloadableFile) error {

_, err = io.Copy(downloadedFile, resp.Body)
return err

}

// WithTestScript mounts the given script into the ./test directory in the container
Expand All @@ -78,7 +75,6 @@ func WithTestScript(scriptPath string) testcontainers.CustomizeRequestOption {
}

return WithTestScriptReader(f, scriptBaseName)

}

// WithTestScriptReader copies files into the Container using the Reader API
Expand Down
3 changes: 1 addition & 2 deletions modules/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,13 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize

// MustConnectionString panics if the address cannot be determined.
func (c *MariaDBContainer) MustConnectionString(ctx context.Context, args ...string) string {
addr, err := c.ConnectionString(ctx,args...)
addr, err := c.ConnectionString(ctx, args...)
if err != nil {
panic(err)
}
return addr
}


func (c *MariaDBContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
containerPort, err := c.MappedPort(ctx, "3306/tcp")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions modules/mariadb/mariadb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func TestMariaDB(t *testing.T) {
t.Fatal(err)
}

mustConnectionString := container.MustConnectionString(ctx,"tls=false")
if mustConnectionString!=connectionString{
mustConnectionString := container.MustConnectionString(ctx, "tls=false")
if mustConnectionString != connectionString {
t.Errorf("ConnectionString was not equal to MustConnectionString")
}

Expand Down
2 changes: 1 addition & 1 deletion modules/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize

// MustConnectionString panics if the address cannot be determined.
func (c *MySQLContainer) MustConnectionString(ctx context.Context, args ...string) string {
addr, err := c.ConnectionString(ctx,args...)
addr, err := c.ConnectionString(ctx, args...)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions modules/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestMySQL(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mustConnectionString := container.MustConnectionString(ctx,"tls=skip-verify")
if mustConnectionString!=connectionString{
mustConnectionString := container.MustConnectionString(ctx, "tls=skip-verify")
if mustConnectionString != connectionString {
t.Errorf("ConnectionString was not equal to MustConnectionString")
}

Expand Down
2 changes: 1 addition & 1 deletion modules/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
}

func (c *NATSContainer) MustConnectionString(ctx context.Context, args ...string) string {
addr, err := c.ConnectionString(ctx,args...)
addr, err := c.ConnectionString(ctx, args...)
if err != nil {
panic(err)
}
Expand Down
4 changes: 1 addition & 3 deletions modules/nats/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ func TestNATS(t *testing.T) {
t.Fatalf("failed to get connection string: %s", err)
}
mustUri := container.MustConnectionString(ctx)
if mustUri!=uri{
if mustUri != uri {
t.Errorf("URI was not equal to MustUri")
}


// perform assertions
nc, err := nats.Connect(uri)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions modules/ollama/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func ExampleRunContainer_withModel_llama2_http() {

_, _, err = ollamaContainer.Exec(ctx, []string{"ollama", "pull", model})
if err != nil {
log.Fatalf("failed to pull model %s: %s", model, err)
log.Fatalf("failed to pull model %s: %s", model, err) // nolint:gocritic
}

_, _, err = ollamaContainer.Exec(ctx, []string{"ollama", "run", model})
if err != nil {
log.Fatalf("failed to run model %s: %s", model, err)
log.Fatalf("failed to run model %s: %s", model, err) // nolint:gocritic
}

connectionStr, err := ollamaContainer.ConnectionString(ctx)
Expand Down Expand Up @@ -121,12 +121,12 @@ func ExampleRunContainer_withModel_llama2_langchain() {

_, _, err = ollamaContainer.Exec(ctx, []string{"ollama", "pull", model})
if err != nil {
log.Fatalf("failed to pull model %s: %s", model, err)
log.Fatalf("failed to pull model %s: %s", model, err) // nolint:gocritic
}

_, _, err = ollamaContainer.Exec(ctx, []string{"ollama", "run", model})
if err != nil {
log.Fatalf("failed to run model %s: %s", model, err)
log.Fatalf("failed to run model %s: %s", model, err) // nolint:gocritic
}

connectionStr, err := ollamaContainer.ConnectionString(ctx)
Expand Down
16 changes: 8 additions & 8 deletions modules/rabbitmq/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func ExampleRunContainer_connectUsingAmqp() {

amqpConnection, err := amqp.Dial(amqpURL)
if err != nil {
log.Fatalf("failed to connect to RabbitMQ: %s", err)
log.Fatalf("failed to connect to RabbitMQ: %s", err) // nolint:gocritic
}
defer func() {
err := amqpConnection.Close()
if err != nil {
log.Fatalf("failed to close connection: %s", err)
log.Fatalf("failed to close connection: %s", err) // nolint:gocritic
}
}()

Expand All @@ -92,7 +92,7 @@ func ExampleRunContainer_withSSL() {

tmpDir := os.TempDir()
certDirs := tmpDir + "/rabbitmq"
if err := os.MkdirAll(certDirs, 0755); err != nil {
if err := os.MkdirAll(certDirs, 0o755); err != nil {
log.Fatalf("failed to create temporary directory: %s", err)
}
defer os.RemoveAll(certDirs)
Expand All @@ -105,7 +105,7 @@ func ExampleRunContainer_withSSL() {
ParentDir: certDirs,
})
if caCert == nil {
log.Fatal("failed to generate CA certificate")
log.Fatal("failed to generate CA certificate") // nolint:gocritic
}

cert := tlscert.SelfSignedFromRequest(tlscert.Request{
Expand All @@ -116,7 +116,7 @@ func ExampleRunContainer_withSSL() {
ParentDir: certDirs,
})
if cert == nil {
log.Fatal("failed to generate certificate")
log.Fatal("failed to generate certificate") // nolint:gocritic
}

sslSettings := rabbitmq.SSLSettings{
Expand All @@ -133,13 +133,13 @@ func ExampleRunContainer_withSSL() {
rabbitmq.WithSSL(sslSettings),
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
log.Fatalf("failed to start container: %s", err) // nolint:gocritic
}
// }

defer func() {
if err := rabbitmqContainer.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
log.Fatalf("failed to terminate container: %s", err) // nolint:gocritic
}
}()

Expand Down Expand Up @@ -205,7 +205,7 @@ func ExampleRunContainer_withCustomConfigFile() {

bytes, err := io.ReadAll(logs)
if err != nil {
log.Fatalf("failed to read logs: %s", err)
log.Fatalf("failed to read logs: %s", err) // nolint:gocritic
}

fmt.Println(strings.Contains(string(bytes), "config file(s) : /etc/rabbitmq/rabbitmq-testcontainers.conf"))
Expand Down
5 changes: 3 additions & 2 deletions modules/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func (c *RegistryContainer) DeleteImage(ctx context.Context, imageRef string) er
return false
}).
WaitUntilReady(ctx, c)

if err != nil {
return fmt.Errorf("failed to get image digest: %w", err)
}
Expand Down Expand Up @@ -177,7 +176,9 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
}

for _, opt := range opts {
opt.Customize(&genericContainerReq)
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}

container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
Expand Down
Loading

0 comments on commit b856769

Please sign in to comment.