github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/peergrouper/machinetracker_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package peergrouper
     5  
     6  import (
     7  	"sort"
     8  
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/network"
    12  	coretesting "github.com/juju/juju/testing"
    13  )
    14  
    15  type machineTrackerSuite struct {
    16  	coretesting.BaseSuite
    17  }
    18  
    19  var _ = gc.Suite(&machineTrackerSuite{})
    20  
    21  func (s *machineTrackerSuite) TestSelectMongoAddressFromSpaceReturnsCorrectAddress(c *gc.C) {
    22  	spaceName := network.SpaceName("ha-space")
    23  
    24  	m := &machineTracker{
    25  		addresses: []network.Address{
    26  			{
    27  				Value:     "192.168.5.5",
    28  				Scope:     network.ScopeCloudLocal,
    29  				SpaceName: spaceName,
    30  			},
    31  			{
    32  				Value:     "192.168.10.5",
    33  				Scope:     network.ScopeCloudLocal,
    34  				SpaceName: network.SpaceName("another-space"),
    35  			},
    36  			{
    37  				Value: "localhost",
    38  				Scope: network.ScopeMachineLocal,
    39  			},
    40  		},
    41  	}
    42  
    43  	addr, err := m.SelectMongoAddressFromSpace(666, spaceName)
    44  	c.Assert(err, gc.IsNil)
    45  	c.Check(addr, gc.Equals, "192.168.5.5:666")
    46  }
    47  
    48  func (s *machineTrackerSuite) TestSelectMongoAddressFromSpaceEmptyWhenNoAddressFound(c *gc.C) {
    49  	m := &machineTracker{
    50  		id: "3",
    51  		addresses: []network.Address{
    52  			{
    53  				Value: "localhost",
    54  				Scope: network.ScopeMachineLocal,
    55  			},
    56  		},
    57  	}
    58  
    59  	addrs, err := m.SelectMongoAddressFromSpace(666, "bad-space")
    60  	c.Check(addrs, gc.Equals, "")
    61  	c.Check(err, gc.ErrorMatches, `addresses for machine "3" in space "bad-space" not found`)
    62  }
    63  
    64  func (s *machineTrackerSuite) TestSelectMongoAddressFromSpaceErrorForEmptySpace(c *gc.C) {
    65  	m := &machineTracker{
    66  		id: "3",
    67  	}
    68  
    69  	_, err := m.SelectMongoAddressFromSpace(666, "")
    70  	c.Check(err, gc.ErrorMatches, `empty space supplied as an argument for selecting Mongo address for machine "3"`)
    71  }
    72  
    73  func (s *machineTrackerSuite) TestGetPotentialMongoHostPortsReturnsAllAddresses(c *gc.C) {
    74  	m := &machineTracker{
    75  		id: "3",
    76  		addresses: []network.Address{
    77  			{
    78  				Value: "192.168.5.5",
    79  				Scope: network.ScopeCloudLocal,
    80  			},
    81  			{
    82  				Value: "10.0.0.1",
    83  				Scope: network.ScopeCloudLocal,
    84  			},
    85  			{
    86  				Value: "185.159.16.82",
    87  				Scope: network.ScopePublic,
    88  			},
    89  		},
    90  	}
    91  
    92  	addrs := network.HostPortsToStrings(m.GetPotentialMongoHostPorts(666))
    93  	sort.Strings(addrs)
    94  	c.Check(addrs, gc.DeepEquals, []string{"10.0.0.1:666", "185.159.16.82:666", "192.168.5.5:666"})
    95  }