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
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*ConsulContainer, error)
Run creates an instance of the Consul container type
Example ¶
// 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) ¶
// 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
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*ConsulContainer, error)
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.
Click to show internal directories.
Click to hide internal directories.