rabbitmq

package module
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	DefaultAMQPSPort = "5671/tcp"
	DefaultAMQPPort  = "5672/tcp"
	DefaultHTTPSPort = "15671/tcp"
	DefaultHTTPPort  = "15672/tcp"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option is an option for the RabbitMQ container.

func WithAdminPassword

func WithAdminPassword(password string) Option

WithAdminPassword sets the password for the default admin user

func WithAdminUsername

func WithAdminUsername(username string) Option

WithAdminUsername sets the default admin username

func WithSSL

func WithSSL(settings SSLSettings) Option

WithSSL enables SSL on the RabbitMQ container, configuring the Erlang config file with the provided settings.

func (Option) Customize

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

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

type RabbitMQContainer

type RabbitMQContainer struct {
	testcontainers.Container
	AdminPassword string
	AdminUsername string
}

RabbitMQContainer represents the RabbitMQ container type used in the module

func RunContainer

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

RunContainer creates an instance of the RabbitMQ container type

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

rabbitmqContainer, err := rabbitmq.RunContainer(ctx,
	testcontainers.WithImage("rabbitmq:3.12.11-management-alpine"),
	rabbitmq.WithAdminUsername("admin"),
	rabbitmq.WithAdminPassword("password"),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

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

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

fmt.Println(state.Running)
Output:

true
Example (ConnectUsingAmqp)
ctx := context.Background()

rabbitmqContainer, err := rabbitmq.RunContainer(ctx,
	testcontainers.WithImage("rabbitmq:3.7.25-management-alpine"),
	rabbitmq.WithAdminUsername("admin"),
	rabbitmq.WithAdminPassword("password"),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}
defer func() {
	if err := rabbitmqContainer.Terminate(ctx); err != nil {
		log.Fatalf("failed to terminate container: %s", err)
	}
}()

amqpURL, err := rabbitmqContainer.AmqpURL(ctx)
if err != nil {
	log.Fatalf("failed to get AMQP URL: %s", err) // nolint:gocritic
}

amqpConnection, err := amqp.Dial(amqpURL)
if err != nil {
	log.Fatalf("failed to connect to RabbitMQ: %s", err)
}
defer func() {
	err := amqpConnection.Close()
	if err != nil {
		log.Fatalf("failed to close connection: %s", err)
	}
}()

fmt.Println(amqpConnection.IsClosed())
Output:

false
Example (WithCustomConfigFile)
ctx := context.Background()

rabbitmqContainer, err := rabbitmq.RunContainer(ctx,
	testcontainers.WithImage("rabbitmq:3.7.25-management-alpine"),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

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

logs, err := rabbitmqContainer.Logs(ctx)
if err != nil {
	log.Fatalf("failed to get logs: %s", err) // nolint:gocritic
}

bytes, err := io.ReadAll(logs)
if err != nil {
	log.Fatalf("failed to read logs: %s", err)
}

fmt.Println(strings.Contains(string(bytes), "config file(s) : /etc/rabbitmq/rabbitmq-testcontainers.conf"))
Output:

true
Example (WithPlugins)
ctx := context.Background()

rabbitmqContainer, err := rabbitmq.RunContainer(ctx,
	testcontainers.WithImage("rabbitmq:3.7.25-management-alpine"),
	// Multiple test implementations of the Executable interface, specific to RabbitMQ, exist in the types_test.go file.
	// Please refer to them for more examples.
	testcontainers.WithAfterReadyCommand(
		testcontainers.NewRawCommand([]string{"rabbitmq_shovel"}),
		testcontainers.NewRawCommand([]string{"rabbitmq_random_exchange"}),
	),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

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

fmt.Println(assertPlugins(rabbitmqContainer, "rabbitmq_shovel", "rabbitmq_random_exchange"))
Output:

true
Example (WithSSL)
// enableSSL {
ctx := context.Background()

sslSettings := rabbitmq.SSLSettings{
	CACertFile:        filepath.Join("testdata", "certs", "server_ca.pem"),
	CertFile:          filepath.Join("testdata", "certs", "server_cert.pem"),
	KeyFile:           filepath.Join("testdata", "certs", "server_key.pem"),
	VerificationMode:  rabbitmq.SSLVerificationModePeer,
	FailIfNoCert:      true,
	VerificationDepth: 1,
}

rabbitmqContainer, err := rabbitmq.RunContainer(ctx,
	testcontainers.WithImage("rabbitmq:3.7.25-management-alpine"),
	rabbitmq.WithSSL(sslSettings),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}
// }

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

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

fmt.Println(state.Running)
Output:

true

func (*RabbitMQContainer) AmqpURL

func (c *RabbitMQContainer) AmqpURL(ctx context.Context) (string, error)

AmqpURL returns the URL for AMQP clients.

func (*RabbitMQContainer) AmqpsURL

func (c *RabbitMQContainer) AmqpsURL(ctx context.Context) (string, error)

AmqpURL returns the URL for AMQPS clients.

func (*RabbitMQContainer) HttpURL

func (c *RabbitMQContainer) HttpURL(ctx context.Context) (string, error)

HttpURL returns the URL for HTTP management.

func (*RabbitMQContainer) HttpsURL

func (c *RabbitMQContainer) HttpsURL(ctx context.Context) (string, error)

HttpsURL returns the URL for HTTPS management.

type SSLSettings

type SSLSettings struct {
	// Path to the CA certificate file
	CACertFile string
	// Path to the client certificate file
	CertFile string
	// Path to the key file
	KeyFile string
	// Verification mode
	VerificationMode SSLVerificationMode
	// Fail if no certificate is provided
	FailIfNoCert bool
	// Depth of certificate chain verification
	VerificationDepth int
}

type SSLVerificationMode

type SSLVerificationMode string
const (
	SSLVerificationModeNone SSLVerificationMode = "verify_none"
	SSLVerificationModePeer SSLVerificationMode = "verify_peer"
)

Jump to

Keyboard shortcuts

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