github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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.State 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) 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 waitForFile(c, filepath.Join(*rsyslog.LogDir, "ca-cert.pem")) 103 caCertPEM, err := ioutil.ReadFile(filepath.Join(*rsyslog.LogDir, "ca-cert.pem")) 104 c.Assert(err, jc.ErrorIsNil) 105 c.Assert(string(caCertPEM), gc.DeepEquals, coretesting.CACert) 106 107 c.Assert(*rsyslog.SyslogTargets, gc.HasLen, 2) 108 s.mu.Lock() 109 s.mu.Unlock() // assert read barrier before accessing s.dialTags 110 for _, dialTag := range s.dialTags { 111 c.Assert(dialTag, gc.Equals, "juju-foo-"+m.Tag().String()) 112 } 113 }