micro/registry/consul/watcher_test.go

87 lines
2.1 KiB
Go
Raw Normal View History

2019-01-15 19:50:37 +03:00
package consul
import (
"testing"
"github.com/hashicorp/consul/api"
2019-01-15 19:50:37 +03:00
"github.com/micro/go-micro/registry"
)
func TestHealthyServiceHandler(t *testing.T) {
watcher := newWatcher()
serviceEntry := newServiceEntry(
"node-name", "node-address", "service-name", "v1.0.0",
[]*api.HealthCheck{
newHealthCheck("node-name", "service-name", "passing"),
},
)
watcher.serviceHandler(1234, []*api.ServiceEntry{serviceEntry})
if len(watcher.services["service-name"][0].Nodes) != 1 {
t.Errorf("Expected length of the service nodes to be 1")
}
}
func TestUnhealthyServiceHandler(t *testing.T) {
watcher := newWatcher()
serviceEntry := newServiceEntry(
"node-name", "node-address", "service-name", "v1.0.0",
[]*api.HealthCheck{
newHealthCheck("node-name", "service-name", "critical"),
},
)
watcher.serviceHandler(1234, []*api.ServiceEntry{serviceEntry})
if len(watcher.services["service-name"][0].Nodes) != 0 {
t.Errorf("Expected length of the service nodes to be 0")
}
}
func TestUnhealthyNodeServiceHandler(t *testing.T) {
watcher := newWatcher()
serviceEntry := newServiceEntry(
"node-name", "node-address", "service-name", "v1.0.0",
[]*api.HealthCheck{
newHealthCheck("node-name", "service-name", "passing"),
newHealthCheck("node-name", "serfHealth", "critical"),
},
)
watcher.serviceHandler(1234, []*api.ServiceEntry{serviceEntry})
if len(watcher.services["service-name"][0].Nodes) != 0 {
t.Errorf("Expected length of the service nodes to be 0")
}
}
func newWatcher() *consulWatcher {
return &consulWatcher{
exit: make(chan bool),
2019-01-15 19:50:37 +03:00
next: make(chan *registry.Result, 10),
services: make(map[string][]*registry.Service),
}
}
func newHealthCheck(node, name, status string) *api.HealthCheck {
return &api.HealthCheck{
Node: node,
Name: name,
Status: status,
ServiceName: name,
}
}
func newServiceEntry(node, address, name, version string, checks []*api.HealthCheck) *api.ServiceEntry {
return &api.ServiceEntry{
Node: &api.Node{Node: node, Address: name},
Service: &api.AgentService{
Service: name,
Address: address,
Tags: encodeVersion(version),
},
Checks: checks,
}
}