This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
topology_test.go
89 lines (73 loc) · 1.85 KB
/
topology_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"reflect"
"testing"
)
var Top = Topology{Hosts: []string{
"host1",
"host2",
"host3",
"host4",
"host5",
}}
func hasRoute(routes []Rule, rule Rule) bool {
for _, r := range routes {
if r.From == rule.From && r.To == rule.To {
return true
}
}
return false
}
func TestRing(t *testing.T) {
ring := Top.Ring()
// 5 nodes that can talk to 2 other nodes each
expectedRingSize := 5 * 2
if len(ring) != expectedRingSize {
t.Fatalf("Expected ring-size to be %d, but is %d", expectedRingSize, len(ring))
}
sees := map[string]int{}
seen := map[string]int{}
for _, rule := range ring {
sees[rule.From]++
seen[rule.To]++
}
for host, seenHosts := range sees {
if seenHosts > 2 {
t.Fatalf("Host %s can see %d hosts: %v", host, seenHosts, sees[host])
}
}
for host, seenByHosts := range seen {
if seenByHosts > 2 {
t.Fatalf("Host %s can see %d hosts: %v", host, seenByHosts, seen[host])
}
}
}
func TestInvert(t *testing.T) {
top := Topology{Hosts: []string{"host1", "host2"}}
full := top.Default()
inverted := top.Invert(full)
if len(inverted) != 0 {
t.Fatalf("Expected the invert of the Full() network to be empty, but is %v", inverted)
}
full2 := top.Invert(inverted)
if !reflect.DeepEqual(full, full2) {
t.Fatalf("Expected the Invert() of an empty list to be the Full() network, but is %v", full2)
}
}
func TestInvert__SubNet(t *testing.T) {
top := Topology{Hosts: []string{"host1", "host2", "host3"}}
rules := []Rule{
Rule{"host1", "host2"},
Rule{"host2", "host1"},
}
inverted := top.Invert(rules)
if hasRoute(inverted, rules[0]) {
t.Fatalf("Expected rule host1->host2 to no longer exist")
}
if hasRoute(inverted, rules[1]) {
t.Fatalf("Expected rule host1->host2 to no longer exist")
}
if !hasRoute(inverted, Rule{"host3", "host1"}) {
t.Fatalf("Expected rule host3->host1 to exist")
}
}