github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	apitesting "github.com/juju/juju/api/testing"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/core/network"
    14  	"github.com/juju/juju/state"
    15  )
    16  
    17  type stateSuite struct {
    18  	uniterSuite
    19  	*apitesting.APIAddresserTests
    20  	*apitesting.ModelWatcherTests
    21  }
    22  
    23  var _ = gc.Suite(&stateSuite{})
    24  
    25  func (s *stateSuite) SetUpTest(c *gc.C) {
    26  	s.uniterSuite.SetUpTest(c)
    27  	s.APIAddresserTests = apitesting.NewAPIAddresserTests(s.uniter, s.BackingState)
    28  	s.ModelWatcherTests = apitesting.NewModelWatcherTests(s.uniter, s.BackingState, s.Model)
    29  }
    30  
    31  func (s *stateSuite) TestProviderType(c *gc.C) {
    32  	cfg, err := s.Model.ModelConfig()
    33  	c.Assert(err, jc.ErrorIsNil)
    34  
    35  	providerType, err := s.uniter.ProviderType()
    36  	c.Assert(err, jc.ErrorIsNil)
    37  	c.Assert(providerType, gc.DeepEquals, cfg.Type())
    38  }
    39  
    40  func (s *stateSuite) TestAllMachinePorts(c *gc.C) {
    41  	// Verify no ports are opened yet on the machine or unit.
    42  	machinePorts, err := s.wordpressMachine.AllPorts()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(machinePorts, gc.HasLen, 0)
    45  	unitPorts, err := s.wordpressUnit.OpenedPorts()
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	c.Assert(unitPorts, gc.HasLen, 0)
    48  
    49  	// Add another wordpress unit on the same machine.
    50  	wordpressUnit1, err := s.wordpressApplication.AddUnit(state.AddUnitParams{})
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	err = wordpressUnit1.AssignToMachine(s.wordpressMachine)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  
    55  	// Open some ports on both units.
    56  	err = s.wordpressUnit.OpenPorts("tcp", 100, 200)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	err = s.wordpressUnit.OpenPorts("udp", 10, 20)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	err = wordpressUnit1.OpenPorts("tcp", 201, 250)
    61  	c.Assert(err, jc.ErrorIsNil)
    62  	err = wordpressUnit1.OpenPorts("udp", 1, 8)
    63  	c.Assert(err, jc.ErrorIsNil)
    64  
    65  	portsMap, err := s.uniter.AllMachinePorts(s.wordpressMachine.Tag().(names.MachineTag))
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(portsMap, jc.DeepEquals, map[network.PortRange]params.RelationUnit{
    68  		{100, 200, "tcp"}: {Unit: s.wordpressUnit.Tag().String()},
    69  		{10, 20, "udp"}:   {Unit: s.wordpressUnit.Tag().String()},
    70  		{201, 250, "tcp"}: {Unit: wordpressUnit1.Tag().String()},
    71  		{1, 8, "udp"}:     {Unit: wordpressUnit1.Tag().String()},
    72  	})
    73  }