2019-01-15 19:50:37 +03:00
|
|
|
package registry
|
2016-05-01 23:21:15 +03:00
|
|
|
|
|
|
|
import (
|
2019-11-19 19:55:33 +03:00
|
|
|
"os"
|
2016-05-01 23:21:15 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMDNS(t *testing.T) {
|
2019-01-15 19:50:37 +03:00
|
|
|
testData := []*Service{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-01 23:21:15 +03:00
|
|
|
Name: "test1",
|
|
|
|
Version: "1.0.1",
|
2019-01-15 19:50:37 +03:00
|
|
|
Nodes: []*Node{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-01 23:21:15 +03:00
|
|
|
Id: "test1-1",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.1:10001",
|
2016-05-01 23:21:15 +03:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-01 23:21:15 +03:00
|
|
|
Name: "test2",
|
|
|
|
Version: "1.0.2",
|
2019-01-15 19:50:37 +03:00
|
|
|
Nodes: []*Node{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-01 23:21:15 +03:00
|
|
|
Id: "test2-1",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.2:10002",
|
2016-05-01 23:21:15 +03:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo2": "bar2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-01 23:21:15 +03:00
|
|
|
Name: "test3",
|
|
|
|
Version: "1.0.3",
|
2019-01-15 19:50:37 +03:00
|
|
|
Nodes: []*Node{
|
2019-11-01 18:07:53 +03:00
|
|
|
{
|
2016-05-01 23:21:15 +03:00
|
|
|
Id: "test3-1",
|
2019-07-08 10:01:42 +03:00
|
|
|
Address: "10.0.0.3:10003",
|
2016-05-01 23:21:15 +03:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo3": "bar3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:50:16 +03:00
|
|
|
travis := os.Getenv("TRAVIS")
|
|
|
|
|
|
|
|
var opts []Option
|
|
|
|
|
|
|
|
if travis == "true" {
|
|
|
|
opts = append(opts, Timeout(time.Millisecond*100))
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:21:15 +03:00
|
|
|
// new registry
|
2019-11-19 19:50:16 +03:00
|
|
|
r := NewRegistry(opts...)
|
2016-05-01 23:21:15 +03:00
|
|
|
|
|
|
|
for _, service := range testData {
|
|
|
|
// register service
|
|
|
|
if err := r.Register(service); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get registered service
|
|
|
|
s, err := r.GetService(service.Name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s) != 1 {
|
|
|
|
t.Fatalf("Expected one result for %s got %d", service.Name, len(s))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s[0].Name != service.Name {
|
2016-05-01 23:30:14 +03:00
|
|
|
t.Fatalf("Expected name %s got %s", service.Name, s[0].Name)
|
2016-05-01 23:21:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if s[0].Version != service.Version {
|
2016-05-01 23:30:14 +03:00
|
|
|
t.Fatalf("Expected version %s got %s", service.Version, s[0].Version)
|
2016-05-01 23:21:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(s[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node, got %d", len(s[0].Nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
node := s[0].Nodes[0]
|
|
|
|
|
|
|
|
if node.Id != service.Nodes[0].Id {
|
|
|
|
t.Fatalf("Expected node id %s got %s", service.Nodes[0].Id, node.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Address != service.Nodes[0].Address {
|
|
|
|
t.Fatalf("Expected node address %s got %s", service.Nodes[0].Address, node.Address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := r.ListServices()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range testData {
|
|
|
|
var seen bool
|
|
|
|
for _, s := range services {
|
|
|
|
if s.Name == service.Name {
|
|
|
|
seen = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
|
|
|
t.Fatalf("Expected service %s got nothing", service.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deregister
|
|
|
|
if err := r.Deregister(service); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 5)
|
|
|
|
|
|
|
|
// check its gone
|
|
|
|
s, _ := r.GetService(service.Name)
|
|
|
|
if len(s) > 0 {
|
|
|
|
t.Fatalf("Expected nothing got %+v", s[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|