github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/api/testing/apiaddresser.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/api/watcher"
    11  	"github.com/juju/juju/network"
    12  	"github.com/juju/juju/state"
    13  	statetesting "github.com/juju/juju/state/testing"
    14  )
    15  
    16  type APIAddresserTests struct {
    17  	state  *state.State
    18  	facade APIAddresserFacade
    19  }
    20  
    21  func NewAPIAddresserTests(facade APIAddresserFacade, st *state.State) *APIAddresserTests {
    22  	return &APIAddresserTests{
    23  		state:  st,
    24  		facade: facade,
    25  	}
    26  }
    27  
    28  type APIAddresserFacade interface {
    29  	APIAddresses() ([]string, error)
    30  	CACert() (string, error)
    31  	APIHostPorts() ([][]network.HostPort, error)
    32  	WatchAPIHostPorts() (watcher.NotifyWatcher, error)
    33  }
    34  
    35  func (s *APIAddresserTests) TestAPIAddresses(c *gc.C) {
    36  	hostPorts := [][]network.HostPort{{{
    37  		Address: network.NewAddress("0.1.2.3", network.ScopeUnknown),
    38  		Port:    1234,
    39  	}}}
    40  
    41  	err := s.state.SetAPIHostPorts(hostPorts)
    42  	c.Assert(err, jc.ErrorIsNil)
    43  
    44  	addresses, err := s.facade.APIAddresses()
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(addresses, gc.DeepEquals, []string{"0.1.2.3:1234"})
    47  }
    48  
    49  func (s *APIAddresserTests) TestAPIHostPorts(c *gc.C) {
    50  	expectServerAddrs := [][]network.HostPort{{{
    51  		Address: network.NewAddress("0.1.2.24", network.ScopeUnknown),
    52  		Port:    999,
    53  	}, {
    54  		Address: network.NewAddress("example.com", network.ScopeUnknown),
    55  		Port:    1234,
    56  	}}, {{
    57  		Address: network.Address{
    58  			Value:       "2001:DB8::1",
    59  			Type:        network.IPv6Address,
    60  			NetworkName: "someNetwork",
    61  			Scope:       network.ScopeCloudLocal,
    62  		},
    63  		Port: 999,
    64  	}}}
    65  
    66  	err := s.state.SetAPIHostPorts(expectServerAddrs)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  
    69  	serverAddrs, err := s.facade.APIHostPorts()
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	c.Assert(serverAddrs, gc.DeepEquals, expectServerAddrs)
    72  }
    73  
    74  func (s *APIAddresserTests) TestCACert(c *gc.C) {
    75  	caCert, err := s.facade.CACert()
    76  	c.Assert(err, jc.ErrorIsNil)
    77  	c.Assert(caCert, gc.DeepEquals, s.state.CACert())
    78  }
    79  
    80  func (s *APIAddresserTests) TestWatchAPIHostPorts(c *gc.C) {
    81  	expectServerAddrs := [][]network.HostPort{{{
    82  		Address: network.NewAddress("0.1.2.3", network.ScopeUnknown),
    83  		Port:    1234,
    84  	}}}
    85  	err := s.state.SetAPIHostPorts(expectServerAddrs)
    86  	c.Assert(err, jc.ErrorIsNil)
    87  
    88  	w, err := s.facade.WatchAPIHostPorts()
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	defer statetesting.AssertStop(c, w)
    91  
    92  	wc := statetesting.NewNotifyWatcherC(c, s.state, w)
    93  
    94  	// Initial event.
    95  	wc.AssertOneChange()
    96  
    97  	// Change the state addresses and check that we get a notification
    98  	expectServerAddrs[0][0].Value = "0.1.99.99"
    99  
   100  	err = s.state.SetAPIHostPorts(expectServerAddrs)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  
   103  	wc.AssertOneChange()
   104  
   105  	statetesting.AssertStop(c, w)
   106  	wc.AssertClosed()
   107  }