-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhostset_test.go
64 lines (55 loc) · 1.69 KB
/
hostset_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
package herd
import (
"testing"
)
func TestHostSetSorting(t *testing.T) {
h1 := NewHost("host-a.example.com", "", HostAttributes{"site": "site1", "role": "db"})
h2 := NewHost("host-b.example.com", "", HostAttributes{"site": "site2", "role": "db"})
h3 := NewHost("host-c.example.com", "", HostAttributes{"site": "site1", "role": "app"})
h4 := NewHost("host-d.example.com", "", HostAttributes{"site": "site2", "role": "app"})
hosts := HostSet{hosts: []*Host{h1, h2, h3, h4}}
if !h1.less(h2, []string{}) {
t.Errorf("Sorting hosts with no fields is failing")
}
if h2.less(h1, []string{}) {
t.Errorf("Sorting hosts with no fields is failing")
}
if !h1.less(h2, []string{"name"}) {
t.Errorf("Sorting hosts by name is failing")
}
if h2.less(h1, []string{"name"}) {
t.Errorf("Sorting hosts by name fields is failing")
}
hosts.SetSortFields([]string{"site", "name"})
hosts.Sort()
if !eq(hosts.hosts, []*Host{h1, h3, h2, h4}) {
t.Errorf("Sorting by site+name is failing, got %v", hosts)
}
hosts.SetSortFields([]string{"site", "role"})
hosts.Sort()
if !eq(hosts.hosts, []*Host{h3, h1, h4, h2}) {
t.Errorf("Sorting by site+role is failing, got %v", hosts)
}
}
func TestHostSetUniq(t *testing.T) {
h1 := NewHost("host-a.example.com", "", HostAttributes{"site": "site1", "role": "db"})
h2 := NewHost("host-a.example.com", "", HostAttributes{"site": "site1", "role": "db"})
hosts := HostSet{hosts: []*Host{h1, h2}}
hosts.Uniq()
if len(hosts.hosts) != 1 {
t.Errorf("Uniq did not deduplicate hosts")
}
hosts = HostSet{hosts: []*Host{}}
hosts.Uniq()
}
func eq(a, b []*Host) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}