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

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package rsyslog_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/api"
    11  	"github.com/juju/juju/api/rsyslog"
    12  	"github.com/juju/juju/juju/testing"
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/state"
    15  	statetesting "github.com/juju/juju/state/testing"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type rsyslogSuite struct {
    20  	testing.JujuConnSuite
    21  
    22  	st      api.Connection
    23  	machine *state.Machine
    24  	rsyslog *rsyslog.State
    25  }
    26  
    27  var _ = gc.Suite(&rsyslogSuite{})
    28  
    29  func (s *rsyslogSuite) SetUpTest(c *gc.C) {
    30  	s.JujuConnSuite.SetUpTest(c)
    31  
    32  	s.st, s.machine = s.OpenAPIAsNewMachine(c, state.JobManageEnviron)
    33  	err := s.machine.SetProviderAddresses(network.NewAddress("0.1.2.3"))
    34  	c.Assert(err, jc.ErrorIsNil)
    35  
    36  	// Create the rsyslog API facade
    37  	s.rsyslog = s.st.Rsyslog()
    38  	c.Assert(s.rsyslog, gc.NotNil)
    39  }
    40  
    41  func (s *rsyslogSuite) TestGetRsyslogConfig(c *gc.C) {
    42  	err := s.APIState.Client().EnvironmentSet(map[string]interface{}{
    43  		"rsyslog-ca-cert": coretesting.CACert,
    44  		"rsyslog-ca-key":  coretesting.CAKey,
    45  	})
    46  	c.Assert(err, jc.ErrorIsNil)
    47  
    48  	cfg, err := s.rsyslog.GetRsyslogConfig(s.machine.Tag().String())
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	c.Assert(cfg, gc.NotNil)
    51  
    52  	c.Assert(cfg.CACert, gc.Equals, coretesting.CACert)
    53  	c.Assert(cfg.HostPorts, gc.HasLen, 1)
    54  	hostPort := cfg.HostPorts[0]
    55  	c.Assert(hostPort.Address.Value, gc.Equals, "0.1.2.3")
    56  
    57  	// the rsyslog port is set by the provider/dummy/environs.go
    58  	c.Assert(hostPort.Port, gc.Equals, 2345)
    59  }
    60  
    61  func (s *rsyslogSuite) TestWatchForRsyslogChanges(c *gc.C) {
    62  	w, err := s.rsyslog.WatchForRsyslogChanges(s.machine.Tag().String())
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	defer statetesting.AssertStop(c, w)
    65  
    66  	wc := statetesting.NewNotifyWatcherC(c, s.BackingState, w)
    67  	// Initial event
    68  	wc.AssertOneChange()
    69  
    70  	// change the API HostPorts
    71  	newHostPorts := network.NewHostPorts(6541, "127.0.0.1")
    72  	err = s.State.SetAPIHostPorts([][]network.HostPort{newHostPorts})
    73  	c.Assert(err, jc.ErrorIsNil)
    74  
    75  	// assert we get notified
    76  	wc.AssertOneChange()
    77  
    78  	statetesting.AssertStop(c, w)
    79  	wc.AssertClosed()
    80  }
    81  
    82  // SetRsyslogCACert is tested in apiserver/rsyslog