yugabytedb

package module
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithDatabaseName

func WithDatabaseName(dbName string) testcontainers.CustomizeRequestOption

WithDatabaseName sets the initial database name for the yugabyteDB container.

func WithDatabasePassword

func WithDatabasePassword(dbPassword string) testcontainers.CustomizeRequestOption

WithDatabasePassword sets the initial database password for the yugabyteDB container.

func WithDatabaseUser

func WithDatabaseUser(dbUser string) testcontainers.CustomizeRequestOption

WithDatabaseUser sets the initial database user for the yugabyteDB container.

func WithKeyspace

func WithKeyspace(keyspace string) testcontainers.CustomizeRequestOption

WithKeyspace sets the initial keyspace for the yugabyteDB container.

func WithPassword

func WithPassword(password string) testcontainers.CustomizeRequestOption

WithPassword sets the initial password for the yugabyteDB container.

func WithUser

func WithUser(user string) testcontainers.CustomizeRequestOption

WithUser sets the initial user for the yugabyteDB container.

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the yugabyteDB container type used in the module

Example (NewCluster)
ctx := context.Background()

yugabytedbContainer, err := yugabytedb.Run(
	ctx,
	"yugabytedb/yugabyte:2024.1.3.0-b105",
)
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

defer func() {
	if err := testcontainers.TerminateContainer(yugabytedbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

yugabytedbContainerHost, err := yugabytedbContainer.Host(ctx)
if err != nil {
	log.Printf("failed to get container host: %s", err)
	return
}

yugabyteContainerPort, err := yugabytedbContainer.MappedPort(ctx, "9042/tcp")
if err != nil {
	log.Printf("failed to get container port: %s", err)
	return
}

cluster := gocql.NewCluster(net.JoinHostPort(yugabytedbContainerHost, yugabyteContainerPort.Port()))
cluster.Keyspace = "yugabyte"
cluster.Authenticator = gocql.PasswordAuthenticator{
	Username: "yugabyte",
	Password: "yugabyte",
}

session, err := cluster.CreateSession()
if err != nil {
	log.Printf("failed to create session: %s", err)
	return
}

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

func Run

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)

Run creates an instance of the yugabyteDB container type and automatically starts it. A default configuration is used for the container, but it can be customized using the provided options. When using default configuration values it is recommended to use the provided *Container.YSQLConnectionString and [*Container.YCQLConfigureClusterConfig] methods to use the container in their respective clients.

Example
// runyugabyteDBContainer {
ctx := context.Background()

yugabytedbContainer, err := yugabytedb.Run(
	ctx,
	"yugabytedb/yugabyte:2024.1.3.0-b105",
	yugabytedb.WithKeyspace("custom-keyspace"),
	yugabytedb.WithUser("custom-user"),
	yugabytedb.WithDatabaseName("custom-db"),
	yugabytedb.WithDatabaseUser("custom-user"),
	yugabytedb.WithDatabasePassword("custom-password"),
)
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

defer func() {
	if err := testcontainers.TerminateContainer(yugabytedbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
// }

state, err := yugabytedbContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true

func (*Container) YSQLConnectionString

func (y *Container) YSQLConnectionString(ctx context.Context, args ...string) (string, error)

YSQLConnectionString returns a connection string for the yugabyteDB container using the configured database name, user, password, port, host and additional arguments. Additional arguments are appended to the connection string as query parameters in the form of key=value pairs separated by "&".

Example
ctx := context.Background()

yugabytedbContainer, err := yugabytedb.Run(
	ctx,
	"yugabytedb/yugabyte:2024.1.3.0-b105",
)
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

defer func() {
	if err := testcontainers.TerminateContainer(yugabytedbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

connStr, err := yugabytedbContainer.YSQLConnectionString(ctx, "sslmode=disable")
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

db, err := sql.Open("postgres", connStr)
if err != nil {
	log.Printf("failed to open connection: %s", err)
	return
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL