Skip to content

Commit

Permalink
Merge pull request #2 from rapid7/add-consul-watcher-tests
Browse files Browse the repository at this point in the history
Add tests for consul watcher
  • Loading branch information
asebastian-r7 authored Aug 27, 2018
2 parents 6f44e75 + fda6661 commit 7c0c807
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 18 deletions.
160 changes: 154 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
# name = "github.com/x/y"
# version = "2.4.0"

[[constraint]]
name = "github.com/hashicorp/consul"
version = "1.2.2"
45 changes: 33 additions & 12 deletions watchers/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,13 @@ func Sync(t time.Time) {
log.Print("consul sync begun")

consulHost := Config.host
consulConfig := api.DefaultConfig()
consulConfig.Address = consulHost
consulConfig.Scheme = "http"

client, err := api.NewClient(consulConfig)
client, err := setUpConsulClient(consulHost)
if err != nil {
log.Errorf("Consul error: %v\n", err)
return
}

catalog := client.Catalog()
qo := api.QueryOptions{}
services, _, err := catalog.Services(&qo)
services, qo, err := getServices(client)
if err != nil {
log.Errorf("Catalog/services error: %v", err)
return
}

Expand All @@ -98,15 +90,44 @@ func Sync(t time.Time) {

wg.Wait()

kv.WriteProperty("consul", healthyNodes)
kv.GetProperty("consul")
writeProperties()

Health = true
Up = true

log.Print("Consul sync is finished")
}

func setUpConsulClient(consulHost string) (*api.Client, error) {
consulConfig := api.DefaultConfig()
consulConfig.Address = consulHost
consulConfig.Scheme = "http"

client, err := api.NewClient(consulConfig)
if err != nil {
log.Errorf("Consul error: %v\n", err)
return nil, err
}

return client, nil
}

func getServices(client *api.Client) (map[string][]string, api.QueryOptions, error) {
catalog := client.Catalog()
qo := api.QueryOptions{}
services, _, err := catalog.Services(&qo)
if err != nil {
log.Errorf("Catalog/services error: %v", err)
return nil, qo, err
}

return services, qo, nil
}

func writeProperties() {
kv.WriteProperty("consul", healthyNodes)
}

func getServiceHealth(key string, client *api.Client, qo api.QueryOptions, m *sync.Mutex) {
h := client.Health()
sh, _, err := h.Service(key, "", true, &qo)
Expand Down
48 changes: 48 additions & 0 deletions watchers/consul/consul_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package consul

import (
"os"
"sync"
"testing"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testutil"
"github.com/stretchr/testify/assert"

"cps/pkg/kv"
)

func TestGetServiceHealth(t *testing.T) {
os.Stdout, _ = os.Open(os.DevNull)

srv1, err := testutil.NewTestServer()
if err != nil {
t.Fatal(err)
}
defer srv1.Stop()

srv1.AddAddressableService(t, "service-one", api.HealthPassing, "127.0.0.1", 8192, []string{"test"})
srv1.AddCheck(t, "service:service-one", "service-one", api.HealthPassing)

client, err := setUpConsulClient(srv1.HTTPAddr)
assert.Nil(t, err, "Expected no error setting up consul client")

services, qo, err := getServices(client)
assert.Nil(t, err, "Expected no error getting services")

healthyNodes = make(map[string][]string)
var mutex = &sync.Mutex{}
for key, _ := range services {
getServiceHealth(key, client, qo, mutex)
}

writeProperties()

em := map[string][]string{
"service-one": {"127.0.0.1"},
"consul": {"127.0.0.1"},
}
c := kv.GetProperty("consul")

assert.Equal(t, c, em, "Expected consul maps to be equal")
}

0 comments on commit 7c0c807

Please sign in to comment.