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