cassandra

package module
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConfigFile

func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption

WithConfigFile sets the YAML config file to be used for the cassandra container It will also set the "configFile" parameter to the path of the config file as a command line argument to the container.

func WithInitScripts

func WithInitScripts(scripts ...string) testcontainers.CustomizeRequestOption

WithInitScripts sets the init cassandra queries to be run when the container starts

Types

type CassandraContainer

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

CassandraContainer represents the Cassandra container type used in the module

func Run added in v0.32.0

Run creates an instance of the Cassandra container type

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

cassandraContainer, err := cassandra.Run(ctx,
	"cassandra:4.1.3",
	cassandra.WithInitScripts(filepath.Join("testdata", "init.cql")),
	cassandra.WithConfigFile(filepath.Join("testdata", "config.yaml")),
)
defer func() {
	if err := testcontainers.TerminateContainer(cassandraContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

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

fmt.Println(state.Running)

connectionHost, err := cassandraContainer.ConnectionHost(ctx)
if err != nil {
	log.Printf("failed to get connection host: %s", err)
	return
}

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

var version string
err = session.Query("SELECT release_version FROM system.local").Scan(&version)
if err != nil {
	log.Printf("failed to query: %s", err)
	return
}

fmt.Println(version)
Output:
true
4.1.3
Example (WithTLS)
// runCassandraContainerWithTLS {
ctx := context.Background()

cassandraContainer, err := cassandra.Run(ctx,
	"cassandra:4.1.3",
	cassandra.WithTLS(),
)
defer func() {
	if err := testcontainers.TerminateContainer(cassandraContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

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

fmt.Println(state.Running)

// getTLSConnectionHost {
connectionHost, err := cassandraContainer.ConnectionHost(ctx)
if err != nil {
	log.Printf("failed to get SSL connection host: %s", err)
	return
}

// Get TLS config for secure connection
tlsConfig, err := cassandraContainer.TLSConfig()
if err != nil {
	log.Printf("failed to get TLS config: %s", err)
	return
}
// }

cluster := gocql.NewCluster(connectionHost)
cluster.SslOpts = &gocql.SslOptions{
	Config: tlsConfig,
}

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

var version string
err = session.Query("SELECT release_version FROM system.local").Scan(&version)
if err != nil {
	log.Printf("failed to query: %s", err)
	return
}

fmt.Println(version)
Output:
true
4.1.3

func RunContainer deprecated

Deprecated: use Run instead RunContainer creates an instance of the Cassandra container type

func (*CassandraContainer) ConnectionHost

func (c *CassandraContainer) ConnectionHost(ctx context.Context) (string, error)

ConnectionHost returns the host and port of the cassandra container, using the default, native 9042 port, and obtaining the host and exposed port from the container

func (*CassandraContainer) TLSConfig added in v0.41.0

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

TLSConfig returns the TLS configuration for secure client connections. Returns an error if TLS is not enabled on the container.

type Option added in v0.41.0

type Option func(*options) error

Option is an option for the Cassandra container.

func WithTLS added in v0.41.0

func WithTLS() Option

WithTLS enables TLS/SSL on the Cassandra container. When enabled, the container will:

  • Generate self-signed certificates
  • Configure Cassandra to use client encryption
  • Expose the SSL port (9142)

Use TLSConfig() on the returned container to get the *tls.Config for client connections.

func (Option) Customize added in v0.41.0

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

Jump to

Keyboard shortcuts

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