testutil - ActiveState ActiveGo 1.8
...

Package testutil

import "github.com/hashicorp/consul/testutil"
Overview
Index
Subdirectories

Overview ▾

Index ▾

Constants
func TempDir(t *testing.T, name string) string
func TempFile(t *testing.T, name string) *os.File
type ServerConfigCallback
type TestAddressConfig
type TestCheck
type TestKVResponse
type TestPerformanceConfig
type TestPortConfig
type TestServer
    func NewTestServer() (*TestServer, error)
    func NewTestServerConfig(cb ServerConfigCallback) (*TestServer, error)
    func NewTestServerConfigT(t *testing.T, cb ServerConfigCallback) (*TestServer, error)
    func (s *TestServer) AddAddressableService(t *testing.T, name, status, address string, port int, tags []string)
    func (s *TestServer) AddCheck(t *testing.T, name, serviceID, status string)
    func (s *TestServer) AddService(t *testing.T, name, status string, tags []string)
    func (s *TestServer) GetKV(t *testing.T, key string) []byte
    func (s *TestServer) GetKVString(t *testing.T, key string) string
    func (s *TestServer) JoinLAN(t *testing.T, addr string)
    func (s *TestServer) JoinWAN(t *testing.T, addr string)
    func (s *TestServer) ListKV(t *testing.T, prefix string) []string
    func (s *TestServer) PopulateKV(t *testing.T, data map[string][]byte)
    func (s *TestServer) SetKV(t *testing.T, key string, val []byte)
    func (s *TestServer) SetKVString(t *testing.T, key string, val string)
    func (s *TestServer) Stop() error
    func (s *TestServer) Wrap(t *testing.T) *WrappedServer
type TestServerConfig
type TestService
type WrappedServer
    func (w *WrappedServer) AddAddressableService(name, status, address string, port int, tags []string)
    func (w *WrappedServer) AddCheck(name, serviceID, status string)
    func (w *WrappedServer) AddService(name, status string, tags []string)
    func (w *WrappedServer) GetKV(key string) []byte
    func (w *WrappedServer) GetKVString(key string) string
    func (w *WrappedServer) JoinLAN(addr string)
    func (w *WrappedServer) JoinWAN(addr string)
    func (w *WrappedServer) ListKV(prefix string) []string
    func (w *WrappedServer) PopulateKV(data map[string][]byte)
    func (w *WrappedServer) SetKV(key string, val []byte)
    func (w *WrappedServer) SetKVString(key string, val string)

Package files

io.go server.go server_methods.go server_wrapper.go

Constants

copied from testutil to break circular dependency

const (
    HealthAny      = "any"
    HealthPassing  = "passing"
    HealthWarning  = "warning"
    HealthCritical = "critical"
    HealthMaint    = "maintenance"
)

func TempDir

func TempDir(t *testing.T, name string) string

TempDir creates a temporary directory within tmpdir with the name 'testname-name'. If the directory cannot be created t.Fatal is called.

func TempFile

func TempFile(t *testing.T, name string) *os.File

TempFile creates a temporary file within tmpdir with the name 'testname-name'. If the file cannot be created t.Fatal is called. If a temporary directory has been created before consider storing the file inside this directory to avoid double cleanup.

type ServerConfigCallback

ServerConfigCallback is a function interface which can be passed to NewTestServerConfig to modify the server config.

type ServerConfigCallback func(c *TestServerConfig)

type TestAddressConfig

TestAddressConfig contains the bind addresses for various components of the Consul server.

type TestAddressConfig struct {
    HTTP string `json:"http,omitempty"`
}

type TestCheck

TestCheck is used to serialize a check definition.

type TestCheck struct {
    ID        string `json:",omitempty"`
    Name      string `json:",omitempty"`
    ServiceID string `json:",omitempty"`
    TTL       string `json:",omitempty"`
}

type TestKVResponse

TestKVResponse is what we use to decode KV data.

type TestKVResponse struct {
    Value string
}

type TestPerformanceConfig

TestPerformanceConfig configures the performance parameters.

