Skip to content

Commit

Permalink
Move to test-suite ahead of TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrowe committed Jan 22, 2024
1 parent 6a5f0b5 commit 9e4fa57
Showing 1 changed file with 90 additions and 36 deletions.
126 changes: 90 additions & 36 deletions modules/cockroachdb/cockroachdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,129 @@ import (
"testing"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

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

func TestCockroach_Ping(t *testing.T) {
func TestCockroach_Insecure(t *testing.T) {
suite.Run(t, &CockroachDBSuite{
url: "postgres://root@localhost:xxxxx/defaultdb?sslmode=disable",
})
}

func TestCockroach_NonRootAuthn(t *testing.T) {
suite.Run(t, &CockroachDBSuite{
url: "postgres://test@localhost:xxxxx/defaultdb?sslmode=disable",
opts: []testcontainers.ContainerCustomizer{
cockroachdb.WithUser("test"),
},
})
}

func TestCockroach_PasswordAuthn(t *testing.T) {
suite.Run(t, &CockroachDBSuite{
url: "postgres://foo:bar@localhost:xxxxx/defaultdb?sslmode=disable",
opts: []testcontainers.ContainerCustomizer{
cockroachdb.WithUser("foo"),
cockroachdb.WithPassword("bar"),
},
})
}

type CockroachDBSuite struct {
suite.Suite
url string
opts []testcontainers.ContainerCustomizer
}

func (suite *CockroachDBSuite) TestConnectionString() {
ctx := context.Background()

container, err := cockroachdb.RunContainer(ctx, suite.opts...)
suite.NoError(err)

Check failure on line 51 in modules/cockroachdb/cockroachdb_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, cockroachdb) / modules/cockroachdb/ubuntu-latest/1.20.x

require-error: for error assertions use require (testifylint)

suite.T().Cleanup(func() {
err := container.Terminate(ctx)
suite.NoError(err)

Check failure on line 55 in modules/cockroachdb/cockroachdb_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, cockroachdb) / modules/cockroachdb/ubuntu-latest/1.20.x

require-error: for error assertions use require (testifylint)
})

connStr, err := removePort(container.MustConnectionString(ctx))
suite.NoError(err)

Check failure on line 59 in modules/cockroachdb/cockroachdb_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, cockroachdb) / modules/cockroachdb/ubuntu-latest/1.20.x

require-error: for error assertions use require (testifylint)

suite.Equal(suite.url, connStr)
}

func (suite *CockroachDBSuite) TestPing() {
ctx := context.Background()

inputs := []struct {
name string
opts []testcontainers.ContainerCustomizer
conn string
}{
{
name: "defaults",
conn: "postgres://root@localhost:xxxxx/defaultdb?sslmode=disable",
// opts: suite.opts
},
{
name: "database",
opts: []testcontainers.ContainerCustomizer{
cockroachdb.WithDatabase("test"),
},
conn: "postgres://root@localhost:xxxxx/test?sslmode=disable",
},
{
name: "user",
opts: []testcontainers.ContainerCustomizer{
cockroachdb.WithUser("foo"),
},
conn: "postgres://foo@localhost:xxxxx/defaultdb?sslmode=disable",
},
{
name: "user & password",
opts: []testcontainers.ContainerCustomizer{
cockroachdb.WithUser("foo"),
cockroachdb.WithPassword("bar"),
},
conn: "postgres://foo:bar@localhost:xxxxx/defaultdb?sslmode=disable",
},
}

for _, input := range inputs {
t.Run(input.name, func(t *testing.T) {
container, err := cockroachdb.RunContainer(ctx, input.opts...)
require.NoError(t, err)
suite.Run(input.name, func() {
opts := append(suite.opts, input.opts...)

Check failure on line 85 in modules/cockroachdb/cockroachdb_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, cockroachdb) / modules/cockroachdb/ubuntu-latest/1.20.x

appendAssign: append result not assigned to the same slice (gocritic)
container, err := cockroachdb.RunContainer(ctx, opts...)
suite.NoError(err)

t.Cleanup(func() {
suite.T().Cleanup(func() {
err := container.Terminate(ctx)
require.NoError(t, err)
suite.NoError(err)
})

connStr := container.MustConnectionString(ctx)
require.Equal(t, input.conn, removePort(t, connStr))

conn, err := pgx.Connect(ctx, connStr)
require.NoError(t, err)
conn, err := pgx.Connect(ctx, container.MustConnectionString(ctx))
suite.NoError(err)

err = conn.Ping(ctx)
require.NoError(t, err)
suite.NoError(err)
})
}
}

func removePort(t *testing.T, dsn string) string {
t.Helper()
func (suite *CockroachDBSuite) TestQuery() {
ctx := context.Background()

container, err := cockroachdb.RunContainer(ctx, suite.opts...)
suite.NoError(err)

suite.T().Cleanup(func() {
err := container.Terminate(ctx)
suite.NoError(err)
})

conn, err := pgx.Connect(ctx, container.MustConnectionString(ctx))
suite.NoError(err)

u, err := url.Parse(dsn)
require.NoError(t, err)
_, err = conn.Exec(ctx, "CREATE TABLE test (id INT PRIMARY KEY)")
suite.NoError(err)

return strings.Replace(dsn, ":"+u.Port(), ":xxxxx", 1)
_, err = conn.Exec(ctx, "INSERT INTO test (id) VALUES (523123)")
suite.NoError(err)

var id int
err = conn.QueryRow(ctx, "SELECT id FROM test").Scan(&id)
suite.NoError(err)
suite.Equal(523123, id)
}

func removePort(s string) (string, error) {
u, err := url.Parse(s)
if err != nil {
return "", err
}
return strings.Replace(s, ":"+u.Port(), ":xxxxx", 1), nil
}

0 comments on commit 9e4fa57

Please sign in to comment.