|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { AssetManifest } from '@aws-cdk/cloud-assembly-schema'; |
| 4 | +import { App, Stack } from '@aws-cdk/core'; |
| 5 | +import { AssetManifestArtifact, CloudArtifact, CloudAssembly } from '@aws-cdk/cx-api'; |
| 6 | +import { DockerImageAsset } from '../lib'; |
| 7 | + |
| 8 | +describe('build cache', () => { |
| 9 | + test('manifest contains cache from options ', () => { |
| 10 | + // GIVEN |
| 11 | + const app = new App(); |
| 12 | + const stack = new Stack(app); |
| 13 | + const asset = new DockerImageAsset(stack, 'DockerImage6', { |
| 14 | + directory: path.join(__dirname, 'demo-image'), |
| 15 | + cacheFrom: [{ type: 'registry', params: { image: 'foo' } }], |
| 16 | + }); |
| 17 | + |
| 18 | + // WHEN |
| 19 | + const asm = app.synth(); |
| 20 | + |
| 21 | + // THEN |
| 22 | + const manifestArtifact = getAssetManifest(asm); |
| 23 | + const manifest = readAssetManifest(manifestArtifact); |
| 24 | + |
| 25 | + expect(Object.keys(manifest.dockerImages ?? {}).length).toBe(1); |
| 26 | + expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheFrom?.length).toBe(1); |
| 27 | + expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheFrom?.[0]).toStrictEqual({ |
| 28 | + type: 'registry', |
| 29 | + params: { image: 'foo' }, |
| 30 | + }); |
| 31 | + }); |
| 32 | + test('manifest contains cache to options ', () => { |
| 33 | + // GIVEN |
| 34 | + const app = new App(); |
| 35 | + const stack = new Stack(app); |
| 36 | + const asset = new DockerImageAsset(stack, 'DockerImage6', { |
| 37 | + directory: path.join(__dirname, 'demo-image'), |
| 38 | + cacheTo: { type: 'inline' }, |
| 39 | + }); |
| 40 | + |
| 41 | + // WHEN |
| 42 | + const asm = app.synth(); |
| 43 | + |
| 44 | + // THEN |
| 45 | + const manifestArtifact = getAssetManifest(asm); |
| 46 | + const manifest = readAssetManifest(manifestArtifact); |
| 47 | + |
| 48 | + expect(Object.keys(manifest.dockerImages ?? {}).length).toBe(1); |
| 49 | + expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheTo).toStrictEqual({ |
| 50 | + type: 'inline', |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + test('manifest does not contain options when not specified', () => { |
| 55 | + // GIVEN |
| 56 | + const app = new App(); |
| 57 | + const stack = new Stack(app); |
| 58 | + const asset = new DockerImageAsset(stack, 'DockerImage6', { |
| 59 | + directory: path.join(__dirname, 'demo-image'), |
| 60 | + }); |
| 61 | + |
| 62 | + // WHEN |
| 63 | + const asm = app.synth(); |
| 64 | + |
| 65 | + // THEN |
| 66 | + const manifestArtifact = getAssetManifest(asm); |
| 67 | + const manifest = readAssetManifest(manifestArtifact); |
| 68 | + expect(Object.keys(manifest.dockerImages ?? {}).length).toBe(1); |
| 69 | + expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheFrom).toBeUndefined(); |
| 70 | + expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheTo).toBeUndefined(); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +function isAssetManifest(x: CloudArtifact): x is AssetManifestArtifact { |
| 75 | + return x instanceof AssetManifestArtifact; |
| 76 | +} |
| 77 | + |
| 78 | +function getAssetManifest(asm: CloudAssembly): AssetManifestArtifact { |
| 79 | + const manifestArtifact = asm.artifacts.filter(isAssetManifest)[0]; |
| 80 | + if (!manifestArtifact) { |
| 81 | + throw new Error('no asset manifest in assembly'); |
| 82 | + } |
| 83 | + return manifestArtifact; |
| 84 | +} |
| 85 | + |
| 86 | +function readAssetManifest(manifestArtifact: AssetManifestArtifact): AssetManifest { |
| 87 | + return JSON.parse(fs.readFileSync(manifestArtifact.file, { encoding: 'utf-8' })); |
| 88 | +} |
0 commit comments