github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	amzec2 "gopkg.in/amz.v3/ec2"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/core/constraints"
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/config"
    15  	"github.com/juju/juju/environs/context"
    16  	"github.com/juju/juju/environs/simplestreams"
    17  	"github.com/juju/juju/network"
    18  )
    19  
    20  // Ensure EC2 provider supports the expected interfaces,
    21  var (
    22  	_ environs.NetworkingEnviron = (*environ)(nil)
    23  	_ config.ConfigSchemaSource  = (*environProvider)(nil)
    24  	_ simplestreams.HasRegion    = (*environ)(nil)
    25  	_ context.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  		rules    []network.IngressRule
   113  		expected []amzec2.IPPerm
   114  	}{{
   115  		about: "single port",
   116  		rules: []network.IngressRule{network.MustNewIngressRule("tcp", 80, 80)},
   117  		expected: []amzec2.IPPerm{{
   118  			Protocol:  "tcp",
   119  			FromPort:  80,
   120  			ToPort:    80,
   121  			SourceIPs: []string{"0.0.0.0/0"},
   122  		}},
   123  	}, {
   124  		about: "multiple ports",
   125  		rules: []network.IngressRule{network.MustNewIngressRule("tcp", 80, 82)},
   126  		expected: []amzec2.IPPerm{{
   127  			Protocol:  "tcp",
   128  			FromPort:  80,
   129  			ToPort:    82,
   130  			SourceIPs: []string{"0.0.0.0/0"},
   131  		}},
   132  	}, {
   133  		about: "multiple port ranges",
   134  		rules: []network.IngressRule{
   135  			network.MustNewIngressRule("tcp", 80, 82),
   136  			network.MustNewIngressRule("tcp", 100, 120),
   137  		},
   138  		expected: []amzec2.IPPerm{{
   139  			Protocol:  "tcp",
   140  			FromPort:  80,
   141  			ToPort:    82,
   142  			SourceIPs: []string{"0.0.0.0/0"},
   143  		}, {
   144  			Protocol:  "tcp",
   145  			FromPort:  100,
   146  			ToPort:    120,
   147  			SourceIPs: []string{"0.0.0.0/0"},
   148  		}},
   149  	}, {
   150  		about: "source ranges",
   151  		rules: []network.IngressRule{network.MustNewIngressRule("tcp", 80, 82, "192.168.1.0/24", "0.0.0.0/0")},
   152  		expected: []amzec2.IPPerm{{
   153  			Protocol:  "tcp",
   154  			FromPort:  80,
   155  			ToPort:    82,
   156  			SourceIPs: []string{"192.168.1.0/24", "0.0.0.0/0"},
   157  		}},
   158  	}}
   159  
   160  	for i, t := range testCases {
   161  		c.Logf("test %d: %s", i, t.about)
   162  		ipperms := rulesToIPPerms(t.rules)
   163  		c.Assert(ipperms, gc.DeepEquals, t.expected)
   164  	}
   165  }
   166  
   167  // These Support checks are currently valid with a 'nil' environ pointer. If
   168  // that changes, the tests will need to be updated. (we know statically what is
   169  // supported.)
   170  func (*Suite) TestSupportsNetworking(c *gc.C) {
   171  	var env *environ
   172  	_, supported := environs.SupportsNetworking(env)
   173  	c.Assert(supported, jc.IsTrue)
   174  }
   175  
   176  func (*Suite) TestSupportsSpaces(c *gc.C) {
   177  	callCtx := context.NewCloudCallContext()
   178  	var env *environ
   179  	supported, err := env.SupportsSpaces(callCtx)
   180  	c.Assert(err, jc.ErrorIsNil)
   181  	c.Assert(supported, jc.IsTrue)
   182  	c.Check(environs.SupportsSpaces(callCtx, env), jc.IsTrue)
   183  }
   184  
   185  func (*Suite) TestSupportsSpaceDiscovery(c *gc.C) {
   186  	var env *environ
   187  	supported, err := env.SupportsSpaceDiscovery(context.NewCloudCallContext())
   188  	// TODO(jam): 2016-02-01 the comment on the interface says the error should
   189  	// conform to IsNotSupported, but all of the implementations just return
   190  	// nil for error and 'false' for supported.
   191  	c.Assert(err, jc.ErrorIsNil)
   192  	c.Assert(supported, jc.IsFalse)
   193  }
   194  
   195  func (*Suite) TestSupportsContainerAddresses(c *gc.C) {
   196  	callCtx := context.NewCloudCallContext()
   197  	var env *environ
   198  	supported, err := env.SupportsContainerAddresses(callCtx)
   199  	c.Assert(err, jc.Satisfies, errors.IsNotSupported)
   200  	c.Assert(supported, jc.IsFalse)
   201  	c.Check(environs.SupportsContainerAddresses(callCtx, env), jc.IsFalse)
   202  }