scylladb

package module
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAlternator

func WithAlternator() testcontainers.CustomizeRequestOption

WithAlternator enables the Alternator (DynamoDB Compatible API) service in the ScyllaDB container, using the default HTTP port 8000. It will set the "alternator-port" parameter to the specified port. It will also set the "alternator-write-isolation" parameter to "always" as a command line argument to the container.

func WithConfig

WithConfig sets the YAML config file as an io.Reader to be used for the ScyllaDB container

func WithCustomCommands

func WithCustomCommands(flags ...string) testcontainers.CustomizeRequestOption

WithCustomCommands sets custom commands with values for the ScyllaDB container. Pass the command and the values as a list of strings in the following format: "--flag1=value", "--flag2", etc. In case of an invalid flag (not starting with "--" or "-"), this option returns an error, not applying any changes to the command line. Else, flags that exist in the command line overwrite the default commands. See more in the ScyllaDB docs.

func WithShardAwareness

func WithShardAwareness() testcontainers.CustomizeRequestOption

WithShardAwareness enable shard-awareness in the ScyllaDB container so you can use the `19042` port.

Types

type Container

type Container struct {
	testcontainers.Container
}

Container represents a ScyllaDB container type used in the module

func Run

Run starts a ScyllaDB container with the specified image and options

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/gocql/gocql"

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

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

	scyllaContainer, err := scylladb.Run(ctx,
		"scylladb/scylla:6.2",
		scylladb.WithShardAwareness(),
	)
	defer func() {
		if err := testcontainers.TerminateContainer(scyllaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}
	// }

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

	fmt.Println(state.Running)

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

	if err := runGoCQLExampleTest(connectionHost); err != nil {
		log.Printf("failed to run Go CQL example test: %s", err)
		return
	}

}

func runGoCQLExampleTest(connectionHost string) error {
	cluster := gocql.NewCluster(connectionHost)
	session, err := cluster.CreateSession()
	if err != nil {
		return fmt.Errorf("create cluster session: %w", err)
	}
	defer session.Close()

	var driver string
	err = session.Query("SELECT driver_name FROM system.clients").Scan(&driver)
	if err != nil {
		return fmt.Errorf("session query: %w", err)
	}

	return nil
}
Output:
true
Example (WithAlternator)
package main

