1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
gc "launchpad.net/gocheck"
11
"launchpad.net/goyaml"
13
"launchpad.net/juju-core/charm"
14
"launchpad.net/juju-core/cmd"
15
"launchpad.net/juju-core/juju/testing"
16
coretesting "launchpad.net/juju-core/testing"
19
// juju get and set tests (because one needs the other)
21
type ConfigSuite struct {
25
var _ = gc.Suite(&ConfigSuite{})
27
var getTests = []struct {
29
expected map[string]interface{}
33
map[string]interface{}{
34
"service": "dummy-service",
36
"settings": map[string]interface{}{
37
"title": map[string]interface{}{
38
"description": "A descriptive title used for the service.",
40
"value": "Nearly There",
42
"skill-level": map[string]interface{}{
43
"description": "A number indicating skill.",
48
"username": map[string]interface{}{
49
"description": "The name of the initial account (given admin permissions).",
54
"outlook": map[string]interface{}{
55
"description": "No default outlook.",
64
// TODO(dfc) add additional services (need more charms)
65
// TODO(dfc) add set tests
68
func (s *ConfigSuite) TestGetConfig(c *gc.C) {
69
sch := s.AddTestingCharm(c, "dummy")
70
svc, err := s.State.AddService("dummy-service", sch)
71
c.Assert(err, gc.IsNil)
72
err = svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"})
73
c.Assert(err, gc.IsNil)
74
for _, t := range getTests {
75
ctx := coretesting.Context(c)
76
code := cmd.Main(&GetCommand{}, ctx, []string{t.service})
77
c.Check(code, gc.Equals, 0)
78
c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
79
// round trip via goyaml to avoid being sucked into a quagmire of
80
// map[interface{}]interface{} vs map[string]interface{}. This is
81
// also required if we add json support to this command.
82
buf, err := goyaml.Marshal(t.expected)
83
c.Assert(err, gc.IsNil)
84
expected := make(map[string]interface{})
85
err = goyaml.Unmarshal(buf, &expected)
86
c.Assert(err, gc.IsNil)
88
actual := make(map[string]interface{})
89
err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
90
c.Assert(err, gc.IsNil)
91
c.Assert(actual, gc.DeepEquals, expected)
95
var setTests = []struct {
97
args []string // command to be executed
98
expect charm.Settings // resulting configuration of the dummy service.
99
err string // error regex
101
about: "invalid option",
102
args: []string{"foo", "bar"},
103
err: "error: invalid option: \"foo\"\n",
105
about: "whack option",
106
args: []string{"=bar"},
107
err: "error: invalid option: \"=bar\"\n",
109
about: "--config missing",
110
args: []string{"--config", "missing.yaml"},
111
err: "error.*no such file or directory\n",
113
about: "set with options",
114
args: []string{"username=hello"},
115
expect: charm.Settings{
119
about: "set with option values containing =",
120
args: []string{"username=hello=foo"},
121
expect: charm.Settings{
122
"username": "hello=foo",
125
about: "--config $FILE test",
126
args: []string{"--config", "testconfig.yaml"},
127
expect: charm.Settings{
128
"username": "admin001",
129
"skill-level": int64(9000), // charm int types are int64
134
func (s *ConfigSuite) TestSetConfig(c *gc.C) {
135
sch := s.AddTestingCharm(c, "dummy")
136
svc, err := s.State.AddService("dummy-service", sch)
137
c.Assert(err, gc.IsNil)
139
setupConfigfile(c, dir)
140
for i, t := range setTests {
141
args := append([]string{"dummy-service"}, t.args...)
142
c.Logf("test %d. %s", i, t.about)
143
ctx := coretesting.ContextForDir(c, dir)
144
code := cmd.Main(&SetCommand{}, ctx, args)
146
c.Check(code, gc.Not(gc.Equals), 0)
147
c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, t.err)
149
c.Check(code, gc.Equals, 0)
150
settings, err := svc.ConfigSettings()
151
c.Assert(err, gc.IsNil)
152
c.Assert(settings, gc.DeepEquals, t.expect)
157
func setupConfigfile(c *gc.C, dir string) string {
158
ctx := coretesting.ContextForDir(c, dir)
159
path := ctx.AbsPath("testconfig.yaml")
160
content := []byte("dummy-service:\n skill-level: 9000\n username: admin001\n\n")
161
err := ioutil.WriteFile(path, content, 0666)
162
c.Assert(err, gc.IsNil)