func (s *MachineSuite) TestManageEnviron(c *C) { m, _ := s.primeAgent(c, state.JobManageEnviron) op := make(chan dummy.Operation, 200) dummy.Listen(op) a := s.newAgent(c, m) done := make(chan error) go func() { done <- a.Run(nil) }() // Check that the provisioner and firewaller are alive by doing // a rudimentary check that it responds to state changes. // Add one unit to a service; it should get allocated a machine // and then its ports should be opened. charm := s.AddTestingCharm(c, "dummy") svc, err := s.Conn.AddService("test-service", charm) c.Assert(err, IsNil) err = svc.SetExposed() c.Assert(err, IsNil) units, err := s.Conn.AddUnits(svc, 1) c.Assert(err, IsNil) c.Check(opRecvTimeout(c, s.State, op, dummy.OpStartInstance{}), NotNil) // Wait for the instance id to show up in the state. id1, err := units[0].AssignedMachineId() c.Assert(err, IsNil) m1, err := s.State.Machine(id1) c.Assert(err, IsNil) w := m1.Watch() defer w.Stop() for _ = range w.Changes() { err = m1.Refresh() c.Assert(err, IsNil) _, err := m1.InstanceId() if state.IsNotFound(err) { continue } c.Assert(err, IsNil) break } err = units[0].OpenPort("tcp", 999) c.Assert(err, IsNil) c.Check(opRecvTimeout(c, s.State, op, dummy.OpOpenPorts{}), NotNil) err = a.Stop() c.Assert(err, IsNil) select { case err := <-done: c.Assert(err, IsNil) case <-time.After(5 * time.Second): c.Fatalf("timed out waiting for agent to terminate") } }
func runCommand(com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) { errc = make(chan error, 1) opc = make(chan dummy.Operation, 200) dummy.Listen(opc) go func() { // signal that we're done with this ops channel. defer dummy.Listen(nil) err := coretesting.InitCommand(com, args) if err != nil { errc <- err return } err = com.Run(cmd.DefaultContext()) errc <- err }() return }
func (s *ProvisionerSuite) SetUpTest(c *C) { s.JujuConnSuite.SetUpTest(c) // Create the operations channel with more than enough space // for those tests that don't listen on it. op := make(chan dummy.Operation, 500) dummy.Listen(op) s.op = op cfg, err := s.State.EnvironConfig() c.Assert(err, IsNil) s.cfg = cfg }
func (s *MachineSuite) TestManageEnviron(c *C) { usefulVersion := version.Current usefulVersion.Series = "series" // to match the charm created below envtesting.UploadFakeToolsVersion(c, s.Conn.Environ.Storage(), usefulVersion) m, _, _ := s.primeAgent(c, state.JobManageEnviron) op := make(chan dummy.Operation, 200) dummy.Listen(op) a := s.newAgent(c, m) // Make sure the agent is stopped even if the test fails. defer a.Stop() done := make(chan error) go func() { done <- a.Run(nil) }() // Check that the provisioner and firewaller are alive by doing // a rudimentary check that it responds to state changes. // Add one unit to a service; it should get allocated a machine // and then its ports should be opened. charm := s.AddTestingCharm(c, "dummy") svc, err := s.State.AddService("test-service", charm) c.Assert(err, IsNil) err = svc.SetExposed() c.Assert(err, IsNil) units, err := s.Conn.AddUnits(svc, 1, "") c.Assert(err, IsNil) c.Check(opRecvTimeout(c, s.State, op, dummy.OpStartInstance{}), NotNil) // Wait for the instance id to show up in the state. id1, err := units[0].AssignedMachineId() c.Assert(err, IsNil) m1, err := s.State.Machine(id1) c.Assert(err, IsNil) w := m1.Watch() defer w.Stop() for _ = range w.Changes() { err = m1.Refresh() c.Assert(err, IsNil) if _, err := m1.InstanceId(); err == nil { break } else { c.Check(err, FitsTypeOf, (*state.NotProvisionedError)(nil)) } } err = units[0].OpenPort("tcp", 999) c.Assert(err, IsNil) c.Check(opRecvTimeout(c, s.State, op, dummy.OpOpenPorts{}), NotNil) err = a.Stop() c.Assert(err, IsNil) select { case err := <-done: c.Assert(err, IsNil) case <-time.After(5 * time.Second): c.Fatalf("timed out waiting for agent to terminate") } }