import (
	"context"
	"fmt"
	"log"

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

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

	// runScyllaDBContainerWithAlternator {

	scyllaContainer, err := scylladb.Run(ctx,
		"scylladb/scylla:6.2",
		scylladb.WithAlternator(),
	)
	// }
	defer func() {
		if err := testcontainers.TerminateContainer(scyllaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

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

	fmt.Println(state.Running)

	// scyllaDbAlternatorConnectionHost {
	// the alternator port is the default port 8000
	_, err = scyllaContainer.AlternatorConnectionHost(ctx)
	// }
	if err != nil {
		log.Printf("failed to get connection host: %s", err)
		return
	}

}
Output:
true
Example (WithConfig)
package main

import (
	"context"
	"fmt"
	"log"
	"strings"

	"github.com/gocql/gocql"

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

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

	cfgBytes := `cluster_name: 'Amazing ScyllaDB Test'
num_tokens: 256
commitlog_sync: periodic
commitlog_sync_period_in_ms: 10000
commitlog_segment_size_in_mb: 32
schema_commitlog_segment_size_in_mb: 128
seed_provider:
  - class_name: org.apache.cassandra.locator.SimpleSeedProvider
    parameters:
      - seeds: "127.0.0.1"
listen_address: localhost
native_transport_port: 9042
native_shard_aware_transport_port: 19042
read_request_timeout_in_ms: 5000
write_request_timeout_in_ms: 2000
cas_contention_timeout_in_ms: 1000
endpoint_snitch: SimpleSnitch
rpc_address: localhost
api_port: 10000
api_address: 127.0.0.1
batch_size_warn_threshold_in_kb: 128
batch_size_fail_threshold_in_kb: 1024
partitioner: org.apache.cassandra.dht.Murmur3Partitioner
commitlog_total_space_in_mb: -1
murmur3_partitioner_ignore_msb_bits: 12
strict_is_not_null_in_views: true
maintenance_socket: ignore
enable_tablets: true
`

	scyllaContainer, err := scylladb.Run(ctx,
		"scylladb/scylla:6.2",
		scylladb.WithConfig(strings.NewReader(cfgBytes)),
	)
	// }
	defer func() {
		if err := testcontainers.TerminateContainer(scyllaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}
	// }

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

	fmt.Println(state.Running)

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

	if err := runGoCQLExampleTest(connectionHost); err != nil {
		log.Printf("failed to run Go CQL example test: %s", err)
		return
	}

}

func runGoCQLExampleTest(connectionHost string) error {
	cluster := gocql.NewCluster(connectionHost)
	session, err := cluster.CreateSession()
	if err != nil {
		return fmt.Errorf("create cluster session: %w", err)
	}
	defer session.Close()

	var driver string
	err = session.Query("SELECT driver_name FROM system.clients").Scan(&driver)
	if err != nil {
		return fmt.Errorf("session query: %w", err)
	}

	return nil
}
Output:
true
Example (WithCustomCommands)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/gocql/gocql"

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

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

	// runScyllaDBContainerWithCustomCommands {
	scyllaContainer, err := scylladb.Run(ctx,
		"scylladb/scylla:6.2",
		scylladb.WithCustomCommands("--memory=1G", "--smp=2"),
	)
	// }
	defer func() {
		if err := testcontainers.TerminateContainer(scyllaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

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

	fmt.Println(state.Running)

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

	if err := runGoCQLExampleTest(connectionHost); err != nil {
		log.Printf("failed to run Go CQL example test: %s", err)
		return
	}

}

func runGoCQLExampleTest(connectionHost string) error {
	cluster := gocql.NewCluster(connectionHost)
	session, err := cluster.CreateSession()
	if err != nil {
		return fmt.Errorf("create cluster session: %w", err)
	}
	defer session.Close()

	var driver string
	err = session.Query("SELECT driver_name FROM system.clients").Scan(&driver)
	if err != nil {
		return fmt.Errorf("session query: %w", err)
	}

	return nil
}
Output:
true
Example (WithShardAwareness)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/gocql/gocql"

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

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

	// runScyllaDBContainerWithShardAwareness {
	scyllaContainer, err := scylladb.Run(ctx,
		"scylladb/scylla:6.2",
		scylladb.WithShardAwareness(),
	)
	// }
	defer func() {
		if err := testcontainers.TerminateContainer(scyllaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

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

	fmt.Println(state.Running)

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

	if err := runGoCQLExampleTest(connectionHost); err != nil {
		log.Printf("failed to run Go CQL example test: %s", err)
		return
	}

}

func runGoCQLExampleTest(connectionHost string) error {
	cluster := gocql.NewCluster(connectionHost)
	session, err := cluster.CreateSession()
	if err != nil {
		return fmt.Errorf("create cluster session: %w", err)
	}
	defer session.Close()

	var driver string
	err = session.Query("SELECT driver_name FROM system.clients").Scan(&driver)
	if err != nil {
		return fmt.Errorf("session query: %w", err)
	}

	return nil
}
Output:
true

func (Container) AlternatorConnectionHost

func (c Container) AlternatorConnectionHost(ctx context.Context) (string, error)

AlternatorConnectionHost returns the host and port of the ScyllaDB container with the alternator port

func (Container) NonShardAwareConnectionHost

func (c Container) NonShardAwareConnectionHost(ctx context.Context) (string, error)

NonShardAwareConnectionHost returns the host and port of the ScyllaDB container with the non-shard-aware port

func (Container) ShardAwareConnectionHost

func (c Container) ShardAwareConnectionHost(ctx context.Context) (string, error)

ShardAwareConnectionHost returns the host and port of the ScyllaDB container with the shard-aware port

Jump to

Keyboard shortcuts

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