github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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  	"github.com/aws/aws-sdk-go-v2/aws"
    10  	"github.com/aws/aws-sdk-go-v2/service/ec2/types"
    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.(*sdkInstance)
    34  	newi := &sdkInstance{
    35  		e: oldi.e,
    36  		i: types.Instance{},
    37  	}
    38  	newi.i = oldi.i
    39  	newi.i.InstanceId = aws.String(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  
    59  	// 22.04:arm64
    60  	makeImage("ami-02204133", "ssd", "hvm", "arm64", "22.04", "test"),
    61  
    62  	// 22.04:amd64
    63  	makeImage("ami-02204133", "ssd", "hvm", "amd64", "22.04", "test"),
    64  	makeImage("ami-02204139", "ebs", "hvm", "amd64", "22.04", "test"),
    65  	makeImage("ami-02204135", "ssd", "pv", "amd64", "22.04", "test"),
    66  
    67  	// 20.04:arm64
    68  	makeImage("ami-02004133", "ssd", "hvm", "arm64", "20.04", "test"),
    69  
    70  	// 20.04:amd64
    71  	makeImage("ami-02004133", "ssd", "hvm", "amd64", "20.04", "test"),
    72  	makeImage("ami-02004139", "ebs", "hvm", "amd64", "20.04", "test"),
    73  	makeImage("ami-02004135", "ssd", "pv", "amd64", "20.04", "test"),
    74  
    75  	// 18.04:arm64
    76  	makeImage("ami-00002133", "ssd", "hvm", "arm64", "18.04", "test"),
    77  
    78  	// 18.04:amd64
    79  	makeImage("ami-00001133", "ssd", "hvm", "amd64", "18.04", "test"),
    80  	makeImage("ami-00001139", "ebs", "hvm", "amd64", "18.04", "test"),
    81  	makeImage("ami-00001135", "ssd", "pv", "amd64", "18.04", "test"),
    82  
    83  	// 16.04:amd64
    84  	makeImage("ami-00000133", "ssd", "hvm", "amd64", "16.04", "test"),
    85  	makeImage("ami-00000139", "ebs", "hvm", "amd64", "16.04", "test"),
    86  	makeImage("ami-00000135", "ssd", "pv", "amd64", "16.04", "test"),
    87  
    88  	// 14.04:amd64
    89  	makeImage("ami-00000033", "ssd", "hvm", "amd64", "14.04", "test"),
    90  
    91  	// 12.10:amd64
    92  	makeImage("ami-01000035", "ssd", "hvm", "amd64", "12.10", "test"),
    93  }
    94  
    95  func MakeTestImageStreamsData(region types.Region) map[string]string {
    96  	testImageMetadataIndex := strings.Replace(testImageMetadataIndex, "$REGION", aws.ToString(region.RegionName), -1)
    97  	testImageMetadataIndex = strings.Replace(testImageMetadataIndex, "$ENDPOINT", aws.ToString(region.Endpoint), -1)
    98  	return map[string]string{
    99  		"/streams/v1/index.json":                         testImageMetadataIndex,
   100  		"/streams/v1/com.ubuntu.cloud:released:aws.json": testImageMetadataProduct,
   101  	}
   102  }
   103  
   104  // LTS-dependent requires new/updated entries upon new LTS release.
   105  const testImageMetadataIndex = `
   106  {
   107   "index": {
   108    "com.ubuntu.cloud:released": {
   109     "updated": "Wed, 01 May 2013 13:31:26 +0000",
   110     "clouds": [
   111      {
   112       "region": "$REGION",
   113       "endpoint": "$ENDPOINT"
   114      }
   115     ],
   116     "cloudname": "aws",
   117     "datatype": "image-ids",
   118     "format": "products:1.0",
   119     "products": [
   120      "com.ubuntu.cloud:server:22.04:amd64",
   121      "com.ubuntu.cloud:server:20.04:amd64",
   122      "com.ubuntu.cloud:server:18.04:amd64",
   123      "com.ubuntu.cloud:server:16.04:amd64",
   124      "com.ubuntu.cloud:server:14.04:amd64"
   125     ],
   126     "path": "streams/v1/com.ubuntu.cloud:released:aws.json"
   127    }
   128   },
   129   "updated": "Wed, 01 May 2013 13:31:26 +0000",
   130   "format": "index:1.0"
   131  }
   132  `
   133  const testImageMetadataProduct = `
   134  {
   135   "content_id": "com.ubuntu.cloud:released:aws",
   136   "products": {
   137      "com.ubuntu.cloud:server:22.04:amd64": {
   138        "release": "jammy",
   139        "version": "22.04",
   140        "arch": "amd64",
   141        "versions": {
   142          "20121218": {
   143            "items": {
   144              "usee1pi": {
   145                "root_store": "instance",
   146                "virt": "pv",
   147                "region": "us-east-1",
   148                "id": "ami-02204111"
   149              },
   150              "usww1pe": {
   151                "root_store": "ssd",
   152                "virt": "pv",
   153                "region": "eu-west-1",
   154                "id": "ami-02204116"
   155              },
   156              "apne1pe": {
   157                "root_store": "ssd",
   158                "virt": "pv",
   159                "region": "ap-northeast-1",
   160                "id": "ami-02204126"
   161              },
   162              "apne1he": {
   163                "root_store": "ssd",
   164                "virt": "hvm",
   165                "region": "ap-northeast-1",
   166                "id": "ami-02204187"
   167              },
   168              "test1peebs": {
   169                "root_store": "ssd",
   170                "virt": "pv",
   171                "region": "test",
   172                "id": "ami-02204133"
   173              },
   174              "test1pessd": {
   175                "root_store": "ebs",
   176                "virt": "pv",
   177                "region": "test",
   178                "id": "ami-02204139"
   179              },
   180              "test1he": {
   181                "root_store": "ssd",
   182                "virt": "hvm",
   183                "region": "test",
   184                "id": "ami-02204135"
   185              }
   186            },
   187            "pubname": "ubuntu-jammy-22.04-amd64-server-20121218",
   188            "label": "release"
   189          }
   190        }
   191      },
   192      "com.ubuntu.cloud:server:20.04:amd64": {
   193        "release": "focal",
   194        "version": "20.04",
   195        "arch": "amd64",
   196        "versions": {
   197          "20121218": {
   198            "items": {
   199              "usee1pi": {
   200                "root_store": "instance",
   201                "virt": "pv",
   202                "region": "us-east-1",
   203                "id": "ami-02004111"
   204              },
   205              "usww1pe": {
   206                "root_store": "ssd",
   207                "virt": "pv",
   208                "region": "eu-west-1",
   209                "id": "ami-02004116"
   210              },
   211              "apne1pe": {
   212                "root_store": "ssd",
   213                "virt": "pv",
   214                "region": "ap-northeast-1",
   215                "id": "ami-02004126"
   216              },
   217              "apne1he": {
   218                "root_store": "ssd",
   219                "virt": "hvm",
   220                "region": "ap-northeast-1",
   221                "id": "ami-02004187"
   222              },
   223              "test1peebs": {
   224                "root_store": "ssd",
   225                "virt": "pv",
   226                "region": "test",
   227                "id": "ami-02004133"
   228              },
   229              "test1pessd": {
   230                "root_store": "ebs",
   231                "virt": "pv",
   232                "region": "test",
   233                "id": "ami-02004139"
   234              },
   235              "test1he": {
   236                "root_store": "ssd",
   237                "virt": "hvm",
   238                "region": "test",
   239                "id": "ami-02004135"
   240              }
   241            },
   242            "pubname": "ubuntu-focal-20.04-amd64-server-20121218",
   243            "label": "release"
   244          }
   245        }
   246      },
   247      "com.ubuntu.cloud:server:18.04:amd64": {
   248        "release": "bionic",
   249        "version": "18.04",
   250        "arch": "amd64",
   251        "versions": {
   252          "20121218": {
   253            "items": {
   254              "usee1pi": {
   255                "root_store": "instance",
   256                "virt": "pv",
   257                "region": "us-east-1",
   258                "id": "ami-00001111"
   259              },
   260              "usww1pe": {
   261                "root_store": "ssd",
   262                "virt": "pv",
   263                "region": "eu-west-1",
   264                "id": "ami-00001116"
   265              },
   266              "apne1pe": {
   267                "root_store": "ssd",
   268                "virt": "pv",
   269                "region": "ap-northeast-1",
   270                "id": "ami-00001126"
   271              },
   272              "apne1he": {
   273                "root_store": "ssd",
   274                "virt": "hvm",
   275                "region": "ap-northeast-1",
   276                "id": "ami-00001187"
   277              },
   278              "test1peebs": {
   279                "root_store": "ssd",
   280                "virt": "pv",
   281                "region": "test",
   282                "id": "ami-00001133"
   283              },
   284              "test1pessd": {
   285                "root_store": "ebs",
   286                "virt": "pv",
   287                "region": "test",
   288                "id": "ami-00001139"
   289              },
   290              "test1he": {
   291                "root_store": "ssd",
   292                "virt": "hvm",
   293                "region": "test",
   294                "id": "ami-00001135"
   295              }
   296            },
   297            "pubname": "ubuntu-bionic-18.04-amd64-server-20121218",
   298            "label": "release"
   299          }
   300        }
   301      },
   302     "com.ubuntu.cloud:server:16.04:amd64": {
   303       "release": "xenial",
   304       "version": "16.04",
   305       "arch": "amd64",
   306       "versions": {
   307         "20121218": {
   308           "items": {
   309             "usee1pi": {
   310               "root_store": "instance",
   311               "virt": "pv",
   312               "region": "us-east-1",
   313               "id": "ami-00000111"
   314             },
   315             "usww1pe": {
   316               "root_store": "ssd",
   317               "virt": "pv",
   318               "region": "eu-west-1",
   319               "id": "ami-00000116"
   320             },
   321             "apne1pe": {
   322               "root_store": "ssd",
   323               "virt": "pv",
   324               "region": "ap-northeast-1",
   325               "id": "ami-00000126"
   326             },
   327             "apne1he": {
   328               "root_store": "ssd",
   329               "virt": "hvm",
   330               "region": "ap-northeast-1",
   331               "id": "ami-00000187"
   332             },
   333             "test1peebs": {
   334               "root_store": "ssd",
   335               "virt": "pv",
   336               "region": "test",
   337               "id": "ami-00000133"
   338             },
   339             "test1pessd": {
   340               "root_store": "ebs",
   341               "virt": "pv",
   342               "region": "test",
   343               "id": "ami-00000139"
   344             },
   345             "test1he": {
   346               "root_store": "ssd",
   347               "virt": "hvm",
   348               "region": "test",
   349               "id": "ami-00000135"
   350             }
   351           },
   352           "pubname": "ubuntu-xenial-16.04-amd64-server-20121218",
   353           "label": "release"
   354         }
   355       }
   356     },
   357     "com.ubuntu.cloud:server:14.04:amd64": {
   358       "release": "trusty",
   359       "version": "14.04",
   360       "arch": "amd64",
   361       "versions": {
   362         "20121218": {
   363           "items": {
   364             "test1peebs": {
   365               "root_store": "ssd",
   366               "virt": "hvm",
   367               "region": "test",
   368               "id": "ami-00000033"
   369             }
   370           },
   371           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   372           "label": "release"
   373         }
   374       }
   375     },
   376     "com.ubuntu.cloud:server:12.10:amd64": {
   377       "release": "quantal",
   378       "version": "12.10",
   379       "arch": "amd64",
   380       "versions": {
   381         "20121218": {
   382           "items": {
   383             "usee1pi": {
   384               "root_store": "instance",
   385               "virt": "pv",
   386               "region": "us-east-1",
   387               "id": "ami-00000011"
   388             },
   389             "usww1pe": {
   390               "root_store": "ssd",
   391               "virt": "pv",
   392               "region": "eu-west-1",
   393               "id": "ami-01000016"
   394             },
   395             "apne1pe": {
   396               "root_store": "ssd",
   397               "virt": "pv",
   398               "region": "ap-northeast-1",
   399               "id": "ami-01000026"
   400             },
   401             "apne1he": {
   402               "root_store": "ssd",
   403               "virt": "hvm",
   404               "region": "ap-northeast-1",
   405               "id": "ami-01000087"
   406             },
   407             "test1he": {
   408               "root_store": "ssd",
   409               "virt": "hvm",
   410               "region": "test",
   411               "id": "ami-01000035"
   412             }
   413           },
   414           "pubname": "ubuntu-quantal-12.10-amd64-server-20121218",
   415           "label": "release"
   416         }
   417       }
   418     }
   419   },
   420   "format": "products:1.0"
   421  }
   422  `