2015-05-15 18:23:59 -04:00
|
|
|
package hostdiscovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
mapset "github.com/deckarep/golang-set"
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
2015-05-15 18:23:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiff(t *testing.T) {
|
|
|
|
existing := mapset.NewSetFromSlice([]interface{}{"1.1.1.1", "2.2.2.2"})
|
|
|
|
addedIP := "3.3.3.3"
|
|
|
|
updated := existing.Clone()
|
|
|
|
updated.Add(addedIP)
|
|
|
|
|
|
|
|
added, removed := diff(existing, updated)
|
|
|
|
if len(added) != 1 {
|
|
|
|
t.Fatalf("Diff failed for an Add update. Expecting 1 element, but got %d elements", len(added))
|
|
|
|
}
|
|
|
|
if added[0].String() != addedIP {
|
|
|
|
t.Fatalf("Expecting : %v, Got : %v", addedIP, added[0])
|
|
|
|
}
|
|
|
|
if len(removed) > 0 {
|
|
|
|
t.Fatalf("Diff failed for remove use-case. Expecting 0 element, but got %d elements", len(removed))
|
|
|
|
}
|
|
|
|
|
|
|
|
updated = mapset.NewSetFromSlice([]interface{}{addedIP})
|
|
|
|
added, removed = diff(existing, updated)
|
|
|
|
if len(removed) != 2 {
|
2016-06-02 01:37:39 -04:00
|
|
|
t.Fatalf("Diff failed for a remove update. Expecting 2 element, but got %d elements", len(removed))
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
if len(added) != 1 {
|
|
|
|
t.Fatalf("Diff failed for add use-case. Expecting 1 element, but got %d elements", len(added))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddedCallback(t *testing.T) {
|
|
|
|
hd := hostDiscovery{}
|
|
|
|
hd.nodes = mapset.NewSetFromSlice([]interface{}{"1.1.1.1"})
|
2016-02-12 08:07:00 -05:00
|
|
|
update := []*discovery.Entry{{Host: "1.1.1.1", Port: "0"}, {Host: "2.2.2.2", Port: "0"}}
|
2015-05-15 18:23:59 -04:00
|
|
|
|
|
|
|
added := false
|
|
|
|
removed := false
|
2015-10-22 00:31:43 -04:00
|
|
|
hd.processCallback(update, func() {}, func(hosts []net.IP) { added = true }, func(hosts []net.IP) { removed = true })
|
2015-05-15 18:23:59 -04:00
|
|
|
if !added {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expecting an Added callback notification. But none received")
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
2018-05-29 05:17:28 -04:00
|
|
|
if removed {
|
|
|
|
t.Fatal("Not expecting a Removed callback notification. But received a callback")
|
|
|
|
}
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovedCallback(t *testing.T) {
|
|
|
|
hd := hostDiscovery{}
|
|
|
|
hd.nodes = mapset.NewSetFromSlice([]interface{}{"1.1.1.1", "2.2.2.2"})
|
2016-02-12 08:07:00 -05:00
|
|
|
update := []*discovery.Entry{{Host: "1.1.1.1", Port: "0"}}
|
2015-05-15 18:23:59 -04:00
|
|
|
|
|
|
|
added := false
|
|
|
|
removed := false
|
2015-10-22 00:31:43 -04:00
|
|
|
hd.processCallback(update, func() {}, func(hosts []net.IP) { added = true }, func(hosts []net.IP) { removed = true })
|
2018-05-29 05:17:28 -04:00
|
|
|
if added {
|
|
|
|
t.Fatal("Not expecting an Added callback notification. But received a callback")
|
|
|
|
}
|
2015-05-15 18:23:59 -04:00
|
|
|
if !removed {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expecting a Removed callback notification. But none received")
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoCallback(t *testing.T) {
|
|
|
|
hd := hostDiscovery{}
|
|
|
|
hd.nodes = mapset.NewSetFromSlice([]interface{}{"1.1.1.1", "2.2.2.2"})
|
2016-02-12 08:07:00 -05:00
|
|
|
update := []*discovery.Entry{{Host: "1.1.1.1", Port: "0"}, {Host: "2.2.2.2", Port: "0"}}
|
2015-05-15 18:23:59 -04:00
|
|
|
|
|
|
|
added := false
|
|
|
|
removed := false
|
2015-10-22 00:31:43 -04:00
|
|
|
hd.processCallback(update, func() {}, func(hosts []net.IP) { added = true }, func(hosts []net.IP) { removed = true })
|
2015-05-15 18:23:59 -04:00
|
|
|
if added || removed {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Not expecting any callback notification. But received a callback")
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
}
|