Skip to content

Commit

Permalink
fix: attended review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
henripqt committed Oct 15, 2024
1 parent 13cac89 commit f4f49e9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 34 deletions.
40 changes: 35 additions & 5 deletions modules/yugabytedb/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
24 changes: 12 additions & 12 deletions modules/yugabytedb/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions modules/yugabytedb/yugabytedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 (
Expand Down
24 changes: 12 additions & 12 deletions modules/yugabytedb/yugabytedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f4f49e9

Please sign in to comment.