launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/juju/debuglog_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 gc "launchpad.net/gocheck" 8 9 "launchpad.net/juju-core/cmd" 10 "launchpad.net/juju-core/testing" 11 ) 12 13 type DebugLogSuite struct { 14 } 15 16 var _ = gc.Suite(&DebugLogSuite{}) 17 18 func runDebugLog(c *gc.C, args ...string) (*DebugLogCommand, error) { 19 cmd := &DebugLogCommand{ 20 sshCmd: &dummySSHCommand{}, 21 } 22 _, err := testing.RunCommand(c, cmd, args) 23 return cmd, err 24 } 25 26 type dummySSHCommand struct { 27 SSHCommand 28 runCalled bool 29 } 30 31 func (c *dummySSHCommand) Run(ctx *cmd.Context) error { 32 c.runCalled = true 33 return nil 34 } 35 36 // debug-log is implemented by invoking juju ssh with the correct arguments. 37 // This test helper checks for the expected invocation. 38 func (s *DebugLogSuite) assertDebugLogInvokesSSHCommand(c *gc.C, expected string, args ...string) { 39 defer testing.MakeEmptyFakeHome(c).Restore() 40 debugLogCmd, err := runDebugLog(c, args...) 41 c.Assert(err, gc.IsNil) 42 debugCmd := debugLogCmd.sshCmd.(*dummySSHCommand) 43 c.Assert(debugCmd.runCalled, gc.Equals, true) 44 c.Assert(debugCmd.Target, gc.Equals, "0") 45 c.Assert([]string{expected}, gc.DeepEquals, debugCmd.Args) 46 } 47 48 const logLocation = "/var/log/juju/all-machines.log" 49 50 func (s *DebugLogSuite) TestDebugLog(c *gc.C) { 51 const expected = "tail -n 10 -f " + logLocation 52 s.assertDebugLogInvokesSSHCommand(c, expected) 53 } 54 55 func (s *DebugLogSuite) TestDebugLogFrom(c *gc.C) { 56 const expected = "tail -n +1 -f " + logLocation 57 s.assertDebugLogInvokesSSHCommand(c, expected, "-n", "+1") 58 s.assertDebugLogInvokesSSHCommand(c, expected, "--lines=+1") 59 } 60 61 func (s *DebugLogSuite) TestDebugLogLast(c *gc.C) { 62 const expected = "tail -n 100 -f " + logLocation 63 s.assertDebugLogInvokesSSHCommand(c, expected, "-n", "100") 64 s.assertDebugLogInvokesSSHCommand(c, expected, "--lines=100") 65 } 66 67 func (s *DebugLogSuite) TestDebugLogValidation(c *gc.C) { 68 defer testing.MakeEmptyFakeHome(c).Restore() 69 _, err := runDebugLog(c, "-n", "0") 70 c.Assert(err, gc.ErrorMatches, "invalid value \"0\" for flag -n: invalid number of lines") 71 _, err = runDebugLog(c, "-n", "-1") 72 c.Assert(err, gc.ErrorMatches, "invalid value \"-1\" for flag -n: invalid number of lines") 73 _, err = runDebugLog(c, "-n", "fnord") 74 c.Assert(err, gc.ErrorMatches, "invalid value \"fnord\" for flag -n: invalid number of lines") 75 }