~ubuntu-branches/ubuntu/saucy/juju-core/saucy-updates

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/cmd/juju/config_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-09-03 14:22:22 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20130903142222-9mes2r8wqr0bs7lp
Tags: 1.13.3-0ubuntu1
New upstream point release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2012, 2013 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package main
5
 
 
6
 
import (
7
 
        "bytes"
8
 
        "io/ioutil"
9
 
 
10
 
        gc "launchpad.net/gocheck"
11
 
        "launchpad.net/goyaml"
12
 
 
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"
17
 
)
18
 
 
19
 
// juju get and set tests (because one needs the other)
20
 
 
21
 
type ConfigSuite struct {
22
 
        testing.JujuConnSuite
23
 
}
24
 
 
25
 
var _ = gc.Suite(&ConfigSuite{})
26
 
 
27
 
var getTests = []struct {
28
 
        service  string
29
 
        expected map[string]interface{}
30
 
}{
31
 
        {
32
 
                "dummy-service",
33
 
                map[string]interface{}{
34
 
                        "service": "dummy-service",
35
 
                        "charm":   "dummy",
36
 
                        "settings": map[string]interface{}{
37
 
                                "title": map[string]interface{}{
38
 
                                        "description": "A descriptive title used for the service.",
39
 
                                        "type":        "string",
40
 
                                        "value":       "Nearly There",
41
 
                                },
42
 
                                "skill-level": map[string]interface{}{
43
 
                                        "description": "A number indicating skill.",
44
 
                                        "type":        "int",
45
 
                                        "default":     true,
46
 
                                        "value":       nil,
47
 
                                },
48
 
                                "username": map[string]interface{}{
49
 
                                        "description": "The name of the initial account (given admin permissions).",
50
 
                                        "type":        "string",
51
 
                                        "value":       "admin001",
52
 
                                        "default":     true,
53
 
                                },
54
 
                                "outlook": map[string]interface{}{
55
 
                                        "description": "No default outlook.",
56
 
                                        "type":        "string",
57
 
                                        "default":     true,
58
 
                                        "value":       nil,
59
 
                                },
60
 
                        },
61
 
                },
62
 
        },
63
 
 
64
 
        // TODO(dfc) add additional services (need more charms)
65
 
        // TODO(dfc) add set tests
66
 
}
67
 
 
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)
87
 
 
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)
92
 
        }
93
 
}
94
 
 
95
 
var setTests = []struct {
96
 
        about  string
97
 
        args   []string       // command to be executed
98
 
        expect charm.Settings // resulting configuration of the dummy service.
99
 
        err    string         // error regex
100
 
}{{
101
 
        about: "invalid option",
102
 
        args:  []string{"foo", "bar"},
103
 
        err:   "error: invalid option: \"foo\"\n",
104
 
}, {
105
 
        about: "whack option",
106
 
        args:  []string{"=bar"},
107
 
        err:   "error: invalid option: \"=bar\"\n",
108
 
}, {
109
 
        about: "--config missing",
110
 
        args:  []string{"--config", "missing.yaml"},
111
 
        err:   "error.*no such file or directory\n",
112
 
}, {
113
 
        about: "set with options",
114
 
        args:  []string{"username=hello"},
115
 
        expect: charm.Settings{
116
 
                "username": "hello",
117
 
        },
118
 
}, {
119
 
        about: "set with option values containing =",
120
 
        args:  []string{"username=hello=foo"},
121
 
        expect: charm.Settings{
122
 
                "username": "hello=foo",
123
 
        },
124
 
}, {
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
130
 
        },
131
 
},
132
 
}
133
 
 
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)
138
 
        dir := c.MkDir()
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)
145
 
                if t.err != "" {
146
 
                        c.Check(code, gc.Not(gc.Equals), 0)
147
 
                        c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, t.err)
148
 
                } else {
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)
153
 
                }
154
 
        }
155
 
}
156
 
 
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)
163
 
        return path
164
 
}