type TestPerformanceConfig struct {
    RaftMultiplier uint `json:"raft_multiplier,omitempty"`
}

type TestPortConfig

TestPortConfig configures the various ports used for services provided by the Consul server.

type TestPortConfig struct {
    DNS     int `json:"dns,omitempty"`
    HTTP    int `json:"http,omitempty"`
    HTTPS   int `json:"https,omitempty"`
    SerfLan int `json:"serf_lan,omitempty"`
    SerfWan int `json:"serf_wan,omitempty"`
    Server  int `json:"server,omitempty"`

    // Deprecated
    RPC int `json:"rpc,omitempty"`
}

type TestServer

TestServer is the main server wrapper struct.

type TestServer struct {
    Config *TestServerConfig

    HTTPAddr  string
    HTTPSAddr string
    LANAddr   string
    WANAddr   string

    HTTPClient *http.Client
    // contains filtered or unexported fields
}

func NewTestServer

func NewTestServer() (*TestServer, error)

NewTestServer is an easy helper method to create a new Consul test server with the most basic configuration.

func NewTestServerConfig

func NewTestServerConfig(cb ServerConfigCallback) (*TestServer, error)

func NewTestServerConfigT

func NewTestServerConfigT(t *testing.T, cb ServerConfigCallback) (*TestServer, error)

NewTestServerConfig creates a new TestServer, and makes a call to an optional callback function to modify the configuration. If there is an error configuring or starting the server, the server will NOT be running when the function returns (thus you do not need to stop it).

func (*TestServer) AddAddressableService

func (s *TestServer) AddAddressableService(t *testing.T, name, status, address string, port int, tags []string)

AddAddressableService adds a new service to the Consul instance by passing "address" and "port". It is helpful when you need to prepare a fakeService that maybe accessed with in target source code. It also automatically adds a health check with the given status, which can be one of "passing", "warning", or "critical", just like `AddService` does.

func (*TestServer) AddCheck

func (s *TestServer) AddCheck(t *testing.T, name, serviceID, status string)

AddCheck adds a check to the Consul instance. If the serviceID is left empty (""), then the check will be associated with the node. The check status may be "passing", "warning", or "critical".

func (*TestServer) AddService

func (s *TestServer) AddService(t *testing.T, name, status string, tags []string)

AddService adds a new service to the Consul instance. It also automatically adds a health check with the given status, which can be one of "passing", "warning", or "critical".

func (*TestServer) GetKV

func (s *TestServer) GetKV(t *testing.T, key string) []byte

GetKV retrieves a single key and returns its value

func (*TestServer) GetKVString

func (s *TestServer) GetKVString(t *testing.T, key string) string

GetKVString retrieves a value from the store, but returns as a string instead of []byte.

func (*TestServer) JoinLAN

func (s *TestServer) JoinLAN(t *testing.T, addr string)

JoinLAN is used to join local datacenters together.

func (*TestServer) JoinWAN

func (s *TestServer) JoinWAN(t *testing.T, addr string)

JoinWAN is used to join remote datacenters together.

func (*TestServer) ListKV

func (s *TestServer) ListKV(t *testing.T, prefix string) []string

ListKV returns a list of keys present in the KV store. This will list all keys under the given prefix recursively and return them as a slice.

func (*TestServer) PopulateKV

func (s *TestServer) PopulateKV(t *testing.T, data map[string][]byte)

PopulateKV fills the Consul KV with data from a generic map.

func (*TestServer) SetKV

func (s *TestServer) SetKV(t *testing.T, key string, val []byte)

SetKV sets an individual key in the K/V store.

func (*TestServer) SetKVString

func (s *TestServer) SetKVString(t *testing.T, key string, val string)

SetKVString sets an individual key in the K/V store, but accepts a string instead of []byte.

func (*TestServer) Stop

func (s *TestServer) Stop() error

Stop stops the test Consul server, and removes the Consul data directory once we are done.

func (*TestServer) Wrap

func (s *TestServer) Wrap(t *testing.T) *WrappedServer

Wrap wraps the test server in a `testing.t` for convenience.

