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