github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/gce/google/network_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package google_test
     5  
     6  import (
     7  	"sort"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	"google.golang.org/api/compute/v1"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/provider/gce/google"
    15  )
    16  
    17  type networkSuite struct {
    18  	google.BaseSuite
    19  }
    20  
    21  var _ = gc.Suite(&networkSuite{})
    22  
    23  func (s *networkSuite) TestNetworkSpecPath(c *gc.C) {
    24  	spec := google.NetworkSpec{
    25  		Name: "spam",
    26  	}
    27  	path := spec.Path()
    28  
    29  	c.Check(path, gc.Equals, "global/networks/spam")
    30  }
    31  
    32  func (s *networkSuite) TestNetworkSpecNewInterface(c *gc.C) {
    33  	spec := google.NetworkSpec{
    34  		Name: "spam",
    35  	}
    36  	netIF := google.NewNetInterface(spec, "eggs")
    37  
    38  	c.Check(netIF, gc.DeepEquals, &compute.NetworkInterface{
    39  		Network: "global/networks/spam",
    40  		AccessConfigs: []*compute.AccessConfig{{
    41  			Name: "eggs",
    42  			Type: google.NetworkAccessOneToOneNAT,
    43  		}},
    44  	})
    45  }
    46  
    47  type ByIPProtocol []*compute.FirewallAllowed
    48  
    49  func (s ByIPProtocol) Len() int {
    50  	return len(s)
    51  }
    52  func (s ByIPProtocol) Swap(i, j int) {
    53  	s[i], s[j] = s[j], s[i]
    54  }
    55  func (s ByIPProtocol) Less(i, j int) bool {
    56  	return s[i].IPProtocol < s[j].IPProtocol
    57  }
    58  
    59  func (s *networkSuite) TestFirewallSpec(c *gc.C) {
    60  	ports := network.NewPortSet(
    61  		network.MustParsePortRange("80-81/tcp"),
    62  		network.MustParsePortRange("8888/tcp"),
    63  		network.MustParsePortRange("1234/udp"),
    64  	)
    65  	fw := google.FirewallSpec("spam", ports)
    66  
    67  	allowed := []*compute.FirewallAllowed{{
    68  		IPProtocol: "tcp",
    69  		Ports:      []string{"80", "81", "8888"},
    70  	}, {
    71  		IPProtocol: "udp",
    72  		Ports:      []string{"1234"},
    73  	}}
    74  	sort.Sort(ByIPProtocol(fw.Allowed))
    75  	for i := range fw.Allowed {
    76  		sort.Strings(fw.Allowed[i].Ports)
    77  	}
    78  	c.Check(fw, jc.DeepEquals, &compute.Firewall{
    79  		Name:         "spam",
    80  		TargetTags:   []string{"spam"},
    81  		SourceRanges: []string{"0.0.0.0/0"},
    82  		Allowed:      allowed,
    83  	})
    84  }
    85  
    86  func (s *networkSuite) TestExtractAddresses(c *gc.C) {
    87  	addresses := google.ExtractAddresses(&s.NetworkInterface)
    88  
    89  	c.Check(addresses, jc.DeepEquals, []network.Address{{
    90  		Value: "10.0.0.1",
    91  		Type:  network.IPv4Address,
    92  		Scope: network.ScopeCloudLocal,
    93  	}})
    94  }
    95  
    96  func (s *networkSuite) TestExtractAddressesExternal(c *gc.C) {
    97  	s.NetworkInterface.NetworkIP = ""
    98  	s.NetworkInterface.AccessConfigs[0].NatIP = "8.8.8.8"
    99  	addresses := google.ExtractAddresses(&s.NetworkInterface)
   100  
   101  	c.Check(addresses, jc.DeepEquals, []network.Address{{
   102  		Value: "8.8.8.8",
   103  		Type:  network.IPv4Address,
   104  		Scope: network.ScopePublic,
   105  	}})
   106  }
   107  
   108  func (s *networkSuite) TestExtractAddressesEmpty(c *gc.C) {
   109  	s.NetworkInterface.AccessConfigs = nil
   110  	s.NetworkInterface.NetworkIP = ""
   111  	addresses := google.ExtractAddresses(&s.NetworkInterface)
   112  
   113  	c.Check(addresses, gc.HasLen, 0)
   114  }