1
// Copyright 2012-2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package application_test
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
12
goyaml "gopkg.in/yaml.v2"
14
"github.com/juju/juju/cmd/juju/application"
15
coretesting "github.com/juju/juju/testing"
18
type GetSuite struct {
19
coretesting.FakeJujuXDGDataHomeSuite
23
var _ = gc.Suite(&GetSuite{})
25
var getTests = []struct {
27
expected map[string]interface{}
31
map[string]interface{}{
32
"application": "dummy-application",
34
"settings": map[string]interface{}{
35
"title": map[string]interface{}{
36
"description": "Specifies title",
38
"value": "Nearly There",
40
"skill-level": map[string]interface{}{
41
"description": "Specifies skill-level",
45
"username": map[string]interface{}{
46
"description": "Specifies username",
50
"outlook": map[string]interface{}{
51
"description": "Specifies outlook",
59
// TODO(dfc) add additional services (need more charms)
60
// TODO(dfc) add set tests
63
func (s *GetSuite) SetUpTest(c *gc.C) {
64
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
65
s.fake = &fakeServiceAPI{serviceName: "dummy-application", charmName: "dummy",
66
values: map[string]interface{}{
67
"title": "Nearly There",
69
"username": "admin001",
74
func (s *GetSuite) TestGetCommandInit(c *gc.C) {
76
err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{})
77
c.Assert(err, gc.ErrorMatches, "no application name specified")
80
func (s *GetSuite) TestGetCommandInitWithApplication(c *gc.C) {
81
err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{"app"})
83
c.Assert(err, jc.ErrorIsNil)
86
func (s *GetSuite) TestGetCommandInitWithKey(c *gc.C) {
87
err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{"app", "key"})
89
c.Assert(err, jc.ErrorIsNil)
92
func (s *GetSuite) TestGetCommandInitTooManyArgs(c *gc.C) {
93
err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{"app", "key", "another"})
94
c.Assert(err, gc.ErrorMatches, `unrecognized args: \["another"\]`)
97
func (s *GetSuite) TestGetConfig(c *gc.C) {
98
for _, t := range getTests {
99
ctx := coretesting.Context(c)
100
code := cmd.Main(application.NewGetCommandForTest(s.fake), ctx, []string{t.application})
101
c.Check(code, gc.Equals, 0)
102
c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
103
// round trip via goyaml to avoid being sucked into a quagmire of
104
// map[interface{}]interface{} vs map[string]interface{}. This is
105
// also required if we add json support to this command.
106
buf, err := goyaml.Marshal(t.expected)
107
c.Assert(err, jc.ErrorIsNil)
108
expected := make(map[string]interface{})
109
err = goyaml.Unmarshal(buf, &expected)
110
c.Assert(err, jc.ErrorIsNil)
112
actual := make(map[string]interface{})
113
err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
114
c.Assert(err, jc.ErrorIsNil)
115
c.Assert(actual, gc.DeepEquals, expected)
119
func (s *GetSuite) TestGetConfigKey(c *gc.C) {
120
ctx := coretesting.Context(c)
121
code := cmd.Main(application.NewGetCommandForTest(s.fake), ctx, []string{"dummy-application", "title"})
122
c.Check(code, gc.Equals, 0)
123
c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
124
c.Assert(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, "Nearly There\n")
127
func (s *GetSuite) TestGetConfigKeyNotFound(c *gc.C) {
128
ctx := coretesting.Context(c)
129
code := cmd.Main(application.NewGetCommandForTest(s.fake), ctx, []string{"dummy-application", "invalid"})
130
c.Check(code, gc.Equals, 1)
131
c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "error: key \"invalid\" not found in \"dummy-application\" application settings.\n")
132
c.Assert(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, "")