github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/state/api/state_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api_test
     5  
     6  import (
     7  	stdtesting "testing"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	jujutesting "github.com/juju/juju/juju/testing"
    12  	"github.com/juju/juju/network"
    13  	"github.com/juju/juju/state/api"
    14  	coretesting "github.com/juju/juju/testing"
    15  )
    16  
    17  func TestAll(t *stdtesting.T) {
    18  	coretesting.MgoTestPackage(t)
    19  }
    20  
    21  type stateSuite struct {
    22  	jujutesting.JujuConnSuite
    23  }
    24  
    25  var _ = gc.Suite(&stateSuite{})
    26  
    27  type slideSuite struct {
    28  	coretesting.BaseSuite
    29  }
    30  
    31  var _ = gc.Suite(&slideSuite{})
    32  
    33  func (s *stateSuite) TestCloseMultipleOk(c *gc.C) {
    34  	c.Assert(s.APIState.Close(), gc.IsNil)
    35  	c.Assert(s.APIState.Close(), gc.IsNil)
    36  	c.Assert(s.APIState.Close(), gc.IsNil)
    37  }
    38  
    39  func (s *stateSuite) TestAPIHostPortsAlwaysIncludesTheConnection(c *gc.C) {
    40  	hostportslist := s.APIState.APIHostPorts()
    41  	c.Check(hostportslist, gc.HasLen, 1)
    42  	serverhostports := hostportslist[0]
    43  	c.Check(serverhostports, gc.HasLen, 1)
    44  	// the other addresses, but always see this one as well.
    45  	info := s.APIInfo(c)
    46  	// We intentionally set this to invalid values
    47  	badValue := network.HostPort{network.Address{
    48  		Value:       "0.1.2.3",
    49  		Type:        network.IPv4Address,
    50  		NetworkName: "",
    51  		Scope:       network.ScopeMachineLocal,
    52  	}, 1234}
    53  	badServer := []network.HostPort{badValue}
    54  	s.State.SetAPIHostPorts([][]network.HostPort{badServer})
    55  	apistate, err := api.Open(info, api.DialOpts{})
    56  	c.Assert(err, gc.IsNil)
    57  	defer apistate.Close()
    58  	hostports := apistate.APIHostPorts()
    59  	c.Check(hostports, gc.DeepEquals, [][]network.HostPort{serverhostports, badServer})
    60  }
    61  
    62  func (s *stateSuite) TestLoginSetsEnvironTag(c *gc.C) {
    63  	env, err := s.State.Environment()
    64  	c.Assert(err, gc.IsNil)
    65  	info := s.APIInfo(c)
    66  	tag := info.Tag
    67  	password := info.Password
    68  	info.Tag = ""
    69  	info.Password = ""
    70  	apistate, err := api.Open(info, api.DialOpts{})
    71  	c.Assert(err, gc.IsNil)
    72  	defer apistate.Close()
    73  	// We haven't called Login yet, so the EnvironTag shouldn't be set.
    74  	c.Check(apistate.EnvironTag(), gc.Equals, "")
    75  	err = apistate.Login(tag, password, "")
    76  	c.Assert(err, gc.IsNil)
    77  	// Now that we've logged in, EnvironTag should be updated correctly.
    78  	c.Check(apistate.EnvironTag(), gc.Equals, env.Tag())
    79  }
    80  
    81  func (s *stateSuite) TestAPIHostPortsMovesConnectedValueFirst(c *gc.C) {
    82  	hostportslist := s.APIState.APIHostPorts()
    83  	c.Check(hostportslist, gc.HasLen, 1)
    84  	serverhostports := hostportslist[0]
    85  	c.Check(serverhostports, gc.HasLen, 1)
    86  	goodAddress := serverhostports[0]
    87  	// the other addresses, but always see this one as well.
    88  	info := s.APIInfo(c)
    89  	// We intentionally set this to invalid values
    90  	badValue := network.HostPort{network.Address{
    91  		Value:       "0.1.2.3",
    92  		Type:        network.IPv4Address,
    93  		NetworkName: "",
    94  		Scope:       network.ScopeMachineLocal,
    95  	}, 1234}
    96  	badServer := []network.HostPort{badValue}
    97  	extraAddress := network.HostPort{network.Address{
    98  		Value:       "0.1.2.4",
    99  		Type:        network.IPv4Address,
   100  		NetworkName: "",
   101  		Scope:       network.ScopeMachineLocal,
   102  	}, 5678}
   103  	extraAddress2 := network.HostPort{network.Address{
   104  		Value:       "0.1.2.1",
   105  		Type:        network.IPv4Address,
   106  		NetworkName: "",
   107  		Scope:       network.ScopeMachineLocal,
   108  	}, 9012}
   109  	serverExtra := []network.HostPort{extraAddress, goodAddress, extraAddress2}
   110  	current := [][]network.HostPort{badServer, serverExtra}
   111  	s.State.SetAPIHostPorts(current)
   112  	apistate, err := api.Open(info, api.DialOpts{})
   113  	c.Assert(err, gc.IsNil)
   114  	defer apistate.Close()
   115  	hostports := apistate.APIHostPorts()
   116  	// We should have rotate the server we connected to as the first item,
   117  	// and the address of that server as the first address
   118  	sortedServer := []network.HostPort{goodAddress, extraAddress, extraAddress2}
   119  	expected := [][]network.HostPort{sortedServer, badServer}
   120  	c.Check(hostports, gc.DeepEquals, expected)
   121  }
   122  
   123  var exampleHostPorts = []network.HostPort{
   124  	{
   125  		Address: network.Address{
   126  			Value:       "0.1.2.3",
   127  			Type:        network.IPv4Address,
   128  			NetworkName: "",
   129  			Scope:       network.ScopeUnknown,
   130  		}, Port: 1234,
   131  	}, {
   132  		Address: network.Address{
   133  			Value:       "0.1.2.4",
   134  			Type:        network.IPv4Address,
   135  			NetworkName: "",
   136  			Scope:       network.ScopeUnknown,
   137  		}, Port: 5678,
   138  	}, {
   139  		Address: network.Address{
   140  			Value:       "0.1.2.1",
   141  			Type:        network.IPv4Address,
   142  			NetworkName: "",
   143  			Scope:       network.ScopeUnknown,
   144  		}, Port: 9012,
   145  	}, {
   146  		Address: network.Address{
   147  			Value:       "0.1.9.1",
   148  			Type:        network.IPv4Address,
   149  			NetworkName: "",
   150  			Scope:       network.ScopeUnknown,
   151  		}, Port: 8888,
   152  	},
   153  }
   154  
   155  func (s *slideSuite) TestSlideToFrontNoOp(c *gc.C) {
   156  	servers := [][]network.HostPort{
   157  		{exampleHostPorts[0]},
   158  		{exampleHostPorts[1]},
   159  	}
   160  	// order should not have changed
   161  	expected := [][]network.HostPort{
   162  		{exampleHostPorts[0]},
   163  		{exampleHostPorts[1]},
   164  	}
   165  	api.SlideAddressToFront(servers, 0, 0)
   166  	c.Check(servers, gc.DeepEquals, expected)
   167  }
   168  
   169  func (s *slideSuite) TestSlideToFrontAddress(c *gc.C) {
   170  	servers := [][]network.HostPort{
   171  		{exampleHostPorts[0], exampleHostPorts[1], exampleHostPorts[2]},
   172  		{exampleHostPorts[3]},
   173  	}
   174  	// server order should not change, but ports should be switched
   175  	expected := [][]network.HostPort{
   176  		{exampleHostPorts[1], exampleHostPorts[0], exampleHostPorts[2]},
   177  		{exampleHostPorts[3]},
   178  	}
   179  	api.SlideAddressToFront(servers, 0, 1)
   180  	c.Check(servers, gc.DeepEquals, expected)
   181  }
   182  
   183  func (s *slideSuite) TestSlideToFrontServer(c *gc.C) {
   184  	servers := [][]network.HostPort{
   185  		{exampleHostPorts[0], exampleHostPorts[1]},
   186  		{exampleHostPorts[2]},
   187  		{exampleHostPorts[3]},
   188  	}
   189  	// server 1 should be slid to the front
   190  	expected := [][]network.HostPort{
   191  		{exampleHostPorts[2]},
   192  		{exampleHostPorts[0], exampleHostPorts[1]},
   193  		{exampleHostPorts[3]},
   194  	}
   195  	api.SlideAddressToFront(servers, 1, 0)
   196  	c.Check(servers, gc.DeepEquals, expected)
   197  }
   198  
   199  func (s *slideSuite) TestSlideToFrontBoth(c *gc.C) {
   200  	servers := [][]network.HostPort{
   201  		{exampleHostPorts[0]},
   202  		{exampleHostPorts[1], exampleHostPorts[2]},
   203  		{exampleHostPorts[3]},
   204  	}
   205  	// server 1 should be slid to the front
   206  	expected := [][]network.HostPort{
   207  		{exampleHostPorts[2], exampleHostPorts[1]},
   208  		{exampleHostPorts[0]},
   209  		{exampleHostPorts[3]},
   210  	}
   211  	api.SlideAddressToFront(servers, 1, 1)
   212  	c.Check(servers, gc.DeepEquals, expected)
   213  }