OLD | NEW |
1 package state_test | 1 package state_test |
2 | 2 |
3 import ( | 3 import ( |
4 . "launchpad.net/gocheck" | 4 . "launchpad.net/gocheck" |
5 "launchpad.net/gozk/zookeeper" | 5 "launchpad.net/gozk/zookeeper" |
6 "launchpad.net/juju-core/juju/state" | 6 "launchpad.net/juju-core/juju/state" |
7 "time" | 7 "time" |
8 ) | 8 ) |
9 | 9 |
10 var serviceWatchConfigData = []map[string]interface{}{ | 10 var serviceWatchConfigData = []map[string]interface{}{ |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 283 |
284 select { | 284 select { |
285 case got, _ := <-w.Changes(): | 285 case got, _ := <-w.Changes(): |
286 c.Fatalf("got unexpected change: %#v", got) | 286 c.Fatalf("got unexpected change: %#v", got) |
287 case <-time.After(100 * time.Millisecond): | 287 case <-time.After(100 * time.Millisecond): |
288 } | 288 } |
289 | 289 |
290 c.Assert(w.Stop(), IsNil) | 290 c.Assert(w.Stop(), IsNil) |
291 } | 291 } |
292 | 292 |
| 293 var watchMachineUnitsTests = []struct { |
| 294 test func(m *state.Machine, units []*state.Unit) error |
| 295 want func(units []*state.Unit) *state.MachineUnitsChange |
| 296 }{ |
| 297 { |
| 298 func(m *state.Machine, units []*state.Unit) error { |
| 299 return units[0].AssignToMachine(m) |
| 300 }, |
| 301 func(units []*state.Unit) *state.MachineUnitsChange { |
| 302 return &state.MachineUnitsChange{Added: []*state.Unit{un
its[0], units[1]}} |
| 303 }, |
| 304 }, |
| 305 { |
| 306 func(m *state.Machine, units []*state.Unit) error { |
| 307 return units[2].AssignToMachine(m) |
| 308 }, |
| 309 func(units []*state.Unit) *state.MachineUnitsChange { |
| 310 return &state.MachineUnitsChange{Added: []*state.Unit{un
its[2]}} |
| 311 }, |
| 312 }, |
| 313 { |
| 314 func(m *state.Machine, units []*state.Unit) error { |
| 315 return units[0].UnassignFromMachine() |
| 316 }, |
| 317 func(units []*state.Unit) *state.MachineUnitsChange { |
| 318 return &state.MachineUnitsChange{Deleted: []*state.Unit{
units[0], units[1]}} |
| 319 }, |
| 320 }, |
| 321 { |
| 322 func(m *state.Machine, units []*state.Unit) error { |
| 323 return units[2].UnassignFromMachine() |
| 324 }, |
| 325 func(units []*state.Unit) *state.MachineUnitsChange { |
| 326 return &state.MachineUnitsChange{Deleted: []*state.Unit{
units[2]}} |
| 327 }, |
| 328 }, |
| 329 } |
| 330 |
| 331 func (s *StateSuite) TestWatchMachineUnits(c *C) { |
| 332 dummy := s.addDummyCharm(c) |
| 333 wordpress, err := s.st.AddService("wordpress", dummy) |
| 334 c.Assert(err, IsNil) |
| 335 |
| 336 subCh := addLoggingCharm(c, s.st) |
| 337 logging, err := s.st.AddService("logging", subCh) |
| 338 c.Assert(err, IsNil) |
| 339 |
| 340 units := make([]*state.Unit, 3) |
| 341 units[0], err = wordpress.AddUnit() |
| 342 c.Assert(err, IsNil) |
| 343 units[1], err = logging.AddUnitSubordinateTo(units[0]) |
| 344 c.Assert(err, IsNil) |
| 345 units[2], err = wordpress.AddUnit() |
| 346 c.Assert(err, IsNil) |
| 347 |
| 348 m, err := s.st.AddMachine() |
| 349 c.Assert(err, IsNil) |
| 350 |
| 351 w := m.WatchUnits() |
| 352 |
| 353 for i, test := range watchMachineUnitsTests { |
| 354 c.Logf("test %d", i) |
| 355 err := test.test(m, units) |
| 356 c.Assert(err, IsNil) |
| 357 want := test.want(units) |
| 358 select { |
| 359 case got, ok := <-w.Changes(): |
| 360 c.Assert(ok, Equals, true) |
| 361 c.Assert(unitNames(got.Added), DeepEquals, unitNames(wan
t.Added)) |
| 362 c.Assert(unitNames(got.Deleted), DeepEquals, unitNames(w
ant.Deleted)) |
| 363 case <-time.After(500 * time.Millisecond): |
| 364 c.Fatalf("didn't get change: %v", want) |
| 365 } |
| 366 } |
| 367 |
| 368 select { |
| 369 case got, _ := <-w.Changes(): |
| 370 c.Fatalf("got unexpected change: %#v", got) |
| 371 case <-time.After(100 * time.Millisecond): |
| 372 } |
| 373 |
| 374 c.Assert(w.Stop(), IsNil) |
| 375 } |
| 376 |
| 377 func unitNames(units []*state.Unit) (s []string) { |
| 378 for _, u := range units { |
| 379 s = append(s, u.Name()) |
| 380 } |
| 381 return |
| 382 } |
| 383 |
293 type any map[string]interface{} | 384 type any map[string]interface{} |
294 | 385 |
295 var environmentWatchTests = []struct { | 386 var environmentWatchTests = []struct { |
296 key string | 387 key string |
297 value interface{} | 388 value interface{} |
298 want map[string]interface{} | 389 want map[string]interface{} |
299 }{ | 390 }{ |
300 {"provider", "dummy", any{"provider": "dummy"}}, | 391 {"provider", "dummy", any{"provider": "dummy"}}, |
301 {"secret", "shhh", any{"provider": "dummy", "secret": "shhh"}}, | 392 {"secret", "shhh", any{"provider": "dummy", "secret": "shhh"}}, |
302 {"provider", "aws", any{"provider": "aws", "secret": "shhh"}}, | 393 {"provider", "aws", any{"provider": "aws", "secret": "shhh"}}, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 {[]string{"B"}, []string{"A", "B", "C"}, nil}, | 437 {[]string{"B"}, []string{"A", "B", "C"}, nil}, |
347 {[]string{"A", "D", "C"}, []string{}, []string{"A", "D", "C"}}, | 438 {[]string{"A", "D", "C"}, []string{}, []string{"A", "D", "C"}}, |
348 {[]string{}, []string{"A", "D", "C"}, nil}, | 439 {[]string{}, []string{"A", "D", "C"}, nil}, |
349 } | 440 } |
350 | 441 |
351 func (*StateSuite) TestDiff(c *C) { | 442 func (*StateSuite) TestDiff(c *C) { |
352 for _, test := range diffTests { | 443 for _, test := range diffTests { |
353 c.Assert(test.want, DeepEquals, state.Diff(test.A, test.B)) | 444 c.Assert(test.want, DeepEquals, state.Diff(test.A, test.B)) |
354 } | 445 } |
355 } | 446 } |
OLD | NEW |