github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/unit_assignment_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/instance"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  type UnitAssignmentSuite struct {
    16  	ConnSuite
    17  }
    18  
    19  var _ = gc.Suite(&UnitAssignmentSuite{})
    20  
    21  func (s *UnitAssignmentSuite) testAddServiceUnitAssignment(c *gc.C) (*state.Application, []state.UnitAssignment) {
    22  	charm := s.AddTestingCharm(c, "dummy")
    23  	svc, err := s.State.AddApplication(state.AddApplicationArgs{
    24  		Name: "dummy", Charm: charm, NumUnits: 2,
    25  		Placement: []*instance.Placement{{s.State.ModelUUID(), "abc"}},
    26  	})
    27  	c.Assert(err, jc.ErrorIsNil)
    28  	units, err := svc.AllUnits()
    29  	c.Assert(err, jc.ErrorIsNil)
    30  	c.Assert(units, gc.HasLen, 2)
    31  	for _, u := range units {
    32  		_, err := u.AssignedMachineId()
    33  		c.Assert(err, jc.Satisfies, errors.IsNotAssigned)
    34  	}
    35  
    36  	assignments, err := s.State.AllUnitAssignments()
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	c.Assert(assignments, jc.SameContents, []state.UnitAssignment{
    39  		{Unit: "dummy/0", Scope: s.State.ModelUUID(), Directive: "abc"},
    40  		{Unit: "dummy/1"},
    41  	})
    42  	return svc, assignments
    43  }
    44  
    45  func (s *UnitAssignmentSuite) TestAddServiceUnitAssignment(c *gc.C) {
    46  	s.testAddServiceUnitAssignment(c)
    47  }
    48  
    49  func (s *UnitAssignmentSuite) TestAssignStagedUnits(c *gc.C) {
    50  	svc, _ := s.testAddServiceUnitAssignment(c)
    51  
    52  	results, err := s.State.AssignStagedUnits([]string{
    53  		"dummy/0", "dummy/1",
    54  	})
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	c.Assert(results, jc.SameContents, []state.UnitAssignmentResult{
    57  		{Unit: "dummy/0"},
    58  		{Unit: "dummy/1"},
    59  	})
    60  
    61  	units, err := svc.AllUnits()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(units, gc.HasLen, 2)
    64  	for _, u := range units {
    65  		_, err := u.AssignedMachineId()
    66  		c.Assert(err, jc.ErrorIsNil)
    67  	}
    68  
    69  	// There should be no staged assignments now.
    70  	assignments, err := s.State.AllUnitAssignments()
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(assignments, gc.HasLen, 0)
    73  }
    74  
    75  func (s *UnitAssignmentSuite) TestAssignUnitWithPlacementMakesContainerInNewMachine(c *gc.C) {
    76  	// Enables juju deploy <charm> --to <container-type>
    77  	// It creates a new machine with a new container of that type.
    78  	// https://bugs.launchpad.net/juju-core/+bug/1590960
    79  	charm := s.AddTestingCharm(c, "dummy")
    80  	placement := instance.Placement{Scope: "lxd"}
    81  	svc, err := s.State.AddApplication(state.AddApplicationArgs{
    82  		Name:      "dummy",
    83  		Charm:     charm,
    84  		NumUnits:  1,
    85  		Placement: []*instance.Placement{&placement},
    86  	})
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	units, err := svc.AllUnits()
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	c.Assert(units, gc.HasLen, 1)
    91  	unit := units[0]
    92  
    93  	err = s.State.AssignUnitWithPlacement(unit, &placement)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  
    96  	machineId, err := unit.AssignedMachineId()
    97  	c.Assert(err, jc.ErrorIsNil)
    98  	machine, err := s.State.Machine(machineId)
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	parentId, isContainer := machine.ParentId()
   101  	c.Assert(isContainer, jc.IsTrue)
   102  	_, err = s.State.Machine(parentId)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  }