github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/v3" 11 "github.com/juju/cmd/v3/cmdtesting" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/core/life" 16 "github.com/juju/juju/worker/uniter/runner/jujuc" 17 ) 18 19 type RelationIdsSuite struct { 20 relationSuite 21 } 22 23 var _ = gc.Suite(&RelationIdsSuite{}) 24 25 func (s *RelationIdsSuite) newHookContext(relid int, remote string) (jujuc.Context, *relationInfo) { 26 hctx, info := s.relationSuite.newHookContext(-1, "", "") 27 info.reset() 28 info.addRelatedApplications("x", 3) 29 info.addRelatedApplications("y", 1) 30 if relid >= 0 { 31 info.SetAsRelationHook(relid, remote) 32 } 33 return hctx, info 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.newHookContext(t.relid, "") 108 com, err := jujuc.NewCommand(hctx, "relation-ids") 109 c.Assert(err, jc.ErrorIsNil) 110 ctx := cmdtesting.Context(c) 111 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(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 131 Summary: 132 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>]", "\nDetails:\nCurrent default relation name is \"x\".\n"}, 146 3: {"relation-ids [options] [<name>]", "\nDetails:\nCurrent default relation name is \"y\".\n"}, 147 } { 148 c.Logf("relid %d", relid) 149 hctx, _ := s.newHookContext(relid, "") 150 com, err := jujuc.NewCommand(hctx, "relation-ids") 151 c.Assert(err, jc.ErrorIsNil) 152 ctx := cmdtesting.Context(c) 153 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(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 } 160 161 func (s *RelationIdsSuite) TestFilterNonLiveRelations(c *gc.C) { 162 hctx, info := s.newHookContext(1, "") 163 164 // Relations starts as alive so they all will end up in the output. 165 exp := "x:0\nx:1\nx:2\n" 166 s.assertOutputMatches(c, hctx, exp) 167 168 // Change relation 1 life to dying and ensure it still shows up in the output 169 info.rels[1].Life = life.Dying 170 exp = "x:0\nx:1\nx:2\n" 171 s.assertOutputMatches(c, hctx, exp) 172 173 // Change relation 0 life to dead and ensure it doesn't show up in the output 174 info.rels[0].Life = life.Dead 175 exp = "x:1\nx:2\n" 176 s.assertOutputMatches(c, hctx, exp) 177 } 178 179 func (s *RelationIdsSuite) assertOutputMatches(c *gc.C, hctx jujuc.Context, expOutput string) { 180 com, err := jujuc.NewCommand(hctx, "relation-ids") 181 c.Assert(err, jc.ErrorIsNil) 182 ctx := cmdtesting.Context(c) 183 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, nil) 184 c.Assert(code, gc.Equals, 0) 185 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 186 c.Assert(bufferString(ctx.Stdout), gc.Equals, expOutput) 187 }