github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/ec2/series_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package ec2
     5  
     6  import (
     7  	"strings"
     8  
     9  	"gopkg.in/amz.v3/aws"
    10  	awsec2 "gopkg.in/amz.v3/ec2"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/environs/imagemetadata"
    14  	"github.com/juju/juju/environs/instances"
    15  	sstesting "github.com/juju/juju/environs/simplestreams/testing"
    16  )
    17  
    18  // TODO: Apart from overriding different hardcoded hosts, these two test helpers are identical. Let's share.
    19  
    20  // UseTestImageData causes the given content to be served
    21  // when the ec2 client asks for image data.
    22  func UseTestImageData(c *gc.C, files map[string]string) {
    23  	if files != nil {
    24  		sstesting.SetRoundTripperFiles(sstesting.AddSignedFiles(c, files), nil)
    25  	} else {
    26  		sstesting.SetRoundTripperFiles(nil, nil)
    27  	}
    28  }
    29  
    30  // FabricateInstance creates a new fictitious instance
    31  // given an existing instance and a new id.
    32  func FabricateInstance(inst instances.Instance, newId string) instances.Instance {
    33  	oldi := inst.(*ec2Instance)
    34  	newi := &ec2Instance{
    35  		e:        oldi.e,
    36  		Instance: &awsec2.Instance{},
    37  	}
    38  	*newi.Instance = *oldi.Instance
    39  	newi.InstanceId = newId
    40  	return newi
    41  }
    42  
    43  func makeImage(id, storage, virtType, arch, version, region string) *imagemetadata.ImageMetadata {
    44  	return &imagemetadata.ImageMetadata{
    45  		Id:         id,
    46  		Storage:    storage,
    47  		VirtType:   virtType,
    48  		Arch:       arch,
    49  		Version:    version,
    50  		RegionName: region,
    51  		Endpoint:   "https://ec2.endpoint.com",
    52  		Stream:     "released",
    53  	}
    54  }
    55  
    56  var TestImageMetadata = []*imagemetadata.ImageMetadata{
    57  	// LTS-dependent requires new entries upon new LTS release.
    58  	// 18.04:arm64
    59  	makeImage("ami-00002133", "ssd", "hvm", "arm64", "18.04", "test"),
    60  
    61  	// 18.04:amd64
    62  	makeImage("ami-00001133", "ssd", "hvm", "amd64", "18.04", "test"),
    63  	makeImage("ami-00001139", "ebs", "hvm", "amd64", "18.04", "test"),
    64  	makeImage("ami-00001135", "ssd", "pv", "amd64", "18.04", "test"),
    65  
    66  	// 16.04:amd64
    67  	makeImage("ami-00000133", "ssd", "hvm", "amd64", "16.04", "test"),
    68  	makeImage("ami-00000139", "ebs", "hvm", "amd64", "16.04", "test"),
    69  	makeImage("ami-00000135", "ssd", "pv", "amd64", "16.04", "test"),
    70  
    71  	// 14.04:amd64
    72  	makeImage("ami-00000033", "ssd", "hvm", "amd64", "14.04", "test"),
    73  
    74  	// 14.04:i386
    75  	makeImage("ami-00000034", "ssd", "pv", "i386", "14.04", "test"),
    76  
    77  	// 12.10:amd64
    78  	makeImage("ami-01000035", "ssd", "hvm", "amd64", "12.10", "test"),
    79  
    80  	// 12.10:i386
    81  	makeImage("ami-01000034", "ssd", "hvm", "i386", "12.10", "test"),
    82  
    83  	// 13.04:i386
    84  	makeImage("ami-02000034", "ssd", "hvm", "i386", "13.04", "test"),
    85  	makeImage("ami-02000035", "ssd", "pv", "i386", "13.04", "test"),
    86  }
    87  
    88  func MakeTestImageStreamsData(region aws.Region) map[string]string {
    89  	testImageMetadataIndex := strings.Replace(testImageMetadataIndex, "$REGION", region.Name, -1)
    90  	testImageMetadataIndex = strings.Replace(testImageMetadataIndex, "$ENDPOINT", region.EC2Endpoint, -1)
    91  	return map[string]string{
    92  		"/streams/v1/index.json":                         testImageMetadataIndex,
    93  		"/streams/v1/com.ubuntu.cloud:released:aws.json": testImageMetadataProduct,
    94  	}
    95  }
    96  
    97  // LTS-dependent requires new/updated entries upon new LTS release.
    98  const testImageMetadataIndex = `
    99  {
   100   "index": {
   101    "com.ubuntu.cloud:released": {
   102     "updated": "Wed, 01 May 2013 13:31:26 +0000",
   103     "clouds": [
   104      {
   105       "region": "$REGION",
   106       "endpoint": "$ENDPOINT"
   107      }
   108     ],
   109     "cloudname": "aws",
   110     "datatype": "image-ids",
   111     "format": "products:1.0",
   112     "products": [
   113      "com.ubuntu.cloud:server:18.04:amd64",
   114      "com.ubuntu.cloud:server:16.04:amd64",
   115      "com.ubuntu.cloud:server:14.04:amd64",
   116      "com.ubuntu.cloud:server:14.04:i386",
   117      "com.ubuntu.cloud:server:12.10:i386",
   118      "com.ubuntu.cloud:server:13.04:i386"
   119     ],
   120     "path": "streams/v1/com.ubuntu.cloud:released:aws.json"
   121    }
   122   },
   123   "updated": "Wed, 01 May 2013 13:31:26 +0000",
   124   "format": "index:1.0"
   125  }
   126  `
   127  const testImageMetadataProduct = `
   128  {
   129   "content_id": "com.ubuntu.cloud:released:aws",
   130   "products": {
   131      "com.ubuntu.cloud:server:18.04:amd64": {
   132        "release": "trusty",
   133        "version": "18.04",
   134        "arch": "amd64",
   135        "versions": {
   136          "20121218": {
   137            "items": {
   138              "usee1pi": {
   139                "root_store": "instance",
   140                "virt": "pv",
   141                "region": "us-east-1",
   142                "id": "ami-00001111"
   143              },
   144              "usww1pe": {
   145                "root_store": "ssd",
   146                "virt": "pv",
   147                "region": "eu-west-1",
   148                "id": "ami-00001116"
   149              },
   150              "apne1pe": {
   151                "root_store": "ssd",
   152                "virt": "pv",
   153                "region": "ap-northeast-1",
   154                "id": "ami-00001126"
   155              },
   156              "apne1he": {
   157                "root_store": "ssd",
   158                "virt": "hvm",
   159                "region": "ap-northeast-1",
   160                "id": "ami-00001187"
   161              },
   162              "test1peebs": {
   163                "root_store": "ssd",
   164                "virt": "pv",
   165                "region": "test",
   166                "id": "ami-00001133"
   167              },
   168              "test1pessd": {
   169                "root_store": "ebs",
   170                "virt": "pv",
   171                "region": "test",
   172                "id": "ami-00001139"
   173              },
   174              "test1he": {
   175                "root_store": "ssd",
   176                "virt": "hvm",
   177                "region": "test",
   178                "id": "ami-00001135"
   179              }
   180            },
   181            "pubname": "ubuntu-trusty-16.04-amd64-server-20121218",
   182            "label": "release"
   183          }
   184        }
   185      },
   186     "com.ubuntu.cloud:server:16.04:amd64": {
   187       "release": "trusty",
   188       "version": "16.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-00000111"
   198             },
   199             "usww1pe": {
   200               "root_store": "ssd",
   201               "virt": "pv",
   202               "region": "eu-west-1",
   203               "id": "ami-00000116"
   204             },
   205             "apne1pe": {
   206               "root_store": "ssd",
   207               "virt": "pv",
   208               "region": "ap-northeast-1",
   209               "id": "ami-00000126"
   210             },
   211             "apne1he": {
   212               "root_store": "ssd",
   213               "virt": "hvm",
   214               "region": "ap-northeast-1",
   215               "id": "ami-00000187"
   216             },
   217             "test1peebs": {
   218               "root_store": "ssd",
   219               "virt": "pv",
   220               "region": "test",
   221               "id": "ami-00000133"
   222             },
   223             "test1pessd": {
   224               "root_store": "ebs",
   225               "virt": "pv",
   226               "region": "test",
   227               "id": "ami-00000139"
   228             },
   229             "test1he": {
   230               "root_store": "ssd",
   231               "virt": "hvm",
   232               "region": "test",
   233               "id": "ami-00000135"
   234             }
   235           },
   236           "pubname": "ubuntu-trusty-16.04-amd64-server-20121218",
   237           "label": "release"
   238         }
   239       }
   240     },
   241     "com.ubuntu.cloud:server:14.04:amd64": {
   242       "release": "trusty",
   243       "version": "14.04",
   244       "arch": "amd64",
   245       "versions": {
   246         "20121218": {
   247           "items": {
   248             "test1peebs": {
   249               "root_store": "ssd",
   250               "virt": "hvm",
   251               "region": "test",
   252               "id": "ami-00000033"
   253  			}
   254           },
   255           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   256           "label": "release"
   257         }
   258       }
   259     },
   260     "com.ubuntu.cloud:server:14.04:i386": {
   261       "release": "trusty",
   262       "version": "14.04",
   263       "arch": "i386",
   264       "versions": {
   265         "20121218": {
   266           "items": {
   267             "test1pe": {
   268               "root_store": "ssd",
   269               "virt": "pv",
   270               "region": "test",
   271               "id": "ami-00000034"
   272             }
   273           },
   274           "pubname": "ubuntu-trusty-14.04-i386-server-20121218",
   275           "label": "release"
   276         }
   277       }
   278     },
   279     "com.ubuntu.cloud:server:12.10:amd64": {
   280       "release": "quantal",
   281       "version": "12.10",
   282       "arch": "amd64",
   283       "versions": {
   284         "20121218": {
   285           "items": {
   286             "usee1pi": {
   287               "root_store": "instance",
   288               "virt": "pv",
   289               "region": "us-east-1",
   290               "id": "ami-00000011"
   291             },
   292             "usww1pe": {
   293               "root_store": "ssd",
   294               "virt": "pv",
   295               "region": "eu-west-1",
   296               "id": "ami-01000016"
   297             },
   298             "apne1pe": {
   299               "root_store": "ssd",
   300               "virt": "pv",
   301               "region": "ap-northeast-1",
   302               "id": "ami-01000026"
   303             },
   304             "apne1he": {
   305               "root_store": "ssd",
   306               "virt": "hvm",
   307               "region": "ap-northeast-1",
   308               "id": "ami-01000087"
   309             },
   310             "test1he": {
   311               "root_store": "ssd",
   312               "virt": "hvm",
   313               "region": "test",
   314               "id": "ami-01000035"
   315             }
   316           },
   317           "pubname": "ubuntu-quantal-12.10-amd64-server-20121218",
   318           "label": "release"
   319         }
   320       }
   321     },
   322     "com.ubuntu.cloud:server:12.10:i386": {
   323       "release": "quantal",
   324       "version": "12.10",
   325       "arch": "i386",
   326       "versions": {
   327         "20121218": {
   328           "items": {
   329             "test1pe": {
   330               "root_store": "ssd",
   331               "virt": "pv",
   332               "region": "test",
   333               "id": "ami-01000034"
   334             },
   335             "apne1pe": {
   336               "root_store": "ssd",
   337               "virt": "pv",
   338               "region": "ap-northeast-1",
   339               "id": "ami-01000023"
   340             }
   341           },
   342           "pubname": "ubuntu-quantal-12.10-i386-server-20121218",
   343           "label": "release"
   344         }
   345       }
   346     },
   347     "com.ubuntu.cloud:server:13.04:i386": {
   348       "release": "raring",
   349       "version": "13.04",
   350       "arch": "i386",
   351       "versions": {
   352         "20121218": {
   353           "items": {
   354             "test1pe": {
   355               "root_store": "ssd",
   356               "virt": "pv",
   357               "region": "test",
   358               "id": "ami-02000034"
   359             }
   360           },
   361           "pubname": "ubuntu-raring-13.04-i386-server-20121218",
   362           "label": "release"
   363         }
   364       }
   365     }
   366   },
   367   "format": "products:1.0"
   368  }
   369  `