Skip to content

Commit

Permalink
adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corymhall committed Oct 2, 2024
1 parent b38d713 commit c8d74c3
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 226 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@
"devDependencies": {
"@aws-cdk/aws-apprunner-alpha": "2.20.0-alpha.0",
"@pulumi/aws": "^6.32.0",
"@pulumi/docker": "^4.5.0",
"@pulumi/aws-native": "0.121.0",
"@pulumi/docker": "^4.5.0",
"@pulumi/pulumi": "^3.117.0",
"@types/archiver": "^6.0.2",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.2",
"@types/mock-fs": "^4.13.4",
"@types/node": "^20.12.13",
"aws-cdk-lib": "2.149.0",
"constructs": "^10.0.111",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"jest": "^29.5.0",
"jest-junit": "^15",
"mock-fs": "^5.3.0",
"prettier": "^2.6.2",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.2",
Expand Down
185 changes: 185 additions & 0 deletions tests/assembly/manifest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import * as path from 'path';
import * as mockfs from 'mock-fs';
import { AssemblyManifestReader } from '../../src/assembly';

describe('cloud assembly manifest reader', () => {
const manifestFile = '/tmp/foo/bar/does/not/exist/manifest.json';
const manifestStack = '/tmp/foo/bar/does/not/exist/test-stack.template.json';
const manifestTree = '/tmp/foo/bar/does/not/exist/tree.json';
const manifestAssets = '/tmp/foo/bar/does/not/exist/test-stack.assets.json';
beforeEach(() => {
mockfs({
// Recursively loads all node_modules
node_modules: mockfs.load(path.resolve(__dirname, '../../node_modules')),
[manifestAssets]: JSON.stringify({
version: '36.0.0',
files: {
abe4e2f4fcc1aaaf53db4829c23a5cf08795d36cce0f68a3321c1c8d728fec44: {
source: {
path: 'asset.abe4e2f4fcc1aaaf53db4829c23a5cf08795d36cce0f68a3321c1c8d728fec44',
packaging: 'zip',
},
destinations: {
'current_account-current_region': {
bucketName: 'cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}',
objectKey: 'abe4e2f4fcc1aaaf53db4829c23a5cf08795d36cce0f68a3321c1c8d728fec44.zip',
},
},
},
cd12352cc95113284dfa6575f1d74d8dea52dddcaa2f46fa695b33b59c1b4579: {
source: {
path: 'stack.template.json',
packaging: 'file',
},
destinations: {
'current_account-current_region': {
bucketName: 'cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}',
objectKey: 'cd12352cc95113284dfa6575f1d74d8dea52dddcaa2f46fa695b33b59c1b4579.json',
},
},
},
},
dockerImages: {},
}),
[manifestTree]: JSON.stringify({
version: 'tree-0.1',
tree: {
id: 'App',
path: '',
children: {
'test-stack': {
id: 'test-stack',
path: 'test-stack',
},
},
},
}),
[manifestStack]: JSON.stringify({
Resources: {
MyFunction1ServiceRole9852B06B: {
Type: 'AWS::IAM::Role',
Properties: {},
},
MyFunction12A744C2E: {
Type: 'AWS::Lambda::Function',
Properties: {},
},
},
}),
[manifestFile]: JSON.stringify({
version: '17.0.0',
artifacts: {
'test-stack.assets': {
type: 'cdk:asset-manifest',
properties: {
file: 'test-stack.assets.json',
},
},
Tree: {
type: 'cdk:tree',
properties: {
file: 'tree.json',
},
},
'test-stack': {
type: 'aws:cloudformation:stack',
environment: 'aws://unknown-account/unknown-region',
properties: {
templateFile: 'test-stack.template.json',
validateOnSynth: false,
},
metadata: {
'/test-stack/MyFunction1/ServiceRole/Resource': [
{
type: 'aws:cdk:logicalId',
data: 'MyFunction1ServiceRole9852B06B',
},
],
'/test-stack/MyFunction1/Resource': [
{
type: 'aws:cdk:logicalId',
data: 'MyFunction12A744C2E',
},
],
},
displayName: 'test-stack',
},
},
}),
});
});

afterEach(() => {
mockfs.restore();
});

test('can read manifest from file', () => {
expect(() => {
AssemblyManifestReader.fromFile(manifestFile);
}).not.toThrow();
});

test('throws if manifest not found', () => {
expect(() => {
AssemblyManifestReader.fromFile('some-other-file');
}).toThrow(/Cannot read manifest 'some-other-file':/);
});

test('throws if manifest file not found', () => {
expect(() => {
AssemblyManifestReader.fromPath('some-other-file');
}).toThrow(/Cannot read manifest at 'some-other-file':/);
});

test('can read manifest file from path', () => {
expect(() => {
AssemblyManifestReader.fromPath(manifestFile);
}).not.toThrow();
});

test('can read manifest from path', () => {
expect(() => {
AssemblyManifestReader.fromPath(path.dirname(manifestFile));
}).not.toThrow();
});

test('fromPath sets directory correctly', () => {
const manifest = AssemblyManifestReader.fromPath(path.dirname(manifestFile));
expect(manifest.directory).toEqual('/tmp/foo/bar/does/not/exist');
});

test('can get stacks from manifest', () => {
const manifest = AssemblyManifestReader.fromFile(manifestFile);

expect(manifest.stackManifests[0]).toEqual({
assets: expect.anything(),
constructTree: { id: 'test-stack', path: 'test-stack' },
directory: '/tmp/foo/bar/does/not/exist',
id: 'test-stack',
metadata: {
'test-stack/MyFunction1/Resource': 'MyFunction12A744C2E',
'test-stack/MyFunction1/ServiceRole/Resource': 'MyFunction1ServiceRole9852B06B',
},
outputs: undefined,
parameters: undefined,
resources: {
MyFunction12A744C2E: { Properties: {}, Type: 'AWS::Lambda::Function' },
MyFunction1ServiceRole9852B06B: { Properties: {}, Type: 'AWS::IAM::Role' },
},
templatePath: 'test-stack.template.json',
});
expect(manifest.stackManifests[0].fileAssets.length).toEqual(1);
expect(manifest.stackManifests[0].fileAssets[0]).toEqual({
destination: {
bucketName: 'cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}',
objectKey: 'abe4e2f4fcc1aaaf53db4829c23a5cf08795d36cce0f68a3321c1c8d728fec44.zip',
},
id: {
assetId: 'abe4e2f4fcc1aaaf53db4829c23a5cf08795d36cce0f68a3321c1c8d728fec44',
destinationId: 'current_account-current_region',
},
packaging: 'zip',
path: '/tmp/foo/bar/does/not/exist/asset.abe4e2f4fcc1aaaf53db4829c23a5cf08795d36cce0f68a3321c1c8d728fec44',
});
});
});
140 changes: 140 additions & 0 deletions tests/assembly/stack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { StackManifest } from '../../src/assembly';

describe('StackManifest', () => {
test('Throws if template has no resources', () => {
expect(() => {
new StackManifest('dir', 'id', 'path', {}, { id: 'id', path: 'path' }, {}, []);
}).toThrow(/CloudFormation template has no resources/);
});

test('get file assets', () => {
const stack = new StackManifest(
'dir',
'id',
'path',
{},
{ id: 'id', path: 'path' },
{
Resources: { SomeResource: { Type: 'sometype', Properties: {} } },
},
[
{
id: {
assetId: 'asset',
destinationId: 'dest',
},
type: 'file',
source: { path: 'somepath' },
destination: { objectKey: 'abc', bucketName: 'bucket' },
genericSource: {},
genericDestination: {},
},
{
id: {
assetId: 'asset2',
destinationId: 'dest2',
},
type: 'docker-image',
source: {},
destination: { imageTag: 'tag', repositoryName: 'repop' },
genericSource: {},
genericDestination: {},
},
],
);
expect(stack.fileAssets.length).toEqual(1);
expect(stack.fileAssets[0]).toEqual({
destination: {
bucketName: 'bucket',
objectKey: 'abc',
},
id: {
assetId: 'asset',
destinationId: 'dest',
},
packaging: 'file',
path: 'dir/somepath',
});
});

test('can get logicalId for path', () => {
const stack = new StackManifest(
'dir',
'id',
'path',
{
'stack/bucket': 'SomeBucket',
},
{
id: 'id',
path: 'path',
},
{
Resources: {
SomeBucket: {
Type: 'AWS::S3::Bucket',
Properties: {},
},
},
},
[],
);
expect(stack.logicalIdForPath('stack/bucket')).toEqual('SomeBucket');
});

test('can get resource for path', () => {
const stack = new StackManifest(
'dir',
'id',
'path',
{
'stack/bucket': 'SomeBucket',
},
{
id: 'id',
path: 'path',
},
{
Resources: {
SomeBucket: {
Type: 'AWS::S3::Bucket',
Properties: { Key: 'Value' },
},
},
},
[],
);
expect(stack.resourceWithPath('stack/bucket')).toEqual({
Type: 'AWS::S3::Bucket',
Properties: { Key: 'Value' },
});
});

test('can get resource for logicalId', () => {
const stack = new StackManifest(
'dir',
'id',
'path',
{
'stack/bucket': 'SomeBucket',
},
{
id: 'id',
path: 'path',
},
{
Resources: {
SomeBucket: {
Type: 'AWS::S3::Bucket',
Properties: { Key: 'Value' },
},
},
},
[],
);
expect(stack.resourceWithLogicalId('SomeBucket')).toEqual({
Type: 'AWS::S3::Bucket',
Properties: { Key: 'Value' },
});
});
});
1 change: 0 additions & 1 deletion tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function setMocks() {
return {};
},
newResource: (args: MockResourceArgs): { id: string; state: any } => {
console.error(args.type);
switch (args.type) {
case 'cdk:index:Stack':
return { id: '', state: {} };
Expand Down
Loading

0 comments on commit c8d74c3

Please sign in to comment.