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 ¶
WithAdminPassword sets the password for the default admin user
func WithAdminUsername ¶
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.
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() tmpDir := os.TempDir() certDirs := tmpDir + "/rabbitmq" if err := os.MkdirAll(certDirs, 0755); err != nil { log.Fatalf("failed to create temporary directory: %s", err) } defer os.RemoveAll(certDirs) // generates the CA certificate and the certificate caCert := tlscert.SelfSignedFromRequest(tlscert.Request{ Name: "ca", Host: "localhost,127.0.0.1", IsCA: true, ParentDir: certDirs, }) if caCert == nil { log.Fatal("failed to generate CA certificate") } cert := tlscert.SelfSignedFromRequest(tlscert.Request{ Name: "client", Host: "localhost,127.0.0.1", IsCA: true, Parent: caCert, ParentDir: certDirs, }) if cert == nil { log.Fatal("failed to generate certificate") } sslSettings := rabbitmq.SSLSettings{ CACertFile: caCert.CertPath, CertFile: cert.CertPath, KeyFile: cert.KeyPath, 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.
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" )
Click to show internal directories.
Click to hide internal directories.