github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/relation-ids_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Copyright 2014 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package jujuc_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/cmd/cmdtesting"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  )
    17  
    18  type RelationIdsSuite struct {
    19  	relationSuite
    20  }
    21  
    22  var _ = gc.Suite(&RelationIdsSuite{})
    23  
    24  func (s *RelationIdsSuite) newHookContext(relid int, remote string) (jujuc.Context, *relationInfo) {
    25  	hctx, info := s.relationSuite.newHookContext(-1, "")
    26  	info.reset()
    27  	info.addRelatedApplications("x", 3)
    28  	info.addRelatedApplications("y", 1)
    29  	if relid >= 0 {
    30  		info.SetAsRelationHook(relid, remote)
    31  	}
    32  	return hctx, info
    33  }
    34  
    35  var relationIdsTests = []struct {
    36  	summary string
    37  	relid   int
    38  	args    []string
    39  	code    int
    40  	out     string
    41  }{
    42  	{
    43  		summary: "no default, no name",
    44  		relid:   -1,
    45  		code:    2,
    46  		out:     "(.|\n)*ERROR no relation name specified\n",
    47  	}, {
    48  		summary: "default name",
    49  		relid:   1,
    50  		out:     "x:0\nx:1\nx:2",
    51  	}, {
    52  		summary: "explicit name",
    53  		relid:   -1,
    54  		args:    []string{"x"},
    55  		out:     "x:0\nx:1\nx:2",
    56  	}, {
    57  		summary: "explicit different name",
    58  		relid:   -1,
    59  		args:    []string{"y"},
    60  		out:     "y:3",
    61  	}, {
    62  		summary: "nonexistent name",
    63  		relid:   -1,
    64  		args:    []string{"z"},
    65  	}, {
    66  		summary: "explicit smart formatting 1",
    67  		args:    []string{"--format", "smart", "x"},
    68  		out:     "x:0\nx:1\nx:2",
    69  	}, {
    70  		summary: "explicit smart formatting 2",
    71  		args:    []string{"--format", "smart", "y"},
    72  		out:     "y:3",
    73  	}, {
    74  		summary: "explicit smart formatting 3",
    75  		args:    []string{"--format", "smart", "z"},
    76  	}, {
    77  		summary: "json formatting 1",
    78  		args:    []string{"--format", "json", "x"},
    79  		out:     `["x:0","x:1","x:2"]`,
    80  	}, {
    81  		summary: "json formatting 2",
    82  		args:    []string{"--format", "json", "y"},
    83  		out:     `["y:3"]`,
    84  	}, {
    85  		summary: "json formatting 3",
    86  		args:    []string{"--format", "json", "z"},
    87  		out:     `[]`,
    88  	}, {
    89  		summary: "yaml formatting 1",
    90  		args:    []string{"--format", "yaml", "x"},
    91  		out:     "- x:0\n- x:1\n- x:2",
    92  	}, {
    93  		summary: "yaml formatting 2",
    94  		args:    []string{"--format", "yaml", "y"},
    95  		out:     "- y:3",
    96  	}, {
    97  		summary: "yaml formatting 3",
    98  		args:    []string{"--format", "yaml", "z"},
    99  		out:     "[]",
   100  	},
   101  }
   102  
   103  func (s *RelationIdsSuite) TestRelationIds(c *gc.C) {
   104  	for i, t := range relationIdsTests {
   105  		c.Logf("test %d: %s", i, t.summary)
   106  		hctx, _ := s.newHookContext(t.relid, "")
   107  		com, err := jujuc.NewCommand(hctx, cmdString("relation-ids"))
   108  		c.Assert(err, jc.ErrorIsNil)
   109  		ctx := cmdtesting.Context(c)
   110  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args)
   111  		c.Assert(code, gc.Equals, t.code)
   112  		if code == 0 {
   113  			c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   114  			expect := t.out
   115  			if expect != "" {
   116  				expect += "\n"
   117  			}
   118  			c.Assert(bufferString(ctx.Stdout), gc.Equals, expect)
   119  		} else {
   120  			c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   121  			c.Assert(bufferString(ctx.Stderr), gc.Matches, t.out)
   122  		}
   123  	}
   124  }
   125  
   126  func (s *RelationIdsSuite) TestHelp(c *gc.C) {
   127  	template := `
   128  Usage: %s
   129  
   130  Summary:
   131  list all relation ids with the given relation name
   132  
   133  Options:
   134  --format  (= smart)
   135      Specify output format (json|smart|yaml)
   136  -o, --output (= "")
   137      Specify an output file
   138  %s`[1:]
   139  
   140  	for relid, t := range map[int]struct {
   141  		usage, doc string
   142  	}{
   143  		-1: {"relation-ids [options] <name>", ""},
   144  		0:  {"relation-ids [options] [<name>]", "\nDetails:\nCurrent default relation name is \"x\".\n"},
   145  		3:  {"relation-ids [options] [<name>]", "\nDetails:\nCurrent default relation name is \"y\".\n"},
   146  	} {
   147  		c.Logf("relid %d", relid)
   148  		hctx, _ := s.newHookContext(relid, "")
   149  		com, err := jujuc.NewCommand(hctx, cmdString("relation-ids"))
   150  		c.Assert(err, jc.ErrorIsNil)
   151  		ctx := cmdtesting.Context(c)
   152  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"})
   153  		c.Assert(code, gc.Equals, 0)
   154  		expect := fmt.Sprintf(template, t.usage, t.doc)
   155  		c.Assert(bufferString(ctx.Stdout), gc.Equals, expect)
   156  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   157  	}
   158  }