~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/application/get_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2012-2015 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package application_test
5
 
 
6
 
import (
7
 
        "bytes"
8
 
 
9
 
        "github.com/juju/cmd"
10
 
        jc "github.com/juju/testing/checkers"
11
 
        gc "gopkg.in/check.v1"
12
 
        goyaml "gopkg.in/yaml.v2"
13
 
 
14
 
        "github.com/juju/juju/cmd/juju/application"
15
 
        coretesting "github.com/juju/juju/testing"
16
 
)
17
 
 
18
 
type GetSuite struct {
19
 
        coretesting.FakeJujuXDGDataHomeSuite
20
 
        fake *fakeServiceAPI
21
 
}
22
 
 
23
 
var _ = gc.Suite(&GetSuite{})
24
 
 
25
 
var getTests = []struct {
26
 
        application string
27
 
        expected    map[string]interface{}
28
 
}{
29
 
        {
30
 
                "dummy-application",
31
 
                map[string]interface{}{
32
 
                        "application": "dummy-application",
33
 
                        "charm":       "dummy",
34
 
                        "settings": map[string]interface{}{
35
 
                                "title": map[string]interface{}{
36
 
                                        "description": "Specifies title",
37
 
                                        "type":        "string",
38
 
                                        "value":       "Nearly There",
39
 
                                },
40
 
                                "skill-level": map[string]interface{}{
41
 
                                        "description": "Specifies skill-level",
42
 
                                        "value":       100,
43
 
                                        "type":        "int",
44
 
                                },
45
 
                                "username": map[string]interface{}{
46
 
                                        "description": "Specifies username",
47
 
                                        "type":        "string",
48
 
                                        "value":       "admin001",
49
 
                                },
50
 
                                "outlook": map[string]interface{}{
51
 
                                        "description": "Specifies outlook",
52
 
                                        "type":        "string",
53
 
                                        "value":       "true",
54
 
                                },
55
 
                        },
56
 
                },
57
 
        },
58
 
 
59
 
        // TODO(dfc) add additional services (need more charms)
60
 
        // TODO(dfc) add set tests
61
 
}
62
 
 
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",
68
 
                        "skill-level": 100,
69
 
                        "username":    "admin001",
70
 
                        "outlook":     "true",
71
 
                }}
72
 
}
73
 
 
74
 
func (s *GetSuite) TestGetCommandInit(c *gc.C) {
75
 
        // missing args
76
 
        err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{})
77
 
        c.Assert(err, gc.ErrorMatches, "no application name specified")
78
 
}
79
 
 
80
 
func (s *GetSuite) TestGetCommandInitWithApplication(c *gc.C) {
81
 
        err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{"app"})
82
 
        // everything ok
83
 
        c.Assert(err, jc.ErrorIsNil)
84
 
}
85
 
 
86
 
func (s *GetSuite) TestGetCommandInitWithKey(c *gc.C) {
87
 
        err := coretesting.InitCommand(application.NewGetCommandForTest(s.fake), []string{"app", "key"})
88
 
        // everything ok
89
 
        c.Assert(err, jc.ErrorIsNil)
90
 
}
91
 
 
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"\]`)
95
 
}
96
 
 
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)
111
 
 
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)
116
 
        }
117
 
}
118
 
 
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")
125
 
}
126
 
 
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, "")
133
 
}