~sinzui/ubuntu/wily/juju-core/wily-1.24.7

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/runner/jujuc/util_test.go

  • Committer: Package Import Robot
  • Author(s): Oleg Strikov
  • Date: 2015-03-26 15:54:39 UTC
  • mfrom: (1.1.32)
  • Revision ID: package-import@ubuntu.com-20150326155439-ot7bwwyoomq13btm
Tags: 1.22.0-0ubuntu1
* New upstream release (LP: #1416051).
* d/patches/fix-detect-new-release.patch: Added upstream patch to redeem
  the ability to handle future Ubuntu releases (LP: #1427879, #1434092).
* d/tests/fake-future.sh: New ability to generate fake /etc/os-release.
* d/copyright: Updated to reflect changes in the codebase.
* d/control:
  - Change build dependency from gccgo to gccgo-go.
  - Use either cloud-image-utils or cloud-utils as dependency for juju-local
    because cloud-image-utils is not available on precise.
  - Compliance to Debian Policy 3.9.6 was declared.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012, 2013 Canonical Ltd.
 
2
// Copyright 2014 Cloudbase Solutions SRL
 
3
// Licensed under the AGPLv3, see LICENCE file for details.
 
4
 
 
5
package jujuc_test
 
6
 
 
7
import (
 
8
        "bytes"
 
9
        "fmt"
 
10
        "io"
 
11
        "sort"
 
12
        "time"
 
13
 
 
14
        jc "github.com/juju/testing/checkers"
 
15
        gc "gopkg.in/check.v1"
 
16
        "gopkg.in/juju/charm.v4"
 
17
 
 
18
        "github.com/juju/juju/apiserver/params"
 
19
        "github.com/juju/juju/network"
 
20
        "github.com/juju/juju/state"
 
21
        "github.com/juju/juju/testing"
 
22
        "github.com/juju/juju/worker/uniter/runner/jujuc"
 
23
)
 
24
 
 
25
func bufferBytes(stream io.Writer) []byte {
 
26
        return stream.(*bytes.Buffer).Bytes()
 
27
}
 
28
 
 
29
func bufferString(w io.Writer) string {
 
30
        return w.(*bytes.Buffer).String()
 
31
}
 
32
 
 
33
type ContextSuite struct {
 
34
        testing.BaseSuite
 
35
        rels map[int]*ContextRelation
 
36
}
 
37
 
 
38
func (s *ContextSuite) SetUpTest(c *gc.C) {
 
39
        s.BaseSuite.SetUpTest(c)
 
40
        s.rels = map[int]*ContextRelation{
 
41
                0: {
 
42
                        id:   0,
 
43
                        name: "peer0",
 
44
                        units: map[string]Settings{
 
45
                                "u/0": {"private-address": "u-0.testing.invalid"},
 
46
                        },
 
47
                },
 
48
                1: {
 
49
                        id:   1,
 
50
                        name: "peer1",
 
51
                        units: map[string]Settings{
 
52
                                "u/0": {"private-address": "u-0.testing.invalid"},
 
53
                        },
 
54
                },
 
55
        }
 
56
}
 
57
 
 
58
func (s *ContextSuite) GetHookContext(c *gc.C, relid int, remote string) *Context {
 
59
        if relid != -1 {
 
60
                _, found := s.rels[relid]
 
61
                c.Assert(found, jc.IsTrue)
 
62
        }
 
63
        return &Context{
 
64
                relid:  relid,
 
65
                remote: remote,
 
66
                rels:   s.rels,
 
67
        }
 
68
}
 
69
 
 
70
func setSettings(c *gc.C, ru *state.RelationUnit, settings map[string]interface{}) {
 
71
        node, err := ru.Settings()
 
72
        c.Assert(err, jc.ErrorIsNil)
 
73
        for _, k := range node.Keys() {
 
74
                node.Delete(k)
 
75
        }
 
76
        node.Update(settings)
 
77
        _, err = node.Write()
 
78
        c.Assert(err, jc.ErrorIsNil)
 
79
}
 
80
 
 
81
type Context struct {
 
82
        ports          []network.PortRange
 
83
        relid          int
 
84
        remote         string
 
85
        rels           map[int]*ContextRelation
 
86
        metrics        []jujuc.Metric
 
87
        canAddMetrics  bool
 
88
        rebootPriority jujuc.RebootPriority
 
89
        shouldError    bool
 
90
}
 
91
 
 
92
func (c *Context) AddMetric(key, value string, created time.Time) error {
 
93
        if !c.canAddMetrics {
 
94
                return fmt.Errorf("metrics disabled")
 
95
        }
 
96
        c.metrics = append(c.metrics, jujuc.Metric{key, value, created})
 
97
        return nil
 
98
}
 
99
 
 
100
func (c *Context) UnitName() string {
 
101
        return "u/0"
 
102
}
 
103
 
 
104
func (c *Context) PublicAddress() (string, bool) {
 
105
        return "gimli.minecraft.testing.invalid", true
 
106
}
 
107
 
 
108
func (c *Context) PrivateAddress() (string, bool) {
 
109
        return "192.168.0.99", true
 
110
}
 
111
 
 
112
func (c *Context) AvailabilityZone() (string, bool) {
 
113
        return "us-east-1a", true
 
114
}
 
115
 
 
116
func (c *Context) OpenPorts(protocol string, fromPort, toPort int) error {
 
117
        c.ports = append(c.ports, network.PortRange{
 
118
                Protocol: protocol,
 
119
                FromPort: fromPort,
 
120
                ToPort:   toPort,
 
121
        })
 
122
        network.SortPortRanges(c.ports)
 
123
        return nil
 
124
}
 
125
 
 
126
func (c *Context) ClosePorts(protocol string, fromPort, toPort int) error {
 
127
        portRange := network.PortRange{
 
128
                Protocol: protocol,
 
129
                FromPort: fromPort,
 
130
                ToPort:   toPort,
 
131
        }
 
132
        for i, port := range c.ports {
 
133
                if port == portRange {
 
134
                        c.ports = append(c.ports[:i], c.ports[i+1:]...)
 
135
                        break
 
136
                }
 
137
        }
 
138
        network.SortPortRanges(c.ports)
 
139
        return nil
 
140
}
 
141
 
 
142
func (c *Context) OpenedPorts() []network.PortRange {
 
143
        return c.ports
 
144
}
 
145
 
 
146
func (c *Context) ConfigSettings() (charm.Settings, error) {
 
147
        return charm.Settings{
 
148
                "empty":               nil,
 
149
                "monsters":            false,
 
150
                "spline-reticulation": 45.0,
 
151
                "title":               "My Title",
 
152
                "username":            "admin001",
 
153
        }, nil
 
154
}
 
155
 
 
156
func (c *Context) ActionParams() (map[string]interface{}, error) {
 
157
        return nil, fmt.Errorf("not running an action")
 
158
}
 
159
 
 
160
func (c *Context) UpdateActionResults(keys []string, value string) error {
 
161
        return fmt.Errorf("not running an action")
 
162
}
 
163
 
 
164
func (c *Context) SetActionFailed() error {
 
165
        return fmt.Errorf("not running an action")
 
166
}
 
167
 
 
168
func (c *Context) SetActionMessage(message string) error {
 
169
        return fmt.Errorf("not running an action")
 
170
}
 
171
 
 
172
func (c *Context) HookRelation() (jujuc.ContextRelation, bool) {
 
173
        return c.Relation(c.relid)
 
174
}
 
175
 
 
176
func (c *Context) RemoteUnitName() (string, bool) {
 
177
        return c.remote, c.remote != ""
 
178
}
 
179
 
 
180
func (c *Context) Relation(id int) (jujuc.ContextRelation, bool) {
 
181
        r, found := c.rels[id]
 
182
        return r, found
 
183
}
 
184
 
 
185
func (c *Context) RelationIds() []int {
 
186
        ids := []int{}
 
187
        for id := range c.rels {
 
188
                ids = append(ids, id)
 
189
        }
 
190
        return ids
 
191
}
 
192
 
 
193
func (c *Context) OwnerTag() string {
 
194
        return "test-owner"
 
195
}
 
196
 
 
197
type ContextRelation struct {
 
198
        id    int
 
199
        name  string
 
200
        units map[string]Settings
 
201
}
 
202
 
 
203
func (r *ContextRelation) Id() int {
 
204
        return r.id
 
205
}
 
206
 
 
207
func (r *ContextRelation) Name() string {
 
208
        return r.name
 
209
}
 
210
 
 
211
func (r *ContextRelation) FakeId() string {
 
212
        return fmt.Sprintf("%s:%d", r.name, r.id)
 
213
}
 
214
 
 
215
func (r *ContextRelation) Settings() (jujuc.Settings, error) {
 
216
        return r.units["u/0"], nil
 
217
}
 
218
 
 
219
func (r *ContextRelation) UnitNames() []string {
 
220
        var s []string // initially nil to match the true context.
 
221
        for name := range r.units {
 
222
                s = append(s, name)
 
223
        }
 
224
        sort.Strings(s)
 
225
        return s
 
226
}
 
227
 
 
228
func (r *ContextRelation) ReadSettings(name string) (params.RelationSettings, error) {
 
229
        s, found := r.units[name]
 
230
        if !found {
 
231
                return nil, fmt.Errorf("unknown unit %s", name)
 
232
        }
 
233
        return s.Map(), nil
 
234
}
 
235
 
 
236
type Settings params.RelationSettings
 
237
 
 
238
func (s Settings) Get(k string) (interface{}, bool) {
 
239
        v, f := s[k]
 
240
        return v, f
 
241
}
 
242
 
 
243
func (s Settings) Set(k, v string) {
 
244
        s[k] = v
 
245
}
 
246
 
 
247
func (s Settings) Delete(k string) {
 
248
        delete(s, k)
 
249
}
 
250
 
 
251
func (s Settings) Map() params.RelationSettings {
 
252
        r := params.RelationSettings{}
 
253
        for k, v := range s {
 
254
                r[k] = v
 
255
        }
 
256
        return r
 
257
}
 
258
 
 
259
func (c *Context) RequestReboot(priority jujuc.RebootPriority) error {
 
260
        c.rebootPriority = priority
 
261
        if c.shouldError {
 
262
                return fmt.Errorf("RequestReboot error!")
 
263
        } else {
 
264
                return nil
 
265
        }
 
266
}
 
267
 
 
268
func cmdString(cmd string) string {
 
269
        return cmd + jujuc.CmdSuffix
 
270
}