github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/plugins/juju-metadata/metadataplugin_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "flag" 8 "fmt" 9 "os" 10 "os/exec" 11 "strings" 12 stdtesting "testing" 13 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/juju/osenv" 17 "github.com/juju/juju/testing" 18 ) 19 20 func Test(t *stdtesting.T) { 21 gc.TestingT(t) 22 } 23 24 type MetadataSuite struct { 25 testing.FakeJujuHomeSuite 26 } 27 28 var _ = gc.Suite(&MetadataSuite{}) 29 30 var metadataCommandNames = []string{ 31 "generate-image", 32 "generate-tools", 33 "help", 34 "sign", 35 "validate-images", 36 "validate-tools", 37 } 38 39 var ( 40 flagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing") 41 ) 42 43 // Reentrancy point for testing (something as close as possible to) the juju 44 // tool itself. 45 func TestRunMain(t *stdtesting.T) { 46 if *flagRunMain { 47 Main(flag.Args()) 48 } 49 } 50 51 func badrun(c *gc.C, exit int, args ...string) string { 52 localArgs := append([]string{"-test.run", "TestRunMain", "-run-main", "--", "juju-metadata"}, args...) 53 54 ps := exec.Command(os.Args[0], localArgs...) 55 56 ps.Env = append(os.Environ(), osenv.JujuHomeEnvKey+"="+osenv.JujuHome()) 57 output, err := ps.CombinedOutput() 58 if exit != 0 { 59 c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit)) 60 } 61 return string(output) 62 } 63 64 func (s *MetadataSuite) TestHelpCommands(c *gc.C) { 65 // Check that we have correctly registered all the sub commands 66 // by checking the help output. 67 out := badrun(c, 0, "--help") 68 c.Log(out) 69 var names []string 70 commandHelp := strings.SplitAfter(out, "commands:")[1] 71 commandHelp = strings.TrimSpace(commandHelp) 72 for _, line := range strings.Split(commandHelp, "\n") { 73 names = append(names, strings.TrimSpace(strings.Split(line, " - ")[0])) 74 } 75 // The names should be output in alphabetical order, so don't sort. 76 c.Assert(names, gc.DeepEquals, metadataCommandNames) 77 } 78 79 func (s *MetadataSuite) assertHelpOutput(c *gc.C, cmd string) { 80 expected := fmt.Sprintf("usage: juju metadata %s [options]", cmd) 81 out := badrun(c, 0, cmd, "--help") 82 lines := strings.Split(out, "\n") 83 c.Assert(lines[0], gc.Equals, expected) 84 } 85 86 func (s *MetadataSuite) TestHelpValidateImages(c *gc.C) { 87 s.assertHelpOutput(c, "validate-images") 88 } 89 90 func (s *MetadataSuite) TestHelpValidateTools(c *gc.C) { 91 s.assertHelpOutput(c, "validate-tools") 92 } 93 94 func (s *MetadataSuite) TestHelpGenerateImage(c *gc.C) { 95 s.assertHelpOutput(c, "generate-image") 96 }