github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 EBSProvider() jujustorage.Provider {
    23  	return &ebsProvider{}
    24  }
    25  
    26  func StorageEC2(vs jujustorage.VolumeSource) *ec2.EC2 {
    27  	return vs.(*ebsVolumeSource).ec2
    28  }
    29  
    30  func JujuGroupName(e environs.Environ) string {
    31  	return e.(*environ).jujuGroupName()
    32  }
    33  
    34  func MachineGroupName(e environs.Environ, machineId string) string {
    35  	return e.(*environ).machineGroupName(machineId)
    36  }
    37  
    38  func EnvironEC2(e environs.Environ) *ec2.EC2 {
    39  	return e.(*environ).ec2()
    40  }
    41  
    42  func InstanceEC2(inst instance.Instance) *ec2.Instance {
    43  	return inst.(*ec2Instance).Instance
    44  }
    45  
    46  func TerminatedInstances(e environs.Environ) ([]instance.Instance, error) {
    47  	return e.(*environ).AllInstancesByState("shutting-down", "terminated")
    48  }
    49  
    50  func InstanceSecurityGroups(e environs.Environ, ids []instance.Id, states ...string) ([]ec2.SecurityGroup, error) {
    51  	return e.(*environ).instanceSecurityGroups(ids, states...)
    52  }
    53  
    54  var (
    55  	EC2AvailabilityZones        = &ec2AvailabilityZones
    56  	AvailabilityZoneAllocations = &availabilityZoneAllocations
    57  	RunInstances                = &runInstances
    58  	BlockDeviceNamer            = blockDeviceNamer
    59  	GetBlockDeviceMappings      = getBlockDeviceMappings
    60  )
    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  	// 14.04:amd64
   157  	makeImage("ami-00000033", "ssd", "pv", "amd64", "14.04", "test"),
   158  	makeImage("ami-00000039", "ebs", "pv", "amd64", "14.04", "test"),
   159  	makeImage("ami-00000035", "ssd", "hvm", "amd64", "14.04", "test"),
   160  
   161  	// 14.04:i386
   162  	makeImage("ami-00000034", "ssd", "pv", "i386", "14.04", "test"),
   163  
   164  	// 12.10:amd64
   165  	makeImage("ami-01000035", "ssd", "hvm", "amd64", "12.10", "test"),
   166  
   167  	// 12.10:i386
   168  	makeImage("ami-01000034", "ssd", "pv", "i386", "12.10", "test"),
   169  
   170  	// 13.04:i386
   171  	makeImage("ami-02000034", "ssd", "pv", "i386", "13.04", "test"),
   172  }
   173  
   174  var TestImagesData = map[string]string{
   175  	"/streams/v1/index.json": `
   176          {
   177           "index": {
   178            "com.ubuntu.cloud:released": {
   179             "updated": "Wed, 01 May 2013 13:31:26 +0000",
   180             "clouds": [
   181              {
   182               "region": "test",
   183               "endpoint": "https://ec2.endpoint.com"
   184              }
   185             ],
   186             "cloudname": "aws",
   187             "datatype": "image-ids",
   188             "format": "products:1.0",
   189             "products": [
   190              "com.ubuntu.cloud:server:14.04:amd64",
   191              "com.ubuntu.cloud:server:14.04:i386",
   192              "com.ubuntu.cloud:server:14.04:amd64",
   193              "com.ubuntu.cloud:server:12.10:amd64",
   194              "com.ubuntu.cloud:server:12.10:i386",
   195              "com.ubuntu.cloud:server:13.04:i386"
   196             ],
   197             "path": "streams/v1/com.ubuntu.cloud:released:aws.json"
   198            }
   199           },
   200           "updated": "Wed, 01 May 2013 13:31:26 +0000",
   201           "format": "index:1.0"
   202          }
   203  `,
   204  	"/streams/v1/com.ubuntu.cloud:released:aws.json": `
   205  {
   206   "content_id": "com.ubuntu.cloud:released:aws",
   207   "products": {
   208     "com.ubuntu.cloud:server:14.04:amd64": {
   209       "release": "trusty",
   210       "version": "14.04",
   211       "arch": "amd64",
   212       "versions": {
   213         "20121218": {
   214           "items": {
   215             "usee1pi": {
   216               "root_store": "instance",
   217               "virt": "pv",
   218               "region": "us-east-1",
   219               "id": "ami-00000011"
   220             },
   221             "usww1pe": {
   222               "root_store": "ssd",
   223               "virt": "pv",
   224               "region": "eu-west-1",
   225               "id": "ami-00000016"
   226             },
   227             "apne1pe": {
   228               "root_store": "ssd",
   229               "virt": "pv",
   230               "region": "ap-northeast-1",
   231               "id": "ami-00000026"
   232             },
   233             "apne1he": {
   234               "root_store": "ssd",
   235               "virt": "hvm",
   236               "region": "ap-northeast-1",
   237               "id": "ami-00000087"
   238             },
   239             "test1peebs": {
   240               "root_store": "ssd",
   241               "virt": "pv",
   242               "region": "test",
   243               "id": "ami-00000033"
   244             },
   245             "test1pessd": {
   246               "root_store": "ebs",
   247               "virt": "pv",
   248               "region": "test",
   249               "id": "ami-00000039"
   250             },
   251             "test1he": {
   252               "root_store": "ssd",
   253               "virt": "hvm",
   254               "region": "test",
   255               "id": "ami-00000035"
   256             }
   257           },
   258           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   259           "label": "release"
   260         }
   261       }
   262     },
   263     "com.ubuntu.cloud:server:14.04:i386": {
   264       "release": "trusty",
   265       "version": "14.04",
   266       "arch": "i386",
   267       "versions": {
   268         "20121218": {
   269           "items": {
   270             "test1pe": {
   271               "root_store": "ssd",
   272               "virt": "pv",
   273               "region": "test",
   274               "id": "ami-00000034"
   275             },
   276             "apne1pe": {
   277               "root_store": "ssd",
   278               "virt": "pv",
   279               "region": "ap-northeast-1",
   280               "id": "ami-00000023"
   281             }
   282           },
   283           "pubname": "ubuntu-trusty-14.04-i386-server-20121218",
   284           "label": "release"
   285         }
   286       }
   287     },
   288     "com.ubuntu.cloud:server:12.10:amd64": {
   289       "release": "quantal",
   290       "version": "12.10",
   291       "arch": "amd64",
   292       "versions": {
   293         "20121218": {
   294           "items": {
   295             "usee1pi": {
   296               "root_store": "instance",
   297               "virt": "pv",
   298               "region": "us-east-1",
   299               "id": "ami-00000011"
   300             },
   301             "usww1pe": {
   302               "root_store": "ssd",
   303               "virt": "pv",
   304               "region": "eu-west-1",
   305               "id": "ami-01000016"
   306             },
   307             "apne1pe": {
   308               "root_store": "ssd",
   309               "virt": "pv",
   310               "region": "ap-northeast-1",
   311               "id": "ami-01000026"
   312             },
   313             "apne1he": {
   314               "root_store": "ssd",
   315               "virt": "hvm",
   316               "region": "ap-northeast-1",
   317               "id": "ami-01000087"
   318             },
   319             "test1he": {
   320               "root_store": "ssd",
   321               "virt": "hvm",
   322               "region": "test",
   323               "id": "ami-01000035"
   324             }
   325           },
   326           "pubname": "ubuntu-quantal-12.10-amd64-server-20121218",
   327           "label": "release"
   328         }
   329       }
   330     },
   331     "com.ubuntu.cloud:server:12.10:i386": {
   332       "release": "quantal",
   333       "version": "12.10",
   334       "arch": "i386",
   335       "versions": {
   336         "20121218": {
   337           "items": {
   338             "test1pe": {
   339               "root_store": "ssd",
   340               "virt": "pv",
   341               "region": "test",
   342               "id": "ami-01000034"
   343             },
   344             "apne1pe": {
   345               "root_store": "ssd",
   346               "virt": "pv",
   347               "region": "ap-northeast-1",
   348               "id": "ami-01000023"
   349             }
   350           },
   351           "pubname": "ubuntu-quantal-12.10-i386-server-20121218",
   352           "label": "release"
   353         }
   354       }
   355     },
   356     "com.ubuntu.cloud:server:13.04:i386": {
   357       "release": "raring",
   358       "version": "13.04",
   359       "arch": "i386",
   360       "versions": {
   361         "20121218": {
   362           "items": {
   363             "test1pe": {
   364               "root_store": "ssd",
   365               "virt": "pv",
   366               "region": "test",
   367               "id": "ami-02000034"
   368             }
   369           },
   370           "pubname": "ubuntu-raring-13.04-i386-server-20121218",
   371           "label": "release"
   372         }
   373       }
   374     }
   375   },
   376   "format": "products:1.0"
   377  }
   378  `,
   379  }
   380  
   381  var TestInstanceTypeCosts = instanceTypeCost{
   382  	"m1.small":    60,
   383  	"m1.medium":   120,
   384  	"m1.large":    240,
   385  	"m1.xlarge":   480,
   386  	"m3.medium":   95,
   387  	"m3.large":    190,
   388  	"m3.xlarge":   385,
   389  	"m3.2xlarge":  765,
   390  	"t1.micro":    20,
   391  	"c1.medium":   145,
   392  	"c1.xlarge":   580,
   393  	"cc2.8xlarge": 2400,
   394  }
   395  
   396  var TestRegions = map[string]aws.Region{
   397  	"test": {
   398  		Name:        "test",
   399  		EC2Endpoint: "https://ec2.endpoint.com",
   400  	},
   401  }