| OLD | NEW |
| 1 package provisioner_test | 1 package provisioner_test |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "time" | 4 "time" |
| 5 | 5 |
| 6 . "launchpad.net/gocheck" | 6 . "launchpad.net/gocheck" |
| 7 "launchpad.net/juju-core/environs" | 7 "launchpad.net/juju-core/environs" |
| 8 "launchpad.net/juju-core/environs/config" | 8 "launchpad.net/juju-core/environs/config" |
| 9 "launchpad.net/juju-core/environs/dummy" | 9 "launchpad.net/juju-core/environs/dummy" |
| 10 "launchpad.net/juju-core/juju/testing" | 10 "launchpad.net/juju-core/juju/testing" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 return s.State.SetEnvironConfig(s.cfg) | 59 return s.State.SetEnvironConfig(s.cfg) |
| 60 } | 60 } |
| 61 | 61 |
| 62 func (s *ProvisionerSuite) stopProvisioner(c *C, p *provisioner.Provisioner) { | 62 func (s *ProvisionerSuite) stopProvisioner(c *C, p *provisioner.Provisioner) { |
| 63 c.Assert(p.Stop(), IsNil) | 63 c.Assert(p.Stop(), IsNil) |
| 64 } | 64 } |
| 65 | 65 |
| 66 // checkStartInstance checks that an instance has been started | 66 // checkStartInstance checks that an instance has been started |
| 67 // with a machine id the same as m's, and that the machine's | 67 // with a machine id the same as m's, and that the machine's |
| 68 // instance id has been set appropriately. | 68 // instance id has been set appropriately. |
| 69 func (s *ProvisionerSuite) checkStartInstance(c *C, m *state.Machine) { | 69 func (s *ProvisionerSuite) checkStartInstance(c *C, m *state.Machine, secret str
ing) { |
| 70 for { | 70 for { |
| 71 select { | 71 select { |
| 72 case o := <-s.op: | 72 case o := <-s.op: |
| 73 switch o := o.(type) { | 73 switch o := o.(type) { |
| 74 case dummy.OpStartInstance: | 74 case dummy.OpStartInstance: |
| 75 c.Assert(o.Info, DeepEquals, s.StateInfo(c)) | 75 c.Assert(o.Info, DeepEquals, s.StateInfo(c)) |
| 76 c.Assert(o.MachineId, Equals, m.Id()) | 76 c.Assert(o.MachineId, Equals, m.Id()) |
| 77 c.Assert(o.Instance, NotNil) | 77 c.Assert(o.Instance, NotNil) |
| 78 s.checkMachineId(c, m, o.Instance) | 78 s.checkMachineId(c, m, o.Instance) |
| 79 c.Assert(o.Secret, Equals, secret) |
| 79 return | 80 return |
| 80 default: | 81 default: |
| 81 c.Logf("ignoring unexpected operation %#v", o) | 82 c.Logf("ignoring unexpected operation %#v", o) |
| 82 } | 83 } |
| 83 case <-time.After(2 * time.Second): | 84 case <-time.After(2 * time.Second): |
| 84 c.Errorf("provisioner did not start an instance") | 85 c.Errorf("provisioner did not start an instance") |
| 85 return | 86 return |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 } | 89 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 id, err := m.InstanceId() | 150 id, err := m.InstanceId() |
| 150 c.Assert(err, IsNil) | 151 c.Assert(err, IsNil) |
| 151 c.Assert(id, Equals, instId) | 152 c.Assert(id, Equals, instId) |
| 152 } | 153 } |
| 153 | 154 |
| 154 func (s *ProvisionerSuite) TestProvisionerStartStop(c *C) { | 155 func (s *ProvisionerSuite) TestProvisionerStartStop(c *C) { |
| 155 p := provisioner.NewProvisioner(s.State) | 156 p := provisioner.NewProvisioner(s.State) |
| 156 c.Assert(p.Stop(), IsNil) | 157 c.Assert(p.Stop(), IsNil) |
| 157 } | 158 } |
| 158 | 159 |
| 159 func (s *ProvisionerSuite) TestProvisionerEnvironmentChange(c *C) { | |
| 160 p := provisioner.NewProvisioner(s.State) | |
| 161 defer s.stopProvisioner(c, p) | |
| 162 // Twiddle with the environment configuration. | |
| 163 cfg, err := s.State.EnvironConfig() | |
| 164 c.Assert(err, IsNil) | |
| 165 cfgAttrs := cfg.AllAttrs() | |
| 166 cfgAttrs["name"] = "testing2" | |
| 167 cfg, err = config.New(cfgAttrs) | |
| 168 c.Assert(err, IsNil) | |
| 169 err = s.State.SetEnvironConfig(cfg) | |
| 170 c.Assert(err, IsNil) | |
| 171 cfgAttrs["name"] = "testing3" | |
| 172 cfg, err = config.New(cfgAttrs) | |
| 173 c.Assert(err, IsNil) | |
| 174 err = s.State.SetEnvironConfig(cfg) | |
| 175 c.Assert(err, IsNil) | |
| 176 } | |
| 177 | |
| 178 func (s *ProvisionerSuite) TestProvisionerStopOnStateClose(c *C) { | 160 func (s *ProvisionerSuite) TestProvisionerStopOnStateClose(c *C) { |
| 179 st, err := state.Open(s.StateInfo(c)) | 161 st, err := state.Open(s.StateInfo(c)) |
| 180 c.Assert(err, IsNil) | 162 c.Assert(err, IsNil) |
| 181 p := provisioner.NewProvisioner(st) | 163 p := provisioner.NewProvisioner(st) |
| 182 | 164 |
| 183 p.CloseState() | 165 p.CloseState() |
| 184 | 166 |
| 185 // must use Check to avoid leaking PA | 167 // must use Check to avoid leaking PA |
| 186 c.Check(p.Wait(), ErrorMatches, ".* zookeeper is closing") | 168 c.Check(p.Wait(), ErrorMatches, ".* zookeeper is closing") |
| 187 c.Assert(p.Stop(), ErrorMatches, ".* zookeeper is closing") | 169 c.Assert(p.Stop(), ErrorMatches, ".* zookeeper is closing") |
| 188 } | 170 } |
| 189 | 171 |
| 190 // Start and stop one machine, watch the PA. | 172 // Start and stop one machine, watch the PA. |
| 191 func (s *ProvisionerSuite) TestSimple(c *C) { | 173 func (s *ProvisionerSuite) TestSimple(c *C) { |
| 192 p := provisioner.NewProvisioner(s.State) | 174 p := provisioner.NewProvisioner(s.State) |
| 193 defer s.stopProvisioner(c, p) | 175 defer s.stopProvisioner(c, p) |
| 194 | 176 |
| 195 // place a new machine into the state | 177 // place a new machine into the state |
| 196 m, err := s.State.AddMachine() | 178 m, err := s.State.AddMachine() |
| 197 c.Assert(err, IsNil) | 179 c.Assert(err, IsNil) |
| 198 | 180 |
| 199 » s.checkStartInstance(c, m) | 181 » s.checkStartInstance(c, m, "pork") |
| 200 | 182 |
| 201 // now remove it | 183 // now remove it |
| 202 c.Assert(s.State.RemoveMachine(m.Id()), IsNil) | 184 c.Assert(s.State.RemoveMachine(m.Id()), IsNil) |
| 203 | 185 |
| 204 // watch the PA remove it | 186 // watch the PA remove it |
| 205 s.checkStopInstance(c) | 187 s.checkStopInstance(c) |
| 206 s.checkMachineId(c, m, nil) | 188 s.checkMachineId(c, m, nil) |
| 207 } | 189 } |
| 208 | 190 |
| 209 func (s *ProvisionerSuite) TestProvisioningDoesNotOccurWithAnInvalidEnvironment(
c *C) { | 191 func (s *ProvisionerSuite) TestProvisioningDoesNotOccurWithAnInvalidEnvironment(
c *C) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 231 // try to create a machine | 213 // try to create a machine |
| 232 m, err := s.State.AddMachine() | 214 m, err := s.State.AddMachine() |
| 233 c.Assert(err, IsNil) | 215 c.Assert(err, IsNil) |
| 234 | 216 |
| 235 // the PA should not create it | 217 // the PA should not create it |
| 236 s.checkNotStartInstance(c) | 218 s.checkNotStartInstance(c) |
| 237 | 219 |
| 238 err = s.fixEnvironment() | 220 err = s.fixEnvironment() |
| 239 c.Assert(err, IsNil) | 221 c.Assert(err, IsNil) |
| 240 | 222 |
| 241 » s.checkStartInstance(c, m) | 223 » s.checkStartInstance(c, m, "pork") |
| 242 } | 224 } |
| 243 | 225 |
| 244 func (s *ProvisionerSuite) TestProvisioningDoesOccurAfterInvalidEnvironmentPubli
shed(c *C) { | 226 func (s *ProvisionerSuite) TestProvisioningDoesOccurAfterInvalidEnvironmentPubli
shed(c *C) { |
| 245 p := provisioner.NewProvisioner(s.State) | 227 p := provisioner.NewProvisioner(s.State) |
| 246 defer s.stopProvisioner(c, p) | 228 defer s.stopProvisioner(c, p) |
| 247 | 229 |
| 248 // place a new machine into the state | 230 // place a new machine into the state |
| 249 m, err := s.State.AddMachine() | 231 m, err := s.State.AddMachine() |
| 250 c.Assert(err, IsNil) | 232 c.Assert(err, IsNil) |
| 251 | 233 |
| 252 » s.checkStartInstance(c, m) | 234 » s.checkStartInstance(c, m, "pork") |
| 253 | 235 |
| 254 err = s.invalidateEnvironment() | 236 err = s.invalidateEnvironment() |
| 255 c.Assert(err, IsNil) | 237 c.Assert(err, IsNil) |
| 256 | 238 |
| 257 // create a second machine | 239 // create a second machine |
| 258 m, err = s.State.AddMachine() | 240 m, err = s.State.AddMachine() |
| 259 c.Assert(err, IsNil) | 241 c.Assert(err, IsNil) |
| 260 | 242 |
| 261 // the PA should create it using the old environment | 243 // the PA should create it using the old environment |
| 262 » s.checkStartInstance(c, m) | 244 » s.checkStartInstance(c, m, "pork") |
| 263 } | 245 } |
| 264 | 246 |
| 265 func (s *ProvisionerSuite) TestProvisioningDoesNotProvisionTheSameMachineAfterRe
start(c *C) { | 247 func (s *ProvisionerSuite) TestProvisioningDoesNotProvisionTheSameMachineAfterRe
start(c *C) { |
| 266 p := provisioner.NewProvisioner(s.State) | 248 p := provisioner.NewProvisioner(s.State) |
| 267 // we are not using defer s.stopProvisioner(c, p) because we need to con
trol when· | 249 // we are not using defer s.stopProvisioner(c, p) because we need to con
trol when· |
| 268 // the PA is restarted in this test. tf. Methods like Fatalf and Assert
should not be used. | 250 // the PA is restarted in this test. tf. Methods like Fatalf and Assert
should not be used. |
| 269 | 251 |
| 270 // create a machine | 252 // create a machine |
| 271 m, err := s.State.AddMachine() | 253 m, err := s.State.AddMachine() |
| 272 c.Check(err, IsNil) | 254 c.Check(err, IsNil) |
| 273 | 255 |
| 274 » s.checkStartInstance(c, m) | 256 » s.checkStartInstance(c, m, "pork") |
| 275 | 257 |
| 276 // restart the PA | 258 // restart the PA |
| 277 c.Check(p.Stop(), IsNil) | 259 c.Check(p.Stop(), IsNil) |
| 278 | 260 |
| 279 p = provisioner.NewProvisioner(s.State) | 261 p = provisioner.NewProvisioner(s.State) |
| 280 | 262 |
| 281 // check that there is only one machine known | 263 // check that there is only one machine known |
| 282 machines, err := p.AllMachines() | 264 machines, err := p.AllMachines() |
| 283 c.Check(err, IsNil) | 265 c.Check(err, IsNil) |
| 284 c.Check(len(machines), Equals, 1) | 266 c.Check(len(machines), Equals, 1) |
| 285 c.Check(machines[0].Id(), Equals, 0) | 267 c.Check(machines[0].Id(), Equals, 0) |
| 286 | 268 |
| 287 // the PA should not create it a second time | 269 // the PA should not create it a second time |
| 288 s.checkNotStartInstance(c) | 270 s.checkNotStartInstance(c) |
| 289 | 271 |
| 290 c.Assert(p.Stop(), IsNil) | 272 c.Assert(p.Stop(), IsNil) |
| 291 } | 273 } |
| 292 | 274 |
| 293 func (s *ProvisionerSuite) TestProvisioningStopsUnknownInstances(c *C) { | 275 func (s *ProvisionerSuite) TestProvisioningStopsUnknownInstances(c *C) { |
| 294 p := provisioner.NewProvisioner(s.State) | 276 p := provisioner.NewProvisioner(s.State) |
| 295 // we are not using defer s.stopProvisioner(c, p) because we need to con
trol when· | 277 // we are not using defer s.stopProvisioner(c, p) because we need to con
trol when· |
| 296 // the PA is restarted in this test. Methods like Fatalf and Assert shou
ld not be used. | 278 // the PA is restarted in this test. Methods like Fatalf and Assert shou
ld not be used. |
| 297 | 279 |
| 298 // create a machine | 280 // create a machine |
| 299 m, err := s.State.AddMachine() | 281 m, err := s.State.AddMachine() |
| 300 c.Check(err, IsNil) | 282 c.Check(err, IsNil) |
| 301 | 283 |
| 302 » s.checkStartInstance(c, m) | 284 » s.checkStartInstance(c, m, "pork") |
| 303 | 285 |
| 304 // create a second machine | 286 // create a second machine |
| 305 m, err = s.State.AddMachine() | 287 m, err = s.State.AddMachine() |
| 306 c.Check(err, IsNil) | 288 c.Check(err, IsNil) |
| 307 | 289 |
| 308 » s.checkStartInstance(c, m) | 290 » s.checkStartInstance(c, m, "pork") |
| 309 | 291 |
| 310 // stop the PA | 292 // stop the PA |
| 311 c.Check(p.Stop(), IsNil) | 293 c.Check(p.Stop(), IsNil) |
| 312 | 294 |
| 313 // remove the machine | 295 // remove the machine |
| 314 err = s.State.RemoveMachine(m.Id()) | 296 err = s.State.RemoveMachine(m.Id()) |
| 315 c.Check(err, IsNil) | 297 c.Check(err, IsNil) |
| 316 | 298 |
| 317 // start a new provisioner | 299 // start a new provisioner |
| 318 p = provisioner.NewProvisioner(s.State) | 300 p = provisioner.NewProvisioner(s.State) |
| 319 | 301 |
| 320 s.checkStopInstance(c) | 302 s.checkStopInstance(c) |
| 321 | 303 |
| 322 c.Assert(p.Stop(), IsNil) | 304 c.Assert(p.Stop(), IsNil) |
| 323 } | 305 } |
| 324 | 306 |
| 325 // This check is different from the one above as it catches the edge case | 307 // This check is different from the one above as it catches the edge case |
| 326 // where the final machine has been removed from the state while the PA was· | 308 // where the final machine has been removed from the state while the PA was· |
| 327 // not running.· | 309 // not running.· |
| 328 func (s *ProvisionerSuite) TestProvisioningStopsOnlyUnknownInstances(c *C) { | 310 func (s *ProvisionerSuite) TestProvisioningStopsOnlyUnknownInstances(c *C) { |
| 329 p := provisioner.NewProvisioner(s.State) | 311 p := provisioner.NewProvisioner(s.State) |
| 330 // we are not using defer s.stopProvisioner(c, p) because we need to con
trol when· | 312 // we are not using defer s.stopProvisioner(c, p) because we need to con
trol when· |
| 331 // the PA is restarted in this test. Methods like Fatalf and Assert shou
ld not be used. | 313 // the PA is restarted in this test. Methods like Fatalf and Assert shou
ld not be used. |
| 332 | 314 |
| 333 // create a machine | 315 // create a machine |
| 334 m, err := s.State.AddMachine() | 316 m, err := s.State.AddMachine() |
| 335 c.Check(err, IsNil) | 317 c.Check(err, IsNil) |
| 336 | 318 |
| 337 » s.checkStartInstance(c, m) | 319 » s.checkStartInstance(c, m, "pork") |
| 338 | 320 |
| 339 // stop the PA | 321 // stop the PA |
| 340 c.Check(p.Stop(), IsNil) | 322 c.Check(p.Stop(), IsNil) |
| 341 | 323 |
| 342 // remove the machine | 324 // remove the machine |
| 343 err = s.State.RemoveMachine(m.Id()) | 325 err = s.State.RemoveMachine(m.Id()) |
| 344 c.Check(err, IsNil) | 326 c.Check(err, IsNil) |
| 345 | 327 |
| 346 machines, err := s.State.AllMachines() | 328 machines, err := s.State.AllMachines() |
| 347 c.Check(err, IsNil) | 329 c.Check(err, IsNil) |
| 348 c.Check(len(machines), Equals, 0) // it's really gone··· | 330 c.Check(len(machines), Equals, 0) // it's really gone··· |
| 349 | 331 |
| 350 // start a new provisioner | 332 // start a new provisioner |
| 351 p = provisioner.NewProvisioner(s.State) | 333 p = provisioner.NewProvisioner(s.State) |
| 352 | 334 |
| 353 s.checkStopInstance(c) | 335 s.checkStopInstance(c) |
| 354 | 336 |
| 355 c.Assert(p.Stop(), IsNil) | 337 c.Assert(p.Stop(), IsNil) |
| 356 } | 338 } |
| 357 | 339 |
| 358 func (s *ProvisionerSuite) TestProvisioningRecoversAfterInvalidEnvironmentPublis
hed(c *C) { | 340 func (s *ProvisionerSuite) TestProvisioningRecoversAfterInvalidEnvironmentPublis
hed(c *C) { |
| 341 // TODO(mue) Add secret testing. |
| 342 // go test -gocheck.f TestProvisioningRecoversAfterInvalidEnvironmentPub
lished |
| 359 p := provisioner.NewProvisioner(s.State) | 343 p := provisioner.NewProvisioner(s.State) |
| 360 defer s.stopProvisioner(c, p) | 344 defer s.stopProvisioner(c, p) |
| 361 | 345 |
| 362 // place a new machine into the state | 346 // place a new machine into the state |
| 363 m, err := s.State.AddMachine() | 347 m, err := s.State.AddMachine() |
| 364 c.Assert(err, IsNil) | 348 c.Assert(err, IsNil) |
| 365 | 349 |
| 366 » s.checkStartInstance(c, m) | 350 » s.checkStartInstance(c, m, "pork") |
| 367 | 351 |
| 368 err = s.invalidateEnvironment() | 352 err = s.invalidateEnvironment() |
| 369 c.Assert(err, IsNil) | 353 c.Assert(err, IsNil) |
| 370 | 354 |
| 371 // create a second machine | 355 // create a second machine |
| 372 m, err = s.State.AddMachine() | 356 m, err = s.State.AddMachine() |
| 373 c.Assert(err, IsNil) | 357 c.Assert(err, IsNil) |
| 374 | 358 |
| 375 // the PA should create it using the old environment | 359 // the PA should create it using the old environment |
| 376 » s.checkStartInstance(c, m) | 360 » s.checkStartInstance(c, m, "pork") |
| 377 | 361 |
| 378 err = s.fixEnvironment() | 362 err = s.fixEnvironment() |
| 379 c.Assert(err, IsNil) | 363 c.Assert(err, IsNil) |
| 380 | 364 |
| 365 cfg, err := s.State.EnvironConfig() |
| 366 c.Assert(err, IsNil) |
| 367 attrs := cfg.AllAttrs() |
| 368 attrs["secret"] = "beef" |
| 369 cfg, err = config.New(attrs) |
| 370 c.Assert(err, IsNil) |
| 371 err = s.State.SetEnvironConfig(cfg) |
| 372 |
| 381 // create a third machine | 373 // create a third machine |
| 382 m, err = s.State.AddMachine() | 374 m, err = s.State.AddMachine() |
| 383 c.Assert(err, IsNil) | 375 c.Assert(err, IsNil) |
| 384 | 376 |
| 385 // the PA should create it using the new environment | 377 // the PA should create it using the new environment |
| 386 » s.checkStartInstance(c, m) | 378 » s.checkStartInstance(c, m, "beef") |
| 387 } | 379 } |
| OLD | NEW |