github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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 amzec2 "gopkg.in/amz.v3/ec2" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/constraints" 11 "github.com/juju/juju/environs" 12 "github.com/juju/juju/environs/config" 13 "github.com/juju/juju/environs/simplestreams" 14 "github.com/juju/juju/instance" 15 "github.com/juju/juju/network" 16 "github.com/juju/juju/state" 17 ) 18 19 // Ensure EC2 provider supports the expected interfaces, 20 var ( 21 _ environs.NetworkingEnviron = (*environ)(nil) 22 _ config.ConfigSchemaSource = (*environProvider)(nil) 23 _ simplestreams.HasRegion = (*environ)(nil) 24 _ state.Prechecker = (*environ)(nil) 25 _ instance.Distributor = (*environ)(nil) 26 ) 27 28 type Suite struct{} 29 30 var _ = gc.Suite(&Suite{}) 31 32 type RootDiskTest struct { 33 series string 34 name string 35 constraint *uint64 36 device amzec2.BlockDeviceMapping 37 } 38 39 var commonInstanceStoreDisks = []amzec2.BlockDeviceMapping{{ 40 DeviceName: "/dev/sdb", 41 VirtualName: "ephemeral0", 42 }, { 43 DeviceName: "/dev/sdc", 44 VirtualName: "ephemeral1", 45 }, { 46 DeviceName: "/dev/sdd", 47 VirtualName: "ephemeral2", 48 }, { 49 DeviceName: "/dev/sde", 50 VirtualName: "ephemeral3", 51 }} 52 53 func (*Suite) TestRootDiskBlockDeviceMapping(c *gc.C) { 54 var rootDiskTests = []RootDiskTest{{ 55 "trusty", 56 "nil constraint ubuntu", 57 nil, 58 amzec2.BlockDeviceMapping{VolumeSize: 8, DeviceName: "/dev/sda1"}, 59 }, { 60 "trusty", 61 "too small constraint ubuntu", 62 pInt(4000), 63 amzec2.BlockDeviceMapping{VolumeSize: 8, DeviceName: "/dev/sda1"}, 64 }, { 65 "trusty", 66 "big constraint ubuntu", 67 pInt(20 * 1024), 68 amzec2.BlockDeviceMapping{VolumeSize: 20, DeviceName: "/dev/sda1"}, 69 }, { 70 "trusty", 71 "round up constraint ubuntu", 72 pInt(20*1024 + 1), 73 amzec2.BlockDeviceMapping{VolumeSize: 21, DeviceName: "/dev/sda1"}, 74 }, { 75 "win2012r2", 76 "nil constraint windows", 77 nil, 78 amzec2.BlockDeviceMapping{VolumeSize: 40, DeviceName: "/dev/sda1"}, 79 }, { 80 "win2012r2", 81 "too small constraint windows", 82 pInt(30 * 1024), 83 amzec2.BlockDeviceMapping{VolumeSize: 40, DeviceName: "/dev/sda1"}, 84 }, { 85 "win2012r2", 86 "big constraint windows", 87 pInt(50 * 1024), 88 amzec2.BlockDeviceMapping{VolumeSize: 50, DeviceName: "/dev/sda1"}, 89 }, { 90 "win2012r2", 91 "round up constraint windows", 92 pInt(50*1024 + 1), 93 amzec2.BlockDeviceMapping{VolumeSize: 51, DeviceName: "/dev/sda1"}, 94 }} 95 96 for _, t := range rootDiskTests { 97 c.Logf("Test %s", t.name) 98 cons := constraints.Value{RootDisk: t.constraint} 99 mappings := getBlockDeviceMappings(cons, t.series, false) 100 expected := append([]amzec2.BlockDeviceMapping{t.device}, commonInstanceStoreDisks...) 101 c.Assert(mappings, gc.DeepEquals, expected) 102 } 103 } 104 105 func pInt(i uint64) *uint64 { 106 return &i 107 } 108 109 func (*Suite) TestPortsToIPPerms(c *gc.C) { 110 testCases := []struct { 111 about string 112 ports []network.PortRange 113 expected []amzec2.IPPerm 114 }{{ 115 about: "single port", 116 ports: []network.PortRange{{ 117 FromPort: 80, 118 ToPort: 80, 119 Protocol: "tcp", 120 }}, 121 expected: []amzec2.IPPerm{{ 122 Protocol: "tcp", 123 FromPort: 80, 124 ToPort: 80, 125 SourceIPs: []string{"0.0.0.0/0"}, 126 }}, 127 }, { 128 about: "multiple ports", 129 ports: []network.PortRange{{ 130 FromPort: 80, 131 ToPort: 82, 132 Protocol: "tcp", 133 }}, 134 expected: []amzec2.IPPerm{{ 135 Protocol: "tcp", 136 FromPort: 80, 137 ToPort: 82, 138 SourceIPs: []string{"0.0.0.0/0"}, 139 }}, 140 }, { 141 about: "multiple port ranges", 142 ports: []network.PortRange{{ 143 FromPort: 80, 144 ToPort: 82, 145 Protocol: "tcp", 146 }, { 147 FromPort: 100, 148 ToPort: 120, 149 Protocol: "tcp", 150 }}, 151 expected: []amzec2.IPPerm{{ 152 Protocol: "tcp", 153 FromPort: 80, 154 ToPort: 82, 155 SourceIPs: []string{"0.0.0.0/0"}, 156 }, { 157 Protocol: "tcp", 158 FromPort: 100, 159 ToPort: 120, 160 SourceIPs: []string{"0.0.0.0/0"}, 161 }}, 162 }} 163 164 for i, t := range testCases { 165 c.Logf("test %d: %s", i, t.about) 166 ipperms := portsToIPPerms(t.ports) 167 c.Assert(ipperms, gc.DeepEquals, t.expected) 168 } 169 }