For example, the following code snippets are equivalent.

server.JoinLAN(t, "1.2.3.4")
server.Wrap(t).JoinLAN("1.2.3.4")

This is useful when you are calling multiple functions and save the wrapped value as another variable to reduce the inclusion of "t".

type TestServerConfig

TestServerConfig is the main server configuration struct.

type TestServerConfig struct {
    NodeName            string                 `json:"node_name"`
    NodeID              string                 `json:"node_id"`
    NodeMeta            map[string]string      `json:"node_meta,omitempty"`
    Performance         *TestPerformanceConfig `json:"performance,omitempty"`
    Bootstrap           bool                   `json:"bootstrap,omitempty"`
    Server              bool                   `json:"server,omitempty"`
    DataDir             string                 `json:"data_dir,omitempty"`
    Datacenter          string                 `json:"datacenter,omitempty"`
    DisableCheckpoint   bool                   `json:"disable_update_check"`
    LogLevel            string                 `json:"log_level,omitempty"`
    Bind                string                 `json:"bind_addr,omitempty"`
    Addresses           *TestAddressConfig     `json:"addresses,omitempty"`
    Ports               *TestPortConfig        `json:"ports,omitempty"`
    RaftProtocol        int                    `json:"raft_protocol,omitempty"`
    ACLMasterToken      string                 `json:"acl_master_token,omitempty"`
    ACLDatacenter       string                 `json:"acl_datacenter,omitempty"`
    ACLDefaultPolicy    string                 `json:"acl_default_policy,omitempty"`
    ACLEnforceVersion8  bool                   `json:"acl_enforce_version_8"`
    Encrypt             string                 `json:"encrypt,omitempty"`
    CAFile              string                 `json:"ca_file,omitempty"`
    CertFile            string                 `json:"cert_file,omitempty"`
    KeyFile             string                 `json:"key_file,omitempty"`
    VerifyIncoming      bool                   `json:"verify_incoming,omitempty"`
    VerifyIncomingRPC   bool                   `json:"verify_incoming_rpc,omitempty"`
    VerifyIncomingHTTPS bool                   `json:"verify_incoming_https,omitempty"`
    VerifyOutgoing      bool                   `json:"verify_outgoing,omitempty"`
    ReadyTimeout        time.Duration          `json:"-"`
    Stdout, Stderr      io.Writer              `json:"-"`
    Args                []string               `json:"-"`
}

type TestService

TestService is used to serialize a service definition.

type TestService struct {
    ID      string   `json:",omitempty"`
    Name    string   `json:",omitempty"`
    Tags    []string `json:",omitempty"`
    Address string   `json:",omitempty"`
    Port    int      `json:",omitempty"`
}

type WrappedServer

type WrappedServer struct {
    // contains filtered or unexported fields
}

func (*WrappedServer) AddAddressableService

func (w *WrappedServer) AddAddressableService(name, status, address string, port int, tags []string)

func (*WrappedServer) AddCheck

func (w *WrappedServer) AddCheck(name, serviceID, status string)

func (*WrappedServer) AddService

func (w *WrappedServer) AddService(name, status string, tags []string)

func (*WrappedServer) GetKV

func (w *WrappedServer) GetKV(key string) []byte

func (*WrappedServer) GetKVString

func (w *WrappedServer) GetKVString(key string) string

func (*WrappedServer) JoinLAN

func (w *WrappedServer) JoinLAN(addr string)

func (*WrappedServer) JoinWAN

func (w *WrappedServer) JoinWAN(addr string)

func (*WrappedServer) ListKV

func (w *WrappedServer) ListKV(prefix string) []string

func (*WrappedServer) PopulateKV

func (w *WrappedServer) PopulateKV(data map[string][]byte)

func (*WrappedServer) SetKV

func (w *WrappedServer) SetKV(key string, val []byte)

func (*WrappedServer) SetKVString

func (w *WrappedServer) SetKVString(key string, val string)

Subdirectories

Name Synopsis
..
retry Package retry provides support for repeating operations in tests.