cockroachdb

package module
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTLSNotEnabled = fmt.Errorf("tls not enabled")

Functions

This section is empty.

Types

type CockroachDBContainer

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

CockroachDBContainer represents the CockroachDB container type used in the module

func RunContainer

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*CockroachDBContainer, error)

RunContainer creates an instance of the CockroachDB container type

Example
package main

import (
	"context"
	"fmt"
	"log"
	"net/url"

	"github.com/testcontainers/testcontainers-go/modules/cockroachdb"
)

func main() {
	// runCockroachDBContainer {
	ctx := context.Background()

	cockroachdbContainer, err := cockroachdb.RunContainer(ctx)
	if err != nil {
		log.Fatalf("failed to start container: %s", err)
	}

	// Clean up the container
	defer func() {
		if err := cockroachdbContainer.Terminate(ctx); err != nil {
			log.Fatalf("failed to terminate container: %s", err)
		}
	}()
	// }

	state, err := cockroachdbContainer.State(ctx)
	if err != nil {
		log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
	}
	fmt.Println(state.Running)

	addr, err := cockroachdbContainer.ConnectionString(ctx)
	if err != nil {
		log.Fatalf("failed to get connection string: %s", err)
	}
	u, err := url.Parse(addr)
	if err != nil {
		log.Fatalf("failed to parse connection string: %s", err)
	}
	u.Host = fmt.Sprintf("%s:%s", u.Hostname(), "xxx")
	fmt.Println(u.String())

}
Output:

true
postgres://root@localhost:xxx/defaultdb?sslmode=disable

func (*CockroachDBContainer) ConnectionString

func (c *CockroachDBContainer) ConnectionString(ctx context.Context) (string, error)

ConnectionString returns the dial address to open a new connection to CockroachDB.

func (*CockroachDBContainer) MustConnectionString

func (c *CockroachDBContainer) MustConnectionString(ctx context.Context) string

MustConnectionString panics if the address cannot be determined.

func (*CockroachDBContainer) TLSConfig

func (c *CockroachDBContainer) TLSConfig() (*tls.Config, error)

TLSConfig returns config necessary to connect to CockroachDB over TLS.

type Option

type Option func(*options)

Option is an option for the CockroachDB container.

func WithDatabase

func WithDatabase(database string) Option

WithDatabase sets the name of the database to use.

func WithPassword

func WithPassword(password string) Option

WithPassword sets the password when using password authentication.

func WithStoreSize

func WithStoreSize(size string) Option

WithStoreSize sets the amount of available in-memory storage. See https://www.cockroachlabs.com/docs/stable/cockroach-start#store

func WithTLS

func WithTLS(cfg *TLSConfig) Option

WithTLS enables TLS on the CockroachDB container. Cert and key must be PEM-encoded.

func WithUser

func WithUser(user string) Option

WithUser creates & sets the user to connect as.

func (Option) Customize

func (o Option) Customize(*testcontainers.GenericContainerRequest) error

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

type TLSConfig

type TLSConfig struct {
	CACert     *x509.Certificate
	NodeCert   []byte
	NodeKey    []byte
	ClientCert []byte
	ClientKey  []byte
}

func NewTLSConfig

func NewTLSConfig() (*TLSConfig, error)

NewTLSConfig creates a new TLSConfig capable of running CockroachDB & connecting over TLS.

Jump to

Keyboard shortcuts

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