~nskaggs/+junk/xenial-test

« back to all changes in this revision

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

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package testing
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
        jc "github.com/juju/testing/checkers"
 
9
        gc "gopkg.in/check.v1"
 
10
 
 
11
        "github.com/juju/juju/apiserver/params"
 
12
        "github.com/juju/juju/network"
 
13
)
 
14
 
 
15
// NetworkInterface holds the values for the hook context.
 
16
type NetworkInterface struct {
 
17
        PublicAddress            string
 
18
        PrivateAddress           string
 
19
        Ports                    []network.PortRange
 
20
        BindingsToNetworkConfigs map[string][]params.NetworkConfig
 
21
}
 
22
 
 
23
// CheckPorts checks the current ports.
 
24
func (ni *NetworkInterface) CheckPorts(c *gc.C, expected []network.PortRange) {
 
25
        c.Check(ni.Ports, jc.DeepEquals, expected)
 
26
}
 
27
 
 
28
// AddPorts adds the specified port range.
 
29
func (ni *NetworkInterface) AddPorts(protocol string, from, to int) {
 
30
        ni.Ports = append(ni.Ports, network.PortRange{
 
31
                Protocol: protocol,
 
32
                FromPort: from,
 
33
                ToPort:   to,
 
34
        })
 
35
        network.SortPortRanges(ni.Ports)
 
36
}
 
37
 
 
38
// RemovePorts removes the specified port range.
 
39
func (ni *NetworkInterface) RemovePorts(protocol string, from, to int) {
 
40
        portRange := network.PortRange{
 
41
                Protocol: protocol,
 
42
                FromPort: from,
 
43
                ToPort:   to,
 
44
        }
 
45
        for i, port := range ni.Ports {
 
46
                if port == portRange {
 
47
                        ni.Ports = append(ni.Ports[:i], ni.Ports[i+1:]...)
 
48
                        break
 
49
                }
 
50
        }
 
51
        network.SortPortRanges(ni.Ports)
 
52
}
 
53
 
 
54
// ContextNetworking is a test double for jujuc.ContextNetworking.
 
55
type ContextNetworking struct {
 
56
        contextBase
 
57
        info *NetworkInterface
 
58
}
 
59
 
 
60
// PublicAddress implements jujuc.ContextNetworking.
 
61
func (c *ContextNetworking) PublicAddress() (string, error) {
 
62
        c.stub.AddCall("PublicAddress")
 
63
 
 
64
        return c.info.PublicAddress, c.stub.NextErr()
 
65
 
 
66
}
 
67
 
 
68
// PrivateAddress implements jujuc.ContextNetworking.
 
69
func (c *ContextNetworking) PrivateAddress() (string, error) {
 
70
        c.stub.AddCall("PrivateAddress")
 
71
 
 
72
        return c.info.PrivateAddress, c.stub.NextErr()
 
73
 
 
74
}
 
75
 
 
76
// OpenPorts implements jujuc.ContextNetworking.
 
77
func (c *ContextNetworking) OpenPorts(protocol string, from, to int) error {
 
78
        c.stub.AddCall("OpenPorts", protocol, from, to)
 
79
        if err := c.stub.NextErr(); err != nil {
 
80
                return errors.Trace(err)
 
81
        }
 
82
 
 
83
        c.info.AddPorts(protocol, from, to)
 
84
        return nil
 
85
}
 
86
 
 
87
// ClosePorts implements jujuc.ContextNetworking.
 
88
func (c *ContextNetworking) ClosePorts(protocol string, from, to int) error {
 
89
        c.stub.AddCall("ClosePorts", protocol, from, to)
 
90
        if err := c.stub.NextErr(); err != nil {
 
91
                return errors.Trace(err)
 
92
        }
 
93
 
 
94
        c.info.RemovePorts(protocol, from, to)
 
95
        return nil
 
96
}
 
97
 
 
98
// OpenedPorts implements jujuc.ContextNetworking.
 
99
func (c *ContextNetworking) OpenedPorts() []network.PortRange {
 
100
        c.stub.AddCall("OpenedPorts")
 
101
        c.stub.NextErr()
 
102
 
 
103
        return c.info.Ports
 
104
}
 
105
 
 
106
// NetworkConfig implements jujuc.ContextNetworking.
 
107
func (c *ContextNetworking) NetworkConfig(bindingName string) ([]params.NetworkConfig, error) {
 
108
        c.stub.AddCall("NetworkConfig", bindingName)
 
109
        if err := c.stub.NextErr(); err != nil {
 
110
                return nil, errors.Trace(err)
 
111
        }
 
112
 
 
113
        netConfig, isBindingKnown := c.info.BindingsToNetworkConfigs[bindingName]
 
114
        if !isBindingKnown {
 
115
                return nil, errors.Errorf("insert server error for unknown binding here")
 
116
        }
 
117
        return netConfig, nil
 
118
}