github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/ec2/subnet_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  package ec2_test
     4  
     5  import (
     6  	jc "github.com/juju/testing/checkers"
     7  	amzec2 "gopkg.in/amz.v3/ec2"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/provider/ec2"
    11  )
    12  
    13  type subnetMatcherSuite struct{}
    14  
    15  var _ = gc.Suite(&subnetMatcherSuite{})
    16  
    17  var cannedSubnets = []amzec2.Subnet{{
    18  	Id:                  "subnet-1234abcd",
    19  	State:               "available",
    20  	VPCId:               "vpc-deadbeef",
    21  	CIDRBlock:           "172.30.0.0/24",
    22  	AvailableIPCount:    250,
    23  	AvailZone:           "eu-west-1a",
    24  	DefaultForAZ:        false,
    25  	MapPublicIPOnLaunch: true,
    26  	Tags:                []amzec2.Tag{{Key: "Name", Value: "a"}},
    27  }, {
    28  	Id:                  "subnet-2345bcde",
    29  	State:               "available",
    30  	VPCId:               "vpc-deadbeef",
    31  	CIDRBlock:           "172.30.1.0/24",
    32  	AvailableIPCount:    250,
    33  	AvailZone:           "eu-west-1b",
    34  	DefaultForAZ:        false,
    35  	MapPublicIPOnLaunch: true,
    36  	Tags:                []amzec2.Tag{{Key: "Name", Value: "b"}},
    37  }, {
    38  	Id:                  "subnet-3456cdef",
    39  	State:               "available",
    40  	VPCId:               "vpc-deadbeef",
    41  	CIDRBlock:           "172.30.2.0/24",
    42  	AvailableIPCount:    250,
    43  	AvailZone:           "eu-west-1c",
    44  	DefaultForAZ:        false,
    45  	MapPublicIPOnLaunch: true,
    46  	Tags:                []amzec2.Tag{{Key: "Name", Value: "c"}},
    47  }, {
    48  	Id:                  "subnet-fedc6543",
    49  	State:               "available",
    50  	VPCId:               "vpc-deadbeef",
    51  	CIDRBlock:           "172.30.100.0/24",
    52  	AvailableIPCount:    250,
    53  	AvailZone:           "eu-west-1a",
    54  	DefaultForAZ:        false,
    55  	MapPublicIPOnLaunch: true,
    56  	Tags:                []amzec2.Tag{{Key: "Name", Value: "db-a"}},
    57  }, {
    58  	Id:                  "subnet-edcb5432",
    59  	State:               "available",
    60  	VPCId:               "vpc-deadbeef",
    61  	CIDRBlock:           "172.30.101.0/24",
    62  	AvailableIPCount:    250,
    63  	AvailZone:           "eu-west-1b",
    64  	DefaultForAZ:        false,
    65  	MapPublicIPOnLaunch: true,
    66  	Tags:                []amzec2.Tag{{Key: "Name", Value: "db-b"}},
    67  }, {
    68  	Id:                  "subnet-dcba4321",
    69  	State:               "available",
    70  	VPCId:               "vpc-deadbeef",
    71  	CIDRBlock:           "172.30.102.0/24",
    72  	AvailableIPCount:    250,
    73  	AvailZone:           "eu-west-1c",
    74  	DefaultForAZ:        false,
    75  	MapPublicIPOnLaunch: true,
    76  	Tags: []amzec2.Tag{
    77  		{Key: "UserTag", Value: "b"},
    78  		{Key: "Name", Value: "db-c"},
    79  	},
    80  }}
    81  
    82  func checkSubnetMatch(c *gc.C, query, expectedSubnetID string) {
    83  	matcher := ec2.CreateSubnetMatcher(query)
    84  	anyMatch := false
    85  	for _, subnet := range cannedSubnets {
    86  		match := matcher.Match(subnet)
    87  		if subnet.Id == expectedSubnetID {
    88  			c.Check(match, jc.IsTrue,
    89  				gc.Commentf("query %q was supposed to match subnet %#v", query, subnet))
    90  		} else {
    91  			c.Check(match, jc.IsFalse,
    92  				gc.Commentf("query %q was not supposed to match subnet %#v", query, subnet))
    93  		}
    94  		if match {
    95  			anyMatch = true
    96  		}
    97  	}
    98  	if expectedSubnetID == "" {
    99  		c.Check(anyMatch, jc.IsFalse, gc.Commentf("we expected there to be no matches"))
   100  	} else {
   101  		c.Check(anyMatch, jc.IsTrue, gc.Commentf("we expected to find at least one match, but found none"))
   102  	}
   103  }
   104  
   105  func (*subnetMatcherSuite) TestCIDRMatch(c *gc.C) {
   106  	checkSubnetMatch(c, "172.30.101.0/24", "subnet-edcb5432")
   107  	// We are a little lenient, the host portion doesn't matter as long as the subnet portion is the same
   108  	checkSubnetMatch(c, "172.30.101.50/24", "subnet-edcb5432")
   109  	// There is no such subnet (yet)
   110  	checkSubnetMatch(c, "172.30.103.0/24", "")
   111  }
   112  
   113  func (*subnetMatcherSuite) TestSubnetIDMatch(c *gc.C) {
   114  	checkSubnetMatch(c, "subnet-dcba4321", "subnet-dcba4321")
   115  	// Typo matches nothing
   116  	checkSubnetMatch(c, "subnet-dcba432", "")
   117  }
   118  
   119  func (*subnetMatcherSuite) TestSubnetNameMatch(c *gc.C) {
   120  	checkSubnetMatch(c, "b", "subnet-2345bcde")
   121  	// We shouldn't be confused by tags other than "Name"
   122  	checkSubnetMatch(c, "db-c", "subnet-dcba4321")
   123  	// No such named subnet
   124  	checkSubnetMatch(c, "db-q", "")
   125  }