github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/runner/jujuc/opened-ports_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/testing"
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  )
    17  
    18  type OpenedPortsSuite struct {
    19  	ContextSuite
    20  }
    21  
    22  var _ = gc.Suite(&OpenedPortsSuite{})
    23  
    24  func (s *OpenedPortsSuite) TestRunAllFormats(c *gc.C) {
    25  	expectedPorts := []network.PortRange{
    26  		{10, 20, "tcp"},
    27  		{80, 80, "tcp"},
    28  		{53, 55, "udp"},
    29  		{63, 63, "udp"},
    30  	}
    31  	network.SortPortRanges(expectedPorts)
    32  	portsAsStrings := make([]string, len(expectedPorts))
    33  	for i, portRange := range expectedPorts {
    34  		portsAsStrings[i] = portRange.String()
    35  	}
    36  	defaultOutput := strings.Join(portsAsStrings, "\n") + "\n"
    37  	jsonOutput := `["` + strings.Join(portsAsStrings, `","`) + `"]` + "\n"
    38  	yamlOutput := "- " + strings.Join(portsAsStrings, "\n- ") + "\n"
    39  
    40  	formatToOutput := map[string]string{
    41  		"":      defaultOutput,
    42  		"smart": defaultOutput,
    43  		"json":  jsonOutput,
    44  		"yaml":  yamlOutput,
    45  	}
    46  	for format, expectedOutput := range formatToOutput {
    47  		hctx := s.getContextAndOpenPorts(c)
    48  		stdout := ""
    49  		stderr := ""
    50  		if format == "" {
    51  			stdout, stderr = s.runCommand(c, hctx)
    52  		} else {
    53  			stdout, stderr = s.runCommand(c, hctx, "--format", format)
    54  		}
    55  		c.Check(stdout, gc.Equals, expectedOutput)
    56  		c.Check(stderr, gc.Equals, "")
    57  		hctx.info.CheckPorts(c, expectedPorts)
    58  	}
    59  }
    60  
    61  func (s *OpenedPortsSuite) TestBadArgs(c *gc.C) {
    62  	hctx := s.GetHookContext(c, -1, "")
    63  	com, err := jujuc.NewCommand(hctx, cmdString("opened-ports"))
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	err = testing.InitCommand(com, []string{"foo"})
    66  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["foo"\]`)
    67  }
    68  
    69  func (s *OpenedPortsSuite) TestHelp(c *gc.C) {
    70  	hctx := s.GetHookContext(c, -1, "")
    71  	openedPorts, err := jujuc.NewCommand(hctx, cmdString("opened-ports"))
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	flags := testing.NewFlagSet()
    74  	c.Assert(string(openedPorts.Info().Help(flags)), gc.Equals, `
    75  usage: opened-ports
    76  purpose: lists all ports or ranges opened by the unit
    77  
    78  Each list entry has format <port>/<protocol> (e.g. "80/tcp") or
    79  <from>-<to>/<protocol> (e.g. "8080-8088/udp").
    80  `[1:])
    81  }
    82  
    83  func (s *OpenedPortsSuite) getContextAndOpenPorts(c *gc.C) *Context {
    84  	hctx := s.GetHookContext(c, -1, "")
    85  	hctx.OpenPorts("tcp", 80, 80)
    86  	hctx.OpenPorts("tcp", 10, 20)
    87  	hctx.OpenPorts("udp", 63, 63)
    88  	hctx.OpenPorts("udp", 53, 55)
    89  	return hctx
    90  }
    91  
    92  func (s *OpenedPortsSuite) runCommand(c *gc.C, hctx *Context, args ...string) (stdout, stderr string) {
    93  	com, err := jujuc.NewCommand(hctx, cmdString("opened-ports"))
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	ctx := testing.Context(c)
    96  	code := cmd.Main(com, ctx, args)
    97  	c.Assert(code, gc.Equals, 0)
    98  	return bufferString(ctx.Stdout), bufferString(ctx.Stderr)
    99  }