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`.
|
|
|
|
|
2016-04-24 03:18:45 +00:00
|
|
|
Following is an example usage:
|
2015-04-23 03:46:59 +00:00
|
|
|
|
|
|
|
```go
|
2016-04-24 03:18:45 +00:00
|
|
|
package my_program
|
2015-04-23 03:46:59 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-09 01:11:17 +00:00
|
|
|
"testing"
|
2016-04-24 03:18:45 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2015-04-23 03:46:59 +00:00
|
|
|
)
|
|
|
|
|
2017-03-23 20:26:05 +00:00
|
|
|
func TestFoo_bar(t *testing.T) {
|
2016-06-20 22:29:38 +00:00
|
|
|
// Create a test Consul server
|
2019-11-08 22:51:49 +00:00
|
|
|
srv1, err := testutil.NewTestServerT(t)
|
2017-03-23 20:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 01:11:17 +00:00
|
|
|
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.
|
2019-11-08 22:51:49 +00:00
|
|
|
srv2, err := testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) {
|
2015-05-09 01:11:17 +00:00
|
|
|
c.Bootstrap = false
|
|
|
|
})
|
2017-03-23 20:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 01:11:17 +00:00
|
|
|
defer srv2.Stop()
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Join the servers together
|
2017-03-23 20:26:05 +00:00
|
|
|
srv1.JoinLAN(t, srv2.LANAddr)
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a test key/value pair
|
2017-03-23 20:26:05 +00:00
|
|
|
srv1.SetKV(t, "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
|
2017-03-23 20:26:05 +00:00
|
|
|
srv1.PopulateKV(t, map[string][]byte{
|
2015-05-09 01:11:17 +00:00
|
|
|
"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
|
2017-03-23 20:26:05 +00:00
|
|
|
srv1.AddService(t, "redis", structs.HealthPassing, []string{"master"})
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2016-09-30 06:26:12 +00:00
|
|
|
// Create a service that will be accessed in target source code
|
|
|
|
srv1.AddAccessibleService("redis", structs.HealthPassing, "127.0.0.1", 6379, []string{"master"})
|
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a service check
|
2017-03-23 20:26:05 +00:00
|
|
|
srv1.AddCheck(t, "service:redis", "redis", structs.HealthPassing)
|
2015-04-23 03:46:59 +00:00
|
|
|
|
2015-05-09 01:11:17 +00:00
|
|
|
// Create a node check
|
2017-03-23 20:26:05 +00:00
|
|
|
srv1.AddCheck(t, "mem", "", structs.HealthCritical)
|
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)
|
2017-03-23 20:26:05 +00:00
|
|
|
|
|
|
|
// All functions also have a wrapper method to limit the passing of "t"
|
|
|
|
wrap := srv1.Wrap(t)
|
|
|
|
wrap.SetKV("foo", []byte("bar"))
|
2015-04-23 03:46:59 +00:00
|
|
|
}
|
|
|
|
```
|