1
// Copyright 2012, 2013 Canonical Ltd.
2
// Copyright 2014 Cloudbase Solutions SRL
3
// Licensed under the AGPLv3, see LICENCE file for details.
14
jc "github.com/juju/testing/checkers"
15
gc "gopkg.in/check.v1"
16
"gopkg.in/juju/charm.v4"
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"
25
func bufferBytes(stream io.Writer) []byte {
26
return stream.(*bytes.Buffer).Bytes()
29
func bufferString(w io.Writer) string {
30
return w.(*bytes.Buffer).String()
33
type ContextSuite struct {
35
rels map[int]*ContextRelation
38
func (s *ContextSuite) SetUpTest(c *gc.C) {
39
s.BaseSuite.SetUpTest(c)
40
s.rels = map[int]*ContextRelation{
44
units: map[string]Settings{
45
"u/0": {"private-address": "u-0.testing.invalid"},
51
units: map[string]Settings{
52
"u/0": {"private-address": "u-0.testing.invalid"},
58
func (s *ContextSuite) GetHookContext(c *gc.C, relid int, remote string) *Context {
60
_, found := s.rels[relid]
61
c.Assert(found, jc.IsTrue)
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() {
78
c.Assert(err, jc.ErrorIsNil)
82
ports []network.PortRange
85
rels map[int]*ContextRelation
86
metrics []jujuc.Metric
88
rebootPriority jujuc.RebootPriority
92
func (c *Context) AddMetric(key, value string, created time.Time) error {
94
return fmt.Errorf("metrics disabled")
96
c.metrics = append(c.metrics, jujuc.Metric{key, value, created})
100
func (c *Context) UnitName() string {
104
func (c *Context) PublicAddress() (string, bool) {
105
return "gimli.minecraft.testing.invalid", true
108
func (c *Context) PrivateAddress() (string, bool) {
109
return "192.168.0.99", true
112
func (c *Context) AvailabilityZone() (string, bool) {
113
return "us-east-1a", true
116
func (c *Context) OpenPorts(protocol string, fromPort, toPort int) error {
117
c.ports = append(c.ports, network.PortRange{
122
network.SortPortRanges(c.ports)
126
func (c *Context) ClosePorts(protocol string, fromPort, toPort int) error {
127
portRange := network.PortRange{
132
for i, port := range c.ports {
133
if port == portRange {
134
c.ports = append(c.ports[:i], c.ports[i+1:]...)
138
network.SortPortRanges(c.ports)
142
func (c *Context) OpenedPorts() []network.PortRange {
146
func (c *Context) ConfigSettings() (charm.Settings, error) {
147
return charm.Settings{
150
"spline-reticulation": 45.0,
152
"username": "admin001",
156
func (c *Context) ActionParams() (map[string]interface{}, error) {
157
return nil, fmt.Errorf("not running an action")
160
func (c *Context) UpdateActionResults(keys []string, value string) error {
161
return fmt.Errorf("not running an action")
164
func (c *Context) SetActionFailed() error {
165
return fmt.Errorf("not running an action")
168
func (c *Context) SetActionMessage(message string) error {
169
return fmt.Errorf("not running an action")
172
func (c *Context) HookRelation() (jujuc.ContextRelation, bool) {
173
return c.Relation(c.relid)
176
func (c *Context) RemoteUnitName() (string, bool) {
177
return c.remote, c.remote != ""
180
func (c *Context) Relation(id int) (jujuc.ContextRelation, bool) {
181
r, found := c.rels[id]
185
func (c *Context) RelationIds() []int {
187
for id := range c.rels {
188
ids = append(ids, id)
193
func (c *Context) OwnerTag() string {
197
type ContextRelation struct {
200
units map[string]Settings
203
func (r *ContextRelation) Id() int {
207
func (r *ContextRelation) Name() string {
211
func (r *ContextRelation) FakeId() string {
212
return fmt.Sprintf("%s:%d", r.name, r.id)
215
func (r *ContextRelation) Settings() (jujuc.Settings, error) {
216
return r.units["u/0"], nil
219
func (r *ContextRelation) UnitNames() []string {
220
var s []string // initially nil to match the true context.
221
for name := range r.units {
228
func (r *ContextRelation) ReadSettings(name string) (params.RelationSettings, error) {
229
s, found := r.units[name]
231
return nil, fmt.Errorf("unknown unit %s", name)
236
type Settings params.RelationSettings
238
func (s Settings) Get(k string) (interface{}, bool) {
243
func (s Settings) Set(k, v string) {
247
func (s Settings) Delete(k string) {
251
func (s Settings) Map() params.RelationSettings {
252
r := params.RelationSettings{}
253
for k, v := range s {
259
func (c *Context) RequestReboot(priority jujuc.RebootPriority) error {
260
c.rebootPriority = priority
262
return fmt.Errorf("RequestReboot error!")
268
func cmdString(cmd string) string {
269
return cmd + jujuc.CmdSuffix