diff --git a/modules/yugabytedb/examples_test.go b/modules/yugabytedb/examples_test.go index b43685f29bf..eeab0788e8d 100644 --- a/modules/yugabytedb/examples_test.go +++ b/modules/yugabytedb/examples_test.go @@ -18,11 +18,11 @@ func ExampleRun() { yugabytedbContainer, err := yugabytedb.Run( ctx, "yugabytedb/yugabyte:2024.1.3.0-b105", - yugabytedb.WithYCQLKeyspace("custom-keyspace"), - yugabytedb.WithYCQLUser("custom-user"), - yugabytedb.WithYSQLDatabaseName("custom-db"), - yugabytedb.WithYSQLDatabaseUser("custom-user"), - yugabytedb.WithYSQLDatabasePassword("custom-password"), + yugabytedb.WithKeyspace("custom-keyspace"), + yugabytedb.WithUser("custom-user"), + yugabytedb.WithDatabaseName("custom-db"), + yugabytedb.WithDatabaseUser("custom-user"), + yugabytedb.WithDatabasePassword("custom-password"), ) if err != nil { @@ -78,6 +78,21 @@ func ExampleYugabyteDBContainer_YSQLConnectionString() { } defer db.Close() + + var ( + i int + row = db.QueryRowContext(ctx, "SELECT 1") + ) + + if err := row.Scan(&i); err != nil { + log.Printf("failed to scan row: %s", err) + return + } + + fmt.Println(i) + + // Output: + // 1 } func ExampleYugabyteDBContainer_YCQLConfigureClusterConfig() { @@ -109,4 +124,19 @@ func ExampleYugabyteDBContainer_YCQLConfigureClusterConfig() { } defer session.Close() + + var i int + if err := session.Query(` + SELECT COUNT(*) + FROM system_schema.keyspaces + WHERE keyspace_name = 'yugabyte' + `).Scan(&i); err != nil { + log.Printf("failed to scan row: %s", err) + return + } + + fmt.Println(i) + + // Output: + // 1 } diff --git a/modules/yugabytedb/options.go b/modules/yugabytedb/options.go index 9cdb3ce28dc..49b5385fd14 100644 --- a/modules/yugabytedb/options.go +++ b/modules/yugabytedb/options.go @@ -4,48 +4,48 @@ import ( "github.com/testcontainers/testcontainers-go" ) -// WithYSQLDatabaseName sets the initial database name for the yugabyteDB container. -func WithYSQLDatabaseName(ysqlDBName string) testcontainers.CustomizeRequestOption { +// WithDatabaseName sets the initial database name for the yugabyteDB container. +func WithDatabaseName(ysqlDBName string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { req.Env[ysqlDatabaseNameEnv] = ysqlDBName return nil } } -// WithYSQLDatabaseUser sets the initial database user for the yugabyteDB container. -func WithYSQLDatabaseUser(ysqlDBUser string) testcontainers.CustomizeRequestOption { +// WithDatabaseUser sets the initial database user for the yugabyteDB container. +func WithDatabaseUser(ysqlDBUser string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { req.Env[ysqlDatabaseUserEnv] = ysqlDBUser return nil } } -// WithYSQLDatabasePassword sets the initial database password for the yugabyteDB container. -func WithYSQLDatabasePassword(ysqlDBPassword string) testcontainers.CustomizeRequestOption { +// WithDatabasePassword sets the initial database password for the yugabyteDB container. +func WithDatabasePassword(ysqlDBPassword string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { req.Env[ysqlDatabasePasswordEnv] = ysqlDBPassword return nil } } -// WithYCQLKeyspace sets the initial keyspace for the yugabyteDB container. -func WithYCQLKeyspace(keyspace string) testcontainers.CustomizeRequestOption { +// WithKeyspace sets the initial keyspace for the yugabyteDB container. +func WithKeyspace(keyspace string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { req.Env[ycqlKeyspaceEnv] = keyspace return nil } } -// WithYCQLUser sets the initial user for the yugabyteDB container. -func WithYCQLUser(user string) testcontainers.CustomizeRequestOption { +// WithUser sets the initial user for the yugabyteDB container. +func WithUser(user string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { req.Env[ycqlUserNameEnv] = user return nil } } -// WithYCQLPassword sets the initial password for the yugabyteDB container. -func WithYCQLPassword(password string) testcontainers.CustomizeRequestOption { +// WithPassword sets the initial password for the yugabyteDB container. +func WithPassword(password string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { req.Env[ycqlPasswordEnv] = password return nil diff --git a/modules/yugabytedb/yugabytedb.go b/modules/yugabytedb/yugabytedb.go index 1180e658db5..2bf7e8ede8a 100644 --- a/modules/yugabytedb/yugabytedb.go +++ b/modules/yugabytedb/yugabytedb.go @@ -167,12 +167,12 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom func (y *YugabyteDBContainer) YSQLConnectionString(ctx context.Context, args ...string) (string, error) { host, err := y.Host(ctx) if err != nil { - return "", err + return "", fmt.Errorf("host: %w", err) } mappedPort, err := y.MappedPort(ctx, ysqlPort) if err != nil { - return "", err + return "", fmt.Errorf("mapped port: %w", err) } return fmt.Sprintf( @@ -204,17 +204,17 @@ func (y *YugabyteDBContainer) YSQLConnectionString(ctx context.Context, args ... func (y *YugabyteDBContainer) YCQLConfigureClusterConfig(ctx context.Context, cfg *gocql.ClusterConfig) error { host, err := y.Host(ctx) if err != nil { - return err + return fmt.Errorf("host: %w", err) } mappedPort, err := y.MappedPort(ctx, ycqlPort) if err != nil { - return err + return fmt.Errorf("mapped port: %w", err) } inspect, err := y.Container.Inspect(ctx) if err != nil { - return err + return fmt.Errorf("inspect: %w", err) } var ( diff --git a/modules/yugabytedb/yugabytedb_test.go b/modules/yugabytedb/yugabytedb_test.go index bb149d12954..6cae389a4e7 100644 --- a/modules/yugabytedb/yugabytedb_test.go +++ b/modules/yugabytedb/yugabytedb_test.go @@ -41,12 +41,12 @@ func TestYugabyteDB_YSQL(t *testing.T) { require.NoError(t, err) }) - t.Run("Run with custom options", func(t *testing.T) { + t.Run("custom-options", func(t *testing.T) { ctx := context.Background() ctr, err := yugabytedb.Run(ctx, "yugabytedb/yugabyte:2024.1.3.0-b105", - yugabytedb.WithYSQLDatabaseName("custom-db"), - yugabytedb.WithYSQLDatabaseUser("custom-user"), - yugabytedb.WithYSQLDatabasePassword("custom-password"), + yugabytedb.WithDatabaseName("custom-db"), + yugabytedb.WithDatabaseUser("custom-user"), + yugabytedb.WithDatabasePassword("custom-password"), ) testcontainers.CleanupContainer(t, ctr) require.NoError(t, err) @@ -83,18 +83,18 @@ func TestYugabyteDB_YCQL(t *testing.T) { session, err := cluster.CreateSession() require.NoError(t, err) - require.NoError(t, session.Close()) + t.Cleanup(func() { session.Close() }) }) - t.Run("Run with custom options", func(t *testing.T) { + t.Run("custom-options", func(t *testing.T) { ctx := context.Background() - opts := []testcontainers.ContainerCustomizer{ - yugabytedb.WithYCQLKeyspace("custom-keyspace"), - yugabytedb.WithYCQLUser("custom-user"), - yugabytedb.WithYCQLPassword("custom-password"), - } - ctr, err := yugabytedb.Run(ctx, "yugabytedb/yugabyte:2024.1.3.0-b105", opts...) + ctr, err := yugabytedb.Run(ctx, "yugabytedb/yugabyte:2024.1.3.0-b105", + yugabytedb.WithKeyspace("custom-keyspace"), + yugabytedb.WithUser("custom-user"), + yugabytedb.WithPassword("custom-password"), + ) + testcontainers.CleanupContainer(t, ctr) require.NoError(t, err)