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