github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/config-get_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 "encoding/json" 9 "io/ioutil" 10 "path/filepath" 11 12 "github.com/juju/cmd" 13 "github.com/juju/cmd/cmdtesting" 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 goyaml "gopkg.in/yaml.v2" 17 18 "github.com/juju/juju/worker/uniter/runner/jujuc" 19 ) 20 21 type ConfigGetSuite struct { 22 ContextSuite 23 } 24 25 var _ = gc.Suite(&ConfigGetSuite{}) 26 27 var configGetKeyTests = []struct { 28 args []string 29 out string 30 }{ 31 {[]string{"monsters"}, "False\n"}, 32 {[]string{"--format", "yaml", "monsters"}, "false\n"}, 33 {[]string{"--format", "json", "monsters"}, "false\n"}, 34 {[]string{"spline-reticulation"}, "45\n"}, 35 {[]string{"--format", "yaml", "spline-reticulation"}, "45\n"}, 36 {[]string{"--format", "json", "spline-reticulation"}, "45\n"}, 37 {[]string{"missing"}, ""}, 38 {[]string{"--format", "yaml", "missing"}, ""}, 39 {[]string{"--format", "json", "missing"}, "null\n"}, 40 } 41 42 func (s *ConfigGetSuite) TestOutputFormatKey(c *gc.C) { 43 for i, t := range configGetKeyTests { 44 c.Logf("test %d: %#v", i, t.args) 45 hctx := s.GetHookContext(c, -1, "") 46 com, err := jujuc.NewCommand(hctx, cmdString("config-get")) 47 c.Assert(err, jc.ErrorIsNil) 48 ctx := cmdtesting.Context(c) 49 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args) 50 c.Check(code, gc.Equals, 0) 51 c.Check(bufferString(ctx.Stderr), gc.Equals, "") 52 c.Check(bufferString(ctx.Stdout), gc.Matches, t.out) 53 } 54 } 55 56 var ( 57 configGetYamlMap = map[string]interface{}{ 58 "monsters": false, 59 "spline-reticulation": 45, 60 "title": "My Title", 61 "username": "admin001", 62 } 63 configGetJsonMap = map[string]interface{}{ 64 "monsters": false, 65 "spline-reticulation": 45.0, 66 "title": "My Title", 67 "username": "admin001", 68 } 69 configGetYamlMapAll = map[string]interface{}{ 70 "empty": nil, 71 "monsters": false, 72 "spline-reticulation": 45, 73 "title": "My Title", 74 "username": "admin001", 75 } 76 configGetJsonMapAll = map[string]interface{}{ 77 "empty": nil, 78 "monsters": false, 79 "spline-reticulation": 45.0, 80 "title": "My Title", 81 "username": "admin001", 82 } 83 ) 84 85 var configGetAllTests = []struct { 86 args []string 87 format int 88 out map[string]interface{} 89 }{ 90 {nil, formatYaml, configGetYamlMap}, 91 {[]string{"--format", "yaml"}, formatYaml, configGetYamlMap}, 92 {[]string{"--format", "json"}, formatJson, configGetJsonMap}, 93 {[]string{"--all", "--format", "yaml"}, formatYaml, configGetYamlMapAll}, 94 {[]string{"--all", "--format", "json"}, formatJson, configGetJsonMapAll}, 95 {[]string{"-a", "--format", "yaml"}, formatYaml, configGetYamlMapAll}, 96 {[]string{"-a", "--format", "json"}, formatJson, configGetJsonMapAll}, 97 } 98 99 func (s *ConfigGetSuite) TestOutputFormatAll(c *gc.C) { 100 for i, t := range configGetAllTests { 101 c.Logf("test %d: %#v", i, t.args) 102 hctx := s.GetHookContext(c, -1, "") 103 com, err := jujuc.NewCommand(hctx, cmdString("config-get")) 104 c.Assert(err, jc.ErrorIsNil) 105 ctx := cmdtesting.Context(c) 106 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args) 107 c.Assert(code, gc.Equals, 0) 108 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 109 110 out := map[string]interface{}{} 111 switch t.format { 112 case formatYaml: 113 c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil) 114 case formatJson: 115 c.Assert(json.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil) 116 } 117 c.Assert(out, gc.DeepEquals, t.out) 118 } 119 } 120 121 func (s *ConfigGetSuite) TestHelp(c *gc.C) { 122 hctx := s.GetHookContext(c, -1, "") 123 com, err := jujuc.NewCommand(hctx, cmdString("config-get")) 124 c.Assert(err, jc.ErrorIsNil) 125 ctx := cmdtesting.Context(c) 126 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"}) 127 c.Assert(code, gc.Equals, 0) 128 c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: config-get [options] [<key>] 129 130 Summary: 131 print application configuration 132 133 Options: 134 -a, --all (= false) 135 print all keys 136 --format (= smart) 137 Specify output format (json|smart|yaml) 138 -o, --output (= "") 139 Specify an output file 140 141 Details: 142 When no <key> is supplied, all keys with values or defaults are printed. If 143 --all is set, all known keys are printed; those without defaults or values are 144 reported as null. <key> and --all are mutually exclusive. 145 `) 146 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 147 } 148 149 func (s *ConfigGetSuite) TestOutputPath(c *gc.C) { 150 hctx := s.GetHookContext(c, -1, "") 151 com, err := jujuc.NewCommand(hctx, cmdString("config-get")) 152 c.Assert(err, jc.ErrorIsNil) 153 ctx := cmdtesting.Context(c) 154 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--output", "some-file", "monsters"}) 155 c.Assert(code, gc.Equals, 0) 156 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 157 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 158 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) 159 c.Assert(err, jc.ErrorIsNil) 160 c.Assert(string(content), gc.Equals, "False\n") 161 } 162 163 func (s *ConfigGetSuite) TestUnknownArg(c *gc.C) { 164 hctx := s.GetHookContext(c, -1, "") 165 com, err := jujuc.NewCommand(hctx, cmdString("config-get")) 166 c.Assert(err, jc.ErrorIsNil) 167 cmdtesting.TestInit(c, jujuc.NewJujucCommandWrappedForTest(com), []string{"multiple", "keys"}, `unrecognized args: \["keys"\]`) 168 } 169 170 func (s *ConfigGetSuite) TestAllPlusKey(c *gc.C) { 171 hctx := s.GetHookContext(c, -1, "") 172 com, err := jujuc.NewCommand(hctx, cmdString("config-get")) 173 c.Assert(err, jc.ErrorIsNil) 174 ctx := cmdtesting.Context(c) 175 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--all", "--format", "json", "monsters"}) 176 c.Assert(code, gc.Equals, 2) 177 c.Assert(bufferString(ctx.Stderr), gc.Equals, "ERROR cannot use argument --all together with key \"monsters\"\n") 178 }