NAME

Test::Consul - Spawn an in-process Consul agent for testing.

SYNOPSIS

use Test::Consul;

my $consul = Test::Consul->new();
$consul->start();

my $url = $consul->api_v1_url() . '/agent/self';
my $port = $consul->http_port();

DESCRIPTION

This module provides a very simple and easy way to launch a per-test Consul agent. This agent is configured to run in dev mode, meaning it writes nothing to disk, and all the ports it uses are set to available random ports.

It is expected that the consul binary will be available and in the PATH env, or otherwise executable from your shell as simply consul. If you have the Consul binary installed somewhere else you can set the "binary" argument (which also supports being set by an env var).

When using this for tests you may find that following this workflow will be the most useful:

use Test::More;
my $consul = Test::Consul->new();
$consul->skip_all_if_binary_unavailable();
$consul->start();
# do your testing...

See more about this at "skip_all_if_binary_unavailable".

LOGGING

This module uses Log::Any to log various information as the Consul agent changes state. If you'd like to see the log output you could add this to your test scripts:

use Log::Any::Adapter 'TAP';

ARGUMENTS

start_timeout

How long to wait for the agent to start when "start" is called. Defaults to 5 (seconds) which is well more than sufficient.

stop_timeout

How long to wait for the agent to stop when "stop" is called. Defaults to 5 (seconds) which is well more than sufficient.

binary

The name of the consul binary, including its path if appropriate. Defaults to just consul.

The CONSUL_BINARY env var may be used to change the default.

ATTRIBUTES

http_port

The port Consul is listening to HTTP requests on. If Consul is not running then this will throw an exception.

http_url

Returns the full URL to Consul's HTTP interface. Throws an exception if Consul is not running.

api_v1_url

Returns the full URL to Consul's API v1 HTTP interface. Throws an exception if Consul is not running.

is_binary_available

Returns true if the "binary" is callable.

METHODS

skip_all_if_binary_unavailable

Calls Test::More's skip_all if "is_binary_available" returns false.

start

Starts the Consul agent and returns once its "http_port" starts listening.

stop

Stops the Consul agent. Note that this isn't necessary to call in normal circumstances the agent will be automatically stop when the object goes out of scope or the process exits.

is_running

Returns true if "start" has been called and the agent process is still running.

AUTHOR

Aran Clary Deltac <bluefeet@gmail.com>

ACKNOWLEDGEMENTS

Thanks to ZipRecruiter for encouraging their employees to contribute back to the open source ecosystem. Without their dedication to quality software development this distribution would not exist.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.