open-consul/testutil
James Phillips ae8edf78e9 Drop testutil wait to 2 seconds.
There's likely a race (related to https://github.com/hashicorp/consul/issues/2644) where the catalog update might be in but the leader tracking doesn't report a leader, so this blocks forever and then times out. As a workaround we can lower the query wait time to always allow for a few retries.
2017-02-06 11:52:00 -08:00
..
README.md Misc comment improvements 2016-06-20 15:29:38 -07:00
server.go Drop testutil wait to 2 seconds. 2017-02-06 11:52:00 -08:00
wait.go Adds a little wait after we get the event we were seeking. 2017-01-26 22:15:54 -08:00
wait_test.go Add test file for testutil 2014-05-07 23:42:15 +02:00

README.md

Consul Testing Utilities

This package provides some generic helpers to facilitate testing in Consul.

TestServer

TestServer is a harness for managing Consul agents and initializing them with test data. Using it, you can form test clusters, create services, add health checks, manipulate the K/V store, etc. This test harness is completely decoupled from Consul's core and API client, meaning it can be easily imported and used in external unit tests for various applications. It works by invoking the Consul CLI, which means it is a requirement to have Consul installed in the $PATH.

Following is an example usage:

package my_program

import (
	"testing"

	"github.com/hashicorp/consul/consul/structs"
	"github.com/hashicorp/consul/testutil"
)

func TestMain(t *testing.T) {
	// Create a test Consul server
	srv1 := testutil.NewTestServer(t)
	defer srv1.Stop()

	// Create a secondary server, passing in configuration
	// to avoid bootstrapping as we are forming a cluster.
	srv2 := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
		c.Bootstrap = false
	})
	defer srv2.Stop()

	// Join the servers together
	srv1.JoinLAN(srv2.LANAddr)

	// Create a test key/value pair
	srv1.SetKV("foo", []byte("bar"))

	// Create lots of test key/value pairs
	srv1.PopulateKV(map[string][]byte{
		"bar": []byte("123"),
		"baz": []byte("456"),
	})

	// Create a service
	srv1.AddService("redis", structs.HealthPassing, []string{"master"})

	// Create a service check
	srv1.AddCheck("service:redis", "redis", structs.HealthPassing)

	// Create a node check
	srv1.AddCheck("mem", "", structs.HealthCritical)

	// The HTTPAddr field contains the address of the Consul
	// API on the new test server instance.
	println(srv1.HTTPAddr)
}