github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 lines := strings.Split(out, "\n") 69 var names []string 70 for _, line := range lines { 71 f := strings.Fields(line) 72 if len(f) == 0 || !strings.HasPrefix(line, " ") { 73 continue 74 } 75 names = append(names, f[0]) 76 } 77 // The names should be output in alphabetical order, so don't sort. 78 c.Assert(names, gc.DeepEquals, metadataCommandNames) 79 } 80 81 func (s *MetadataSuite) assertHelpOutput(c *gc.C, cmd string) { 82 expected := fmt.Sprintf("usage: juju metadata %s [options]", cmd) 83 out := badrun(c, 0, cmd, "--help") 84 lines := strings.Split(out, "\n") 85 c.Assert(lines[0], gc.Equals, expected) 86 } 87 88 func (s *MetadataSuite) TestHelpValidateImages(c *gc.C) { 89 s.assertHelpOutput(c, "validate-images") 90 } 91 92 func (s *MetadataSuite) TestHelpValidateTools(c *gc.C) { 93 s.assertHelpOutput(c, "validate-tools") 94 } 95 96 func (s *MetadataSuite) TestHelpGenerateImage(c *gc.C) { 97 s.assertHelpOutput(c, "generate-image") 98 }