-
Notifications
You must be signed in to change notification settings - Fork 715
/
Copy pathweave_test.go
103 lines (90 loc) · 1.82 KB
/
weave_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package app_test
import (
"net"
"sync"
"testing"
"time"
fsouza "github.com/fsouza/go-dockerclient"
"github.com/weaveworks/scope/app"
"github.com/weaveworks/scope/test"
)
type mockDockerClient struct{}
func (mockDockerClient) ListContainers(fsouza.ListContainersOptions) ([]fsouza.APIContainers, error) {
return []fsouza.APIContainers{
{
Names: []string{"/" + containerName},
ID: containerID,
},
{
Names: []string{"/notme"},
ID: "1234abcd",
},
}, nil
}
type entry struct {
containerid string
ip net.IP
}
type mockWeaveClient struct {
sync.Mutex
published map[string]entry
}
func (m *mockWeaveClient) AddDNSEntry(hostname, containerid string, ip net.IP) error {
m.Lock()
defer m.Unlock()
m.published[hostname] = entry{containerid, ip}
return nil
}
func (m *mockWeaveClient) Expose() error {
return nil
}
const (
hostname = "foo.weave"
containerName = "bar"
containerID = "a1b2c3d4"
)
var (
ip = net.ParseIP("1.2.3.4")
)
func TestWeave(t *testing.T) {
weaveClient := &mockWeaveClient{
published: map[string]entry{},
}
dockerClient := mockDockerClient{}
interfaces := func() ([]app.Interface, error) {
return []app.Interface{
{
Name: "eth0",
Addrs: []net.Addr{
&net.IPAddr{
IP: ip,
},
},
},
{
Name: "docker0",
Addrs: []net.Addr{
&net.IPAddr{
IP: net.ParseIP("4.3.2.1"),
},
},
},
}, nil
}
publisher := app.NewWeavePublisher(
weaveClient, dockerClient, interfaces,
hostname, containerName)
defer publisher.Stop()
want := map[string]entry{
hostname: {containerID, ip},
}
test.Poll(t, 100*time.Millisecond, want, func() interface{} {
weaveClient.Lock()
defer weaveClient.Unlock()
result := map[string]entry{}
for k, v := range weaveClient.published {
result[k] = v
}
return result
})
}