github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/machine_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	jujutesting "github.com/juju/juju/juju/testing"
    12  	"github.com/juju/juju/state"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  // MachineSuite tests the connectivity of all the machine subcommands. These
    17  // tests go from the command line, api client, api server, db. The db changes
    18  // are then checked.  Only one test for each command is done here to check
    19  // connectivity.  Exhaustive unit tests are at each layer.
    20  type MachineSuite struct {
    21  	jujutesting.JujuConnSuite
    22  }
    23  
    24  var _ = gc.Suite(&MachineSuite{})
    25  
    26  func (s *MachineSuite) RunMachineCommand(c *gc.C, commands ...string) (*cmd.Context, error) {
    27  	args := []string{"machine"}
    28  	args = append(args, commands...)
    29  	context := testing.Context(c)
    30  	juju := NewJujuCommand(context)
    31  	if err := testing.InitCommand(juju, args); err != nil {
    32  		return context, err
    33  	}
    34  	return context, juju.Run(context)
    35  }
    36  
    37  func (s *MachineSuite) TestMachineAdd(c *gc.C) {
    38  	machines, err := s.State.AllMachines()
    39  	c.Assert(err, jc.ErrorIsNil)
    40  	count := len(machines)
    41  
    42  	ctx, err := s.RunMachineCommand(c, "add")
    43  	c.Assert(testing.Stderr(ctx), jc.Contains, `created machine`)
    44  
    45  	machines, err = s.State.AllMachines()
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	c.Assert(machines, gc.HasLen, count+1)
    48  }
    49  
    50  func (s *MachineSuite) TestMachineRemove(c *gc.C) {
    51  	machine := s.Factory.MakeMachine(c, nil)
    52  
    53  	ctx, err := s.RunMachineCommand(c, "remove", machine.Id())
    54  	c.Assert(testing.Stdout(ctx), gc.Equals, "")
    55  
    56  	err = machine.Refresh()
    57  	c.Assert(err, jc.ErrorIsNil)
    58  
    59  	c.Assert(machine.Life(), gc.Equals, state.Dying)
    60  }