nats

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: 5 Imported by: 0

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

func WithArgument(flag string, value string) CmdOption

WithArgument adds an argument and its value to the NATS container. The argument flag does not need to include the dashes.

func WithPassword

func WithPassword(password string) CmdOption

func WithUsername

func WithUsername(username string) CmdOption

func (CmdOption) Customize

func (o CmdOption) Customize(req *testcontainers.GenericContainerRequest)

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

type NATSContainer

type NATSContainer struct {
	testcontainers.Container
	User     string
	Password string
}

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

func (c *NATSContainer) ConnectionString(ctx context.Context, args ...string) (string, error)

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

Jump to

Keyboard shortcuts

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