| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 package state_test | 1 package state_test |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "fmt" | 4 "fmt" |
| 5 . "launchpad.net/gocheck" | 5 . "launchpad.net/gocheck" |
| 6 "launchpad.net/juju-core/charm" | 6 "launchpad.net/juju-core/charm" |
| 7 "launchpad.net/juju-core/state" | 7 "launchpad.net/juju-core/state" |
| 8 "sort" | 8 "sort" |
| 9 "time" | 9 "time" |
| 10 ) | 10 ) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 } | 26 } |
| 27 | 27 |
| 28 func (s *ServiceSuite) TestServiceCharm(c *C) { | 28 func (s *ServiceSuite) TestServiceCharm(c *C) { |
| 29 ch, force, err := s.service.Charm() | 29 ch, force, err := s.service.Charm() |
| 30 c.Assert(err, IsNil) | 30 c.Assert(err, IsNil) |
| 31 c.Assert(ch.URL(), DeepEquals, s.charm.URL()) | 31 c.Assert(ch.URL(), DeepEquals, s.charm.URL()) |
| 32 c.Assert(force, Equals, false) | 32 c.Assert(force, Equals, false) |
| 33 | 33 |
| 34 // TODO: SetCharm must validate the change (version, relations, etc) | 34 // TODO: SetCharm must validate the change (version, relations, etc) |
| 35 wp := s.AddTestingCharm(c, "wordpress") | 35 wp := s.AddTestingCharm(c, "wordpress") |
| 36 err = s.service.SetCharm(wp, true) | |
| 37 ch, force, err1 := s.service.Charm() | |
| 38 c.Assert(err1, IsNil) | |
| 39 c.Assert(ch.URL(), DeepEquals, wp.URL()) | |
| 40 c.Assert(force, Equals, true) | |
| 36 | 41 |
| 37 testWhenDying(c, s.service, notAliveErr, notAliveErr, func() error { | 42 testWhenDying(c, s.service, notAliveErr, notAliveErr, func() error { |
| 38 » » err := s.service.SetCharm(wp, true) | 43 » » return s.service.SetCharm(wp, true) |
| 39 » » if err == nil { | |
| 40 » » » ch, force, err1 := s.service.Charm() | |
| 41 » » » c.Assert(err1, IsNil) | |
| 42 » » » c.Assert(ch.URL(), DeepEquals, wp.URL()) | |
| 43 » » » c.Assert(force, Equals, true) | |
| 44 » » } | |
| 45 » » return err | |
| 46 }) | 44 }) |
| 47 } | 45 } |
| 48 | 46 |
| 49 func (s *ServiceSuite) TestServiceRefesh(c *C) { | 47 func (s *ServiceSuite) TestServiceRefesh(c *C) { |
| 50 s1, err := s.State.Service(s.service.Name()) | 48 s1, err := s.State.Service(s.service.Name()) |
| 51 c.Assert(err, IsNil) | 49 c.Assert(err, IsNil) |
| 52 | 50 |
| 53 err = s.service.SetCharm(s.charm, true) | 51 err = s.service.SetCharm(s.charm, true) |
| 54 c.Assert(err, IsNil) | 52 c.Assert(err, IsNil) |
| 55 | 53 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 // not alive. | 115 // not alive. |
| 118 testWhenDying(c, principalUnit, errPat, errPat, func() error { | 116 testWhenDying(c, principalUnit, errPat, errPat, func() error { |
| 119 _, err := loggingService.AddUnitSubordinateTo(principalUnit) | 117 _, err := loggingService.AddUnitSubordinateTo(principalUnit) |
| 120 return err | 118 return err |
| 121 }) | 119 }) |
| 122 | 120 |
| 123 // Test that AddUnitSubordinateTo fails when the service is | 121 // Test that AddUnitSubordinateTo fails when the service is |
| 124 // not alive. | 122 // not alive. |
| 125 principalUnit, err = principalService.AddUnit() | 123 principalUnit, err = principalService.AddUnit() |
| 126 c.Assert(err, IsNil) | 124 c.Assert(err, IsNil) |
| 125 removeAllUnits(c, loggingService) | |
| 127 testWhenDying(c, loggingService, errPat, errPat, func() error { | 126 testWhenDying(c, loggingService, errPat, errPat, func() error { |
| 128 _, err := loggingService.AddUnitSubordinateTo(principalUnit) | 127 _, err := loggingService.AddUnitSubordinateTo(principalUnit) |
| 129 return err | 128 return err |
| 130 }) | 129 }) |
| 131 } | 130 } |
| 132 | 131 |
| 133 func (s *ServiceSuite) TestAddUnit(c *C) { | 132 func (s *ServiceSuite) TestAddUnit(c *C) { |
| 133 c.Assert(s.service.UnitCount(), Equals, 0) | |
| 134 // Check that principal units can be added on their own. | 134 // Check that principal units can be added on their own. |
| 135 unitZero, err := s.service.AddUnit() | 135 unitZero, err := s.service.AddUnit() |
| 136 c.Assert(err, IsNil) | 136 c.Assert(err, IsNil) |
| 137 c.Assert(s.service.UnitCount(), Equals, 1) | |
| 137 c.Assert(unitZero.Name(), Equals, "mysql/0") | 138 c.Assert(unitZero.Name(), Equals, "mysql/0") |
| 138 principal := unitZero.IsPrincipal() | 139 principal := unitZero.IsPrincipal() |
| 139 c.Assert(principal, Equals, true) | 140 c.Assert(principal, Equals, true) |
| 140 unitOne, err := s.service.AddUnit() | 141 unitOne, err := s.service.AddUnit() |
| 141 c.Assert(err, IsNil) | 142 c.Assert(err, IsNil) |
| 142 c.Assert(unitOne.Name(), Equals, "mysql/1") | 143 c.Assert(unitOne.Name(), Equals, "mysql/1") |
| 143 principal = unitOne.IsPrincipal() | 144 principal = unitOne.IsPrincipal() |
| 144 c.Assert(principal, Equals, true) | 145 c.Assert(principal, Equals, true) |
| 145 | 146 |
| 146 // Check that principal units cannot be added to principal units. | 147 // Check that principal units cannot be added to principal units. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 171 c.Assert(id, Equals, m.Id()) | 172 c.Assert(id, Equals, m.Id()) |
| 172 | 173 |
| 173 // Check that subordinate units must be added to other units. | 174 // Check that subordinate units must be added to other units. |
| 174 _, err = logging.AddUnit() | 175 _, err = logging.AddUnit() |
| 175 c.Assert(err, ErrorMatches, `cannot add unit to service "logging": unit is a subordinate`) | 176 c.Assert(err, ErrorMatches, `cannot add unit to service "logging": unit is a subordinate`) |
| 176 | 177 |
| 177 // Check that subordinate units cannnot be added to subordinate units. | 178 // Check that subordinate units cannnot be added to subordinate units. |
| 178 _, err = logging.AddUnitSubordinateTo(subZero) | 179 _, err = logging.AddUnitSubordinateTo(subZero) |
| 179 c.Assert(err, ErrorMatches, `cannot add unit to service "logging" as a s ubordinate of "logging/0": unit is not a principal`) | 180 c.Assert(err, ErrorMatches, `cannot add unit to service "logging" as a s ubordinate of "logging/0": unit is not a principal`) |
| 180 | 181 |
| 182 removeAllUnits(c, s.service) | |
| 181 const errPat = ".*: service is not alive" | 183 const errPat = ".*: service is not alive" |
| 182 testWhenDying(c, s.service, errPat, errPat, func() error { | 184 testWhenDying(c, s.service, errPat, errPat, func() error { |
| 183 _, err = s.service.AddUnit() | 185 _, err = s.service.AddUnit() |
| 184 return err | 186 return err |
| 185 }) | 187 }) |
| 186 } | 188 } |
| 187 | 189 |
| 188 func (s *ServiceSuite) TestReadUnit(c *C) { | 190 func (s *ServiceSuite) TestReadUnit(c *C) { |
| 189 _, err := s.service.AddUnit() | 191 _, err := s.service.AddUnit() |
| 190 c.Assert(err, IsNil) | 192 c.Assert(err, IsNil) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 // BUG(aram): use error strings from state. | 229 // BUG(aram): use error strings from state. |
| 228 unit, err = s.service.Unit("wordpress/0") | 230 unit, err = s.service.Unit("wordpress/0") |
| 229 c.Assert(err, ErrorMatches, `cannot get unit "wordpress/0" from service "mysql": .*`) | 231 c.Assert(err, ErrorMatches, `cannot get unit "wordpress/0" from service "mysql": .*`) |
| 230 | 232 |
| 231 units, err := s.service.AllUnits() | 233 units, err := s.service.AllUnits() |
| 232 c.Assert(err, IsNil) | 234 c.Assert(err, IsNil) |
| 233 c.Assert(sortedUnitNames(units), DeepEquals, []string{"mysql/0", "mysql/ 1"}) | 235 c.Assert(sortedUnitNames(units), DeepEquals, []string{"mysql/0", "mysql/ 1"}) |
| 234 } | 236 } |
| 235 | 237 |
| 236 func (s *ServiceSuite) TestReadUnitWhenDying(c *C) { | 238 func (s *ServiceSuite) TestReadUnitWhenDying(c *C) { |
| 237 » // Test that we can read units from the service whatever | 239 » // Test that we can still read units when the service is Dying... |
| 238 » // their life state. | 240 » unit, err := s.service.AddUnit() |
| 239 » _, err := s.service.AddUnit() | |
| 240 c.Assert(err, IsNil) | 241 c.Assert(err, IsNil) |
| 241 » checkAllUnits := func() error { | 242 » err = s.service.EnsureDying() |
| 242 » » // Check that retrieving all units works. | 243 » c.Assert(err, IsNil) |
| 244 » _, err = s.service.AllUnits() | |
| 245 » c.Assert(err, IsNil) | |
| 246 » _, err = s.service.Unit("mysql/0") | |
| 247 » c.Assert(err, IsNil) | |
| 248 | |
| 249 » // ...and when those units are Dying or Dead... | |
|
niemeyer
2012/09/21 16:57:37
Doesn't that cover also what's done above?
fwereade
2012/09/21 17:43:48
No :).
| |
| 250 » testWhenDying(c, unit, noErr, noErr, func() error { | |
| 243 _, err := s.service.AllUnits() | 251 _, err := s.service.AllUnits() |
| 244 return err | 252 return err |
| 245 » } | 253 » }, func() error { |
| 246 » checkUnit := func() error { | |
| 247 _, err := s.service.Unit("mysql/0") | 254 _, err := s.service.Unit("mysql/0") |
| 248 return err | 255 return err |
| 249 » } | 256 » }) |
| 250 | 257 |
| 251 » testWhenDying(c, s.service, noErr, noErr, checkAllUnits, checkUnit) | 258 » // ...and that we can even read the empty list of units once the |
| 252 | 259 » // service itself is Dead. |
| 253 » unit0, err := s.service.Unit("mysql/0") | 260 » removeAllUnits(c, s.service) |
| 261 » err = s.service.EnsureDead() | |
| 262 » _, err = s.service.AllUnits() | |
| 254 c.Assert(err, IsNil) | 263 c.Assert(err, IsNil) |
| 255 testWhenDying(c, unit0, noErr, noErr, checkAllUnits, checkUnit) | |
| 256 } | 264 } |
| 257 | 265 |
| 258 func (s *ServiceSuite) TestRemoveUnit(c *C) { | 266 func (s *ServiceSuite) TestRemoveUnit(c *C) { |
| 259 _, err := s.service.AddUnit() | 267 _, err := s.service.AddUnit() |
| 260 c.Assert(err, IsNil) | 268 c.Assert(err, IsNil) |
| 261 _, err = s.service.AddUnit() | 269 _, err = s.service.AddUnit() |
| 262 c.Assert(err, IsNil) | 270 c.Assert(err, IsNil) |
| 271 c.Assert(s.service.UnitCount(), Equals, 2) | |
| 263 | 272 |
| 264 // Check that removing a unit works. | 273 // Check that removing a unit works. |
| 265 unit, err := s.service.Unit("mysql/0") | 274 unit, err := s.service.Unit("mysql/0") |
| 266 c.Assert(err, IsNil) | 275 c.Assert(err, IsNil) |
| 267 err = s.service.RemoveUnit(unit) | 276 err = s.service.RemoveUnit(unit) |
| 268 c.Assert(err, ErrorMatches, `cannot remove unit "mysql/0": unit is not d ead`) | 277 c.Assert(err, ErrorMatches, `cannot remove unit "mysql/0": unit is not d ead`) |
| 269 » err = unit.Die() | 278 » err = unit.EnsureDead() |
| 270 c.Assert(err, IsNil) | 279 c.Assert(err, IsNil) |
| 271 err = s.service.RemoveUnit(unit) | 280 err = s.service.RemoveUnit(unit) |
| 272 c.Assert(err, IsNil) | 281 c.Assert(err, IsNil) |
| 273 | 282 |
| 274 units, err := s.service.AllUnits() | 283 units, err := s.service.AllUnits() |
| 275 c.Assert(err, IsNil) | 284 c.Assert(err, IsNil) |
| 276 c.Assert(units, HasLen, 1) | 285 c.Assert(units, HasLen, 1) |
| 277 c.Assert(units[0].Name(), Equals, "mysql/1") | 286 c.Assert(units[0].Name(), Equals, "mysql/1") |
| 287 c.Assert(s.service.UnitCount(), Equals, 1) | |
| 278 | 288 |
| 279 err = s.service.RemoveUnit(unit) | 289 err = s.service.RemoveUnit(unit) |
| 280 c.Assert(err, IsNil) | 290 c.Assert(err, IsNil) |
| 291 c.Assert(s.service.UnitCount(), Equals, 1) | |
| 281 } | 292 } |
| 282 | 293 |
| 294 func (s *ServiceSuite) TestLifeWithUnits(c *C) { | |
| 295 unit, err := s.service.AddUnit() | |
| 296 c.Assert(err, IsNil) | |
| 297 err = s.service.EnsureDying() | |
| 298 c.Assert(err, IsNil) | |
| 299 err = s.service.EnsureDead() | |
| 300 c.Assert(err, ErrorMatches, `cannot set life to Dead for service "mysql" : service still has units`) | |
| 301 err = unit.EnsureDead() | |
| 302 c.Assert(err, IsNil) | |
|
niemeyer
2012/09/21 16:57:37
One more time here?
err = s.service.EnsureDead()
fwereade
2012/09/21 17:43:48
Done.
| |
| 303 err = s.service.RemoveUnit(unit) | |
| 304 c.Assert(err, IsNil) | |
| 305 err = s.service.EnsureDead() | |
| 306 c.Assert(err, IsNil) | |
| 307 } | |
| 308 | |
| 283 func (s *ServiceSuite) TestReadUnitWithChangingState(c *C) { | 309 func (s *ServiceSuite) TestReadUnitWithChangingState(c *C) { |
| 284 // Check that reading a unit after removing the service | 310 // Check that reading a unit after removing the service |
| 285 // fails nicely. | 311 // fails nicely. |
| 286 err := s.State.RemoveService(s.service) | 312 err := s.State.RemoveService(s.service) |
| 287 c.Assert(err, ErrorMatches, `cannot remove service "mysql": service is n ot dead`) | 313 c.Assert(err, ErrorMatches, `cannot remove service "mysql": service is n ot dead`) |
| 288 » err = s.service.Die() | 314 » err = s.service.EnsureDead() |
| 289 c.Assert(err, IsNil) | 315 c.Assert(err, IsNil) |
| 290 err = s.State.RemoveService(s.service) | 316 err = s.State.RemoveService(s.service) |
| 291 c.Assert(err, IsNil) | 317 c.Assert(err, IsNil) |
| 292 _, err = s.State.Unit("mysql/0") | 318 _, err = s.State.Unit("mysql/0") |
| 293 c.Assert(err, ErrorMatches, `cannot get unit "mysql/0": not found`) | 319 c.Assert(err, ErrorMatches, `cannot get unit "mysql/0": not found`) |
| 294 } | 320 } |
| 295 | 321 |
| 296 func (s *ServiceSuite) TestServiceConfig(c *C) { | 322 func (s *ServiceSuite) TestServiceConfig(c *C) { |
| 297 env, err := s.service.Config() | 323 env, err := s.service.Config() |
| 298 c.Assert(err, IsNil) | 324 c.Assert(err, IsNil) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 c.Assert(err, IsNil) | 367 c.Assert(err, IsNil) |
| 342 _, err = service.AddUnit() | 368 _, err = service.AddUnit() |
| 343 c.Assert(err, IsNil) | 369 c.Assert(err, IsNil) |
| 344 }, | 370 }, |
| 345 added: []string{"mysql/2", "mysql/3"}, | 371 added: []string{"mysql/2", "mysql/3"}, |
| 346 }, | 372 }, |
| 347 { | 373 { |
| 348 test: func(c *C, s *state.State, service *state.Service) { | 374 test: func(c *C, s *state.State, service *state.Service) { |
| 349 unit3, err := service.Unit("mysql/3") | 375 unit3, err := service.Unit("mysql/3") |
| 350 c.Assert(err, IsNil) | 376 c.Assert(err, IsNil) |
| 351 » » » err = unit3.Die() | 377 » » » err = unit3.EnsureDead() |
| 352 c.Assert(err, IsNil) | 378 c.Assert(err, IsNil) |
| 353 err = service.RemoveUnit(unit3) | 379 err = service.RemoveUnit(unit3) |
| 354 c.Assert(err, IsNil) | 380 c.Assert(err, IsNil) |
| 355 }, | 381 }, |
| 356 removed: []string{"mysql/3"}, | 382 removed: []string{"mysql/3"}, |
| 357 }, | 383 }, |
| 358 { | 384 { |
| 359 test: func(c *C, s *state.State, service *state.Service) { | 385 test: func(c *C, s *state.State, service *state.Service) { |
| 360 unit0, err := service.Unit("mysql/0") | 386 unit0, err := service.Unit("mysql/0") |
| 361 c.Assert(err, IsNil) | 387 c.Assert(err, IsNil) |
| 362 » » » err = unit0.Die() | 388 » » » err = unit0.EnsureDead() |
| 363 c.Assert(err, IsNil) | 389 c.Assert(err, IsNil) |
| 364 err = service.RemoveUnit(unit0) | 390 err = service.RemoveUnit(unit0) |
| 365 c.Assert(err, IsNil) | 391 c.Assert(err, IsNil) |
| 366 unit2, err := service.Unit("mysql/2") | 392 unit2, err := service.Unit("mysql/2") |
| 367 c.Assert(err, IsNil) | 393 c.Assert(err, IsNil) |
| 368 » » » err = unit2.Die() | 394 » » » err = unit2.EnsureDead() |
| 369 c.Assert(err, IsNil) | 395 c.Assert(err, IsNil) |
| 370 err = service.RemoveUnit(unit2) | 396 err = service.RemoveUnit(unit2) |
| 371 c.Assert(err, IsNil) | 397 c.Assert(err, IsNil) |
| 372 }, | 398 }, |
| 373 removed: []string{"mysql/0", "mysql/2"}, | 399 removed: []string{"mysql/0", "mysql/2"}, |
| 374 }, | 400 }, |
| 375 { | 401 { |
| 376 test: func(c *C, s *state.State, service *state.Service) { | 402 test: func(c *C, s *state.State, service *state.Service) { |
| 377 _, err := service.AddUnit() | 403 _, err := service.AddUnit() |
| 378 c.Assert(err, IsNil) | 404 c.Assert(err, IsNil) |
| 379 unit1, err := service.Unit("mysql/1") | 405 unit1, err := service.Unit("mysql/1") |
| 380 c.Assert(err, IsNil) | 406 c.Assert(err, IsNil) |
| 381 » » » err = unit1.Die() | 407 » » » err = unit1.EnsureDead() |
| 382 c.Assert(err, IsNil) | 408 c.Assert(err, IsNil) |
| 383 err = service.RemoveUnit(unit1) | 409 err = service.RemoveUnit(unit1) |
| 384 c.Assert(err, IsNil) | 410 c.Assert(err, IsNil) |
| 385 }, | 411 }, |
| 386 added: []string{"mysql/4"}, | 412 added: []string{"mysql/4"}, |
| 387 removed: []string{"mysql/1"}, | 413 removed: []string{"mysql/1"}, |
| 388 }, | 414 }, |
| 389 { | 415 { |
| 390 test: func(c *C, s *state.State, service *state.Service) { | 416 test: func(c *C, s *state.State, service *state.Service) { |
| 391 units := [20]*state.Unit{} | 417 units := [20]*state.Unit{} |
| 392 var err error | 418 var err error |
| 393 for i := 0; i < len(units); i++ { | 419 for i := 0; i < len(units); i++ { |
| 394 units[i], err = service.AddUnit() | 420 units[i], err = service.AddUnit() |
| 395 c.Assert(err, IsNil) | 421 c.Assert(err, IsNil) |
| 396 } | 422 } |
| 397 for i := 10; i < len(units); i++ { | 423 for i := 10; i < len(units); i++ { |
| 398 » » » » err = units[i].Die() | 424 » » » » err = units[i].EnsureDead() |
| 399 c.Assert(err, IsNil) | 425 c.Assert(err, IsNil) |
| 400 err = service.RemoveUnit(units[i]) | 426 err = service.RemoveUnit(units[i]) |
| 401 c.Assert(err, IsNil) | 427 c.Assert(err, IsNil) |
| 402 } | 428 } |
| 403 }, | 429 }, |
| 404 added: []string{"mysql/10", "mysql/11", "mysql/12", "mysql/13", "mysql/14", "mysql/5", "mysql/6", "mysql/7", "mysql/8", "mysql/9"}, | 430 added: []string{"mysql/10", "mysql/11", "mysql/12", "mysql/13", "mysql/14", "mysql/5", "mysql/6", "mysql/7", "mysql/8", "mysql/9"}, |
| 405 }, | 431 }, |
| 406 { | 432 { |
| 407 test: func(c *C, s *state.State, service *state.Service) { | 433 test: func(c *C, s *state.State, service *state.Service) { |
| 408 _, err := service.AddUnit() | 434 _, err := service.AddUnit() |
| 409 c.Assert(err, IsNil) | 435 c.Assert(err, IsNil) |
| 410 unit9, err := service.Unit("mysql/9") | 436 unit9, err := service.Unit("mysql/9") |
| 411 c.Assert(err, IsNil) | 437 c.Assert(err, IsNil) |
| 412 » » » err = unit9.Die() | 438 » » » err = unit9.EnsureDead() |
| 413 c.Assert(err, IsNil) | 439 c.Assert(err, IsNil) |
| 414 err = service.RemoveUnit(unit9) | 440 err = service.RemoveUnit(unit9) |
| 415 c.Assert(err, IsNil) | 441 c.Assert(err, IsNil) |
| 416 }, | 442 }, |
| 417 added: []string{"mysql/25"}, | 443 added: []string{"mysql/25"}, |
| 418 removed: []string{"mysql/9"}, | 444 removed: []string{"mysql/9"}, |
| 419 }, | 445 }, |
| 420 { | 446 { |
| 421 test: func(c *C, s *state.State, service *state.Service) { | 447 test: func(c *C, s *state.State, service *state.Service) { |
| 422 _, err := service.AddUnit() | 448 _, err := service.AddUnit() |
| 423 c.Assert(err, IsNil) | 449 c.Assert(err, IsNil) |
| 424 _, err = service.AddUnit() | 450 _, err = service.AddUnit() |
| 425 c.Assert(err, IsNil) | 451 c.Assert(err, IsNil) |
| 426 ch, _, err := service.Charm() | 452 ch, _, err := service.Charm() |
| 427 c.Assert(err, IsNil) | 453 c.Assert(err, IsNil) |
| 428 svc, err := s.AddService("bacon", ch) | 454 svc, err := s.AddService("bacon", ch) |
| 429 c.Assert(err, IsNil) | 455 c.Assert(err, IsNil) |
| 430 _, err = svc.AddUnit() | 456 _, err = svc.AddUnit() |
| 431 c.Assert(err, IsNil) | 457 c.Assert(err, IsNil) |
| 432 _, err = svc.AddUnit() | 458 _, err = svc.AddUnit() |
| 433 c.Assert(err, IsNil) | 459 c.Assert(err, IsNil) |
| 434 unit14, err := service.Unit("mysql/14") | 460 unit14, err := service.Unit("mysql/14") |
| 435 c.Assert(err, IsNil) | 461 c.Assert(err, IsNil) |
| 436 » » » err = unit14.Die() | 462 » » » err = unit14.EnsureDead() |
| 437 c.Assert(err, IsNil) | 463 c.Assert(err, IsNil) |
| 438 err = service.RemoveUnit(unit14) | 464 err = service.RemoveUnit(unit14) |
| 439 c.Assert(err, IsNil) | 465 c.Assert(err, IsNil) |
| 440 }, | 466 }, |
| 441 added: []string{"mysql/26", "mysql/27"}, | 467 added: []string{"mysql/26", "mysql/27"}, |
| 442 removed: []string{"mysql/14"}, | 468 removed: []string{"mysql/14"}, |
| 443 }, | 469 }, |
| 444 } | 470 } |
| 445 | 471 |
| 446 func (s *ServiceSuite) TestWatchUnits(c *C) { | 472 func (s *ServiceSuite) TestWatchUnits(c *C) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 572 // Add another relation; check change. | 598 // Add another relation; check change. |
| 573 wp2ep := state.RelationEndpoint{"wp2", "ifce", "baz", state.RoleRequirer , charm.ScopeGlobal} | 599 wp2ep := state.RelationEndpoint{"wp2", "ifce", "baz", state.RoleRequirer , charm.ScopeGlobal} |
| 574 _, err = s.State.AddRelation(mysqlep, wp2ep) | 600 _, err = s.State.AddRelation(mysqlep, wp2ep) |
| 575 c.Assert(err, IsNil) | 601 c.Assert(err, IsNil) |
| 576 s.State.StartSync() | 602 s.State.StartSync() |
| 577 assertChange([]int{1}, nil) | 603 assertChange([]int{1}, nil) |
| 578 assertNoChange() | 604 assertNoChange() |
| 579 | 605 |
| 580 s.State.StartSync() | 606 s.State.StartSync() |
| 581 // Remove a relation; check change. | 607 // Remove a relation; check change. |
| 582 » err = rel.Die() | 608 » err = rel.EnsureDead() |
| 583 c.Assert(err, IsNil) | 609 c.Assert(err, IsNil) |
| 584 err = s.State.RemoveRelation(rel) | 610 err = s.State.RemoveRelation(rel) |
| 585 c.Assert(err, IsNil) | 611 c.Assert(err, IsNil) |
| 586 s.State.StartSync() | 612 s.State.StartSync() |
| 587 assertChange(nil, []int{0}) | 613 assertChange(nil, []int{0}) |
| 588 | 614 |
| 589 // Stop watcher; check change chan is closed. | 615 // Stop watcher; check change chan is closed. |
| 590 err = relationsWatcher.Stop() | 616 err = relationsWatcher.Stop() |
| 591 c.Assert(err, IsNil) | 617 c.Assert(err, IsNil) |
| 592 assertClosed := func() { | 618 assertClosed := func() { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 633 var err error | 659 var err error |
| 634 mysqlep := state.RelationEndpoint{"mysql", "ifce", "foo", state.RoleProv ider, charm.ScopeGlobal} | 660 mysqlep := state.RelationEndpoint{"mysql", "ifce", "foo", state.RoleProv ider, charm.ScopeGlobal} |
| 635 | 661 |
| 636 for i := 0; i < 5; i++ { | 662 for i := 0; i < 5; i++ { |
| 637 _, err := s.State.AddService("wp"+fmt.Sprint(i), s.charm) | 663 _, err := s.State.AddService("wp"+fmt.Sprint(i), s.charm) |
| 638 c.Assert(err, IsNil) | 664 c.Assert(err, IsNil) |
| 639 endpoints[i] = state.RelationEndpoint{"wp" + fmt.Sprint(i), "ifc e", "spam" + fmt.Sprint(i), state.RoleRequirer, charm.ScopeGlobal} | 665 endpoints[i] = state.RelationEndpoint{"wp" + fmt.Sprint(i), "ifc e", "spam" + fmt.Sprint(i), state.RoleRequirer, charm.ScopeGlobal} |
| 640 relations[i], err = s.State.AddRelation(mysqlep, endpoints[i]) | 666 relations[i], err = s.State.AddRelation(mysqlep, endpoints[i]) |
| 641 c.Assert(err, IsNil) | 667 c.Assert(err, IsNil) |
| 642 } | 668 } |
| 643 » err = relations[4].Die() | 669 » err = relations[4].EnsureDead() |
| 644 c.Assert(err, IsNil) | 670 c.Assert(err, IsNil) |
| 645 err = s.State.RemoveRelation(relations[4]) | 671 err = s.State.RemoveRelation(relations[4]) |
| 646 c.Assert(err, IsNil) | 672 c.Assert(err, IsNil) |
| 647 relations[4] = nil | 673 relations[4] = nil |
| 648 want = &state.RelationsChange{Added: relations[:4]} | 674 want = &state.RelationsChange{Added: relations[:4]} |
| 649 s.State.StartSync() | 675 s.State.StartSync() |
| 650 got := &state.RelationsChange{} | 676 got := &state.RelationsChange{} |
| 651 for { | 677 for { |
| 652 select { | 678 select { |
| 653 case new, ok := <-relationsWatcher.Changes(): | 679 case new, ok := <-relationsWatcher.Changes(): |
| 654 c.Assert(ok, Equals, true) | 680 c.Assert(ok, Equals, true) |
| 655 addRelationChanges(got, new) | 681 addRelationChanges(got, new) |
| 656 if moreRelationsRequired(got, want) { | 682 if moreRelationsRequired(got, want) { |
| 657 continue | 683 continue |
| 658 } | 684 } |
| 659 c.Assert(got, DeepEquals, want) | 685 c.Assert(got, DeepEquals, want) |
| 660 case <-time.After(500 * time.Millisecond): | 686 case <-time.After(500 * time.Millisecond): |
| 661 c.Fatalf("didn't get change: %#v", want) | 687 c.Fatalf("didn't get change: %#v", want) |
| 662 } | 688 } |
| 663 break | 689 break |
| 664 } | 690 } |
| 665 | 691 |
| 666 for i := 0; i < 4; i++ { | 692 for i := 0; i < 4; i++ { |
| 667 » » err = relations[i].Die() | 693 » » err = relations[i].EnsureDead() |
| 668 c.Assert(err, IsNil) | 694 c.Assert(err, IsNil) |
| 669 } | 695 } |
| 670 want.Removed = relations[:4] | 696 want.Removed = relations[:4] |
| 671 for i := 0; i < 4; i++ { | 697 for i := 0; i < 4; i++ { |
| 672 err = s.State.RemoveRelation(relations[i]) | 698 err = s.State.RemoveRelation(relations[i]) |
| 673 c.Assert(err, IsNil) | 699 c.Assert(err, IsNil) |
| 674 } | 700 } |
| 675 _, err = s.State.AddService("wp", s.charm) | 701 _, err = s.State.AddService("wp", s.charm) |
| 676 ep := state.RelationEndpoint{"wp", "ifce", "spam", state.RoleRequirer, c harm.ScopeGlobal} | 702 ep := state.RelationEndpoint{"wp", "ifce", "spam", state.RoleRequirer, c harm.ScopeGlobal} |
| 677 rel, err := s.State.AddRelation(mysqlep, ep) | 703 rel, err := s.State.AddRelation(mysqlep, ep) |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 704 | 730 |
| 705 func moreRelationsRequired(got, want *state.RelationsChange) bool { | 731 func moreRelationsRequired(got, want *state.RelationsChange) bool { |
| 706 return len(got.Added)+len(got.Removed) < len(want.Added)+len(want.Remove d) | 732 return len(got.Added)+len(got.Removed) < len(want.Added)+len(want.Remove d) |
| 707 } | 733 } |
| 708 | 734 |
| 709 func addRelationChanges(changes *state.RelationsChange, more *state.RelationsCha nge) { | 735 func addRelationChanges(changes *state.RelationsChange, more *state.RelationsCha nge) { |
| 710 changes.Added = append(changes.Added, more.Added...) | 736 changes.Added = append(changes.Added, more.Added...) |
| 711 changes.Removed = append(changes.Removed, more.Removed...) | 737 changes.Removed = append(changes.Removed, more.Removed...) |
| 712 } | 738 } |
| 713 | 739 |
| 740 func removeAllUnits(c *C, s *state.Service) { | |
| 741 us, err := s.AllUnits() | |
| 742 c.Assert(err, IsNil) | |
| 743 for _, u := range us { | |
| 744 err = u.EnsureDead() | |
| 745 c.Assert(err, IsNil) | |
| 746 err = s.RemoveUnit(u) | |
| 747 c.Assert(err, IsNil) | |
| 748 } | |
| 749 } | |
| 750 | |
| 714 var watchServiceTests = []struct { | 751 var watchServiceTests = []struct { |
| 715 test func(m *state.Service) error | 752 test func(m *state.Service) error |
| 716 Exposed bool | 753 Exposed bool |
| 717 Life state.Life | 754 Life state.Life |
| 718 }{ | 755 }{ |
| 719 { | 756 { |
| 720 test: func(s *state.Service) error { | 757 test: func(s *state.Service) error { |
| 721 return s.SetExposed() | 758 return s.SetExposed() |
| 722 }, | 759 }, |
| 723 Exposed: true, | 760 Exposed: true, |
| 724 }, | 761 }, |
| 725 { | 762 { |
| 726 test: func(s *state.Service) error { | 763 test: func(s *state.Service) error { |
| 727 return s.ClearExposed() | 764 return s.ClearExposed() |
| 728 }, | 765 }, |
| 729 Exposed: false, | 766 Exposed: false, |
| 730 }, | 767 }, |
| 731 { | 768 { |
| 732 test: func(s *state.Service) error { | 769 test: func(s *state.Service) error { |
| 733 » » » return s.Kill() | 770 » » » return s.EnsureDying() |
| 734 }, | 771 }, |
| 735 Life: state.Dying, | 772 Life: state.Dying, |
| 736 }, | 773 }, |
| 737 } | 774 } |
| 738 | 775 |
| 739 func (s *ServiceSuite) TestWatchService(c *C) { | 776 func (s *ServiceSuite) TestWatchService(c *C) { |
| 740 w := s.service.Watch() | 777 w := s.service.Watch() |
| 741 defer func() { | 778 defer func() { |
| 742 c.Assert(w.Stop(), IsNil) | 779 c.Assert(w.Stop(), IsNil) |
| 743 }() | 780 }() |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 768 case <-time.After(500 * time.Millisecond): | 805 case <-time.After(500 * time.Millisecond): |
| 769 c.Fatalf("did not get change: %v %v", test.Exposed, test .Life) | 806 c.Fatalf("did not get change: %v %v", test.Exposed, test .Life) |
| 770 } | 807 } |
| 771 } | 808 } |
| 772 select { | 809 select { |
| 773 case got := <-w.Changes(): | 810 case got := <-w.Changes(): |
| 774 c.Fatalf("got unexpected change: %#v", got) | 811 c.Fatalf("got unexpected change: %#v", got) |
| 775 case <-time.After(100 * time.Millisecond): | 812 case <-time.After(100 * time.Millisecond): |
| 776 } | 813 } |
| 777 } | 814 } |
| OLD | NEW |