2019-01-15 16:50:37 +00:00
|
|
|
package consul
|
2016-08-26 08:36:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-04-17 15:31:50 +01:00
|
|
|
"net"
|
2016-08-26 08:36:45 +01:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2018-11-23 17:11:37 +10:00
|
|
|
"time"
|
2016-08-26 08:36:45 +01:00
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
2019-01-15 16:50:37 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
2016-08-26 08:36:45 +01:00
|
|
|
)
|
|
|
|
|
2017-04-17 15:31:50 +01:00
|
|
|
type mockRegistry struct {
|
|
|
|
body []byte
|
|
|
|
status int
|
|
|
|
err error
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeData(obj interface{}) ([]byte, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
if err := enc.Encode(obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockServer(rg *mockRegistry, l net.Listener) error {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc(rg.url, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if rg.err != nil {
|
|
|
|
http.Error(w, rg.err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(rg.status)
|
|
|
|
w.Write(rg.body)
|
|
|
|
})
|
|
|
|
return http.Serve(l, mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConsulTestRegistry(r *mockRegistry) (*consulRegistry, func()) {
|
2017-11-09 14:21:26 +00:00
|
|
|
l, err := net.Listen("tcp", "localhost:0")
|
2017-04-17 15:31:50 +01:00
|
|
|
if err != nil {
|
|
|
|
// blurgh?!!
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
cfg := consul.DefaultConfig()
|
|
|
|
cfg.Address = l.Addr().String()
|
|
|
|
cl, _ := consul.NewClient(cfg)
|
|
|
|
|
|
|
|
go newMockServer(r, l)
|
|
|
|
|
|
|
|
return &consulRegistry{
|
2018-11-23 17:11:37 +10:00
|
|
|
Address: cfg.Address,
|
|
|
|
Client: cl,
|
2019-01-15 16:50:37 +00:00
|
|
|
opts: registry.Options{},
|
2018-11-23 17:11:37 +10:00
|
|
|
register: make(map[string]uint64),
|
|
|
|
lastChecked: make(map[string]time.Time),
|
|
|
|
queryOptions: &consul.QueryOptions{
|
|
|
|
AllowStale: true,
|
|
|
|
},
|
2017-04-17 15:31:50 +01:00
|
|
|
}, func() {
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServiceList(svc []*consul.ServiceEntry) []byte {
|
|
|
|
bts, _ := encodeData(svc)
|
|
|
|
return bts
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:36:45 +01:00
|
|
|
func TestConsul_GetService_WithError(t *testing.T) {
|
2017-04-17 15:31:50 +01:00
|
|
|
cr, cl := newConsulTestRegistry(&mockRegistry{
|
2016-08-26 08:36:45 +01:00
|
|
|
err: errors.New("client-error"),
|
2017-04-17 15:31:50 +01:00
|
|
|
url: "/v1/health/service/service-name",
|
2016-08-26 08:36:45 +01:00
|
|
|
})
|
2017-04-17 15:31:50 +01:00
|
|
|
defer cl()
|
2016-08-26 08:36:45 +01:00
|
|
|
|
|
|
|
if _, err := cr.GetService("test-service"); err == nil {
|
|
|
|
t.Fatalf("Expected error not to be `nil`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsul_GetService_WithHealthyServiceNodes(t *testing.T) {
|
|
|
|
// warning is still seen as healthy, critical is not
|
|
|
|
svcs := []*consul.ServiceEntry{
|
|
|
|
newServiceEntry(
|
|
|
|
"node-name-1", "node-address-1", "service-name", "v1.0.0",
|
|
|
|
[]*consul.HealthCheck{
|
|
|
|
newHealthCheck("node-name-1", "service-name", "passing"),
|
|
|
|
newHealthCheck("node-name-1", "service-name", "warning"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
newServiceEntry(
|
|
|
|
"node-name-2", "node-address-2", "service-name", "v1.0.0",
|
|
|
|
[]*consul.HealthCheck{
|
|
|
|
newHealthCheck("node-name-2", "service-name", "passing"),
|
|
|
|
newHealthCheck("node-name-2", "service-name", "warning"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:31:50 +01:00
|
|
|
cr, cl := newConsulTestRegistry(&mockRegistry{
|
2016-08-26 08:36:45 +01:00
|
|
|
status: 200,
|
|
|
|
body: newServiceList(svcs),
|
|
|
|
url: "/v1/health/service/service-name",
|
|
|
|
})
|
2017-04-17 15:31:50 +01:00
|
|
|
defer cl()
|
2016-08-26 08:36:45 +01:00
|
|
|
|
2017-11-09 14:16:35 +00:00
|
|
|
svc, err := cr.GetService("service-name")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error", err)
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:36:45 +01:00
|
|
|
if exp, act := 1, len(svc); exp != act {
|
|
|
|
t.Fatalf("Expected len of svc to be `%d`, got `%d`.", exp, act)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exp, act := 2, len(svc[0].Nodes); exp != act {
|
|
|
|
t.Fatalf("Expected len of nodes to be `%d`, got `%d`.", exp, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsul_GetService_WithUnhealthyServiceNode(t *testing.T) {
|
|
|
|
// warning is still seen as healthy, critical is not
|
|
|
|
svcs := []*consul.ServiceEntry{
|
|
|
|
newServiceEntry(
|
|
|
|
"node-name-1", "node-address-1", "service-name", "v1.0.0",
|
|
|
|
[]*consul.HealthCheck{
|
|
|
|
newHealthCheck("node-name-1", "service-name", "passing"),
|
|
|
|
newHealthCheck("node-name-1", "service-name", "warning"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
newServiceEntry(
|
|
|
|
"node-name-2", "node-address-2", "service-name", "v1.0.0",
|
|
|
|
[]*consul.HealthCheck{
|
|
|
|
newHealthCheck("node-name-2", "service-name", "passing"),
|
|
|
|
newHealthCheck("node-name-2", "service-name", "critical"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:31:50 +01:00
|
|
|
cr, cl := newConsulTestRegistry(&mockRegistry{
|
2016-08-26 08:36:45 +01:00
|
|
|
status: 200,
|
|
|
|
body: newServiceList(svcs),
|
|
|
|
url: "/v1/health/service/service-name",
|
|
|
|
})
|
2017-04-17 15:31:50 +01:00
|
|
|
defer cl()
|
2016-08-26 08:36:45 +01:00
|
|
|
|
2017-11-09 14:16:35 +00:00
|
|
|
svc, err := cr.GetService("service-name")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error", err)
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:36:45 +01:00
|
|
|
if exp, act := 1, len(svc); exp != act {
|
|
|
|
t.Fatalf("Expected len of svc to be `%d`, got `%d`.", exp, act)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exp, act := 1, len(svc[0].Nodes); exp != act {
|
|
|
|
t.Fatalf("Expected len of nodes to be `%d`, got `%d`.", exp, act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsul_GetService_WithUnhealthyServiceNodes(t *testing.T) {
|
|
|
|
// warning is still seen as healthy, critical is not
|
|
|
|
svcs := []*consul.ServiceEntry{
|
|
|
|
newServiceEntry(
|
|
|
|
"node-name-1", "node-address-1", "service-name", "v1.0.0",
|
|
|
|
[]*consul.HealthCheck{
|
|
|
|
newHealthCheck("node-name-1", "service-name", "passing"),
|
|
|
|
newHealthCheck("node-name-1", "service-name", "critical"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
newServiceEntry(
|
|
|
|
"node-name-2", "node-address-2", "service-name", "v1.0.0",
|
|
|
|
[]*consul.HealthCheck{
|
|
|
|
newHealthCheck("node-name-2", "service-name", "passing"),
|
|
|
|
newHealthCheck("node-name-2", "service-name", "critical"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:31:50 +01:00
|
|
|
cr, cl := newConsulTestRegistry(&mockRegistry{
|
2016-08-26 08:36:45 +01:00
|
|
|
status: 200,
|
|
|
|
body: newServiceList(svcs),
|
|
|
|
url: "/v1/health/service/service-name",
|
|
|
|
})
|
2017-04-17 15:31:50 +01:00
|
|
|
defer cl()
|
2016-08-26 08:36:45 +01:00
|
|
|
|
2017-11-09 14:16:35 +00:00
|
|
|
svc, err := cr.GetService("service-name")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error", err)
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:36:45 +01:00
|
|
|
if exp, act := 1, len(svc); exp != act {
|
|
|
|
t.Fatalf("Expected len of svc to be `%d`, got `%d`.", exp, act)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exp, act := 0, len(svc[0].Nodes); exp != act {
|
|
|
|
t.Fatalf("Expected len of nodes to be `%d`, got `%d`.", exp, act)
|
|
|
|
}
|
|
|
|
}
|