Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CmdOption ¶
type CmdOption func(opts *options)
CmdOption is an option for the NATS container.
func WithArgument ¶
WithArgument adds an argument and its value to the NATS container. The argument flag does not need to include the dashes.
func WithPassword ¶
func WithUsername ¶
type NATSContainer ¶
NATSContainer represents the NATS container type used in the module
func RunContainer ¶
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*NATSContainer, error)
RunContainer creates an instance of the NATS container type
Example ¶
// runNATSContainer { ctx := context.Background() natsContainer, err := nats.RunContainer(ctx, testcontainers.WithImage("nats:2.9"), ) if err != nil { log.Fatalf("failed to start container: %s", err) } // Clean up the container defer func() { if err := natsContainer.Terminate(ctx); err != nil { log.Fatalf("failed to terminate container: %s", err) } }() // } state, err := natsContainer.State(ctx) if err != nil { log.Fatalf("failed to get container state: %s", err) // nolint:gocritic } fmt.Println(state.Running)
Output: true
Example (ConnectWithCredentials) ¶
package main import ( "context" "fmt" "log" natsgo "github.com/nats-io/nats.go" "github.com/testcontainers/testcontainers-go/modules/nats" ) func main() { // natsConnect { ctx := context.Background() container, err := nats.RunContainer(ctx, nats.WithUsername("foo"), nats.WithPassword("bar")) if err != nil { log.Fatalf("failed to start container: %s", err) } // Clean up the container defer func() { if err := container.Terminate(ctx); err != nil { log.Fatalf("failed to terminate container: %s", err) } }() uri, err := container.ConnectionString(ctx) if err != nil { log.Fatalf("failed to get connection string: %s", err) // nolint:gocritic } nc, err := natsgo.Connect(uri, natsgo.UserInfo(container.User, container.Password)) if err != nil { log.Fatalf("failed to connect to NATS: %s", err) } defer nc.Close() // } fmt.Println(nc.IsConnected()) }
Output: true
func (*NATSContainer) ConnectionString ¶
ConnectionString returns a connection string for the NATS container
func (*NATSContainer) MustConnectionString ¶ added in v0.30.0
func (c *NATSContainer) MustConnectionString(ctx context.Context, args ...string) string
Click to show internal directories.
Click to hide internal directories.