consul

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

Documentation

Index

Examples

Constants

View Source
const (
	DefaultBaseImage = "docker.io/hashicorp/consul:1.15"
)

Variables

This section is empty.

Functions

func WithConfigFile

func WithConfigFile(configPath string) testcontainers.CustomizeRequestOption

WithConfigFile takes in a path to a JSON file to define a configuration to be used by the instance.

func WithConfigString

func WithConfigString(config string) testcontainers.CustomizeRequestOption

WithConfigString takes in a JSON string of keys and values to define a configuration to be used by the instance.

Types

type ConsulContainer

type ConsulContainer struct {
	testcontainers.Container
}

ConsulContainer represents the Consul container type used in the module.

func RunContainer

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

RunContainer creates an instance of the Consul container type

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

consulContainer, err := consul.RunContainer(ctx,
	testcontainers.WithImage("docker.io/hashicorp/consul:1.15"),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

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

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

fmt.Println(state.Running)
Output:

true
Example (Connect)
// connectConsul {
ctx := context.Background()

consulContainer, err := consul.RunContainer(ctx,
	testcontainers.WithImage("docker.io/hashicorp/consul:1.15"),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

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

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

config := capi.DefaultConfig()
config.Address = endpoint
client, err := capi.NewClient(config)
if err != nil {
	log.Fatalf("failed to connect to Consul: %s", err)
}
// }

node_name, err := client.Agent().NodeName()
if err != nil {
	log.Fatalf("failed to get node name: %s", err) // nolint:gocritic
}
fmt.Println(len(node_name) > 0)
Output:

true

func (*ConsulContainer) ApiEndpoint

func (c *ConsulContainer) ApiEndpoint(ctx context.Context) (string, error)

ApiEndpoint returns host:port for the HTTP API endpoint.

Jump to

Keyboard shortcuts

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