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