github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/state-get_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "github.com/juju/cmd/v3" 8 "github.com/juju/cmd/v3/cmdtesting" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/worker/uniter/runner/jujuc" 13 ) 14 15 type stateGetSuite struct { 16 stateSuite 17 } 18 19 var _ = gc.Suite(&stateGetSuite{}) 20 21 func (s *stateGetSuite) TestHelp(c *gc.C) { 22 toolCmd, err := jujuc.NewCommand(nil, "state-get") 23 c.Assert(err, jc.ErrorIsNil) 24 25 ctx := cmdtesting.Context(c) 26 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(toolCmd), ctx, []string{"--help"}) 27 c.Check(code, gc.Equals, 0) 28 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 29 30 var expectedHelp = ` 31 Usage: state-get [options] [<key>] 32 33 Summary: 34 print server-side-state value 35 36 Options: 37 --format (= smart) 38 Specify output format (json|smart|yaml) 39 -o, --output (= "") 40 Specify an output file 41 --strict (= false) 42 Return an error if the requested key does not exist 43 44 Details: 45 state-get prints the value of the server side state specified by key. 46 If no key is given, or if the key is "-", all keys and values will be printed. 47 48 See also: 49 state-delete 50 state-set 51 `[1:] 52 c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp) 53 } 54 55 type runStateGetCmd struct { 56 description string 57 args []string 58 out string 59 err string 60 code int 61 expect func() 62 } 63 64 func (s *stateGetSuite) TestStateGet(c *gc.C) { 65 runStateGetCmdTests := []runStateGetCmd{ 66 { 67 description: "get all values with no args", 68 args: nil, 69 out: "one: two\n" + "three: four\n", 70 expect: s.expectStateGetTwo, 71 }, 72 { 73 description: "get all values with -", 74 args: []string{"-"}, 75 out: "one: two\n" + "three: four\n", 76 expect: s.expectStateGetTwo, 77 }, 78 { 79 description: "get value of key", 80 args: []string{"one"}, 81 out: "two\n", 82 expect: s.expectStateGetValueOne, 83 }, 84 { 85 description: "key not found, give me the error", 86 args: []string{"--strict", "five"}, 87 err: "ERROR \"five\" not found\n", 88 out: "", 89 expect: s.expectStateGetValueNotFound, 90 code: 1, 91 }, 92 { 93 description: "key not found", 94 args: []string{"five"}, 95 err: "", 96 out: "", 97 expect: s.expectStateGetValueNotFound, 98 }, 99 { 100 description: "empty result", 101 args: []string{"five"}, 102 err: "", 103 out: "", 104 expect: s.expectStateGetValueEmpty, 105 }, 106 } 107 108 for i, test := range runStateGetCmdTests { 109 c.Logf("test %d of %d: %s", i+1, len(runStateGetCmdTests), test.description) 110 defer s.setupMocks(c).Finish() 111 test.expect() 112 113 toolCmd, err := jujuc.NewCommand(s.mockContext, "state-get") 114 c.Assert(err, jc.ErrorIsNil) 115 116 ctx := cmdtesting.Context(c) 117 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(toolCmd), ctx, test.args) 118 c.Check(code, gc.Equals, test.code) 119 c.Assert(bufferString(ctx.Stderr), gc.Equals, test.err) 120 c.Assert(bufferString(ctx.Stdout), gc.Equals, test.out) 121 } 122 }