Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add basic unit tests for cache options
- Loading branch information
1 parent
c2ff00b
commit bc87be0
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
packages/@aws-cdk/aws-ecr-assets/test/build-image-cache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { AssetManifest } from '@aws-cdk/cloud-assembly-schema'; | ||
import { App, Stack } from '@aws-cdk/core'; | ||
import { AssetManifestArtifact, CloudArtifact, CloudAssembly } from '@aws-cdk/cx-api'; | ||
import { DockerImageAsset } from '../lib'; | ||
|
||
describe('build cache', () => { | ||
test('manifest contains cache from options ', () => { | ||
// GIVEN | ||
const app = new App(); | ||
const stack = new Stack(app); | ||
const asset = new DockerImageAsset(stack, 'DockerImage6', { | ||
directory: path.join(__dirname, 'demo-image'), | ||
cacheFrom: [{ type: 'registry', params: { image: 'foo' } }], | ||
}); | ||
|
||
// WHEN | ||
const asm = app.synth(); | ||
|
||
// THEN | ||
const manifestArtifact = getAssetManifest(asm); | ||
const manifest = readAssetManifest(manifestArtifact); | ||
|
||
expect(Object.keys(manifest.dockerImages ?? {}).length).toBe(1); | ||
expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheFrom?.length).toBe(1); | ||
expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheFrom?.[0]).toStrictEqual({ | ||
type: 'registry', | ||
params: { image: 'foo' }, | ||
}); | ||
}); | ||
test('manifest contains cache to options ', () => { | ||
// GIVEN | ||
const app = new App(); | ||
const stack = new Stack(app); | ||
const asset = new DockerImageAsset(stack, 'DockerImage6', { | ||
directory: path.join(__dirname, 'demo-image'), | ||
cacheTo: { type: 'inline' }, | ||
}); | ||
|
||
// WHEN | ||
const asm = app.synth(); | ||
|
||
// THEN | ||
const manifestArtifact = getAssetManifest(asm); | ||
const manifest = readAssetManifest(manifestArtifact); | ||
|
||
expect(Object.keys(manifest.dockerImages ?? {}).length).toBe(1); | ||
expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheTo).toStrictEqual({ | ||
type: 'inline', | ||
}); | ||
}); | ||
|
||
test('manifest does not contain options when not specified', () => { | ||
// GIVEN | ||
const app = new App(); | ||
const stack = new Stack(app); | ||
const asset = new DockerImageAsset(stack, 'DockerImage6', { | ||
directory: path.join(__dirname, 'demo-image'), | ||
}); | ||
|
||
// WHEN | ||
const asm = app.synth(); | ||
|
||
// THEN | ||
const manifestArtifact = getAssetManifest(asm); | ||
const manifest = readAssetManifest(manifestArtifact); | ||
expect(Object.keys(manifest.dockerImages ?? {}).length).toBe(1); | ||
expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheFrom).toBeUndefined(); | ||
expect(manifest.dockerImages?.[asset.assetHash]?.source.cacheTo).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
function isAssetManifest(x: CloudArtifact): x is AssetManifestArtifact { | ||
return x instanceof AssetManifestArtifact; | ||
} | ||
|
||
function getAssetManifest(asm: CloudAssembly): AssetManifestArtifact { | ||
const manifestArtifact = asm.artifacts.filter(isAssetManifest)[0]; | ||
if (!manifestArtifact) { | ||
throw new Error('no asset manifest in assembly'); | ||
} | ||
return manifestArtifact; | ||
} | ||
|
||
function readAssetManifest(manifestArtifact: AssetManifestArtifact): AssetManifest { | ||
return JSON.parse(fs.readFileSync(manifestArtifact.file, { encoding: 'utf-8' })); | ||
} |