github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 jc "github.com/juju/testing/checkers" 15 "github.com/juju/utils/set" 16 gc "gopkg.in/check.v1" 17 18 "github.com/juju/juju/feature" 19 "github.com/juju/juju/testing" 20 ) 21 22 func Test(t *stdtesting.T) { 23 gc.TestingT(t) 24 } 25 26 type MetadataSuite struct { 27 testing.FakeJujuXDGDataHomeSuite 28 } 29 30 var _ = gc.Suite(&MetadataSuite{}) 31 32 var metadataCommandNames = []string{ 33 "add-image", 34 "delete-image", 35 "generate-image", 36 "generate-tools", 37 "help", 38 "list-images", 39 "sign", 40 "validate-images", 41 "validate-tools", 42 } 43 44 var ( 45 flagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing") 46 ) 47 48 // Reentrancy point for testing (something as close as possible to) the juju 49 // tool itself. 50 func TestRunMain(t *stdtesting.T) { 51 if *flagRunMain { 52 Main(flag.Args()) 53 } 54 } 55 56 func badrun(c *gc.C, exit int, args ...string) string { 57 localArgs := append([]string{"-test.run", "TestRunMain", "-run-main", "--", "juju-metadata"}, args...) 58 59 ps := exec.Command(os.Args[0], localArgs...) 60 output, err := ps.CombinedOutput() 61 if exit != 0 { 62 c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit)) 63 } 64 return string(output) 65 } 66 67 func getHelpCommandNames(c *gc.C) []string { 68 out := badrun(c, 0, "--help") 69 c.Log(out) 70 var names []string 71 commandHelpStrings := strings.SplitAfter(out, "commands:") 72 c.Assert(len(commandHelpStrings), gc.Equals, 2) 73 commandHelp := strings.TrimSpace(commandHelpStrings[1]) 74 for _, line := range strings.Split(commandHelp, "\n") { 75 names = append(names, strings.TrimSpace(strings.Split(line, " - ")[0])) 76 } 77 return names 78 } 79 80 func (s *MetadataSuite) TestHelpCommands(c *gc.C) { 81 // Check that we have correctly registered all the sub commands 82 // by checking the help output. 83 84 // Remove add/list-image for the first test because the feature is not 85 // enabled by default. 86 devFeatures := set.NewStrings("add-image", "list-images", "delete-image") 87 88 // Remove features behind dev_flag for the first test since they are not 89 // enabled. 90 cmdSet := set.NewStrings(metadataCommandNames...).Difference(devFeatures) 91 92 // Test default commands. 93 // The names should be output in alphabetical order, so don't sort. 94 c.Assert(getHelpCommandNames(c), jc.SameContents, cmdSet.Values()) 95 96 // Enable development features, and test again. We should now see the 97 // development commands. 98 s.SetFeatureFlags(feature.ImageMetadata) 99 c.Assert(getHelpCommandNames(c), jc.SameContents, metadataCommandNames) 100 } 101 102 func (s *MetadataSuite) assertHelpOutput(c *gc.C, cmd string) { 103 expected := fmt.Sprintf("Usage: juju metadata %s [options]", cmd) 104 out := badrun(c, 0, cmd, "--help") 105 lines := strings.Split(out, "\n") 106 c.Assert(lines[0], gc.Equals, expected) 107 } 108 109 func (s *MetadataSuite) TestHelpValidateImages(c *gc.C) { 110 s.assertHelpOutput(c, "validate-images") 111 } 112 113 func (s *MetadataSuite) TestHelpValidateTools(c *gc.C) { 114 s.assertHelpOutput(c, "validate-tools") 115 } 116 117 func (s *MetadataSuite) TestHelpGenerateImage(c *gc.C) { 118 s.assertHelpOutput(c, "generate-image") 119 } 120 121 func (s *MetadataSuite) TestHelpListImages(c *gc.C) { 122 s.SetFeatureFlags(feature.ImageMetadata) 123 s.assertHelpOutput(c, "list-images") 124 } 125 126 func (s *MetadataSuite) TestHelpAddImage(c *gc.C) { 127 s.SetFeatureFlags(feature.ImageMetadata) 128 s.assertHelpOutput(c, "add-image") 129 } 130 131 func (s *MetadataSuite) TestHelpDeleteImage(c *gc.C) { 132 s.SetFeatureFlags(feature.ImageMetadata) 133 s.assertHelpOutput(c, "delete-image") 134 }