2020-07-27 13:22:00 +01:00
|
|
|
package mdns
|
2016-05-01 21:21:15 +01:00
|
|
|
|
|
|
|
import (
|
2019-11-19 16:55:33 +00:00
|
|
|
"os"
|
2016-05-01 21:21:15 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-07-27 13:22:00 +01:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v3/registry"
|
2016-05-01 21:21:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMDNS(t *testing.T) {
|
2020-02-08 02:45:32 +03:00
|
|
|
// skip test in travis because of sendto: operation not permitted error
|
|
|
|
if travis := os.Getenv("TRAVIS"); travis == "true" {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
testData := []*registry.Service{
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2016-05-01 21:21:15 +01:00
|
|
|
Name: "test1",
|
|
|
|
Version: "1.0.1",
|
2020-07-27 13:22:00 +01:00
|
|
|
Nodes: []*registry.Node{
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2016-05-01 21:21:15 +01:00
|
|
|
Id: "test1-1",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "10.0.0.1:10001",
|
2016-05-01 21:21:15 +01:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2016-05-01 21:21:15 +01:00
|
|
|
Name: "test2",
|
|
|
|
Version: "1.0.2",
|
2020-07-27 13:22:00 +01:00
|
|
|
Nodes: []*registry.Node{
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2016-05-01 21:21:15 +01:00
|
|
|
Id: "test2-1",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "10.0.0.2:10002",
|
2016-05-01 21:21:15 +01:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo2": "bar2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2016-05-01 21:21:15 +01:00
|
|
|
Name: "test3",
|
|
|
|
Version: "1.0.3",
|
2020-07-27 13:22:00 +01:00
|
|
|
Nodes: []*registry.Node{
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2016-05-01 21:21:15 +01:00
|
|
|
Id: "test3-1",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "10.0.0.3:10003",
|
2016-05-01 21:21:15 +01:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo3": "bar3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:50:16 +00:00
|
|
|
travis := os.Getenv("TRAVIS")
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
var opts []registry.Option
|
2019-11-19 16:50:16 +00:00
|
|
|
|
|
|
|
if travis == "true" {
|
2020-07-27 13:22:00 +01:00
|
|
|
opts = append(opts, registry.Timeout(time.Millisecond*100))
|
2019-11-19 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 21:21:15 +01:00
|
|
|
// new registry
|
2019-11-19 16:50:16 +00:00
|
|
|
r := NewRegistry(opts...)
|
2016-05-01 21:21:15 +01: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 21:30:14 +01:00
|
|
|
t.Fatalf("Expected name %s got %s", service.Name, s[0].Name)
|
2016-05-01 21:21:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if s[0].Version != service.Version {
|
2016-05-01 21:30:14 +01:00
|
|
|
t.Fatalf("Expected version %s got %s", service.Version, s[0].Version)
|
2016-05-01 21:21:15 +01: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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-10 17:19:26 +01:00
|
|
|
|
|
|
|
func TestEncoding(t *testing.T) {
|
|
|
|
testData := []*mdnsTxt{
|
|
|
|
{
|
|
|
|
Version: "1.0.0",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2020-07-27 13:22:00 +01:00
|
|
|
Endpoints: []*registry.Endpoint{
|
2020-04-10 17:19:26 +01:00
|
|
|
{
|
|
|
|
Name: "endpoint1",
|
2020-07-27 13:22:00 +01:00
|
|
|
Request: ®istry.Value{
|
2020-04-10 17:19:26 +01:00
|
|
|
Name: "request",
|
|
|
|
Type: "request",
|
|
|
|
},
|
2020-07-27 13:22:00 +01:00
|
|
|
Response: ®istry.Value{
|
2020-04-10 17:19:26 +01:00
|
|
|
Name: "response",
|
|
|
|
Type: "response",
|
|
|
|
},
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo1": "bar1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
|
|
|
encoded, err := encode(d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, txt := range encoded {
|
|
|
|
if len(txt) > 255 {
|
|
|
|
t.Fatalf("One of parts for txt is %d characters", len(txt))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := decode(encoded)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if decoded.Version != d.Version {
|
|
|
|
t.Fatalf("Expected version %s got %s", d.Version, decoded.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(decoded.Endpoints) != len(d.Endpoints) {
|
|
|
|
t.Fatalf("Expected %d endpoints, got %d", len(d.Endpoints), len(decoded.Endpoints))
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range d.Metadata {
|
|
|
|
if val := decoded.Metadata[k]; val != v {
|
|
|
|
t.Fatalf("Expected %s=%s got %s=%s", k, v, k, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-12 11:01:09 +01:00
|
|
|
|
|
|
|
func TestWatcher(t *testing.T) {
|
|
|
|
if travis := os.Getenv("TRAVIS"); travis == "true" {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
testData := []*registry.Service{
|
2020-04-12 11:01:09 +01:00
|
|
|
{
|
|
|
|
Name: "test1",
|
|
|
|
Version: "1.0.1",
|
2020-07-27 13:22:00 +01:00
|
|
|
Nodes: []*registry.Node{
|
2020-04-12 11:01:09 +01:00
|
|
|
{
|
|
|
|
Id: "test1-1",
|
|
|
|
Address: "10.0.0.1:10001",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test2",
|
|
|
|
Version: "1.0.2",
|
2020-07-27 13:22:00 +01:00
|
|
|
Nodes: []*registry.Node{
|
2020-04-12 11:01:09 +01:00
|
|
|
{
|
|
|
|
Id: "test2-1",
|
|
|
|
Address: "10.0.0.2:10002",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo2": "bar2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "test3",
|
|
|
|
Version: "1.0.3",
|
2020-07-27 13:22:00 +01:00
|
|
|
Nodes: []*registry.Node{
|
2020-04-12 11:01:09 +01:00
|
|
|
{
|
|
|
|
Id: "test3-1",
|
|
|
|
Address: "10.0.0.3:10003",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo3": "bar3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
testFn := func(service, s *registry.Service) {
|
2020-04-12 11:01:09 +01:00
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("Expected one result for %s got nil", service.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Name != service.Name {
|
|
|
|
t.Fatalf("Expected name %s got %s", service.Name, s.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Version != service.Version {
|
|
|
|
t.Fatalf("Expected version %s got %s", service.Version, s.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node, got %d", len(s.Nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
node := s.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
travis := os.Getenv("TRAVIS")
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
var opts []registry.Option
|
2020-04-12 11:01:09 +01:00
|
|
|
|
|
|
|
if travis == "true" {
|
2020-07-27 13:22:00 +01:00
|
|
|
opts = append(opts, registry.Timeout(time.Millisecond*100))
|
2020-04-12 11:01:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// new registry
|
|
|
|
r := NewRegistry(opts...)
|
|
|
|
|
|
|
|
w, err := r.Watch()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
for _, service := range testData {
|
|
|
|
// register service
|
|
|
|
if err := r.Register(service); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Service.Name != service.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Action != "create" {
|
|
|
|
t.Fatalf("Expected create event got %s for %s", res.Action, res.Service.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
testFn(service, res.Service)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// deregister
|
|
|
|
if err := r.Deregister(service); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Service.Name != service.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Action != "delete" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
testFn(service, res.Service)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|