~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/runner/jujuc/opened-ports_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
77
Summary:
 
78
lists all ports or ranges opened by the unit
 
79
 
 
80
Details:
 
81
Each list entry has format <port>/<protocol> (e.g. "80/tcp") or
 
82
<from>-<to>/<protocol> (e.g. "8080-8088/udp").
 
83
`[1:])
 
84
}
 
85
 
 
86
func (s *OpenedPortsSuite) getContextAndOpenPorts(c *gc.C) *Context {
 
87
        hctx := s.GetHookContext(c, -1, "")
 
88
        hctx.OpenPorts("tcp", 80, 80)
 
89
        hctx.OpenPorts("tcp", 10, 20)
 
90
        hctx.OpenPorts("udp", 63, 63)
 
91
        hctx.OpenPorts("udp", 53, 55)
 
92
        return hctx
 
93
}
 
94
 
 
95
func (s *OpenedPortsSuite) runCommand(c *gc.C, hctx *Context, args ...string) (stdout, stderr string) {
 
96
        com, err := jujuc.NewCommand(hctx, cmdString("opened-ports"))
 
97
        c.Assert(err, jc.ErrorIsNil)
 
98
        ctx := testing.Context(c)
 
99
        code := cmd.Main(com, ctx, args)
 
100
        c.Assert(code, gc.Equals, 0)
 
101
        return bufferString(ctx.Stdout), bufferString(ctx.Stderr)
 
102
}