-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEC2.test.js
57 lines (49 loc) · 1.54 KB
/
EC2.test.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
const Test = require('thunk-test')
const assert = require('assert')
const AwsCredentials = require('./internal/AwsCredentials')
const EC2 = require('./EC2')
const test = new Test('EC2', async function () {
const awsCreds = await AwsCredentials('default').catch(error => {
if (error.code == 'ENOENT') {
const accessKeyId = process.env.AWS_ACCESS_KEY_ID
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
if (accessKeyId == null || secretAccessKey == null) {
throw new Error('No AWS credential file or environment variables')
}
return { accessKeyId, secretAccessKey }
}
throw error
})
awsCreds.region = 'us-west-1'
const ec2 = new EC2({
...awsCreds,
})
// there is one test instance in this region
{ // listInstances all instances
const response = await ec2.listInstances()
assert(
response.Instances.filter(instance => instance.State.Name == 'running').length == 1,
'There is not a running instance, check the aws account'
)
assert.equal(response.NextToken, null)
}
{ // listInstances with tag
const response = await ec2.listInstances({
'tag:Env': 'test',
})
assert.equal(response.Instances.length, 1)
assert.equal(response.NextToken, null)
}
// terminateInstances with nonexistent instanceId
await assert.rejects(
ec2.terminateInstances(['i-dne']),
{
message: 'Invalid id: "i-dne"',
name: 'InvalidInstanceID.Malformed',
},
)
}).case()
if (process.argv[1] == __filename) {
test()
}
module.exports = test