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