github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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, bool) {
    60  	c.stub.AddCall("PublicAddress")
    61  	c.stub.NextErr()
    62  
    63  	if c.info.PublicAddress == "" {
    64  		return "", false
    65  	}
    66  	return c.info.PublicAddress, true
    67  }
    68  
    69  // PrivateAddress implements jujuc.ContextNetworking.
    70  func (c *ContextNetworking) PrivateAddress() (string, bool) {
    71  	c.stub.AddCall("PrivateAddress")
    72  	c.stub.NextErr()
    73  
    74  	if c.info.PrivateAddress == "" {
    75  		return "", false
    76  	}
    77  	return c.info.PrivateAddress, true
    78  }
    79  
    80  // OpenPorts implements jujuc.ContextNetworking.
    81  func (c *ContextNetworking) OpenPorts(protocol string, from, to int) error {
    82  	c.stub.AddCall("OpenPorts", protocol, from, to)
    83  	if err := c.stub.NextErr(); err != nil {
    84  		return errors.Trace(err)
    85  	}
    86  
    87  	c.info.AddPorts(protocol, from, to)
    88  	return nil
    89  }
    90  
    91  // ClosePorts implements jujuc.ContextNetworking.
    92  func (c *ContextNetworking) ClosePorts(protocol string, from, to int) error {
    93  	c.stub.AddCall("ClosePorts", protocol, from, to)
    94  	if err := c.stub.NextErr(); err != nil {
    95  		return errors.Trace(err)
    96  	}
    97  
    98  	c.info.RemovePorts(protocol, from, to)
    99  	return nil
   100  }
   101  
   102  // OpenedPorts implements jujuc.ContextNetworking.
   103  func (c *ContextNetworking) OpenedPorts() []network.PortRange {
   104  	c.stub.AddCall("OpenedPorts")
   105  	c.stub.NextErr()
   106  
   107  	return c.info.Ports
   108  }