github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/uniter/runner/jujuc/testing/networking.go (about)

     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/network"
    12  )
    13  
    14  // NetworkInterface holds the values for the hook context.
    15  type NetworkInterface struct {
    16  	PublicAddress  string
    17  	PrivateAddress string
    18  	Ports          []network.PortRange
    19  }
    20  
    21  // CheckPorts checks the current ports.
    22  func (ni *NetworkInterface) CheckPorts(c *gc.C, expected []network.PortRange) {
    23  	c.Check(ni.Ports, jc.DeepEquals, expected)
    24  }
    25  
    26  // AddPorts adds the specified port range.
    27  func (ni *NetworkInterface) AddPorts(protocol string, from, to int) {
    28  	ni.Ports = append(ni.Ports, network.PortRange{
    29  		Protocol: protocol,
    30  		FromPort: from,
    31  		ToPort:   to,
    32  	})
    33  	network.SortPortRanges(ni.Ports)
    34  }
    35  
    36  // RemovePorts removes the specified port range.
    37  func (ni *NetworkInterface) RemovePorts(protocol string, from, to int) {
    38  	portRange := network.PortRange{
    39  		Protocol: protocol,
    40  		FromPort: from,
    41  		ToPort:   to,
    42  	}
    43  	for i, port := range ni.Ports {
    44  		if port == portRange {
    45  			ni.Ports = append(ni.Ports[:i], ni.Ports[i+1:]...)
    46  			break
    47  		}
    48  	}
    49  	network.SortPortRanges(ni.Ports)
    50  }
    51  
    52  // ContextNetworking is a test double for jujuc.ContextNetworking.
    53  type ContextNetworking struct {
    54  	contextBase
    55  	info *NetworkInterface
    56  }
    57  
    58  // PublicAddress implements jujuc.ContextNetworking.
    59  func (c *ContextNetworking) PublicAddress() (string, error) {
    60  	c.stub.AddCall("PublicAddress")
    61  
    62  	return c.info.PublicAddress, c.stub.NextErr()
    63  
    64  }
    65  
    66  // PrivateAddress implements jujuc.ContextNetworking.
    67  func (c *ContextNetworking) PrivateAddress() (string, error) {
    68  	c.stub.AddCall("PrivateAddress")
    69  
    70  	return c.info.PrivateAddress, c.stub.NextErr()
    71  
    72  }
    73  
    74  // OpenPorts implements jujuc.ContextNetworking.
    75  func (c *ContextNetworking) OpenPorts(protocol string, from, to int) error {
    76  	c.stub.AddCall("OpenPorts", protocol, from, to)
    77  	if err := c.stub.NextErr(); err != nil {
    78  		return errors.Trace(err)
    79  	}
    80  
    81  	c.info.AddPorts(protocol, from, to)
    82  	return nil
    83  }
    84  
    85  // ClosePorts implements jujuc.ContextNetworking.
    86  func (c *ContextNetworking) ClosePorts(protocol string, from, to int) error {
    87  	c.stub.AddCall("ClosePorts", protocol, from, to)
    88  	if err := c.stub.NextErr(); err != nil {
    89  		return errors.Trace(err)
    90  	}
    91  
    92  	c.info.RemovePorts(protocol, from, to)
    93  	return nil
    94  }
    95  
    96  // OpenedPorts implements jujuc.ContextNetworking.
    97  func (c *ContextNetworking) OpenedPorts() []network.PortRange {
    98  	c.stub.AddCall("OpenedPorts")
    99  	c.stub.NextErr()
   100  
   101  	return c.info.Ports
   102  }