github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/nomad/structs/network_test.go (about) 1 package structs 2 3 import ( 4 "net" 5 "reflect" 6 "testing" 7 ) 8 9 func TestNetworkIndex_Overcommitted(t *testing.T) { 10 idx := NewNetworkIndex() 11 12 // Consume some network 13 reserved := &NetworkResource{ 14 Device: "eth0", 15 IP: "192.168.0.100", 16 MBits: 505, 17 ReservedPorts: []int{8000, 9000}, 18 } 19 collide := idx.AddReserved(reserved) 20 if collide { 21 t.Fatalf("bad") 22 } 23 if !idx.Overcommitted() { 24 t.Fatalf("have no resources") 25 } 26 27 // Add resources 28 n := &Node{ 29 Resources: &Resources{ 30 Networks: []*NetworkResource{ 31 &NetworkResource{ 32 Device: "eth0", 33 CIDR: "192.168.0.100/32", 34 MBits: 1000, 35 }, 36 }, 37 }, 38 } 39 idx.SetNode(n) 40 if idx.Overcommitted() { 41 t.Fatalf("have resources") 42 } 43 44 // Double up our ussage 45 idx.AddReserved(reserved) 46 if !idx.Overcommitted() { 47 t.Fatalf("should be overcommitted") 48 } 49 } 50 51 func TestNetworkIndex_SetNode(t *testing.T) { 52 idx := NewNetworkIndex() 53 n := &Node{ 54 Resources: &Resources{ 55 Networks: []*NetworkResource{ 56 &NetworkResource{ 57 Device: "eth0", 58 CIDR: "192.168.0.100/32", 59 MBits: 1000, 60 }, 61 }, 62 }, 63 Reserved: &Resources{ 64 Networks: []*NetworkResource{ 65 &NetworkResource{ 66 Device: "eth0", 67 IP: "192.168.0.100", 68 ReservedPorts: []int{22}, 69 MBits: 1, 70 }, 71 }, 72 }, 73 } 74 collide := idx.SetNode(n) 75 if collide { 76 t.Fatalf("bad") 77 } 78 79 if len(idx.AvailNetworks) != 1 { 80 t.Fatalf("Bad") 81 } 82 if idx.AvailBandwidth["eth0"] != 1000 { 83 t.Fatalf("Bad") 84 } 85 if idx.UsedBandwidth["eth0"] != 1 { 86 t.Fatalf("Bad") 87 } 88 if _, ok := idx.UsedPorts["192.168.0.100"][22]; !ok { 89 t.Fatalf("Bad") 90 } 91 } 92 93 func TestNetworkIndex_AddAllocs(t *testing.T) { 94 idx := NewNetworkIndex() 95 allocs := []*Allocation{ 96 &Allocation{ 97 TaskResources: map[string]*Resources{ 98 "web": &Resources{ 99 Networks: []*NetworkResource{ 100 &NetworkResource{ 101 Device: "eth0", 102 IP: "192.168.0.100", 103 MBits: 20, 104 ReservedPorts: []int{8000, 9000}, 105 }, 106 }, 107 }, 108 }, 109 }, 110 &Allocation{ 111 TaskResources: map[string]*Resources{ 112 "api": &Resources{ 113 Networks: []*NetworkResource{ 114 &NetworkResource{ 115 Device: "eth0", 116 IP: "192.168.0.100", 117 MBits: 50, 118 ReservedPorts: []int{10000}, 119 }, 120 }, 121 }, 122 }, 123 }, 124 } 125 collide := idx.AddAllocs(allocs) 126 if collide { 127 t.Fatalf("bad") 128 } 129 130 if idx.UsedBandwidth["eth0"] != 70 { 131 t.Fatalf("Bad") 132 } 133 if _, ok := idx.UsedPorts["192.168.0.100"][8000]; !ok { 134 t.Fatalf("Bad") 135 } 136 if _, ok := idx.UsedPorts["192.168.0.100"][9000]; !ok { 137 t.Fatalf("Bad") 138 } 139 if _, ok := idx.UsedPorts["192.168.0.100"][10000]; !ok { 140 t.Fatalf("Bad") 141 } 142 } 143 144 func TestNetworkIndex_AddReserved(t *testing.T) { 145 idx := NewNetworkIndex() 146 147 reserved := &NetworkResource{ 148 Device: "eth0", 149 IP: "192.168.0.100", 150 MBits: 20, 151 ReservedPorts: []int{8000, 9000}, 152 } 153 collide := idx.AddReserved(reserved) 154 if collide { 155 t.Fatalf("bad") 156 } 157 158 if idx.UsedBandwidth["eth0"] != 20 { 159 t.Fatalf("Bad") 160 } 161 if _, ok := idx.UsedPorts["192.168.0.100"][8000]; !ok { 162 t.Fatalf("Bad") 163 } 164 if _, ok := idx.UsedPorts["192.168.0.100"][9000]; !ok { 165 t.Fatalf("Bad") 166 } 167 168 // Try to reserve the same network 169 collide = idx.AddReserved(reserved) 170 if !collide { 171 t.Fatalf("bad") 172 } 173 } 174 175 func TestNetworkIndex_yieldIP(t *testing.T) { 176 idx := NewNetworkIndex() 177 n := &Node{ 178 Resources: &Resources{ 179 Networks: []*NetworkResource{ 180 &NetworkResource{ 181 Device: "eth0", 182 CIDR: "192.168.0.100/30", 183 MBits: 1000, 184 }, 185 }, 186 }, 187 Reserved: &Resources{ 188 Networks: []*NetworkResource{ 189 &NetworkResource{ 190 Device: "eth0", 191 IP: "192.168.0.100", 192 ReservedPorts: []int{22}, 193 MBits: 1, 194 }, 195 }, 196 }, 197 } 198 idx.SetNode(n) 199 200 var out []string 201 idx.yieldIP(func(n *NetworkResource, ip net.IP) (stop bool) { 202 out = append(out, ip.String()) 203 return 204 }) 205 206 expect := []string{"192.168.0.100", "192.168.0.101", 207 "192.168.0.102", "192.168.0.103"} 208 if !reflect.DeepEqual(out, expect) { 209 t.Fatalf("bad: %v", out) 210 } 211 } 212 213 func TestNetworkIndex_AssignNetwork(t *testing.T) { 214 idx := NewNetworkIndex() 215 n := &Node{ 216 Resources: &Resources{ 217 Networks: []*NetworkResource{ 218 &NetworkResource{ 219 Device: "eth0", 220 CIDR: "192.168.0.100/30", 221 MBits: 1000, 222 }, 223 }, 224 }, 225 Reserved: &Resources{ 226 Networks: []*NetworkResource{ 227 &NetworkResource{ 228 Device: "eth0", 229 IP: "192.168.0.100", 230 ReservedPorts: []int{22}, 231 MBits: 1, 232 }, 233 }, 234 }, 235 } 236 idx.SetNode(n) 237 238 allocs := []*Allocation{ 239 &Allocation{ 240 TaskResources: map[string]*Resources{ 241 "web": &Resources{ 242 Networks: []*NetworkResource{ 243 &NetworkResource{ 244 Device: "eth0", 245 IP: "192.168.0.100", 246 MBits: 20, 247 ReservedPorts: []int{8000, 9000}, 248 }, 249 }, 250 }, 251 }, 252 }, 253 &Allocation{ 254 TaskResources: map[string]*Resources{ 255 "api": &Resources{ 256 Networks: []*NetworkResource{ 257 &NetworkResource{ 258 Device: "eth0", 259 IP: "192.168.0.100", 260 MBits: 50, 261 ReservedPorts: []int{10000}, 262 }, 263 }, 264 }, 265 }, 266 }, 267 } 268 idx.AddAllocs(allocs) 269 270 // Ask for a reserved port 271 ask := &NetworkResource{ 272 ReservedPorts: []int{8000}, 273 } 274 offer, err := idx.AssignNetwork(ask) 275 if err != nil { 276 t.Fatalf("err: %v", err) 277 } 278 if offer == nil { 279 t.Fatalf("bad") 280 } 281 if offer.IP != "192.168.0.101" { 282 t.Fatalf("bad: %#v", offer) 283 } 284 if len(offer.ReservedPorts) != 1 || offer.ReservedPorts[0] != 8000 { 285 t.Fatalf("bad: %#v", offer) 286 } 287 288 // Ask for dynamic ports 289 ask = &NetworkResource{ 290 DynamicPorts: []string{"http", "https", "admin"}, 291 } 292 offer, err = idx.AssignNetwork(ask) 293 if err != nil { 294 t.Fatalf("err: %v", err) 295 } 296 if offer == nil { 297 t.Fatalf("bad") 298 } 299 if offer.IP != "192.168.0.100" { 300 t.Fatalf("bad: %#v", offer) 301 } 302 if len(offer.ReservedPorts) != 3 { 303 t.Fatalf("bad: %#v", offer) 304 } 305 306 // Ask for reserved + dynamic ports 307 ask = &NetworkResource{ 308 ReservedPorts: []int{12345}, 309 DynamicPorts: []string{"http", "https", "admin"}, 310 } 311 offer, err = idx.AssignNetwork(ask) 312 if err != nil { 313 t.Fatalf("err: %v", err) 314 } 315 if offer == nil { 316 t.Fatalf("bad") 317 } 318 if offer.IP != "192.168.0.100" { 319 t.Fatalf("bad: %#v", offer) 320 } 321 if len(offer.ReservedPorts) != 4 || offer.ReservedPorts[0] != 12345 { 322 t.Fatalf("bad: %#v", offer) 323 } 324 325 // Ask for too much bandwidth 326 ask = &NetworkResource{ 327 MBits: 1000, 328 } 329 offer, err = idx.AssignNetwork(ask) 330 if err.Error() != "bandwidth exceeded" { 331 t.Fatalf("err: %v", err) 332 } 333 if offer != nil { 334 t.Fatalf("bad") 335 } 336 } 337 338 func TestIntContains(t *testing.T) { 339 l := []int{1, 2, 10, 20} 340 if IntContains(l, 50) { 341 t.Fatalf("bad") 342 } 343 if !IntContains(l, 20) { 344 t.Fatalf("bad") 345 } 346 if !IntContains(l, 1) { 347 t.Fatalf("bad") 348 } 349 }