~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        . "launchpad.net/gocheck"
 
6
        "launchpad.net/juju-core/testing"
 
7
        "strings"
 
8
)
 
9
 
 
10
type GetEnvironmentSuite struct {
 
11
        repoSuite
 
12
}
 
13
 
 
14
var _ = Suite(&GetEnvironmentSuite{})
 
15
 
 
16
var singleValueTests = []struct {
 
17
        key    string
 
18
        output string
 
19
        err    string
 
20
}{
 
21
        {
 
22
                key:    "type",
 
23
                output: "dummy",
 
24
        }, {
 
25
                key:    "name",
 
26
                output: "dummyenv",
 
27
        }, {
 
28
                key:    "authorized-keys",
 
29
                output: "i-am-a-key",
 
30
        }, {
 
31
                key: "unknown",
 
32
                err: `Key "unknown" not found in "dummyenv" environment.`,
 
33
        },
 
34
}
 
35
 
 
36
func (s *GetEnvironmentSuite) TestSingleValue(c *C) {
 
37
 
 
38
        for _, t := range singleValueTests {
 
39
                context, err := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{t.key})
 
40
                if t.err != "" {
 
41
                        c.Assert(err, ErrorMatches, t.err)
 
42
                } else {
 
43
                        output := strings.TrimSpace(testing.Stdout(context))
 
44
                        c.Assert(err, IsNil)
 
45
                        c.Assert(output, Equals, t.output)
 
46
                }
 
47
        }
 
48
}
 
49
 
 
50
func (s *GetEnvironmentSuite) TestTooManyArgs(c *C) {
 
51
        _, err := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{"name", "type"})
 
52
        c.Assert(err, ErrorMatches, `unrecognized args: \["type"\]`)
 
53
}
 
54
 
 
55
func (s *GetEnvironmentSuite) TestAllValues(c *C) {
 
56
 
 
57
        context, _ := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{})
 
58
        output := strings.TrimSpace(testing.Stdout(context))
 
59
 
 
60
        // Make sure that all the environment keys are there.
 
61
        any := "(.|\n)*" // because . doesn't match new lines.
 
62
        for key, _ := range s.Conn.Environ.Config().AllAttrs() {
 
63
                c.Assert(output, Matches, fmt.Sprintf("%s%s: %s", any, key, any))
 
64
        }
 
65
}
 
66
 
 
67
type SetEnvironmentSuite struct {
 
68
        repoSuite
 
69
}
 
70
 
 
71
var _ = Suite(&SetEnvironmentSuite{})
 
72
 
 
73
var setEnvInitTests = []struct {
 
74
        args     []string
 
75
        expected attributes
 
76
        err      string
 
77
}{
 
78
        {
 
79
                args: []string{},
 
80
                err:  "No key, value pairs specified",
 
81
        }, {
 
82
                args: []string{"agent-version=1.2.3"},
 
83
                err:  `agent-version must be set via upgrade-juju`,
 
84
        }, {
 
85
                args: []string{"missing"},
 
86
                err:  `Missing "=" in arg 1: "missing"`,
 
87
        }, {
 
88
                args: []string{"key=value"},
 
89
                expected: attributes{
 
90
                        "key": "value",
 
91
                },
 
92
        }, {
 
93
                args: []string{"key=value", "key=other"},
 
94
                err:  `Key "key" specified more than once`,
 
95
        }, {
 
96
                args: []string{"key=value", "other=embedded=equal"},
 
97
                expected: attributes{
 
98
                        "key":   "value",
 
99
                        "other": "embedded=equal",
 
100
                },
 
101
        },
 
102
}
 
103
 
 
104
func (s *SetEnvironmentSuite) TestInit(c *C) {
 
105
 
 
106
        for _, t := range setEnvInitTests {
 
107
                command := &SetEnvironmentCommand{}
 
108
                testing.TestInit(c, command, t.args, t.err)
 
109
                if t.expected != nil {
 
110
                        c.Assert(command.values, DeepEquals, t.expected)
 
111
                }
 
112
        }
 
113
}
 
114
 
 
115
func (s *SetEnvironmentSuite) TestChangeDefaultSeries(c *C) {
 
116
        _, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=raring"})
 
117
        c.Assert(err, IsNil)
 
118
 
 
119
        stateConfig, err := s.State.EnvironConfig()
 
120
        c.Assert(err, IsNil)
 
121
        c.Assert(stateConfig.DefaultSeries(), Equals, "raring")
 
122
}
 
123
 
 
124
func (s *SetEnvironmentSuite) TestChangeMultipleValues(c *C) {
 
125
        _, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=spartan", "broken=nope", "secret=sekrit"})
 
126
        c.Assert(err, IsNil)
 
127
 
 
128
        stateConfig, err := s.State.EnvironConfig()
 
129
        c.Assert(err, IsNil)
 
130
        attrs := stateConfig.AllAttrs()
 
131
        c.Assert(attrs["default-series"].(string), Equals, "spartan")
 
132
        c.Assert(attrs["broken"].(string), Equals, "nope")
 
133
        c.Assert(attrs["secret"].(string), Equals, "sekrit")
 
134
}
 
135
 
 
136
func (s *SetEnvironmentSuite) TestChangeAsCommandPair(c *C) {
 
137
        _, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=raring"})
 
138
        c.Assert(err, IsNil)
 
139
 
 
140
        context, err := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{"default-series"})
 
141
        c.Assert(err, IsNil)
 
142
        output := strings.TrimSpace(testing.Stdout(context))
 
143
 
 
144
        c.Assert(output, Equals, "raring")
 
145
}
 
146
 
 
147
var immutableConfigTests = map[string]string{
 
148
        "name":          "foo",
 
149
        "type":          "foo",
 
150
        "firewall-mode": "global",
 
151
}
 
152
 
 
153
func (s *SetEnvironmentSuite) TestImmutableConfigValues(c *C) {
 
154
        for name, value := range immutableConfigTests {
 
155
                param := fmt.Sprintf("%s=%s", name, value)
 
156
                _, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{param})
 
157
                errorPattern := fmt.Sprintf("cannot change %s from .* to %q", name, value)
 
158
                c.Assert(err, ErrorMatches, errorPattern)
 
159
        }
 
160
}