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