github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	corenetwork "github.com/juju/juju/core/network"
    14  	"github.com/juju/juju/network"
    15  	"github.com/juju/juju/provider/gce/google"
    16  )
    17  
    18  type networkSuite struct {
    19  	google.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&networkSuite{})
    23  
    24  func (s *networkSuite) TestNetworkSpecPath(c *gc.C) {
    25  	spec := google.NetworkSpec{
    26  		Name: "spam",
    27  	}
    28  	path := spec.Path()
    29  
    30  	c.Check(path, gc.Equals, "global/networks/spam")
    31  }
    32  
    33  func (s *networkSuite) TestNetworkSpecNewInterface(c *gc.C) {
    34  	spec := google.NetworkSpec{
    35  		Name: "spam",
    36  	}
    37  	netIF := google.NewNetInterface(spec, "eggs")
    38  
    39  	c.Check(netIF, gc.DeepEquals, &compute.NetworkInterface{
    40  		Network: "global/networks/spam",
    41  		AccessConfigs: []*compute.AccessConfig{{
    42  			Name: "eggs",
    43  			Type: google.NetworkAccessOneToOneNAT,
    44  		}},
    45  	})
    46  }
    47  
    48  type ByIPProtocol []*compute.FirewallAllowed
    49  
    50  func (s ByIPProtocol) Len() int {
    51  	return len(s)
    52  }
    53  func (s ByIPProtocol) Swap(i, j int) {
    54  	s[i], s[j] = s[j], s[i]
    55  }
    56  func (s ByIPProtocol) Less(i, j int) bool {
    57  	return s[i].IPProtocol < s[j].IPProtocol
    58  }
    59  
    60  func (s *networkSuite) TestFirewallSpec(c *gc.C) {
    61  	ports := map[string][]corenetwork.PortRange{
    62  		"tcp": {{FromPort: 80, ToPort: 81}, {FromPort: 8888, ToPort: 8888}},
    63  		"udp": {{FromPort: 1234, ToPort: 1234}},
    64  	}
    65  	fw := google.FirewallSpec("spam", "target", []string{"192.168.1.0/24", "10.0.0.0/24"}, 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{"target"},
    81  		SourceRanges: []string{"192.168.1.0/24", "10.0.0.0/24"},
    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  }