github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/utils/exec/exec_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package exec_test 5 6 import ( 7 gc "launchpad.net/gocheck" 8 9 jc "launchpad.net/juju-core/testing/checkers" 10 "launchpad.net/juju-core/testing/testbase" 11 "launchpad.net/juju-core/utils/exec" 12 ) 13 14 type execSuite struct { 15 testbase.LoggingSuite 16 } 17 18 var _ = gc.Suite(&execSuite{}) 19 20 func (*execSuite) TestRunCommands(c *gc.C) { 21 newDir := c.MkDir() 22 23 for i, test := range []struct { 24 message string 25 commands string 26 workingDir string 27 environment []string 28 stdout string 29 stderr string 30 code int 31 }{ 32 { 33 message: "test stdout capture", 34 commands: "echo testing stdout", 35 stdout: "testing stdout\n", 36 }, { 37 message: "test stderr capture", 38 commands: "echo testing stderr >&2", 39 stderr: "testing stderr\n", 40 }, { 41 message: "test return code", 42 commands: "exit 42", 43 code: 42, 44 }, { 45 message: "test working dir", 46 commands: "pwd", 47 workingDir: newDir, 48 stdout: newDir + "\n", 49 }, { 50 message: "test environment", 51 commands: "echo $OMG_IT_WORKS", 52 environment: []string{"OMG_IT_WORKS=like magic"}, 53 stdout: "like magic\n", 54 }, 55 } { 56 c.Logf("%v: %s", i, test.message) 57 58 result, err := exec.RunCommands( 59 exec.RunParams{ 60 Commands: test.commands, 61 WorkingDir: test.workingDir, 62 Environment: test.environment, 63 }) 64 c.Assert(err, gc.IsNil) 65 c.Assert(string(result.Stdout), gc.Equals, test.stdout) 66 c.Assert(string(result.Stderr), gc.Equals, test.stderr) 67 c.Assert(result.Code, gc.Equals, test.code) 68 } 69 } 70 71 func (*execSuite) TestExecUnknownCommand(c *gc.C) { 72 result, err := exec.RunCommands( 73 exec.RunParams{ 74 Commands: "unknown-command", 75 }, 76 ) 77 c.Assert(err, gc.IsNil) 78 c.Assert(result.Stdout, gc.HasLen, 0) 79 c.Assert(string(result.Stderr), jc.Contains, "unknown-command: command not found") 80 // 127 is a special bash return code meaning command not found. 81 c.Assert(result.Code, gc.Equals, 127) 82 }