github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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 "sync" 12 stdtesting "testing" 13 "time" 14 15 "github.com/juju/syslog" 16 "github.com/juju/testing" 17 jc "github.com/juju/testing/checkers" 18 gc "gopkg.in/check.v1" 19 20 "github.com/juju/juju/api" 21 jujutesting "github.com/juju/juju/juju/testing" 22 "github.com/juju/juju/network" 23 "github.com/juju/juju/state" 24 coretesting "github.com/juju/juju/testing" 25 "github.com/juju/juju/worker/rsyslog" 26 ) 27 28 func TestPackage(t *stdtesting.T) { 29 coretesting.MgoTestPackage(t) 30 } 31 32 type RsyslogSuite struct { 33 jujutesting.JujuConnSuite 34 35 st api.Connection 36 machine *state.Machine 37 mu sync.Mutex // protects dialTags 38 dialTags []string 39 } 40 41 var _ = gc.Suite(&RsyslogSuite{}) 42 43 func waitForFile(c *gc.C, file string) { 44 timeout := time.After(coretesting.LongWait) 45 for { 46 select { 47 case <-timeout: 48 c.Fatalf("timed out waiting for %s to be written", file) 49 case <-time.After(coretesting.ShortWait): 50 if _, err := os.Stat(file); err == nil { 51 return 52 } 53 } 54 } 55 } 56 57 func (s *RsyslogSuite) SetUpSuite(c *gc.C) { 58 s.JujuConnSuite.SetUpSuite(c) 59 // TODO(waigani) 2014-03-19 bug 1294462 60 // Add patch for suite functions 61 restore := testing.PatchValue(rsyslog.LookupUser, func(username string) (uid, gid int, err error) { 62 // worker will not attempt to chown files if uid/gid is 0 63 return 0, 0, nil 64 }) 65 s.AddSuiteCleanup(func(*gc.C) { restore() }) 66 } 67 68 func (s *RsyslogSuite) SetUpTest(c *gc.C) { 69 s.JujuConnSuite.SetUpTest(c) 70 s.PatchValue(rsyslog.RestartRsyslog, func() error { return nil }) 71 s.PatchValue(rsyslog.DialSyslog, func(network, raddr string, priority syslog.Priority, tag string, tlsCfg *tls.Config) (*syslog.Writer, error) { 72 s.mu.Lock() 73 s.dialTags = append(s.dialTags, tag) 74 s.mu.Unlock() 75 return &syslog.Writer{}, nil 76 }) 77 s.PatchValue(rsyslog.LogDir, c.MkDir()) 78 s.PatchValue(rsyslog.RsyslogConfDir, c.MkDir()) 79 80 s.mu.Lock() 81 s.dialTags = nil 82 s.mu.Unlock() 83 s.st, s.machine = s.OpenAPIAsNewMachine(c, state.JobManageEnviron) 84 err := s.machine.SetProviderAddresses(network.NewAddress("0.1.2.3")) 85 c.Assert(err, jc.ErrorIsNil) 86 } 87 88 func (s *RsyslogSuite) TestModeForwarding(c *gc.C) { 89 err := s.APIState.Client().EnvironmentSet(map[string]interface{}{ 90 "rsyslog-ca-cert": coretesting.CACert, 91 "rsyslog-ca-key": coretesting.CAKey, 92 }) 93 c.Assert(err, jc.ErrorIsNil) 94 st, m := s.OpenAPIAsNewMachine(c, state.JobHostUnits) 95 addrs := []string{"0.1.2.3", "0.2.4.6"} 96 worker, err := rsyslog.NewRsyslogConfigWorker(st.Rsyslog(), rsyslog.RsyslogModeForwarding, m.Tag(), "foo", addrs, s.ConfDir()) 97 c.Assert(err, jc.ErrorIsNil) 98 defer func() { c.Assert(worker.Wait(), gc.IsNil) }() 99 defer worker.Kill() 100 101 // We should get a ca-cert.pem with the contents introduced into state config. 102 dirname := filepath.Join(s.ConfDir()+"-foo", "rsyslog") 103 waitForFile(c, filepath.Join(dirname, "ca-cert.pem")) 104 caCertPEM, err := ioutil.ReadFile(filepath.Join(dirname, "ca-cert.pem")) 105 c.Assert(err, jc.ErrorIsNil) 106 c.Assert(string(caCertPEM), gc.DeepEquals, coretesting.CACert) 107 108 c.Assert(*rsyslog.SyslogTargets, gc.HasLen, 2) 109 s.mu.Lock() 110 tags := s.dialTags 111 s.mu.Unlock() 112 for _, dialTag := range tags { 113 c.Check(dialTag, gc.Equals, "juju-foo-"+m.Tag().String()) 114 } 115 } 116 117 func (s *RsyslogSuite) TestNoNamespace(c *gc.C) { 118 err := s.APIState.Client().EnvironmentSet(map[string]interface{}{ 119 "rsyslog-ca-cert": coretesting.CACert, 120 "rsyslog-ca-key": coretesting.CAKey, 121 }) 122 c.Assert(err, jc.ErrorIsNil) 123 st, m := s.OpenAPIAsNewMachine(c, state.JobHostUnits) 124 addrs := []string{"0.1.2.3", "0.2.4.6"} 125 worker, err := rsyslog.NewRsyslogConfigWorker(st.Rsyslog(), rsyslog.RsyslogModeForwarding, m.Tag(), "", addrs, s.ConfDir()) 126 c.Assert(err, jc.ErrorIsNil) 127 defer func() { c.Assert(worker.Wait(), gc.IsNil) }() 128 defer worker.Kill() 129 130 // We should get a ca-cert.pem with the contents introduced into state config. 131 dirname := filepath.Join(s.ConfDir(), "rsyslog") 132 waitForFile(c, filepath.Join(dirname, "ca-cert.pem")) 133 caCertPEM, err := ioutil.ReadFile(filepath.Join(dirname, "ca-cert.pem")) 134 c.Assert(err, jc.ErrorIsNil) 135 c.Assert(string(caCertPEM), gc.DeepEquals, coretesting.CACert) 136 137 c.Assert(*rsyslog.SyslogTargets, gc.HasLen, 2) 138 s.mu.Lock() 139 tags := s.dialTags 140 s.mu.Unlock() 141 for _, dialTag := range tags { 142 c.Check(dialTag, gc.Equals, "juju-"+m.Tag().String()) 143 } 144 }