github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/featuretests/cmd_juju_dumplogs_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package featuretests 5 6 import ( 7 "bufio" 8 "fmt" 9 "os" 10 "time" 11 12 "github.com/juju/loggo" 13 "github.com/juju/names" 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/agent" 18 agenttesting "github.com/juju/juju/cmd/jujud/agent/testing" 19 "github.com/juju/juju/cmd/jujud/dumplogs" 20 "github.com/juju/juju/state" 21 "github.com/juju/juju/testing" 22 "github.com/juju/juju/testing/factory" 23 ) 24 25 type dumpLogsCommandSuite struct { 26 agenttesting.AgentSuite 27 } 28 29 func (s *dumpLogsCommandSuite) SetUpTest(c *gc.C) { 30 s.AgentSuite.SetUpTest(c) 31 } 32 33 func (s *dumpLogsCommandSuite) TestRun(c *gc.C) { 34 // Create a controller machine and an agent for it. 35 m, password := s.Factory.MakeMachineReturningPassword(c, &factory.MachineParams{ 36 Jobs: []state.MachineJob{state.JobManageModel}, 37 Nonce: agent.BootstrapNonce, 38 }) 39 err := m.SetMongoPassword(password) 40 c.Assert(err, jc.ErrorIsNil) 41 42 s.PrimeStateAgent(c, m.Tag(), password) 43 44 // Create multiple environments and add some logs for each. 45 st1 := s.Factory.MakeModel(c, nil) 46 defer st1.Close() 47 st2 := s.Factory.MakeModel(c, nil) 48 defer st2.Close() 49 states := []*state.State{s.State, st1, st2} 50 51 t := time.Date(2015, 11, 4, 3, 2, 1, 0, time.UTC) 52 for _, st := range states { 53 w := state.NewDbLogger(st, names.NewMachineTag("42")) 54 defer w.Close() 55 for i := 0; i < 3; i++ { 56 err := w.Log(t, "module", "location", loggo.INFO, fmt.Sprintf("%d", i)) 57 c.Assert(err, jc.ErrorIsNil) 58 } 59 } 60 61 // Run the juju-dumplogs command 62 command := dumplogs.NewCommand() 63 context, err := testing.RunCommand(c, command, "--data-dir", s.DataDir()) 64 c.Assert(err, jc.ErrorIsNil) 65 66 // Check the log file for each environment 67 expectedLog := "machine-42: 2015-11-04 03:02:01 INFO module %d" 68 for _, st := range states { 69 logName := context.AbsPath(fmt.Sprintf("%s.log", st.ModelUUID())) 70 logFile, err := os.Open(logName) 71 c.Assert(err, jc.ErrorIsNil) 72 scanner := bufio.NewScanner(logFile) 73 for i := 0; scanner.Scan(); i++ { 74 c.Assert(scanner.Text(), gc.Equals, fmt.Sprintf(expectedLog, i)) 75 } 76 c.Assert(scanner.Err(), jc.ErrorIsNil) 77 } 78 }