forked from control-center/serviced
/
agent_test.go
130 lines (121 loc) · 3.14 KB
/
agent_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package serviced
import (
"encoding/json"
"github.com/zenoss/serviced/dao"
"net"
"testing"
)
const example_state = `
[{
"ID": "af5163d96bdc1532875f0f0601ae32a7eafadfdd287d9df6cf2b2020ddfb930d",
"Created": "2013-09-04T22:35:32.473288901-05:00",
"Path": "/serviced/serviced",
"Args": [
"proxy",
"dda3d6af-61ef-35ff-4632-086af9b78c90",
"/bin/nc -l 3306"
],
"Config": {
"Hostname": "af5163d96bdc",
"User": "",
"Memory": 0,
"MemorySwap": 0,
"CpuShares": 0,
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"PortSpecs": [
"3306"
],
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"/serviced/serviced",
"proxy",
"dda3d6af-61ef-35ff-4632-086af9b78c90",
"/bin/nc -l 3306"
],
"Dns": [
"8.8.8.8",
"8.8.4.4"
],
"Image": "base",
"Volumes": {
"/serviced": {}
},
"VolumesFrom": "",
"WorkingDir": "",
"Entrypoint": [],
"NetworkDisabled": false,
"Privileged": false
},
"State": {
"Running": true,
"Pid": 5232,
"ExitCode": 0,
"StartedAt": "2013-09-04T22:35:32.485677934-05:00",
"Ghost": false
},
"Image": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc",
"NetworkSettings": {
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"Gateway": "172.17.42.1",
"Bridge": "docker0",
"PortMapping": {
"Tcp": {
"3306": "49156"
},
"Udp": {}
}
},
"SysInitPath": "/usr/bin/docker",
"ResolvConfPath": "/var/lib/docker/containers/af5163d96bdc1532875f0f0601ae32a7eafadfdd287d9df6cf2b2020ddfb930d/resolv.conf",
"Volumes": {
"/serviced": "/home/daniel/mygo/src/github.com/zenoss/serviced/serviced"
},
"VolumesRW": {
"/serviced": true
}
}]
`
// Test parsing container state from docker.
func TestParseContainerState(t *testing.T) {
var testState []ContainerState
err := json.Unmarshal([]byte(example_state), &testState)
if err != nil {
t.Fatalf("Problem unmarshaling test state: ", err)
}
}
func TestRegisterIPResources(t *testing.T) {
var ipResult dao.HostIPs
addrs, err := net.InterfaceAddrs()
if err != nil {
t.Fatalf("Error readin addresses %v", err)
}
fn := func(ips dao.HostIPs, unused *int) error {
ipResult = ips
return nil
}
registerIPs("testId", fn)
if len(addrs) != len(ipResult.IPs) {
t.Fatalf("IP addresse length differed %v and %v", addrs, ipResult.IPs)
}
if ipResult.HostId != "testId" {
t.Fatalf("Expected host id %v, got %v", ipResult.HostId, "testId")
}
//make a lookup map (set) of result IPs
resultIps := make(map[string]bool)
for _, ip := range ipResult.IPs {
resultIps[ip.IPAddress] = true
}
//now see if IPs match IPs from std lib
for _, addr := range addrs {
_, ok := resultIps[addr.String()]
if !ok {
t.Fatalf("IP %v not find in %v", addr, ipResult)
}
}
}