2015-04-23 03:46:59 +00:00
|
|
|
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 some example usage:
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-09 01:11:17 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
|
|
|
"testing"
|
2015-04-23 03:46:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(t *testing.T) {
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a server
|
|
|
|
srv1 := testutil.NewTestServer(t)
|
|
|
|
defer srv1.Stop()
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a secondary server, passing in configuration
|
2015-05-09 01:16:35 +00:00
|
|
|
// to avoid bootstrapping as we are forming a cluster.
|
2015-05-09 01:11:17 +00:00
|
|
|
srv2 := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
|
|
|
|
c.Bootstrap = false
|
|
|
|
})
|
|
|
|
defer srv2.Stop()
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Join the servers together
|
|
|
|
srv1.JoinLAN(srv2.LANAddr)
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a test key/value pair
|
|
|
|
srv1.SetKV("foo", []byte("bar"))
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create lots of test key/value pairs
|
|
|
|
srv1.PopulateKV(map[string][]byte{
|
|
|
|
"bar": []byte("123"),
|
|
|
|
"baz": []byte("456"),
|
|
|
|
})
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a service
|
|
|
|
srv1.AddService("redis", "passing", []string{"master"})
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a service check
|
|
|
|
srv1.AddCheck("service:redis", "redis", "passing")
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a node check
|
|
|
|
srv1.AddCheck("mem", "", "critical")
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// The HTTPAddr field contains the address of the Consul
|
|
|
|
// API on the new test server instance.
|
|
|
|
println(srv1.HTTPAddr)
|
2015-04-23 03:46:59 +00:00
|
|
|
}
|
|
|
|
```
|