github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	"add-image",
    32  	"generate-image",
    33  	"generate-tools",
    34  	"help",
    35  	"list-images",
    36  	"sign",
    37  	"validate-images",
    38  	"validate-tools",
    39  }
    40  
    41  var (
    42  	flagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing")
    43  )
    44  
    45  // Reentrancy point for testing (something as close as possible to) the juju
    46  // tool itself.
    47  func TestRunMain(t *stdtesting.T) {
    48  	if *flagRunMain {
    49  		Main(flag.Args())
    50  	}
    51  }
    52  
    53  func badrun(c *gc.C, exit int, args ...string) string {
    54  	localArgs := append([]string{"-test.run", "TestRunMain", "-run-main", "--", "juju-metadata"}, args...)
    55  
    56  	ps := exec.Command(os.Args[0], localArgs...)
    57  
    58  	ps.Env = append(os.Environ(), osenv.JujuHomeEnvKey+"="+osenv.JujuHome())
    59  	output, err := ps.CombinedOutput()
    60  	if exit != 0 {
    61  		c.Assert(err, gc.ErrorMatches, fmt.Sprintf("exit status %d", exit))
    62  	}
    63  	return string(output)
    64  }
    65  
    66  func (s *MetadataSuite) TestHelpCommands(c *gc.C) {
    67  	// Check that we have correctly registered all the sub commands
    68  	// by checking the help output.
    69  	out := badrun(c, 0, "--help")
    70  	c.Log(out)
    71  	var names []string
    72  	commandHelp := strings.SplitAfter(out, "commands:")[1]
    73  	commandHelp = strings.TrimSpace(commandHelp)
    74  	for _, line := range strings.Split(commandHelp, "\n") {
    75  		names = append(names, strings.TrimSpace(strings.Split(line, " - ")[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  }
    99  
   100  func (s *MetadataSuite) TestHelpListImages(c *gc.C) {
   101  	s.assertHelpOutput(c, "list-images")
   102  }
   103  
   104  func (s *MetadataSuite) TestHelpAddImage(c *gc.C) {
   105  	s.assertHelpOutput(c, "add-image")
   106  }