github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/rsyslog/rsyslog_common_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  	"crypto/tls"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	stdtesting "testing"
    12  	"time"
    13  
    14  	"github.com/juju/syslog"
    15  	"github.com/juju/testing"
    16  	jc "github.com/juju/testing/checkers"
    17  	gc "gopkg.in/check.v1"
    18  
    19  	"github.com/juju/juju/api"
    20  	jujutesting "github.com/juju/juju/juju/testing"
    21  	"github.com/juju/juju/network"
    22  	"github.com/juju/juju/state"
    23  	coretesting "github.com/juju/juju/testing"
    24  	"github.com/juju/juju/worker/rsyslog"
    25  )
    26  
    27  func TestPackage(t *stdtesting.T) {
    28  	coretesting.MgoTestPackage(t)
    29  }
    30  
    31  type RsyslogSuite struct {
    32  	jujutesting.JujuConnSuite
    33  
    34  	st       *api.State
    35  	machine  *state.Machine
    36  	dialTags []string
    37  }
    38  
    39  var _ = gc.Suite(&RsyslogSuite{})
    40  
    41  func waitForFile(c *gc.C, file string) {
    42  	timeout := time.After(coretesting.LongWait)
    43  	for {
    44  		select {
    45  		case <-timeout:
    46  			c.Fatalf("timed out waiting for %s to be written", file)
    47  		case <-time.After(coretesting.ShortWait):
    48  			if _, err := os.Stat(file); err == nil {
    49  				return
    50  			}
    51  		}
    52  	}
    53  }
    54  
    55  func (s *RsyslogSuite) SetUpSuite(c *gc.C) {
    56  	s.JujuConnSuite.SetUpSuite(c)
    57  	// TODO(waigani) 2014-03-19 bug 1294462
    58  	// Add patch for suite functions
    59  	restore := testing.PatchValue(rsyslog.LookupUser, func(username string) (uid, gid int, err error) {
    60  		// worker will not attempt to chown files if uid/gid is 0
    61  		return 0, 0, nil
    62  	})
    63  	s.AddSuiteCleanup(func(*gc.C) { restore() })
    64  }
    65  
    66  func (s *RsyslogSuite) SetUpTest(c *gc.C) {
    67  	s.JujuConnSuite.SetUpTest(c)
    68  	s.PatchValue(rsyslog.RestartRsyslog, func() error { return nil })
    69  	s.dialTags = nil
    70  	s.PatchValue(rsyslog.DialSyslog, func(network, raddr string, priority syslog.Priority, tag string, tlsCfg *tls.Config) (*syslog.Writer, error) {
    71  		s.dialTags = append(s.dialTags, tag)
    72  		return &syslog.Writer{}, nil
    73  	})
    74  	s.PatchValue(rsyslog.LogDir, c.MkDir())
    75  	s.PatchValue(rsyslog.RsyslogConfDir, c.MkDir())
    76  
    77  	s.st, s.machine = s.OpenAPIAsNewMachine(c, state.JobManageEnviron)
    78  	err := s.machine.SetAddresses(network.NewAddress("0.1.2.3", network.ScopeUnknown))
    79  	c.Assert(err, jc.ErrorIsNil)
    80  }
    81  
    82  func (s *RsyslogSuite) TestModeForwarding(c *gc.C) {
    83  	err := s.APIState.Client().EnvironmentSet(map[string]interface{}{"rsyslog-ca-cert": coretesting.CACert})
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	st, m := s.OpenAPIAsNewMachine(c, state.JobHostUnits)
    86  	addrs := []string{"0.1.2.3", "0.2.4.6"}
    87  	worker, err := rsyslog.NewRsyslogConfigWorker(st.Rsyslog(), rsyslog.RsyslogModeForwarding, m.Tag(), "foo", addrs)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  	defer func() { c.Assert(worker.Wait(), gc.IsNil) }()
    90  	defer worker.Kill()
    91  
    92  	// We should get a ca-cert.pem with the contents introduced into state config.
    93  	waitForFile(c, filepath.Join(*rsyslog.LogDir, "ca-cert.pem"))
    94  	caCertPEM, err := ioutil.ReadFile(filepath.Join(*rsyslog.LogDir, "ca-cert.pem"))
    95  	c.Assert(err, jc.ErrorIsNil)
    96  	c.Assert(string(caCertPEM), gc.DeepEquals, coretesting.CACert)
    97  
    98  	c.Assert(*rsyslog.SyslogTargets, gc.HasLen, 2)
    99  	for _, dialTag := range s.dialTags {
   100  		c.Assert(dialTag, gc.Equals, "juju-foo-"+m.Tag().String())
   101  	}
   102  }