Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MustConn): Add MustConnectionString on (some) dbs #2343

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return &MariaDBContainer{container, username, password, database}, nil
}

// MustConnectionString panics if the address cannot be determined.
func (c *MariaDBContainer) MustConnectionString(ctx context.Context, args ...string) string {
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
5 changes: 5 additions & 0 deletions modules/mariadb/mariadb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestMariaDB(t *testing.T) {
t.Fatal(err)
}

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

db, err := sql.Open("mysql", connectionString)
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 9 additions & 0 deletions modules/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return &MySQLContainer{container, username, password, database}, nil
}

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

func (c *MySQLContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
containerPort, err := c.MappedPort(ctx, "3306/tcp")
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions modules/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func TestMySQL(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mustConnectionString := container.MustConnectionString(ctx,"tls=skip-verify")
if mustConnectionString!=connectionString{
t.Errorf("ConnectionString was not equal to MustConnectionString")
}

db, err := sql.Open("mysql", connectionString)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions modules/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return &natsContainer, nil
}

func (c *NATSContainer) MustConnectionString(ctx context.Context, args ...string) string {
addr, err := c.ConnectionString(ctx,args...)
if err != nil {
panic(err)
}
return addr
}

// ConnectionString returns a connection string for the NATS container
func (c *NATSContainer) ConnectionString(ctx context.Context, args ...string) (string, error) {
mappedPort, err := c.MappedPort(ctx, defaultClientPort)
Expand Down
5 changes: 5 additions & 0 deletions modules/nats/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func TestNATS(t *testing.T) {
if err != nil {
t.Fatalf("failed to get connection string: %s", err)
}
mustUri := container.MustConnectionString(ctx)
if mustUri!=uri{
t.Errorf("URI was not equal to MustUri")
}


// perform assertions
nc, err := nats.Connect(uri)
Expand Down
10 changes: 10 additions & 0 deletions modules/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ type PostgresContainer struct {
snapshotName string
}


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

// ConnectionString returns the connection string for the postgres container, using the default 5432 port, and
// obtaining the host and exposed port from the container. It also accepts a variadic list of extra arguments
// which will be appended to the connection string. The format of the extra arguments is the same as the
Expand Down
7 changes: 6 additions & 1 deletion modules/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func TestPostgres(t *testing.T) {
connStr, err := container.ConnectionString(ctx, "sslmode=disable", "application_name=test")
// }
require.NoError(t, err)


mustConnStr := container.MustConnectionString(ctx,"sslmode=disable", "application_name=test")
if mustConnStr!=connStr{
t.Errorf("ConnectionString was not equal to MustConnectionString")
}

// Ensure connection string is using generic format
id, err := container.MappedPort(ctx, "5432/tcp")
require.NoError(t, err)
Expand Down
Loading