launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/uniter/jujuc/relation-ids_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"fmt"
     8  	gc "launchpad.net/gocheck"
     9  
    10  	"launchpad.net/juju-core/cmd"
    11  	"launchpad.net/juju-core/testing"
    12  	"launchpad.net/juju-core/worker/uniter/jujuc"
    13  )
    14  
    15  type RelationIdsSuite struct {
    16  	ContextSuite
    17  }
    18  
    19  var _ = gc.Suite(&RelationIdsSuite{})
    20  
    21  func (s *RelationIdsSuite) SetUpTest(c *gc.C) {
    22  	s.ContextSuite.SetUpTest(c)
    23  	s.rels = map[int]*ContextRelation{}
    24  	s.AddRelatedServices(c, "x", 3)
    25  	s.AddRelatedServices(c, "y", 1)
    26  }
    27  
    28  func (s *RelationIdsSuite) AddRelatedServices(c *gc.C, relname string, count int) {
    29  	for i := 0; i < count; i++ {
    30  		id := len(s.rels)
    31  		s.rels[id] = &ContextRelation{id, relname, nil}
    32  	}
    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.GetHookContext(c, t.relid, "")
   107  		com, err := jujuc.NewCommand(hctx, "relation-ids")
   108  		c.Assert(err, gc.IsNil)
   109  		ctx := testing.Context(c)
   110  		code := cmd.Main(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  purpose: list all relation ids with the given relation name
   130  
   131  options:
   132  --format  (= smart)
   133      specify output format (json|smart|yaml)
   134  -o, --output (= "")
   135      specify an output file
   136  %s`[1:]
   137  
   138  	for relid, t := range map[int]struct {
   139  		usage, doc string
   140  	}{
   141  		-1: {"relation-ids [options] <name>", ""},
   142  		0:  {"relation-ids [options] [<name>]", "\nCurrent default relation name is \"x\".\n"},
   143  		3:  {"relation-ids [options] [<name>]", "\nCurrent default relation name is \"y\".\n"},
   144  	} {
   145  		c.Logf("relid %d", relid)
   146  		hctx := s.GetHookContext(c, relid, "")
   147  		com, err := jujuc.NewCommand(hctx, "relation-ids")
   148  		c.Assert(err, gc.IsNil)
   149  		ctx := testing.Context(c)
   150  		code := cmd.Main(com, ctx, []string{"--help"})
   151  		c.Assert(code, gc.Equals, 0)
   152  		expect := fmt.Sprintf(template, t.usage, t.doc)
   153  		c.Assert(bufferString(ctx.Stdout), gc.Equals, expect)
   154  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   155  	}
   156  }