github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/ec2/export_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package ec2
     5  
     6  import (
     7  	"io"
     8  
     9  	"gopkg.in/amz.v3/aws"
    10  	"gopkg.in/amz.v3/ec2"
    11  	"gopkg.in/amz.v3/s3"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/environs"
    15  	"github.com/juju/juju/environs/imagemetadata"
    16  	sstesting "github.com/juju/juju/environs/simplestreams/testing"
    17  	"github.com/juju/juju/environs/storage"
    18  	"github.com/juju/juju/instance"
    19  	jujustorage "github.com/juju/juju/storage"
    20  )
    21  
    22  func StorageEC2(vs jujustorage.VolumeSource) *ec2.EC2 {
    23  	return vs.(*ebsVolumeSource).env.ec2
    24  }
    25  
    26  func JujuGroupName(e environs.Environ) string {
    27  	return e.(*environ).jujuGroupName()
    28  }
    29  
    30  func MachineGroupName(e environs.Environ, machineId string) string {
    31  	return e.(*environ).machineGroupName(machineId)
    32  }
    33  
    34  func EnvironEC2(e environs.Environ) *ec2.EC2 {
    35  	return e.(*environ).ec2
    36  }
    37  
    38  func InstanceEC2(inst instance.Instance) *ec2.Instance {
    39  	return inst.(*ec2Instance).Instance
    40  }
    41  
    42  func TerminatedInstances(e environs.Environ) ([]instance.Instance, error) {
    43  	return e.(*environ).AllInstancesByState("shutting-down", "terminated")
    44  }
    45  
    46  func InstanceSecurityGroups(e environs.Environ, ids []instance.Id, states ...string) ([]ec2.SecurityGroup, error) {
    47  	return e.(*environ).instanceSecurityGroups(ids, states...)
    48  }
    49  
    50  var (
    51  	EC2AvailabilityZones        = &ec2AvailabilityZones
    52  	AvailabilityZoneAllocations = &availabilityZoneAllocations
    53  	RunInstances                = &runInstances
    54  	BlockDeviceNamer            = blockDeviceNamer
    55  	GetBlockDeviceMappings      = getBlockDeviceMappings
    56  	IsVPCNotUsableError         = isVPCNotUsableError
    57  	IsVPCNotRecommendedError    = isVPCNotRecommendedError
    58  )
    59  
    60  const VPCIDNone = vpcIDNone
    61  
    62  // BucketStorage returns a storage instance addressing
    63  // an arbitrary s3 bucket.
    64  func BucketStorage(b *s3.Bucket) storage.Storage {
    65  	return &ec2storage{
    66  		bucket: b,
    67  	}
    68  }
    69  
    70  // DeleteBucket deletes the s3 bucket used by the storage instance.
    71  func DeleteBucket(s storage.Storage) error {
    72  	return deleteBucket(s.(*ec2storage))
    73  }
    74  
    75  // TODO: Apart from overriding different hardcoded hosts, these two test helpers are identical. Let's share.
    76  
    77  // UseTestImageData causes the given content to be served
    78  // when the ec2 client asks for image data.
    79  func UseTestImageData(c *gc.C, files map[string]string) {
    80  	if files != nil {
    81  		sstesting.SetRoundTripperFiles(sstesting.AddSignedFiles(c, files), nil)
    82  	} else {
    83  		sstesting.SetRoundTripperFiles(nil, nil)
    84  	}
    85  }
    86  
    87  func UseTestRegionData(content map[string]aws.Region) {
    88  	if content != nil {
    89  		allRegions = content
    90  	} else {
    91  		allRegions = aws.Regions
    92  	}
    93  }
    94  
    95  // UseTestInstanceTypeData causes the given instance type
    96  // cost data to be served for the "test" region.
    97  func UseTestInstanceTypeData(content instanceTypeCost) {
    98  	if content != nil {
    99  		allRegionCosts["test"] = content
   100  	} else {
   101  		delete(allRegionCosts, "test")
   102  	}
   103  }
   104  
   105  var (
   106  	ShortAttempt                   = &shortAttempt
   107  	StorageAttempt                 = &storageAttempt
   108  	DestroyVolumeAttempt           = &destroyVolumeAttempt
   109  	DeleteSecurityGroupInsistently = &deleteSecurityGroupInsistently
   110  	TerminateInstancesById         = &terminateInstancesById
   111  )
   112  
   113  func EC2ErrCode(err error) string {
   114  	return ec2ErrCode(err)
   115  }
   116  
   117  // FabricateInstance creates a new fictitious instance
   118  // given an existing instance and a new id.
   119  func FabricateInstance(inst instance.Instance, newId string) instance.Instance {
   120  	oldi := inst.(*ec2Instance)
   121  	newi := &ec2Instance{
   122  		e:        oldi.e,
   123  		Instance: &ec2.Instance{},
   124  	}
   125  	*newi.Instance = *oldi.Instance
   126  	newi.InstanceId = newId
   127  	return newi
   128  }
   129  
   130  // Access non exported methods on ec2.storage
   131  type Storage interface {
   132  	Put(file string, r io.Reader, length int64) error
   133  	ResetMadeBucket()
   134  }
   135  
   136  func (s *ec2storage) ResetMadeBucket() {
   137  	s.Lock()
   138  	defer s.Unlock()
   139  	s.madeBucket = false
   140  }
   141  
   142  func makeImage(id, storage, virtType, arch, version, region string) *imagemetadata.ImageMetadata {
   143  	return &imagemetadata.ImageMetadata{
   144  		Id:         id,
   145  		Storage:    storage,
   146  		VirtType:   virtType,
   147  		Arch:       arch,
   148  		Version:    version,
   149  		RegionName: region,
   150  		Endpoint:   "https://ec2.endpoint.com",
   151  		Stream:     "released",
   152  	}
   153  }
   154  
   155  var TestImageMetadata = []*imagemetadata.ImageMetadata{
   156  	// LTS-dependent requires new entries upon new LTS release.
   157  	// 16.04:amd64
   158  	makeImage("ami-00000133", "ssd", "pv", "amd64", "16.04", "test"),
   159  	makeImage("ami-00000139", "ebs", "pv", "amd64", "16.04", "test"),
   160  	makeImage("ami-00000135", "ssd", "hvm", "amd64", "16.04", "test"),
   161  
   162  	// 14.04:amd64
   163  	makeImage("ami-00000033", "ssd", "pv", "amd64", "14.04", "test"),
   164  
   165  	// 14.04:i386
   166  	makeImage("ami-00000034", "ssd", "pv", "i386", "14.04", "test"),
   167  
   168  	// 12.10:amd64
   169  	makeImage("ami-01000035", "ssd", "hvm", "amd64", "12.10", "test"),
   170  
   171  	// 12.10:i386
   172  	makeImage("ami-01000034", "ssd", "pv", "i386", "12.10", "test"),
   173  
   174  	// 13.04:i386
   175  	makeImage("ami-02000034", "ssd", "pv", "i386", "13.04", "test"),
   176  }
   177  
   178  var TestImagesData = map[string]string{
   179  	// LTS-dependent requires new/updated entries upon new LTS release.
   180  	"/streams/v1/index.json": `
   181          {
   182           "index": {
   183            "com.ubuntu.cloud:released": {
   184             "updated": "Wed, 01 May 2013 13:31:26 +0000",
   185             "clouds": [
   186              {
   187               "region": "test",
   188               "endpoint": "https://ec2.endpoint.com"
   189              }
   190             ],
   191             "cloudname": "aws",
   192             "datatype": "image-ids",
   193             "format": "products:1.0",
   194             "products": [
   195              "com.ubuntu.cloud:server:16.04:amd64",
   196              "com.ubuntu.cloud:server:14.04:amd64",
   197              "com.ubuntu.cloud:server:14.04:i386",
   198              "com.ubuntu.cloud:server:12.10:i386",
   199              "com.ubuntu.cloud:server:13.04:i386"
   200             ],
   201             "path": "streams/v1/com.ubuntu.cloud:released:aws.json"
   202            }
   203           },
   204           "updated": "Wed, 01 May 2013 13:31:26 +0000",
   205           "format": "index:1.0"
   206          }
   207  `,
   208  	"/streams/v1/com.ubuntu.cloud:released:aws.json": `
   209  {
   210   "content_id": "com.ubuntu.cloud:released:aws",
   211   "products": {
   212     "com.ubuntu.cloud:server:16.04:amd64": {
   213       "release": "trusty",
   214       "version": "16.04",
   215       "arch": "amd64",
   216       "versions": {
   217         "20121218": {
   218           "items": {
   219             "usee1pi": {
   220               "root_store": "instance",
   221               "virt": "pv",
   222               "region": "us-east-1",
   223               "id": "ami-00000111"
   224             },
   225             "usww1pe": {
   226               "root_store": "ssd",
   227               "virt": "pv",
   228               "region": "eu-west-1",
   229               "id": "ami-00000116"
   230             },
   231             "apne1pe": {
   232               "root_store": "ssd",
   233               "virt": "pv",
   234               "region": "ap-northeast-1",
   235               "id": "ami-00000126"
   236             },
   237             "apne1he": {
   238               "root_store": "ssd",
   239               "virt": "hvm",
   240               "region": "ap-northeast-1",
   241               "id": "ami-00000187"
   242             },
   243             "test1peebs": {
   244               "root_store": "ssd",
   245               "virt": "pv",
   246               "region": "test",
   247               "id": "ami-00000133"
   248             },
   249             "test1pessd": {
   250               "root_store": "ebs",
   251               "virt": "pv",
   252               "region": "test",
   253               "id": "ami-00000139"
   254             },
   255             "test1he": {
   256               "root_store": "ssd",
   257               "virt": "hvm",
   258               "region": "test",
   259               "id": "ami-00000135"
   260             }
   261           },
   262           "pubname": "ubuntu-trusty-16.04-amd64-server-20121218",
   263           "label": "release"
   264         }
   265       }
   266     },
   267     "com.ubuntu.cloud:server:14.04:amd64": {
   268       "release": "trusty",
   269       "version": "14.04",
   270       "arch": "amd64",
   271       "versions": {
   272         "20121218": {
   273           "items": {
   274             "test1peebs": {
   275               "root_store": "ssd",
   276               "virt": "pv",
   277               "region": "test",
   278               "id": "ami-00000033"
   279  			}
   280           },
   281           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   282           "label": "release"
   283         }
   284       }
   285     },
   286     "com.ubuntu.cloud:server:14.04:i386": {
   287       "release": "trusty",
   288       "version": "14.04",
   289       "arch": "i386",
   290       "versions": {
   291         "20121218": {
   292           "items": {
   293             "test1pe": {
   294               "root_store": "ssd",
   295               "virt": "pv",
   296               "region": "test",
   297               "id": "ami-00000034"
   298             }
   299           },
   300           "pubname": "ubuntu-trusty-14.04-i386-server-20121218",
   301           "label": "release"
   302         }
   303       }
   304     },
   305     "com.ubuntu.cloud:server:12.10:amd64": {
   306       "release": "quantal",
   307       "version": "12.10",
   308       "arch": "amd64",
   309       "versions": {
   310         "20121218": {
   311           "items": {
   312             "usee1pi": {
   313               "root_store": "instance",
   314               "virt": "pv",
   315               "region": "us-east-1",
   316               "id": "ami-00000011"
   317             },
   318             "usww1pe": {
   319               "root_store": "ssd",
   320               "virt": "pv",
   321               "region": "eu-west-1",
   322               "id": "ami-01000016"
   323             },
   324             "apne1pe": {
   325               "root_store": "ssd",
   326               "virt": "pv",
   327               "region": "ap-northeast-1",
   328               "id": "ami-01000026"
   329             },
   330             "apne1he": {
   331               "root_store": "ssd",
   332               "virt": "hvm",
   333               "region": "ap-northeast-1",
   334               "id": "ami-01000087"
   335             },
   336             "test1he": {
   337               "root_store": "ssd",
   338               "virt": "hvm",
   339               "region": "test",
   340               "id": "ami-01000035"
   341             }
   342           },
   343           "pubname": "ubuntu-quantal-12.10-amd64-server-20121218",
   344           "label": "release"
   345         }
   346       }
   347     },
   348     "com.ubuntu.cloud:server:12.10:i386": {
   349       "release": "quantal",
   350       "version": "12.10",
   351       "arch": "i386",
   352       "versions": {
   353         "20121218": {
   354           "items": {
   355             "test1pe": {
   356               "root_store": "ssd",
   357               "virt": "pv",
   358               "region": "test",
   359               "id": "ami-01000034"
   360             },
   361             "apne1pe": {
   362               "root_store": "ssd",
   363               "virt": "pv",
   364               "region": "ap-northeast-1",
   365               "id": "ami-01000023"
   366             }
   367           },
   368           "pubname": "ubuntu-quantal-12.10-i386-server-20121218",
   369           "label": "release"
   370         }
   371       }
   372     },
   373     "com.ubuntu.cloud:server:13.04:i386": {
   374       "release": "raring",
   375       "version": "13.04",
   376       "arch": "i386",
   377       "versions": {
   378         "20121218": {
   379           "items": {
   380             "test1pe": {
   381               "root_store": "ssd",
   382               "virt": "pv",
   383               "region": "test",
   384               "id": "ami-02000034"
   385             }
   386           },
   387           "pubname": "ubuntu-raring-13.04-i386-server-20121218",
   388           "label": "release"
   389         }
   390       }
   391     }
   392   },
   393   "format": "products:1.0"
   394  }
   395  `,
   396  }
   397  
   398  var TestInstanceTypeCosts = instanceTypeCost{
   399  	"m1.small":    60,
   400  	"m1.medium":   120,
   401  	"m1.large":    240,
   402  	"m1.xlarge":   480,
   403  	"m3.medium":   95,
   404  	"m3.large":    190,
   405  	"m3.xlarge":   385,
   406  	"m3.2xlarge":  765,
   407  	"t1.micro":    20,
   408  	"c1.medium":   145,
   409  	"c1.xlarge":   580,
   410  	"cc2.8xlarge": 2400,
   411  }
   412  
   413  var TestRegions = map[string]aws.Region{
   414  	"test": {
   415  		Name:        "test",
   416  		EC2Endpoint: "https://ec2.endpoint.com",
   417  	},
   418  }