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 ¶
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.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) ¶
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.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.
Click to show internal directories.
Click to hide internal directories.