github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  )
   122  
   123  func EC2ErrCode(err error) string {
   124  	return ec2ErrCode(err)
   125  }
   126  
   127  // FabricateInstance creates a new fictitious instance
   128  // given an existing instance and a new id.
   129  func FabricateInstance(inst instance.Instance, newId string) instance.Instance {
   130  	oldi := inst.(*ec2Instance)
   131  	newi := &ec2Instance{
   132  		e:        oldi.e,
   133  		Instance: &ec2.Instance{},
   134  	}
   135  	*newi.Instance = *oldi.Instance
   136  	newi.InstanceId = newId
   137  	return newi
   138  }
   139  
   140  // Access non exported methods on ec2.storage
   141  type Storage interface {
   142  	Put(file string, r io.Reader, length int64) error
   143  	ResetMadeBucket()
   144  }
   145  
   146  func (s *ec2storage) ResetMadeBucket() {
   147  	s.Lock()
   148  	defer s.Unlock()
   149  	s.madeBucket = false
   150  }
   151  
   152  var TestImagesData = map[string]string{
   153  	"/streams/v1/index.json": `
   154          {
   155           "index": {
   156            "com.ubuntu.cloud:released": {
   157             "updated": "Wed, 01 May 2013 13:31:26 +0000",
   158             "clouds": [
   159              {
   160               "region": "test",
   161               "endpoint": "https://ec2.endpoint.com"
   162              }
   163             ],
   164             "cloudname": "aws",
   165             "datatype": "image-ids",
   166             "format": "products:1.0",
   167             "products": [
   168              "com.ubuntu.cloud:server:14.04:amd64",
   169              "com.ubuntu.cloud:server:14.04:i386",
   170              "com.ubuntu.cloud:server:14.04:amd64",
   171              "com.ubuntu.cloud:server:12.10:amd64",
   172              "com.ubuntu.cloud:server:12.10:i386",
   173              "com.ubuntu.cloud:server:13.04:i386"
   174             ],
   175             "path": "streams/v1/com.ubuntu.cloud:released:aws.js"
   176            }
   177           },
   178           "updated": "Wed, 01 May 2013 13:31:26 +0000",
   179           "format": "index:1.0"
   180          }
   181  `,
   182  	"/streams/v1/com.ubuntu.cloud:released:aws.js": `
   183  {
   184   "content_id": "com.ubuntu.cloud:released:aws",
   185   "products": {
   186     "com.ubuntu.cloud:server:14.04:amd64": {
   187       "release": "trusty",
   188       "version": "14.04",
   189       "arch": "amd64",
   190       "versions": {
   191         "20121218": {
   192           "items": {
   193             "usee1pi": {
   194               "root_store": "instance",
   195               "virt": "pv",
   196               "region": "us-east-1",
   197               "id": "ami-00000011"
   198             },
   199             "usww1pe": {
   200               "root_store": "ssd",
   201               "virt": "pv",
   202               "region": "eu-west-1",
   203               "id": "ami-00000016"
   204             },
   205             "apne1pe": {
   206               "root_store": "ssd",
   207               "virt": "pv",
   208               "region": "ap-northeast-1",
   209               "id": "ami-00000026"
   210             },
   211             "apne1he": {
   212               "root_store": "ssd",
   213               "virt": "hvm",
   214               "region": "ap-northeast-1",
   215               "id": "ami-00000087"
   216             },
   217             "test1peebs": {
   218               "root_store": "ssd",
   219               "virt": "pv",
   220               "region": "test",
   221               "id": "ami-00000033"
   222             },
   223             "test1pessd": {
   224               "root_store": "ebs",
   225               "virt": "pv",
   226               "region": "test",
   227               "id": "ami-00000039"
   228             },
   229             "test1he": {
   230               "root_store": "ssd",
   231               "virt": "hvm",
   232               "region": "test",
   233               "id": "ami-00000035"
   234             }
   235           },
   236           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   237           "label": "release"
   238         }
   239       }
   240     },
   241     "com.ubuntu.cloud:server:14.04:i386": {
   242       "release": "trusty",
   243       "version": "14.04",
   244       "arch": "i386",
   245       "versions": {
   246         "20121218": {
   247           "items": {
   248             "test1pe": {
   249               "root_store": "ssd",
   250               "virt": "pv",
   251               "region": "test",
   252               "id": "ami-00000034"
   253             },
   254             "apne1pe": {
   255               "root_store": "ssd",
   256               "virt": "pv",
   257               "region": "ap-northeast-1",
   258               "id": "ami-00000023"
   259             }
   260           },
   261           "pubname": "ubuntu-trusty-14.04-i386-server-20121218",
   262           "label": "release"
   263         }
   264       }
   265     },
   266     "com.ubuntu.cloud:server:12.10:amd64": {
   267       "release": "quantal",
   268       "version": "12.10",
   269       "arch": "amd64",
   270       "versions": {
   271         "20121218": {
   272           "items": {
   273             "usee1pi": {
   274               "root_store": "instance",
   275               "virt": "pv",
   276               "region": "us-east-1",
   277               "id": "ami-00000011"
   278             },
   279             "usww1pe": {
   280               "root_store": "ssd",
   281               "virt": "pv",
   282               "region": "eu-west-1",
   283               "id": "ami-01000016"
   284             },
   285             "apne1pe": {
   286               "root_store": "ssd",
   287               "virt": "pv",
   288               "region": "ap-northeast-1",
   289               "id": "ami-01000026"
   290             },
   291             "apne1he": {
   292               "root_store": "ssd",
   293               "virt": "hvm",
   294               "region": "ap-northeast-1",
   295               "id": "ami-01000087"
   296             },
   297             "test1he": {
   298               "root_store": "ssd",
   299               "virt": "hvm",
   300               "region": "test",
   301               "id": "ami-01000035"
   302             }
   303           },
   304           "pubname": "ubuntu-quantal-12.10-amd64-server-20121218",
   305           "label": "release"
   306         }
   307       }
   308     },
   309     "com.ubuntu.cloud:server:12.10:i386": {
   310       "release": "quantal",
   311       "version": "12.10",
   312       "arch": "i386",
   313       "versions": {
   314         "20121218": {
   315           "items": {
   316             "test1pe": {
   317               "root_store": "ssd",
   318               "virt": "pv",
   319               "region": "test",
   320               "id": "ami-01000034"
   321             },
   322             "apne1pe": {
   323               "root_store": "ssd",
   324               "virt": "pv",
   325               "region": "ap-northeast-1",
   326               "id": "ami-01000023"
   327             }
   328           },
   329           "pubname": "ubuntu-quantal-12.10-i386-server-20121218",
   330           "label": "release"
   331         }
   332       }
   333     },
   334     "com.ubuntu.cloud:server:13.04:i386": {
   335       "release": "raring",
   336       "version": "13.04",
   337       "arch": "i386",
   338       "versions": {
   339         "20121218": {
   340           "items": {
   341             "test1pe": {
   342               "root_store": "ssd",
   343               "virt": "pv",
   344               "region": "test",
   345               "id": "ami-02000034"
   346             }
   347           },
   348           "pubname": "ubuntu-raring-13.04-i386-server-20121218",
   349           "label": "release"
   350         }
   351       }
   352     }
   353   },
   354   "format": "products:1.0"
   355  }
   356  `,
   357  }
   358  
   359  var TestInstanceTypeCosts = instanceTypeCost{
   360  	"m1.small":    60,
   361  	"m1.medium":   120,
   362  	"m1.large":    240,
   363  	"m1.xlarge":   480,
   364  	"t1.micro":    20,
   365  	"c1.medium":   145,
   366  	"c1.xlarge":   580,
   367  	"cc2.8xlarge": 2400,
   368  }
   369  
   370  var TestRegions = map[string]aws.Region{
   371  	"test": {
   372  		Name:        "test",
   373  		EC2Endpoint: "https://ec2.endpoint.com",
   374  	},
   375  }