This repository was archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlive-integration-tests.js
252 lines (232 loc) · 7.53 KB
/
live-integration-tests.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"use strict";
require("mocha")
var aws = require("aws-sdk")
var keysOf = require("keys-of-obj")
var expect = require("must")
var s = require("util").format
var ec2Mock = require("./index")
var ec2 = new aws.EC2({
"accessKeyId": process.env.KEY,
"secretAccessKey": process.env.SECRET,
"region": process.env.REGION
})
var INSTANCEID_RUNNING = "i-cb7b2946"
var INSTANCEID_STOPPED = "i-32ac1f8a"
var SECS = 1000
var MINUTES = 60 * SECS
describe("validating environment", function () {
this.timeout(10 * SECS)
it("should have a running instance", function (done) {
ec2.describeInstances(
{InstanceIds: [INSTANCEID_RUNNING]},
function (err, data) {
if (err) throw err
expect(data.Reservations[0].Instances[0].State.Name).to.eql("running")
done()
})
})
it("should have a stopped instance", function (done) {
ec2.describeInstances(
{InstanceIds: [INSTANCEID_STOPPED]},
function (err, data) {
if (err) throw err
expect(data.Reservations[0].Instances[0].State.Name).to.eql("stopped")
done()
})
})
})
describe("ec2 live integration tests", function () {
this.timeout(10 * MINUTES)
var ids = []
var register = function (id) { ids.push(id) }
after(function (done) {
if (ids.length)
ec2.terminateInstances({InstanceIds: ids}, function (err) {
if (err) throw err
console.log("cleaned up", ids)
ids = []
done()
})
else done()
})
var spawnTestMachine = function (cb) {
var args = {
ImageId: "ami-3faf9f48", InstanceType: "t1.micro",
MaxCount: 1, MinCount: 1
}
ec2.runInstances(args, function (err, data) {
if (err) return cb(err)
var instanceId = data.Instances[0].InstanceId
register(instanceId)
setTimeout(function () {
cb(null, instanceId)
})
}, 1000)
}
describe("#createTags", function () {
it("should match expected structure", function (done) {
spawnTestMachine(function (err, id) {
var args = {
Resources: [id],
Tags: [{Key: "Name", Value: "foo"}]
}
ec2.createTags(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.createTags()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
})
describe("#describeInstances", function () {
it("should match type of naked call", function (done) {
ec2.describeInstances(function (err, data) {
if (err) throw err
var mockedData = ec2Mock.describeInstances()
expect(typeof data).to.eql(typeof mockedData)
done()
})
})
it("should match structure for a running machine", function (done) {
var args = {InstanceIds: [INSTANCEID_RUNNING]}
ec2.describeInstances(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.describeInstances.singleInstance(
"Reservations[0].Instances[0].Tags", [])
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
it("should match structure for a stopped machine", function (done) {
var args = {InstanceIds: [INSTANCEID_STOPPED]}
ec2.describeInstances(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.describeInstances.stoppedInstance()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
describe("#getPasswordData", function () {
it("should match expected structure", function (done) {
var args = {InstanceId: INSTANCEID_RUNNING}
ec2.getPasswordData(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.getPasswordData.windowsInstance()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
describe("#runInstances", function () {
it("should match structure on econ-style spawn", function (done) {
var args = {
ImageId: "ami-3faf9f48",
InstanceType: "t1.micro",
KeyName: "kon-tiki-development",
MaxCount: 1, MinCount: 1,
NetworkInterfaces: [{
AssociatePublicIpAddress: true,
SubnetId: "subnet-3a3b177c",
DeviceIndex: 0,
Groups: ["sg-9040a2f5"]
}],
UserData: new Buffer("<powershell>ls</powershell>").toString('base64')
}
ec2.runInstances(args, function (err, data) {
if (err) throw err
register(data.Instances[0].InstanceId)
var mockedData = ec2Mock.runInstances.econSpawnerMachine()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
describe("#startInstances", function () {
it("should match expected structure", function (done) {
spawnTestMachine(function (err, id) {
if (err) throw err
waitUntilState(id, "running", function (err) {
if (err) throw err
var args = {InstanceIds: [id]}
ec2.stopInstances(args, function (err) {
if (err) throw err
waitUntilState(id, "stopped", function (err) {
if (err) throw err
ec2.startInstances(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.startInstances.stoppedInstance()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
})
})
})
})
describe("#stopInstances", function () {
it("should match expected structure", function (done) {
spawnTestMachine(function (err, id) {
if (err) throw err
waitUntilState(id, "running", function (err) {
if (err) throw err
var args = {InstanceIds: [id]}
ec2.stopInstances(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.stopInstances.runningInstance()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
})
})
describe("#terminate", function () {
it("should match expected structure", function (done) {
spawnTestMachine(function (err, id) {
var args = {InstanceIds: [id]}
ec2.terminateInstances(args, function (err, data) {
if (err) throw err
var mockedData = ec2Mock.terminateInstances.spawnedMachine()
expect(keysOf(data), notCompareMsg(mockedData, data))
.to.eql(keysOf(mockedData))
done()
})
})
})
})
})
var notCompareMsg = function (expected, actual) {
return s("Objects do not match:\n" +
"expected: %j\n" +
"actual: %j\n", expected, actual)
}
var machineState = function (id, cb) {
ec2.describeInstances({InstanceIds: [id]}, function (err, data) {
if (err) return cb(err)
cb(null, data.Reservations[0].Instances[0].State.Name)
})
}
var waitUntilState = function (id, desiredState, cb) {
machineState(id, function (err, state) {
if (err) return cb(err)
if (state === desiredState) {
cb(null)
} else {
console.log(s("machine not ready, state %s but wants %s. Retrying...",
state, desiredState))
setTimeout(function () {
waitUntilState(id, desiredState, cb)
}, 3000)
}
})
}