forked from claudiajs/claudia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy-spec.js
158 lines (156 loc) · 6.57 KB
/
destroy-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const underTest = require('../src/commands/destroy'),
create = require('../src/commands/create'),
retriableWrap = require('../src/util/retriable-wrap'),
tmppath = require('../src/util/tmppath'),
fs = require('fs'),
path = require('path'),
aws = require('aws-sdk'),
readjson = require('../src/util/readjson'),
fsPromise = require('../src/util/fs-promise'),
fsUtil = require('../src/util/fs-util'),
awsRegion = require('./util/test-aws-region');
describe('destroy', () => {
'use strict';
let workingdir, testRunName, newObjects, iam;
beforeEach(() => {
workingdir = tmppath();
testRunName = 'test' + Date.now();
iam = new aws.IAM({ region: awsRegion });
newObjects = { workingdir: workingdir };
fs.mkdirSync(workingdir);
});
it('fails when the source dir does not contain the project config file', done => {
underTest({ source: workingdir })
.then(done.fail, reason => expect(reason).toEqual('claudia.json does not exist in the source folder'))
.then(done);
});
it('fails when the project config file does not contain the lambda name', done => {
fs.writeFileSync(path.join(workingdir, 'claudia.json'), '{}', 'utf8');
underTest({ source: workingdir })
.then(done.fail, reason => expect(reason).toEqual('invalid configuration -- lambda.name missing from claudia.json'))
.then(done);
});
it('fails when the project config file does not contain the lambda region', done => {
fs.writeFileSync(path.join(workingdir, 'claudia.json'), JSON.stringify({ lambda: { name: 'xxx' } }), 'utf8');
underTest({ source: workingdir })
.then(done.fail, reason => expect(reason).toEqual('invalid configuration -- lambda.region missing from claudia.json'))
.then(done);
});
describe('when only a lambda function exists', () => {
beforeEach(done => {
fsUtil.copy('spec/test-projects/hello-world', workingdir, true);
create({ name: testRunName, region: awsRegion, source: workingdir, handler: 'main.handler' })
.then(result => {
newObjects.lambdaFunction = result.lambda && result.lambda.name;
newObjects.lambdaRole = result.lambda && result.lambda.role;
})
.then(done, done.fail);
});
it('destroys the lambda function', done => {
underTest({ source: workingdir })
.then(() => {
const lambda = new aws.Lambda({ region: awsRegion });
return lambda.listVersionsByFunction({ FunctionName: testRunName }).promise();
})
.catch(expectedException => expect(expectedException.message).toContain(newObjects.lambdaFunction))
.then(done, done.fail);
});
it('destroys the roles for the lambda function', done => {
underTest({ source: workingdir })
.then(() => iam.getRole({ RoleName: newObjects.lambdaRole }).promise())
.catch(expectedException => expect(expectedException.code).toEqual('NoSuchEntity'))
.then(done, done.fail);
});
it('destroys the policies for the lambda function', done => {
underTest({ source: workingdir })
.then(() => iam.listRolePolicies({ RoleName: newObjects.lambdaRole }).promise())
.catch(expectedException => expect(expectedException.message).toContain(newObjects.lambdaRole))
.then(done, done.fail);
});
it('keeps the role if it was shared', done => {
const configPath = path.join(workingdir, 'claudia.json');
readjson(configPath)
.then(json => {
json.lambda.sharedRole = true;
return fsPromise.writeFileAsync(configPath, JSON.stringify(json), 'utf8');
})
.then(() => underTest({ source: workingdir }))
.then(() => iam.getRole({ RoleName: newObjects.lambdaRole }).promise())
.then(done, done.fail);
});
});
describe('removing the config file', () => {
beforeEach(done => {
fsUtil.copy('spec/test-projects/hello-world', workingdir, true);
create({ name: testRunName, region: awsRegion, source: workingdir, handler: 'main.handler' })
.then(result => {
newObjects.lambdaFunction = result.lambda && result.lambda.name;
newObjects.lambdaRole = result.lambda && result.lambda.role;
})
.then(done, done.fail);
});
it('removes claudia.json if --config is not provided', done => {
underTest({ source: workingdir })
.then(() => expect(fs.existsSync(path.join(workingdir, 'claudia.json'))).toBeFalsy())
.then(done, done.fail);
});
it('removes specified config if --config is provided', done => {
const otherPath = tmppath();
fs.writeFileSync(otherPath, fs.readFileSync(path.join(workingdir, 'claudia.json')));
underTest({ source: workingdir, config: otherPath})
.then(() => {
expect(fs.existsSync(path.join(workingdir, 'claudia.json'))).toBeTruthy();
expect(fs.existsSync(path.join(workingdir, otherPath))).toBeFalsy();
})
.then(done, e => {
console.log(e.stack || e.message || e);
done.fail(e);
});
});
});
describe('when the lambda project contains a web api', () => {
beforeEach(done => {
fsUtil.copy('spec/test-projects/api-gw-hello-world', workingdir, true);
create({ name: testRunName, region: awsRegion, source: workingdir, 'api-module': 'main' })
.then(result => {
newObjects.lambdaRole = result.lambda && result.lambda.role;
newObjects.lambdaFunction = result.lambda && result.lambda.name;
newObjects.restApi = result.api && result.api.id;
})
.then(done, done.fail);
});
it('destroys the lambda function', done => {
underTest({ source: workingdir })
.then(() => {
const lambda = new aws.Lambda({ region: awsRegion });
return lambda.listVersionsByFunction({ FunctionName: testRunName }).promise();
})
.catch(expectedException => expect(expectedException.message).toContain(newObjects.lambdaFunction))
.then(done, done.fail);
});
it('destroys the web api', done => {
underTest({ source: workingdir })
.then(() => {
const apiGateway = retriableWrap(new aws.APIGateway({ region: awsRegion }));
return apiGateway.getRestApi({ restApiId: newObjects.restApi }).promise();
})
.catch(expectedException => {
expect(expectedException.message).toMatch(/^Invalid API identifier specified/);
expect(expectedException.code).toEqual('NotFoundException');
})
.then(done, done.fail);
});
it('destroys the roles for the lambda function', done => {
underTest({ source: workingdir })
.then(() => iam.getRole({ RoleName: newObjects.lambdaRole }).promise())
.catch(expectedException => expect(expectedException.code).toEqual('NoSuchEntity'))
.then(done, done.fail);
});
it('destroys the policies for the lambda function', done => {
underTest({ source: workingdir })
.then(() => iam.listRolePolicies({ RoleName: newObjects.lambdaRole }).promise())
.catch(expectedException => expect(expectedException.message).toContain(newObjects.lambdaRole))
.then(done, done.fail);
});
});
});