Merge pull request #2993 from hashicorp/sethvargo/pr-2375

Add `AddAccessibleService` to testutil
This commit is contained in:
Seth Vargo 2017-05-01 14:03:31 -07:00 committed by GitHub
commit 2ca69bf15f
3 changed files with 20 additions and 2 deletions

View File

@ -58,6 +58,9 @@ func TestFoo_bar(t *testing.T) {
// Create a service // Create a service
srv1.AddService(t, "redis", structs.HealthPassing, []string{"master"}) srv1.AddService(t, "redis", structs.HealthPassing, []string{"master"})
// Create a service that will be accessed in target source code
srv1.AddAccessibleService("redis", structs.HealthPassing, "127.0.0.1", 6379, []string{"master"})
// Create a service check // Create a service check
srv1.AddCheck(t, "service:redis", "redis", structs.HealthPassing) srv1.AddCheck(t, "service:redis", "redis", structs.HealthPassing)

View File

@ -109,9 +109,20 @@ func (s *TestServer) ListKV(t *testing.T, prefix string) []string {
// automatically adds a health check with the given status, which // automatically adds a health check with the given status, which
// can be one of "passing", "warning", or "critical". // can be one of "passing", "warning", or "critical".
func (s *TestServer) AddService(t *testing.T, name, status string, tags []string) { func (s *TestServer) AddService(t *testing.T, name, status string, tags []string) {
s.AddAddressableService(t, name, status, "", 0, tags) // set empty address and 0 as port for non-accessible service
}
// 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 (s *TestServer) AddAddressableService(t *testing.T, name, status, address string, port int, tags []string) {
svc := &TestService{ svc := &TestService{
Name: name, Name: name,
Tags: tags, Tags: tags,
Address: address,
Port: port,
} }
payload, err := s.encodePayload(svc) payload, err := s.encodePayload(svc)
if err != nil { if err != nil {

View File

@ -56,6 +56,10 @@ func (w *WrappedServer) AddService(name, status string, tags []string) {
w.s.AddService(w.t, name, status, tags) w.s.AddService(w.t, name, status, tags)
} }
func (w *WrappedServer) AddAddressableService(name, status, address string, port int, tags []string) {
w.s.AddAddressableService(w.t, name, status, address, port, tags)
}
func (w *WrappedServer) AddCheck(name, serviceID, status string) { func (w *WrappedServer) AddCheck(name, serviceID, status string) {
w.s.AddCheck(w.t, name, serviceID, status) w.s.AddCheck(w.t, name, serviceID, status)
} }