github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/api/uniter/state_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	apitesting "github.com/juju/juju/api/testing"
    13  	"github.com/juju/juju/api/uniter"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/network"
    16  )
    17  
    18  type stateSuite struct {
    19  	uniterSuite
    20  	*apitesting.APIAddresserTests
    21  	*apitesting.EnvironWatcherTests
    22  }
    23  
    24  var _ = gc.Suite(&stateSuite{})
    25  
    26  func (s *stateSuite) SetUpTest(c *gc.C) {
    27  	s.uniterSuite.SetUpTest(c)
    28  	s.APIAddresserTests = apitesting.NewAPIAddresserTests(s.uniter, s.BackingState)
    29  	s.EnvironWatcherTests = apitesting.NewEnvironWatcherTests(s.uniter, s.BackingState, apitesting.NoSecrets)
    30  }
    31  
    32  func (s *stateSuite) TestProviderType(c *gc.C) {
    33  	cfg, err := s.State.EnvironConfig()
    34  	c.Assert(err, jc.ErrorIsNil)
    35  
    36  	providerType, err := s.uniter.ProviderType()
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	c.Assert(providerType, gc.DeepEquals, cfg.Type())
    39  }
    40  
    41  func (s *stateSuite) TestAllMachinePortsV0NotImplemented(c *gc.C) {
    42  	s.patchNewState(c, uniter.NewStateV0)
    43  
    44  	ports, err := s.uniter.AllMachinePorts(s.wordpressMachine.Tag().(names.MachineTag))
    45  	c.Assert(err, jc.Satisfies, errors.IsNotImplemented)
    46  	c.Assert(err.Error(), gc.Equals, "AllMachinePorts() (need V1+) not implemented")
    47  	c.Assert(ports, gc.IsNil)
    48  }
    49  
    50  func (s *stateSuite) TestAllMachinePortsV1(c *gc.C) {
    51  	s.patchNewState(c, uniter.NewStateV1)
    52  
    53  	// Verify no ports are opened yet on the machine or unit.
    54  	machinePorts, err := s.wordpressMachine.AllPorts()
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	c.Assert(machinePorts, gc.HasLen, 0)
    57  	unitPorts, err := s.wordpressUnit.OpenedPorts()
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(unitPorts, gc.HasLen, 0)
    60  
    61  	// Add another wordpress unit on the same machine.
    62  	wordpressUnit1, err := s.wordpressService.AddUnit()
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	err = wordpressUnit1.AssignToMachine(s.wordpressMachine)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  
    67  	// Open some ports on both units.
    68  	err = s.wordpressUnit.OpenPorts("tcp", 100, 200)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	err = s.wordpressUnit.OpenPorts("udp", 10, 20)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	err = wordpressUnit1.OpenPorts("tcp", 201, 250)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	err = wordpressUnit1.OpenPorts("udp", 1, 8)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  
    77  	portsMap, err := s.uniter.AllMachinePorts(s.wordpressMachine.Tag().(names.MachineTag))
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(portsMap, jc.DeepEquals, map[network.PortRange]params.RelationUnit{
    80  		network.PortRange{100, 200, "tcp"}: {Unit: s.wordpressUnit.Tag().String()},
    81  		network.PortRange{10, 20, "udp"}:   {Unit: s.wordpressUnit.Tag().String()},
    82  		network.PortRange{201, 250, "tcp"}: {Unit: wordpressUnit1.Tag().String()},
    83  		network.PortRange{1, 8, "udp"}:     {Unit: wordpressUnit1.Tag().String()},
    84  	})
    85  }