github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/commands/cmd_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "flag" 8 "fmt" 9 "os" 10 "os/exec" 11 stdtesting "testing" 12 13 "github.com/juju/cmd/cmdtesting" 14 "github.com/juju/testing" 15 gc "gopkg.in/check.v1" 16 ) 17 18 // flagRunMain is used to indicate that the -run-main flag was used. 19 var flagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing") 20 21 type CmdSuite struct { 22 testing.IsolationSuite 23 } 24 25 var _ = gc.Suite(&CmdSuite{}) 26 27 func initSSHCommand(args ...string) (*sshCommand, error) { 28 com := &sshCommand{} 29 return com, cmdtesting.InitCommand(com, args) 30 } 31 32 func (*CmdSuite) TestSSHCommandInit(c *gc.C) { 33 // missing args 34 _, err := initSSHCommand() 35 c.Assert(err, gc.ErrorMatches, "no target name specified") 36 } 37 38 func initSCPCommand(args ...string) (*scpCommand, error) { 39 com := &scpCommand{} 40 return com, cmdtesting.InitCommand(com, args) 41 } 42 43 func (*CmdSuite) TestSCPCommandInit(c *gc.C) { 44 // missing args 45 _, err := initSCPCommand() 46 c.Assert(err, gc.ErrorMatches, "at least two arguments required") 47 48 // not enough args 49 _, err = initSCPCommand("mysql/0:foo") 50 c.Assert(err, gc.ErrorMatches, "at least two arguments required") 51 } 52 53 // Reentrancy point for testing (something as close as possible to) the juju 54 // tool itself. 55 func TestRunMain(t *stdtesting.T) { 56 if *flagRunMain { 57 os.Exit(Main(flag.Args())) 58 } 59 } 60 61 // badrun is used to run a command, check the exit code, and return the output. 62 func badrun(c *gc.C, exit int, args ...string) string { 63 localArgs := append([]string{"-test.run", "TestRunMain", "-run-main", "--", "juju"}, args...) 64 ps := exec.Command(os.Args[0], localArgs...) 65 output, err := ps.CombinedOutput() 66 c.Logf("command output: %q", output) 67 if exit != 0 { 68 c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit)) 69 } 70 return string(output) 71 }