github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/imagemetadata"
    15  	"github.com/juju/juju/environs/jujutest"
    16  	"github.com/juju/juju/environs/storage"
    17  	"github.com/juju/juju/instance"
    18  	jujustorage "github.com/juju/juju/storage"
    19  )
    20  
    21  func EBSProvider() jujustorage.Provider {
    22  	return &ebsProvider{}
    23  }
    24  
    25  func StorageEC2(vs jujustorage.VolumeSource) *ec2.EC2 {
    26  	return vs.(*ebsVolumeSource).ec2
    27  }
    28  
    29  func ControlBucketName(e environs.Environ) string {
    30  	return e.(*environ).ecfg().controlBucket()
    31  }
    32  
    33  func JujuGroupName(e environs.Environ) string {
    34  	return e.(*environ).jujuGroupName()
    35  }
    36  
    37  func MachineGroupName(e environs.Environ, machineId string) string {
    38  	return e.(*environ).machineGroupName(machineId)
    39  }
    40  
    41  func EnvironEC2(e environs.Environ) *ec2.EC2 {
    42  	return e.(*environ).ec2()
    43  }
    44  
    45  func EnvironS3(e environs.Environ) *s3.S3 {
    46  	return e.(*environ).s3()
    47  }
    48  
    49  func InstanceEC2(inst instance.Instance) *ec2.Instance {
    50  	return inst.(*ec2Instance).Instance
    51  }
    52  
    53  var (
    54  	EC2AvailabilityZones        = &ec2AvailabilityZones
    55  	AvailabilityZoneAllocations = &availabilityZoneAllocations
    56  	RunInstances                = &runInstances
    57  	BlockDeviceNamer            = blockDeviceNamer
    58  	GetBlockDeviceMappings      = getBlockDeviceMappings
    59  )
    60  
    61  // BucketStorage returns a storage instance addressing
    62  // an arbitrary s3 bucket.
    63  func BucketStorage(b *s3.Bucket) storage.Storage {
    64  	return &ec2storage{
    65  		bucket: b,
    66  	}
    67  }
    68  
    69  // DeleteBucket deletes the s3 bucket used by the storage instance.
    70  func DeleteBucket(s storage.Storage) error {
    71  	return deleteBucket(s.(*ec2storage))
    72  }
    73  
    74  var testRoundTripper = &jujutest.ProxyRoundTripper{}
    75  
    76  func init() {
    77  	// Prepare mock http transport for overriding metadata and images output in tests.
    78  	testRoundTripper.RegisterForScheme("test")
    79  }
    80  
    81  // TODO: Apart from overriding different hardcoded hosts, these two test helpers are identical. Let's share.
    82  
    83  var origImagesUrl = imagemetadata.DefaultBaseURL
    84  
    85  // UseTestImageData causes the given content to be served
    86  // when the ec2 client asks for image data.
    87  func UseTestImageData(files map[string]string) {
    88  	if files != nil {
    89  		testRoundTripper.Sub = jujutest.NewCannedRoundTripper(files, nil)
    90  		imagemetadata.DefaultBaseURL = "test:"
    91  		signedImageDataOnly = false
    92  	} else {
    93  		signedImageDataOnly = true
    94  		testRoundTripper.Sub = nil
    95  		imagemetadata.DefaultBaseURL = origImagesUrl
    96  	}
    97  }
    98  
    99  func UseTestRegionData(content map[string]aws.Region) {
   100  	if content != nil {
   101  		allRegions = content
   102  	} else {
   103  		allRegions = aws.Regions
   104  	}
   105  
   106  }
   107  
   108  // UseTestInstanceTypeData causes the given instance type
   109  // cost data to be served for the "test" region.
   110  func UseTestInstanceTypeData(content instanceTypeCost) {
   111  	if content != nil {
   112  		allRegionCosts["test"] = content
   113  	} else {
   114  		delete(allRegionCosts, "test")
   115  	}
   116  }
   117  
   118  var (
   119  	ShortAttempt         = &shortAttempt
   120  	StorageAttempt       = &storageAttempt
   121  	DestroyVolumeAttempt = &destroyVolumeAttempt
   122  )
   123  
   124  func EC2ErrCode(err error) string {
   125  	return ec2ErrCode(err)
   126  }
   127  
   128  // FabricateInstance creates a new fictitious instance
   129  // given an existing instance and a new id.
   130  func FabricateInstance(inst instance.Instance, newId string) instance.Instance {
   131  	oldi := inst.(*ec2Instance)
   132  	newi := &ec2Instance{
   133  		e:        oldi.e,
   134  		Instance: &ec2.Instance{},
   135  	}
   136  	*newi.Instance = *oldi.Instance
   137  	newi.InstanceId = newId
   138  	return newi
   139  }
   140  
   141  // Access non exported methods on ec2.storage
   142  type Storage interface {
   143  	Put(file string, r io.Reader, length int64) error
   144  	ResetMadeBucket()
   145  }
   146  
   147  func (s *ec2storage) ResetMadeBucket() {
   148  	s.Lock()
   149  	defer s.Unlock()
   150  	s.madeBucket = false
   151  }
   152  
   153  var TestImagesData = map[string]string{
   154  	"/streams/v1/index.json": `
   155          {
   156           "index": {
   157            "com.ubuntu.cloud:released": {
   158             "updated": "Wed, 01 May 2013 13:31:26 +0000",
   159             "clouds": [
   160              {
   161               "region": "test",
   162               "endpoint": "https://ec2.endpoint.com"
   163              }
   164             ],
   165             "cloudname": "aws",
   166             "datatype": "image-ids",
   167             "format": "products:1.0",
   168             "products": [
   169              "com.ubuntu.cloud:server:14.04:amd64",
   170              "com.ubuntu.cloud:server:14.04:i386",
   171              "com.ubuntu.cloud:server:14.04:amd64",
   172              "com.ubuntu.cloud:server:12.10:amd64",
   173              "com.ubuntu.cloud:server:12.10:i386",
   174              "com.ubuntu.cloud:server:13.04:i386"
   175             ],
   176             "path": "streams/v1/com.ubuntu.cloud:released:aws.js"
   177            }
   178           },
   179           "updated": "Wed, 01 May 2013 13:31:26 +0000",
   180           "format": "index:1.0"
   181          }
   182  `,
   183  	"/streams/v1/com.ubuntu.cloud:released:aws.js": `
   184  {
   185   "content_id": "com.ubuntu.cloud:released:aws",
   186   "products": {
   187     "com.ubuntu.cloud:server:14.04:amd64": {
   188       "release": "trusty",
   189       "version": "14.04",
   190       "arch": "amd64",
   191       "versions": {
   192         "20121218": {
   193           "items": {
   194             "usee1pi": {
   195               "root_store": "instance",
   196               "virt": "pv",
   197               "region": "us-east-1",
   198               "id": "ami-00000011"
   199             },
   200             "usww1pe": {
   201               "root_store": "ssd",
   202               "virt": "pv",
   203               "region": "eu-west-1",
   204               "id": "ami-00000016"
   205             },
   206             "apne1pe": {
   207               "root_store": "ssd",
   208               "virt": "pv",
   209               "region": "ap-northeast-1",
   210               "id": "ami-00000026"
   211             },
   212             "apne1he": {
   213               "root_store": "ssd",
   214               "virt": "hvm",
   215               "region": "ap-northeast-1",
   216               "id": "ami-00000087"
   217             },
   218             "test1peebs": {
   219               "root_store": "ssd",
   220               "virt": "pv",
   221               "region": "test",
   222               "id": "ami-00000033"
   223             },
   224             "test1pessd": {
   225               "root_store": "ebs",
   226               "virt": "pv",
   227               "region": "test",
   228               "id": "ami-00000039"
   229             },
   230             "test1he": {
   231               "root_store": "ssd",
   232               "virt": "hvm",
   233               "region": "test",
   234               "id": "ami-00000035"
   235             }
   236           },
   237           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   238           "label": "release"
   239         }
   240       }
   241     },
   242     "com.ubuntu.cloud:server:14.04:i386": {
   243       "release": "trusty",
   244       "version": "14.04",
   245       "arch": "i386",
   246       "versions": {
   247         "20121218": {
   248           "items": {
   249             "test1pe": {
   250               "root_store": "ssd",
   251               "virt": "pv",
   252               "region": "test",
   253               "id": "ami-00000034"
   254             },
   255             "apne1pe": {
   256               "root_store": "ssd",
   257               "virt": "pv",
   258               "region": "ap-northeast-1",
   259               "id": "ami-00000023"
   260             }
   261           },
   262           "pubname": "ubuntu-trusty-14.04-i386-server-20121218",
   263           "label": "release"
   264         }
   265       }
   266     },
   267     "com.ubuntu.cloud:server:12.10:amd64": {
   268       "release": "quantal",
   269       "version": "12.10",
   270       "arch": "amd64",
   271       "versions": {
   272         "20121218": {
   273           "items": {
   274             "usee1pi": {
   275               "root_store": "instance",
   276               "virt": "pv",
   277               "region": "us-east-1",
   278               "id": "ami-00000011"
   279             },
   280             "usww1pe": {
   281               "root_store": "ssd",
   282               "virt": "pv",
   283               "region": "eu-west-1",
   284               "id": "ami-01000016"
   285             },
   286             "apne1pe": {
   287               "root_store": "ssd",
   288               "virt": "pv",
   289               "region": "ap-northeast-1",
   290               "id": "ami-01000026"
   291             },
   292             "apne1he": {
   293               "root_store": "ssd",
   294               "virt": "hvm",
   295               "region": "ap-northeast-1",
   296               "id": "ami-01000087"
   297             },
   298             "test1he": {
   299               "root_store": "ssd",
   300               "virt": "hvm",
   301               "region": "test",
   302               "id": "ami-01000035"
   303             }
   304           },
   305           "pubname": "ubuntu-quantal-12.10-amd64-server-20121218",
   306           "label": "release"
   307         }
   308       }
   309     },
   310     "com.ubuntu.cloud:server:12.10:i386": {
   311       "release": "quantal",
   312       "version": "12.10",
   313       "arch": "i386",
   314       "versions": {
   315         "20121218": {
   316           "items": {
   317             "test1pe": {
   318               "root_store": "ssd",
   319               "virt": "pv",
   320               "region": "test",
   321               "id": "ami-01000034"
   322             },
   323             "apne1pe": {
   324               "root_store": "ssd",
   325               "virt": "pv",
   326               "region": "ap-northeast-1",
   327               "id": "ami-01000023"
   328             }
   329           },
   330           "pubname": "ubuntu-quantal-12.10-i386-server-20121218",
   331           "label": "release"
   332         }
   333       }
   334     },
   335     "com.ubuntu.cloud:server:13.04:i386": {
   336       "release": "raring",
   337       "version": "13.04",
   338       "arch": "i386",
   339       "versions": {
   340         "20121218": {
   341           "items": {
   342             "test1pe": {
   343               "root_store": "ssd",
   344               "virt": "pv",
   345               "region": "test",
   346               "id": "ami-02000034"
   347             }
   348           },
   349           "pubname": "ubuntu-raring-13.04-i386-server-20121218",
   350           "label": "release"
   351         }
   352       }
   353     }
   354   },
   355   "format": "products:1.0"
   356  }
   357  `,
   358  }
   359  
   360  var TestInstanceTypeCosts = instanceTypeCost{
   361  	"m1.small":    60,
   362  	"m1.medium":   120,
   363  	"m1.large":    240,
   364  	"m1.xlarge":   480,
   365  	"t1.micro":    20,
   366  	"c1.medium":   145,
   367  	"c1.xlarge":   580,
   368  	"cc2.8xlarge": 2400,
   369  }
   370  
   371  var TestRegions = map[string]aws.Region{
   372  	"test": {
   373  		Name:        "test",
   374  		EC2Endpoint: "https://ec2.endpoint.com",
   375  	},
   376  }