github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/ec2/environ_test.go (about)

     1  // Copyright 2011, 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package ec2
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	amzec2 "gopkg.in/amz.v3/ec2"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/constraints"
    12  	"github.com/juju/juju/network"
    13  )
    14  
    15  type Suite struct{}
    16  
    17  var _ = gc.Suite(&Suite{})
    18  
    19  type RootDiskTest struct {
    20  	name       string
    21  	constraint *uint64
    22  	disksize   uint64
    23  	device     amzec2.BlockDeviceMapping
    24  }
    25  
    26  var commonInstanceStoreDisks = []amzec2.BlockDeviceMapping{{
    27  	DeviceName:  "/dev/sdb",
    28  	VirtualName: "ephemeral0",
    29  }, {
    30  	DeviceName:  "/dev/sdc",
    31  	VirtualName: "ephemeral1",
    32  }, {
    33  	DeviceName:  "/dev/sdd",
    34  	VirtualName: "ephemeral2",
    35  }, {
    36  	DeviceName:  "/dev/sde",
    37  	VirtualName: "ephemeral3",
    38  }}
    39  
    40  var rootDiskTests = []RootDiskTest{
    41  	{
    42  		"nil constraint",
    43  		nil,
    44  		8192,
    45  		amzec2.BlockDeviceMapping{VolumeSize: 8, DeviceName: "/dev/sda1"},
    46  	},
    47  	{
    48  		"too small constraint",
    49  		pInt(4000),
    50  		8192,
    51  		amzec2.BlockDeviceMapping{VolumeSize: 8, DeviceName: "/dev/sda1"},
    52  	},
    53  	{
    54  		"big constraint",
    55  		pInt(20 * 1024),
    56  		20 * 1024,
    57  		amzec2.BlockDeviceMapping{VolumeSize: 20, DeviceName: "/dev/sda1"},
    58  	},
    59  	{
    60  		"round up constraint",
    61  		pInt(20*1024 + 1),
    62  		21 * 1024,
    63  		amzec2.BlockDeviceMapping{VolumeSize: 21, DeviceName: "/dev/sda1"},
    64  	},
    65  }
    66  
    67  func (*Suite) TestRootDiskBlockDeviceMapping(c *gc.C) {
    68  	for _, t := range rootDiskTests {
    69  		c.Logf("Test %s", t.name)
    70  		cons := constraints.Value{RootDisk: t.constraint}
    71  		mappings, err := getBlockDeviceMappings(cons)
    72  		c.Assert(err, jc.ErrorIsNil)
    73  		expected := append([]amzec2.BlockDeviceMapping{t.device}, commonInstanceStoreDisks...)
    74  		c.Assert(mappings, gc.DeepEquals, expected)
    75  	}
    76  }
    77  
    78  func pInt(i uint64) *uint64 {
    79  	return &i
    80  }
    81  
    82  func (*Suite) TestPortsToIPPerms(c *gc.C) {
    83  	testCases := []struct {
    84  		about    string
    85  		ports    []network.PortRange
    86  		expected []amzec2.IPPerm
    87  	}{{
    88  		about: "single port",
    89  		ports: []network.PortRange{{
    90  			FromPort: 80,
    91  			ToPort:   80,
    92  			Protocol: "tcp",
    93  		}},
    94  		expected: []amzec2.IPPerm{{
    95  			Protocol:  "tcp",
    96  			FromPort:  80,
    97  			ToPort:    80,
    98  			SourceIPs: []string{"0.0.0.0/0"},
    99  		}},
   100  	}, {
   101  		about: "multiple ports",
   102  		ports: []network.PortRange{{
   103  			FromPort: 80,
   104  			ToPort:   82,
   105  			Protocol: "tcp",
   106  		}},
   107  		expected: []amzec2.IPPerm{{
   108  			Protocol:  "tcp",
   109  			FromPort:  80,
   110  			ToPort:    82,
   111  			SourceIPs: []string{"0.0.0.0/0"},
   112  		}},
   113  	}, {
   114  		about: "multiple port ranges",
   115  		ports: []network.PortRange{{
   116  			FromPort: 80,
   117  			ToPort:   82,
   118  			Protocol: "tcp",
   119  		}, {
   120  			FromPort: 100,
   121  			ToPort:   120,
   122  			Protocol: "tcp",
   123  		}},
   124  		expected: []amzec2.IPPerm{{
   125  			Protocol:  "tcp",
   126  			FromPort:  80,
   127  			ToPort:    82,
   128  			SourceIPs: []string{"0.0.0.0/0"},
   129  		}, {
   130  			Protocol:  "tcp",
   131  			FromPort:  100,
   132  			ToPort:    120,
   133  			SourceIPs: []string{"0.0.0.0/0"},
   134  		}},
   135  	}}
   136  
   137  	for i, t := range testCases {
   138  		c.Logf("test %d: %s", i, t.about)
   139  		ipperms := portsToIPPerms(t.ports)
   140  		c.Assert(ipperms, gc.DeepEquals, t.expected)
   141  	}
   142  }