From ecbbd627a2260d55352a00210045ebac997feaa4 Mon Sep 17 00:00:00 2001 From: Reo Uehara <47747828+uh-zz@users.noreply.github.com> Date: Sun, 17 Mar 2024 08:36:16 +0900 Subject: [PATCH 1/2] fix: typo in ci-test-go.yml (#2394) --- .github/workflows/ci-test-go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-test-go.yml b/.github/workflows/ci-test-go.yml index 2b31e6f435..a56dab64a4 100644 --- a/.github/workflows/ci-test-go.yml +++ b/.github/workflows/ci-test-go.yml @@ -55,7 +55,7 @@ jobs: if: ${{ inputs.rootless-docker }} uses: ScribeMD/rootless-docker@6bd157a512c2fafa4e0243a8aa87d964eb890886 # v0.2.2 - - name: Remove Docket root socket + - name: Remove Docker root socket if: ${{ inputs.rootless-docker }} run: sudo rm -rf /var/run/docker.sock From 9f1d656ea700b51e9a92a5976c13b1b8cb3a2c5c Mon Sep 17 00:00:00 2001 From: Barrett Strausser Date: Mon, 18 Mar 2024 05:31:22 -0400 Subject: [PATCH 2/2] feat(MustConn): Add MustConnectionString on (some) dbs (#2343) * Add MustConnectionString on (some) dbs * Remove requires * Update mod/sum after removing testify --------- Co-authored-by: bstrausser --- modules/mariadb/mariadb.go | 10 ++++++++++ modules/mariadb/mariadb_test.go | 5 +++++ modules/mysql/mysql.go | 9 +++++++++ modules/mysql/mysql_test.go | 4 ++++ modules/nats/nats.go | 8 ++++++++ modules/nats/nats_test.go | 5 +++++ modules/postgres/postgres.go | 10 ++++++++++ modules/postgres/postgres_test.go | 7 ++++++- 8 files changed, 57 insertions(+), 1 deletion(-) diff --git a/modules/mariadb/mariadb.go b/modules/mariadb/mariadb.go index b84d82c2ec..503473f3a5 100644 --- a/modules/mariadb/mariadb.go +++ b/modules/mariadb/mariadb.go @@ -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 { diff --git a/modules/mariadb/mariadb_test.go b/modules/mariadb/mariadb_test.go index e7399bdb09..a520f25758 100644 --- a/modules/mariadb/mariadb_test.go +++ b/modules/mariadb/mariadb_test.go @@ -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) diff --git a/modules/mysql/mysql.go b/modules/mysql/mysql.go index 2ebfce1710..4c004d38e2 100644 --- a/modules/mysql/mysql.go +++ b/modules/mysql/mysql.go @@ -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 { diff --git a/modules/mysql/mysql_test.go b/modules/mysql/mysql_test.go index daf5d879dc..cee869f494 100644 --- a/modules/mysql/mysql_test.go +++ b/modules/mysql/mysql_test.go @@ -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 { diff --git a/modules/nats/nats.go b/modules/nats/nats.go index 42cebc1590..3471824658 100644 --- a/modules/nats/nats.go +++ b/modules/nats/nats.go @@ -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) diff --git a/modules/nats/nats_test.go b/modules/nats/nats_test.go index c5aa12a86a..2fa6a7bf37 100644 --- a/modules/nats/nats_test.go +++ b/modules/nats/nats_test.go @@ -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) diff --git a/modules/postgres/postgres.go b/modules/postgres/postgres.go index 111a2a0953..cb3d01694b 100644 --- a/modules/postgres/postgres.go +++ b/modules/postgres/postgres.go @@ -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 diff --git a/modules/postgres/postgres_test.go b/modules/postgres/postgres_test.go index 3ce2692d26..2a74e147f6 100644 --- a/modules/postgres/postgres_test.go +++ b/modules/postgres/postgres_test.go @@ -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)