consul

package module
v0.43.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 4 Imported by: 3

Documentation

Index

Examples

Constants

View Source
const (
	// Deprecated: it will be removed in the next major version.
	DefaultBaseImage = "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 Run added in v0.32.0

Run creates an instance of the Consul container type

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/consul"
)

func main() {
	// runConsulContainer {
	ctx := context.Background()

	consulContainer, err := consul.Run(ctx, "hashicorp/consul:1.15")
	defer func() {
		if err := testcontainers.TerminateContainer(consulContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}
	// }

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

	fmt.Println(state.Running)

}
Output:
true
Example (Connect)
package main

import (
	"context"
	"fmt"
	"log"

	capi "github.com/hashicorp/consul/api"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/consul"
)

func main() {
	// connectConsul {
	ctx := context.Background()

	consulContainer, err := consul.Run(ctx, "hashicorp/consul:1.15")
	defer func() {
		if err := testcontainers.TerminateContainer(consulContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

	endpoint, err := consulContainer.ApiEndpoint(ctx)
	if err != nil {
		log.Printf("failed to get endpoint: %s", err)
		return
	}

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

	nodeName, err := client.Agent().NodeName()
	if err != nil {
		log.Printf("failed to get node name: %s", err)
		return
	}
	fmt.Println(len(nodeName) > 0)

}
Output:
true

func RunContainer deprecated

Deprecated: use Run instead RunContainer creates an instance of the Consul container type

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