github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/action-set_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/cmd" 10 "github.com/juju/cmd/cmdtesting" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/worker/uniter/runner/jujuc" 15 "github.com/juju/juju/worker/uniter/runner/jujuc/jujuctesting" 16 ) 17 18 var _ = gc.Suite(&ActionSetSuite{}) 19 20 type ActionSetSuite struct { 21 jujuctesting.ContextSuite 22 } 23 24 type actionSettingContext struct { 25 jujuc.Context 26 commands [][]string 27 } 28 29 func (a *actionSettingContext) UpdateActionResults(keys []string, value string) error { 30 if a.commands == nil { 31 a.commands = make([][]string, 0) 32 } 33 34 a.commands = append(a.commands, append(keys, value)) 35 return nil 36 } 37 38 type nonActionSettingContext struct { 39 jujuc.Context 40 } 41 42 func (a *nonActionSettingContext) UpdateActionResults(keys []string, value string) error { 43 return fmt.Errorf("not running an action") 44 } 45 46 func (s *ActionSetSuite) TestActionSetOnNonActionContextFails(c *gc.C) { 47 hctx := &nonActionSettingContext{} 48 com, err := jujuc.NewCommand(hctx, cmdString("action-set")) 49 c.Assert(err, jc.ErrorIsNil) 50 ctx := cmdtesting.Context(c) 51 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"oops=nope"}) 52 c.Check(code, gc.Equals, 1) 53 c.Check(bufferString(ctx.Stdout), gc.Equals, "") 54 expect := fmt.Sprintf(`(\n)*ERROR %s\n`, "not running an action") 55 c.Check(bufferString(ctx.Stderr), gc.Matches, expect) 56 } 57 58 func (s *ActionSetSuite) TestActionSet(c *gc.C) { 59 var actionSetTests = []struct { 60 summary string 61 command []string 62 expected [][]string 63 errMsg string 64 code int 65 }{{ 66 summary: "bare value(s) are an Init error", 67 command: []string{"result"}, 68 errMsg: "ERROR argument \"result\" must be of the form key...=value\n", 69 code: 2, 70 }, { 71 summary: "invalid keys are an error", 72 command: []string{"result-Value=5"}, 73 errMsg: "ERROR key \"result-Value\" must start and end with lowercase alphanumeric, and contain only lowercase alphanumeric, hyphens and periods\n", 74 code: 2, 75 }, { 76 summary: "empty values are not an error", 77 command: []string{"result="}, 78 expected: [][]string{ 79 {"result", ""}, 80 }, 81 }, { 82 summary: "a response of one key to one value", 83 command: []string{"outfile=foo.bz2"}, 84 expected: [][]string{ 85 {"outfile", "foo.bz2"}, 86 }, 87 }, { 88 summary: "two keys, two values", 89 command: []string{"outfile=foo.bz2", "size=10G"}, 90 expected: [][]string{ 91 {"outfile", "foo.bz2"}, 92 {"size", "10G"}, 93 }, 94 }, { 95 summary: "multiple = are ok", 96 command: []string{"outfile=foo=bz2"}, 97 expected: [][]string{ 98 {"outfile", "foo=bz2"}, 99 }, 100 }, { 101 summary: "several interleaved values", 102 command: []string{"outfile.name=foo.bz2", 103 "outfile.kind.util=bzip2", 104 "outfile.kind.ratio=high"}, 105 expected: [][]string{ 106 {"outfile", "name", "foo.bz2"}, 107 {"outfile", "kind", "util", "bzip2"}, 108 {"outfile", "kind", "ratio", "high"}, 109 }, 110 }, { 111 summary: "conflicting simple values", 112 command: []string{"util=bzip2", "util=5"}, 113 expected: [][]string{ 114 {"util", "bzip2"}, 115 {"util", "5"}, 116 }, 117 }, { 118 summary: "conflicted map spec: {map1:{key:val}} vs {map1:val2}", 119 command: []string{"map1.key=val", "map1=val"}, 120 expected: [][]string{ 121 {"map1", "key", "val"}, 122 {"map1", "val"}, 123 }, 124 }} 125 126 for i, t := range actionSetTests { 127 c.Logf("test %d: %s", i, t.summary) 128 hctx := &actionSettingContext{} 129 com, err := jujuc.NewCommand(hctx, cmdString("action-set")) 130 c.Assert(err, jc.ErrorIsNil) 131 ctx := cmdtesting.Context(c) 132 c.Logf(" command list: %#v", t.command) 133 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.command) 134 c.Check(code, gc.Equals, t.code) 135 c.Check(bufferString(ctx.Stderr), gc.Equals, t.errMsg) 136 c.Check(hctx.commands, jc.DeepEquals, t.expected) 137 } 138 } 139 140 func (s *ActionSetSuite) TestHelp(c *gc.C) { 141 hctx := &actionSettingContext{} 142 com, err := jujuc.NewCommand(hctx, cmdString("action-set")) 143 c.Assert(err, jc.ErrorIsNil) 144 ctx := cmdtesting.Context(c) 145 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"}) 146 c.Assert(code, gc.Equals, 0) 147 c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: action-set <key>=<value> [<key>=<value> ...] 148 149 Summary: 150 set action results 151 152 Details: 153 action-set adds the given values to the results map of the Action. This map 154 is returned to the user after the completion of the Action. Keys must start 155 and end with lowercase alphanumeric, and contain only lowercase alphanumeric, 156 hyphens and periods. 157 158 Example usage: 159 action-set outfile.size=10G 160 action-set foo.bar=2 161 action-set foo.baz.val=3 162 action-set foo.bar.zab=4 163 action-set foo.baz=1 164 165 will yield: 166 167 outfile: 168 size: "10G" 169 foo: 170 bar: 171 zab: "4" 172 baz: "1" 173 `) 174 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 175 }