|
| 1 | +import { randomItem } from 'support/util/random'; |
| 2 | + |
| 3 | +import { isImageDeprecated } from 'src/components/ImageSelect/utilities'; |
| 4 | + |
| 5 | +import type { Image, ImageCapabilities } from '@linode/api-v4'; |
| 6 | +/** |
| 7 | + * Images that cannot be selected using `chooseImages()`. |
| 8 | + */ |
| 9 | +const disallowedImageIds: string[] = []; |
| 10 | + |
| 11 | +/** |
| 12 | + * All Linode images available to the current Cloud Manager user. |
| 13 | + * |
| 14 | + * Retrieved via Linode APIv4 during Cypress start-up. |
| 15 | + */ |
| 16 | +export const images: Image[] = Cypress.env('cloudManagerImages') as Image[]; |
| 17 | + |
| 18 | +/** |
| 19 | + * Returns a known Cloud Manager image at random, or returns a user-chosen |
| 20 | + * image if one was specified. |
| 21 | + * |
| 22 | + * @param options - Image selection options. |
| 23 | + * |
| 24 | + * @returns Object describing a Cloud Manager image to use during tests. |
| 25 | + */ |
| 26 | +export const chooseImage = (options?: ChooseImageOptions): Image => { |
| 27 | + return randomItem(resolveSearchImages(options)); |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Returns an array of Image objects that meet the given criteria. |
| 32 | + * |
| 33 | + * @param options - Object describing Image selection criteria. |
| 34 | + * |
| 35 | + * @throws If no images meet the desired criteria. |
| 36 | + * @throws If an override image is specified which does not meet the given criteria. |
| 37 | + * |
| 38 | + * @returns Array of Image objects that meet criteria specified by `options` param. |
| 39 | + */ |
| 40 | +const resolveSearchImages = (options?: ChooseImageOptions): Image[] => { |
| 41 | + const imageFixtures = options?.images ?? images; |
| 42 | + const currentImages = imageFixtures.filter( |
| 43 | + (image) => !isImageDeprecated(image) |
| 44 | + ); |
| 45 | + const requiredCapabilities = options?.capabilities ?? []; |
| 46 | + const allDisallowedImageIds = [ |
| 47 | + ...disallowedImageIds, |
| 48 | + ...(options?.exclude ?? []), |
| 49 | + ]; |
| 50 | + const capableImages = imagesWithCapabilities( |
| 51 | + currentImages, |
| 52 | + requiredCapabilities |
| 53 | + ).filter((image: Image) => !allDisallowedImageIds.includes(image.id)); |
| 54 | + |
| 55 | + if (!capableImages.length) { |
| 56 | + throw new Error( |
| 57 | + `No images are available with the required capabilities: ${requiredCapabilities.join( |
| 58 | + ', ' |
| 59 | + )}` |
| 60 | + ); |
| 61 | + } |
| 62 | + return capableImages; |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Returns `true` if the given Image has all of the given capabilities and availability for each capability. |
| 67 | + * |
| 68 | + * @param image - Image to check capabilities. |
| 69 | + * @param capabilities - ImageCapabilities to check. |
| 70 | + * |
| 71 | + * @returns `true` if `image` has all of the given capabilities. |
| 72 | + */ |
| 73 | +const imageHasCapabilities = ( |
| 74 | + image: Image, |
| 75 | + capabilities: ImageCapabilities[] |
| 76 | +): boolean => { |
| 77 | + return capabilities.every((capability) => |
| 78 | + image.capabilities.includes(capability) |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +/** |
| 83 | + * Returns an array of Image objects that have all of the given capabilities. |
| 84 | + * |
| 85 | + * @param images - Images from which to search. |
| 86 | + * @param capabilities - ImageCapabilities to check. |
| 87 | + * |
| 88 | + * @returns Array of Image objects containing the required capabilities. |
| 89 | + */ |
| 90 | +const imagesWithCapabilities = ( |
| 91 | + images: Image[], |
| 92 | + capabilities: ImageCapabilities[] |
| 93 | +): Image[] => { |
| 94 | + return images.filter((image: Image) => |
| 95 | + imageHasCapabilities(image, capabilities) |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Returns an object describing a Cloud Manager image with the given label. |
| 101 | + * |
| 102 | + * If no known image exists with the given human-readable label, an error is |
| 103 | + * thrown. |
| 104 | + * |
| 105 | + * @param label - Label (API or Cloud-specific) of the image to find. |
| 106 | + * @param searchImages - Optional array of Images from which to search. |
| 107 | + * |
| 108 | + * @throws When no image exists in the `images` array with the given label. |
| 109 | + */ |
| 110 | +export const getImageByLabel = (label: string, searchImages?: Image[]) => { |
| 111 | + const image = (searchImages ?? images).find( |
| 112 | + (findImage: Image) => findImage.label === label |
| 113 | + ); |
| 114 | + if (!image) { |
| 115 | + throw new Error( |
| 116 | + `Unable to find image by label. Unknown image label '${label}'.` |
| 117 | + ); |
| 118 | + } |
| 119 | + return image; |
| 120 | +}; |
| 121 | + |
| 122 | +interface ChooseImageOptions { |
| 123 | + /** |
| 124 | + * If specified, the image returned will support the defined capabilities |
| 125 | + * @example ['cloud-init', 'distributed-sites'] |
| 126 | + */ |
| 127 | + capabilities?: ImageCapabilities[]; |
| 128 | + |
| 129 | + /** |
| 130 | + * Array of image IDs to exclude from results. |
| 131 | + */ |
| 132 | + exclude?: string[]; |
| 133 | + |
| 134 | + /** |
| 135 | + * Images from which to choose. If unspecified, Images exposed by the API will be used. |
| 136 | + */ |
| 137 | + images?: Image[]; |
| 138 | +} |
0 commit comments