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 Run ¶ added in v0.32.0
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*RabbitMQContainer, error)
Run creates an instance of the RabbitMQ container type
Example ¶
// runRabbitMQContainer { ctx := context.Background() rabbitmqContainer, err := rabbitmq.Run(ctx, "rabbitmq:3.12.11-management-alpine", rabbitmq.WithAdminUsername("admin"), rabbitmq.WithAdminPassword("password"), ) defer func() { if err := testcontainers.TerminateContainer(rabbitmqContainer); err != nil { log.Printf("failed to terminate container: %s", err) } }() if err != nil { log.Printf("failed to start container: %s", err) return } // } state, err := rabbitmqContainer.State(ctx) if err != nil { log.Printf("failed to get container state: %s", err) return } fmt.Println(state.Running)
Output: true
Example (ConnectUsingAmqp) ¶
ctx := context.Background() rabbitmqContainer, err := rabbitmq.Run(ctx, "rabbitmq:3.7.25-management-alpine", rabbitmq.WithAdminUsername("admin"), rabbitmq.WithAdminPassword("password"), ) defer func() { if err := testcontainers.TerminateContainer(rabbitmqContainer); err != nil { log.Printf("failed to terminate container: %s", err) } }() if err != nil { log.Printf("failed to start container: %s", err) return } amqpURL, err := rabbitmqContainer.AmqpURL(ctx) if err != nil { log.Printf("failed to get AMQP URL: %s", err) return } amqpConnection, err := amqp.Dial(amqpURL) if err != nil { log.Printf("failed to connect to RabbitMQ: %s", err) return } defer func() { err := amqpConnection.Close() if err != nil { log.Printf("failed to close connection: %s", err) } }() fmt.Println(amqpConnection.IsClosed())
Output: false
Example (WithCustomConfigFile) ¶
ctx := context.Background() rabbitmqContainer, err := rabbitmq.Run(ctx, "rabbitmq:3.7.25-management-alpine", ) defer func() { if err := testcontainers.TerminateContainer(rabbitmqContainer); err != nil { log.Printf("failed to terminate container: %s", err) } }() if err != nil { log.Printf("failed to start container: %s", err) return } logs, err := rabbitmqContainer.Logs(ctx) if err != nil { log.Printf("failed to get logs: %s", err) return } bytes, err := io.ReadAll(logs) if err != nil { log.Printf("failed to read logs: %s", err) return } fmt.Println(strings.Contains(string(bytes), "config file(s) : /etc/rabbitmq/rabbitmq-testcontainers.conf"))
Output: true
Example (WithPlugins) ¶
ctx := context.Background() rabbitmqContainer, err := rabbitmq.Run(ctx, "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"}), ), ) defer func() { if err := testcontainers.TerminateContainer(rabbitmqContainer); err != nil { log.Printf("failed to terminate container: %s", err) } }() if err != nil { log.Printf("failed to start container: %s", err) return } if err = assertPlugins(rabbitmqContainer, "rabbitmq_shovel", "rabbitmq_random_exchange"); err != nil { log.Printf("failed to find plugin: %s", err) return } fmt.Println(true)
Output: true
Example (WithSSL) ¶
// enableSSL { ctx := context.Background() tmpDir := os.TempDir() certDirs := tmpDir + "/rabbitmq" if err := os.MkdirAll(certDirs, 0o755); err != nil { log.Printf("failed to create temporary directory: %s", err) return } defer os.RemoveAll(certDirs) // generates the CA certificate and the certificate // exampleSelfSignedCert { caCert := tlscert.SelfSignedFromRequest(tlscert.Request{ Name: "ca", Host: "localhost,127.0.0.1", IsCA: true, ParentDir: certDirs, }) if caCert == nil { log.Print("failed to generate CA certificate") return } // } // exampleSignSelfSignedCert { cert := tlscert.SelfSignedFromRequest(tlscert.Request{ Name: "client", Host: "localhost,127.0.0.1", IsCA: true, Parent: caCert, ParentDir: certDirs, }) if cert == nil { log.Print("failed to generate certificate") return } // } sslSettings := rabbitmq.SSLSettings{ CACertFile: caCert.CertPath, CertFile: cert.CertPath, KeyFile: cert.KeyPath, VerificationMode: rabbitmq.SSLVerificationModePeer, FailIfNoCert: true, VerificationDepth: 1, } rabbitmqContainer, err := rabbitmq.Run(ctx, "rabbitmq:3.7.25-management-alpine", rabbitmq.WithSSL(sslSettings), ) defer func() { if err := testcontainers.TerminateContainer(rabbitmqContainer); err != nil { log.Printf("failed to terminate container: %s", err) } }() if err != nil { log.Printf("failed to start container: %s", err) return } // } state, err := rabbitmqContainer.State(ctx) if err != nil { log.Printf("failed to get container state: %s", err) return } fmt.Println(state.Running)
Output: true
func RunContainer
deprecated
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*RabbitMQContainer, error)
Deprecated: use Run instead RunContainer creates an instance of the RabbitMQ container type
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.