github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/audit/auditlogfile_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package audit_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "runtime" 11 "time" 12 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/audit" 17 coretesting "github.com/juju/juju/testing" 18 "github.com/juju/testing" 19 ) 20 21 type auditLogFileSuite struct { 22 testing.IsolationSuite 23 } 24 25 var _ = gc.Suite(&auditLogFileSuite{}) 26 27 func (s *auditLogFileSuite) TestLogging(c *gc.C) { 28 dir := c.MkDir() 29 sink := audit.NewLogFileSink(dir) 30 31 modelUUID := coretesting.ModelTag.Id() 32 t0 := time.Date(2015, time.June, 1, 23, 2, 1, 0, time.UTC) 33 t1 := time.Date(2015, time.June, 1, 23, 2, 2, 0, time.UTC) 34 35 err := sink(audit.AuditEntry{ 36 Timestamp: t0, 37 ModelUUID: modelUUID, 38 RemoteAddress: "10.0.0.1", 39 OriginType: "API", 40 OriginName: "user-admin", 41 Operation: "deploy", 42 Data: map[string]interface{}{"foo": "bar"}, 43 }) 44 c.Assert(err, jc.ErrorIsNil) 45 err = sink(audit.AuditEntry{ 46 Timestamp: t1, 47 ModelUUID: modelUUID, 48 RemoteAddress: "10.0.0.2", 49 OriginType: "API", 50 OriginName: "user-admin", 51 Operation: "status", 52 }) 53 c.Assert(err, jc.ErrorIsNil) 54 55 // Check that the audit log file was populated as expected 56 logPath := filepath.Join(dir, "audit.log") 57 logContents, err := ioutil.ReadFile(logPath) 58 c.Assert(err, jc.ErrorIsNil) 59 line0 := "2015-06-01 23:02:01," + modelUUID + ",10.0.0.1,user-admin,API,deploy,map[foo:bar]\n" 60 line1 := "2015-06-01 23:02:02," + modelUUID + ",10.0.0.2,user-admin,API,status,map[]\n" 61 c.Assert(string(logContents), gc.Equals, line0+line1) 62 63 // Check the file mode is as expected. This doesn't work on 64 // Windows (but this code is very unlikely to run on Windows so 65 // it's ok). 66 if runtime.GOOS != "windows" { 67 info, err := os.Stat(logPath) 68 c.Assert(err, jc.ErrorIsNil) 69 c.Assert(info.Mode(), gc.Equals, os.FileMode(0600)) 70 } 71 }