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