Skip to content

Commit

Permalink
chore: enforce test package in modules (#2241)
Browse files Browse the repository at this point in the history
* fix: include import of the log package

* chore: use test package in new modules

* chore: use test package in modules

* chore: improve mssql test

* chore: fix test

* fix: lint
  • Loading branch information
mdelapenya committed Feb 16, 2024
1 parent 5054ae1 commit a3f02ab
Show file tree
Hide file tree
Showing 20 changed files with 475 additions and 428 deletions.
1 change: 1 addition & 0 deletions modulegen/_template/examples_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"fmt"
"log"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/{{ ParentDir }}/{{ $lower }}"
Expand Down
5 changes: 3 additions & 2 deletions modulegen/_template/module_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{{ $entrypoint := Entrypoint }}{{ $image := Image }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}
{{ $entrypoint := Entrypoint }}{{ $image := Image }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}_test

import (
"context"
"testing"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/{{ ParentDir }}/{{ $lower }}"
)

func Test{{ $title }}(t *testing.T) {
ctx := context.Background()

container, err := {{ $entrypoint }}(ctx, testcontainers.WithImage("{{ $image }}"))
container, err := {{ $lower }}.{{ $entrypoint }}(ctx, testcontainers.WithImage("{{ $image }}"))
if err != nil {
t.Fatal(err)
}
Expand Down
24 changes: 12 additions & 12 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,15 @@ func assertExamplesTestContent(t *testing.T, module context.TestcontainersModule
title := module.Title()

data := sanitiseContent(content)
assert.Equal(t, data[0], "package "+lower+"_test")
assert.Equal(t, "\t\"github.com/testcontainers/testcontainers-go\"", data[6])
assert.Equal(t, data[7], "\t\"github.com/testcontainers/testcontainers-go/modules/"+lower+"\"")
assert.Equal(t, data[10], "func Example"+entrypoint+"() {")
assert.Equal(t, data[11], "\t// run"+title+"Container {")
assert.Equal(t, data[14], "\t"+lower+"Container, err := "+lower+"."+entrypoint+"(ctx, testcontainers.WithImage(\""+module.Image+"\"))")
assert.Equal(t, "\tfmt.Println(state.Running)", data[32])
assert.Equal(t, "\t// Output:", data[34])
assert.Equal(t, "\t// true", data[35])
assert.Equal(t, "package "+lower+"_test", data[0])
assert.Equal(t, "\t\"github.com/testcontainers/testcontainers-go\"", data[7])
assert.Equal(t, "\t\"github.com/testcontainers/testcontainers-go/modules/"+lower+"\"", data[8])
assert.Equal(t, "func Example"+entrypoint+"() {", data[11])
assert.Equal(t, "\t// run"+title+"Container {", data[12])
assert.Equal(t, "\t"+lower+"Container, err := "+lower+"."+entrypoint+"(ctx, testcontainers.WithImage(\""+module.Image+"\"))", data[15])
assert.Equal(t, "\tfmt.Println(state.Running)", data[33])
assert.Equal(t, "\t// Output:", data[35])
assert.Equal(t, "\t// true", data[36])
}

// assert content module test
Expand All @@ -447,9 +447,9 @@ func assertModuleTestContent(t *testing.T, module context.TestcontainersModule,
require.NoError(t, err)

data := sanitiseContent(content)
assert.Equal(t, data[0], "package "+module.Lower())
assert.Equal(t, data[9], "func Test"+module.Title()+"(t *testing.T) {")
assert.Equal(t, data[12], "\tcontainer, err := "+module.Entrypoint()+"(ctx, testcontainers.WithImage(\""+module.Image+"\"))")
assert.Equal(t, "package "+module.Lower()+"_test", data[0])
assert.Equal(t, "func Test"+module.Title()+"(t *testing.T) {", data[10])
assert.Equal(t, "\tcontainer, err := "+module.Lower()+"."+module.Entrypoint()+"(ctx, testcontainers.WithImage(\""+module.Image+"\"))", data[13])
}

// assert content module
Expand Down
12 changes: 7 additions & 5 deletions modules/cassandra/cassandra_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cassandra
package cassandra_test

import (
"context"
Expand All @@ -8,6 +8,8 @@ import (
"github.com/gocql/gocql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/modules/cassandra"
)

type Test struct {
Expand All @@ -18,7 +20,7 @@ type Test struct {
func TestCassandra(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx)
container, err := cassandra.RunContainer(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -58,7 +60,7 @@ func TestCassandra(t *testing.T) {
func TestCassandraWithConfigFile(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx, WithConfigFile(filepath.Join("testdata", "config.yaml")))
container, err := cassandra.RunContainer(ctx, cassandra.WithConfigFile(filepath.Join("testdata", "config.yaml")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -89,7 +91,7 @@ func TestCassandraWithInitScripts(t *testing.T) {
ctx := context.Background()

// withInitScripts {
container, err := RunContainer(ctx, WithInitScripts(filepath.Join("testdata", "init.cql")))
container, err := cassandra.RunContainer(ctx, cassandra.WithInitScripts(filepath.Join("testdata", "init.cql")))
// }
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -121,7 +123,7 @@ func TestCassandraWithInitScripts(t *testing.T) {
t.Run("with init bash script", func(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx, WithInitScripts(filepath.Join("testdata", "init.sh")))
container, err := cassandra.RunContainer(ctx, cassandra.WithInitScripts(filepath.Join("testdata", "init.sh")))
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions modules/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const (
// ClickHouseContainer represents the ClickHouse container type used in the module
type ClickHouseContainer struct {
testcontainers.Container
dbName string
user string
password string
DbName string
User string
Password string
}

// ConnectionHost returns the host and port of the clickhouse container, using the default, native 9000 port, and
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c *ClickHouseContainer) ConnectionString(ctx context.Context, args ...stri
extraArgs = "?" + extraArgs
}

connectionString := fmt.Sprintf("clickhouse://%s:%s@%s/%s%s", c.user, c.password, host, c.dbName, extraArgs)
connectionString := fmt.Sprintf("clickhouse://%s:%s@%s/%s%s", c.User, c.Password, host, c.DbName, extraArgs)
return connectionString, nil
}

Expand Down Expand Up @@ -237,5 +237,5 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
password := req.Env["CLICKHOUSE_PASSWORD"]
dbName := req.Env["CLICKHOUSE_DB"]

return &ClickHouseContainer{Container: container, dbName: dbName, password: password, user: user}, nil
return &ClickHouseContainer{Container: container, DbName: dbName, Password: password, User: user}, nil
}
53 changes: 27 additions & 26 deletions modules/clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package clickhouse
package clickhouse_test

import (
"context"
Expand All @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/clickhouse"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand All @@ -29,7 +30,7 @@ type Test struct {
func TestClickHouseDefaultConfig(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx)
container, err := clickhouse.RunContainer(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,9 +46,9 @@ func TestClickHouseDefaultConfig(t *testing.T) {
conn, err := ch.Open(&ch.Options{
Addr: []string{connectionHost},
Auth: ch.Auth{
Database: container.dbName,
Username: container.user,
Password: container.password,
Database: container.DbName,
Username: container.User,
Password: container.Password,
},
})
require.NoError(t, err)
Expand All @@ -61,10 +62,10 @@ func TestClickHouseDefaultConfig(t *testing.T) {
func TestClickHouseConnectionHost(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx,
WithUsername(user),
WithPassword(password),
WithDatabase(dbname),
container, err := clickhouse.RunContainer(ctx,
clickhouse.WithUsername(user),
clickhouse.WithPassword(password),
clickhouse.WithDatabase(dbname),
)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestClickHouseConnectionHost(t *testing.T) {
func TestClickHouseDSN(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx, WithUsername(user), WithPassword(password), WithDatabase(dbname))
container, err := clickhouse.RunContainer(ctx, clickhouse.WithUsername(user), clickhouse.WithPassword(password), clickhouse.WithDatabase(dbname))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -134,11 +135,11 @@ func TestClickHouseWithInitScripts(t *testing.T) {
ctx := context.Background()

// withInitScripts {
container, err := RunContainer(ctx,
WithUsername(user),
WithPassword(password),
WithDatabase(dbname),
WithInitScripts(filepath.Join("testdata", "init-db.sh")),
container, err := clickhouse.RunContainer(ctx,
clickhouse.WithUsername(user),
clickhouse.WithPassword(password),
clickhouse.WithDatabase(dbname),
clickhouse.WithInitScripts(filepath.Join("testdata", "init-db.sh")),
)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -178,15 +179,15 @@ func TestClickHouseWithConfigFile(t *testing.T) {
desc string
configOption testcontainers.CustomizeRequestOption
}{
{"XML_Config", WithConfigFile(filepath.Join("testdata", "config.xml"))}, // <allow_no_password>1</allow_no_password>
{"YAML_Config", WithYamlConfigFile(filepath.Join("testdata", "config.yaml"))}, // allow_no_password: true
{"XML_Config", clickhouse.WithConfigFile(filepath.Join("testdata", "config.xml"))}, // <allow_no_password>1</allow_no_password>
{"YAML_Config", clickhouse.WithYamlConfigFile(filepath.Join("testdata", "config.yaml"))}, // allow_no_password: true
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
container, err := RunContainer(ctx,
WithUsername(user),
WithPassword(""),
WithDatabase(dbname),
container, err := clickhouse.RunContainer(ctx,
clickhouse.WithUsername(user),
clickhouse.WithPassword(""),
clickhouse.WithDatabase(dbname),
tC.configOption,
)
if err != nil {
Expand Down Expand Up @@ -244,11 +245,11 @@ func TestClickHouseWithZookeeper(t *testing.T) {
t.Fatal(err)
}

container, err := RunContainer(ctx,
WithUsername(user),
WithPassword(password),
WithDatabase(dbname),
WithZookeeper(ipaddr, zkPort.Port()),
container, err := clickhouse.RunContainer(ctx,
clickhouse.WithUsername(user),
clickhouse.WithPassword(password),
clickhouse.WithDatabase(dbname),
clickhouse.WithZookeeper(ipaddr, zkPort.Port()),
)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 4 additions & 2 deletions modules/k6/k6_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package k6
package k6_test

import (
"context"
"path/filepath"
"testing"

"github.com/testcontainers/testcontainers-go/modules/k6"
)

func TestK6(t *testing.T) {
Expand Down Expand Up @@ -34,7 +36,7 @@ func TestK6(t *testing.T) {
t.Fatal(err)
}

container, err := RunContainer(ctx, WithCache(), WithTestScript(absPath))
container, err := k6.RunContainer(ctx, k6.WithCache(), k6.WithTestScript(absPath))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/kafka/consumer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kafka
package kafka_test

import (
"testing"
Expand Down

0 comments on commit a3f02ab

Please sign in to comment.