1 | const SpotFleet = require('../functions/spot-fleet');
|
2 | const test = require('tape');
|
3 | const AWS = require('@mapbox/mock-aws-sdk-js');
|
4 | const http = require('http');
|
5 |
|
6 | test('[spot-fleet] create handles errors', assert => {
|
7 | AWS.stub('EC2', 'requestSpotFleet', (opts, cb) => {
|
8 | assert.deepEquals(opts, {
|
9 | SpotFleetRequestConfig: {
|
10 | AllocationStrategy : 'diversified',
|
11 | ExcessCapacityTerminationPolicy : 'noTermination',
|
12 | IamFleetRole : 'IAM Fleet Role',
|
13 | LaunchSpecifications : [{
|
14 | InstanceType: 'r3.8xlarge',
|
15 | WeightedCapacity: 2.44
|
16 | }],
|
17 | SpotPrice : '20',
|
18 | TargetCapacity : 5,
|
19 | TerminateInstancesWithExpiration : false,
|
20 | ValidFrom : 'start date',
|
21 | ValidUntil : 'end date'
|
22 | },
|
23 | DryRun: false
|
24 | }, 'requestSpotFleet parameters');
|
25 | return cb(new Error('random error'));
|
26 | });
|
27 |
|
28 | const spotfleet = new SpotFleet({
|
29 | AllocationStrategy : 'diversified',
|
30 | ExcessCapacityTerminationPolicy : 'noTermination',
|
31 | IamFleetRole : 'IAM Fleet Role',
|
32 | LaunchSpecifications : [{
|
33 | InstanceType: 'r3.8xlarge',
|
34 | WeightedCapacity: 2.44
|
35 | }],
|
36 | SpotPrice : '20',
|
37 | TargetCapacity : 5,
|
38 | TerminateInstancesWithExpiration : 'false',
|
39 | ValidFrom : 'start date',
|
40 | ValidUntil : 'end date'
|
41 | }, 'us-east-1', false, 'requestId');
|
42 |
|
43 | spotfleet.create((err) => {
|
44 | assert.equals(err.message, 'random error', 'error occurs');
|
45 | AWS.EC2.restore();
|
46 | assert.end();
|
47 | });
|
48 | });
|
49 |
|
50 | test('[spot-fleet] create handles success', assert => {
|
51 | const requestSpotFleet = AWS.stub('EC2', 'requestSpotFleet', (opts, cb) => {
|
52 | assert.deepEquals(opts, {
|
53 | SpotFleetRequestConfig: {
|
54 | AllocationStrategy : 'diversified',
|
55 | ExcessCapacityTerminationPolicy : 'noTermination',
|
56 | IamFleetRole : 'IAM Fleet Role',
|
57 | LaunchSpecifications : [{
|
58 | InstanceType: 'r3.8xlarge',
|
59 | WeightedCapacity: 2.44
|
60 | }],
|
61 | SpotPrice : '20',
|
62 | TargetCapacity : 5,
|
63 | TerminateInstancesWithExpiration : false,
|
64 | ValidFrom : 'start date',
|
65 | ValidUntil : 'end date'
|
66 | },
|
67 | DryRun: false
|
68 | });
|
69 | return cb(null, { SpotFleetRequestId: 'sfr-firstrequestid1111111111111111111111'});
|
70 | });
|
71 |
|
72 | const spotfleet = new SpotFleet({
|
73 | AllocationStrategy : 'diversified',
|
74 | ExcessCapacityTerminationPolicy : 'noTermination',
|
75 | IamFleetRole : 'IAM Fleet Role',
|
76 | LaunchSpecifications : [{
|
77 | InstanceType: 'r3.8xlarge',
|
78 | WeightedCapacity: 2.44
|
79 | }],
|
80 | SpotPrice : '20',
|
81 | TargetCapacity : 5,
|
82 | TerminateInstancesWithExpiration : 'false',
|
83 | ValidFrom : 'start date',
|
84 | ValidUntil : 'end date'
|
85 | }, 'us-east-1', false, 'sfr-firstrequestid1111111111111111111111');
|
86 |
|
87 | spotfleet.create((err) => {
|
88 | assert.ifError(err, 'no error');
|
89 | assert.equals(requestSpotFleet.callCount, 1, 'requestSpotFleet called');
|
90 | requestSpotFleet.restore();
|
91 | assert.end();
|
92 | });
|
93 | });
|
94 |
|
95 | test('[spot-fleet] update updates the requestId, yields the requestId from _this, not this', (assert) => {
|
96 | const spotfleet = new SpotFleet({
|
97 | AllocationStrategy : 'diversified',
|
98 | ExcessCapacityTerminationPolicy : 'noTermination',
|
99 | IamFleetRole : 'IAM Fleet Role',
|
100 | LaunchSpecifications : [{
|
101 | InstanceType: 'r3.8xlarge',
|
102 | WeightedCapacity: 2.44
|
103 | }],
|
104 | SpotPrice : '20',
|
105 | TargetCapacity : 1,
|
106 | TerminateInstancesWithExpiration : 'false',
|
107 | ValidFrom : 'start date',
|
108 | ValidUntil : 'end date'
|
109 | }, 'us-east-1', false, 'sfr-firstrequestid1111111111111111111111');
|
110 |
|
111 | const describeSpotFleetRequests = AWS.stub('EC2', 'describeSpotFleetRequests').yields(null, {
|
112 | SpotFleetRequestConfigs: [
|
113 | {
|
114 | SpotFleetRequestConfig: {
|
115 | TargetCapacity: 1
|
116 | }
|
117 | }
|
118 | ]
|
119 | });
|
120 | AWS.stub('EC2', 'requestSpotFleet').yields(null, { SpotFleetRequestId: 'sfr-secondrequestid222222222222222222222' });
|
121 | AWS.stub('EC2', 'cancelSpotFleetRequests').yields(null, 'sfr-firstrequestid1111111111111111111111 canceled');
|
122 |
|
123 | spotfleet.update((err, res) => {
|
124 | assert.ifError(err, 'should not error');
|
125 | assert.equal(describeSpotFleetRequests.callCount, 2, 'describeSpotFleetRequests called twice');
|
126 | assert.equal(res, 'sfr-secondrequestid222222222222222222222', 'request id coming from _this' );
|
127 | AWS.EC2.restore();
|
128 | assert.end();
|
129 | });
|
130 |
|
131 | });
|
132 |
|
133 | test('[spot-fleet] delete cancels spot fleet request', (assert) => {
|
134 | const describeSpotFleetRequests = AWS.stub('EC2', 'describeSpotFleetRequests').yields(null);
|
135 | const cancelSpotFleetRequests = AWS.stub('EC2', 'cancelSpotFleetRequests').yields(null, 'sfr-firstrequestid1111111111111111111111 canceled');
|
136 | const spotfleet = new SpotFleet({
|
137 | AllocationStrategy : 'diversified',
|
138 | ExcessCapacityTerminationPolicy : 'noTermination',
|
139 | IamFleetRole : 'IAM Fleet Role',
|
140 | LaunchSpecifications : [{
|
141 | InstanceType: 'r3.8xlarge',
|
142 | WeightedCapacity: 2.44
|
143 | }],
|
144 | SpotPrice : '20',
|
145 | TargetCapacity : 1,
|
146 | TerminateInstancesWithExpiration : 'false',
|
147 | ValidFrom : 'start date',
|
148 | ValidUntil : 'end date'
|
149 | }, 'us-east-1', false, 'sfr-firstrequestid1111111111111111111111');
|
150 |
|
151 | spotfleet.delete((err) => {
|
152 | assert.ifError(err, 'should not error');
|
153 | assert.equal(describeSpotFleetRequests.callCount, 1, 'calls describeSpotFleetRequests once');
|
154 | assert.equal(cancelSpotFleetRequests.callCount, 1, 'calls cancelSpotFleetRequests once');
|
155 | AWS.EC2.restore();
|
156 | assert.end();
|
157 | });
|
158 | });
|
159 |
|
160 | test('[spot-fleet] delete does nothing if request id not found', (assert) => {
|
161 | const describeSpotFleetRequests = AWS.stub('EC2', 'describeSpotFleetRequests').yields({ code: 'InvalidSpotFleetRequestId.NotFound' });
|
162 | const cancelSpotFleetRequests = AWS.stub('EC2', 'cancelSpotFleetRequests').yields(null, 'sfr-firstrequestid1111111111111111111111 canceled');
|
163 | const spotfleet = new SpotFleet({
|
164 | AllocationStrategy : 'diversified',
|
165 | ExcessCapacityTerminationPolicy : 'noTermination',
|
166 | IamFleetRole : 'IAM Fleet Role',
|
167 | LaunchSpecifications : [{
|
168 | InstanceType: 'r3.8xlarge',
|
169 | WeightedCapacity: 2.44
|
170 | }],
|
171 | SpotPrice : '20',
|
172 | TargetCapacity : 1,
|
173 | TerminateInstancesWithExpiration : 'false',
|
174 | ValidFrom : 'start date',
|
175 | ValidUntil : 'end date'
|
176 | }, 'us-east-1', false, 'sfr-firstrequestid1111111111111111111111');
|
177 |
|
178 | spotfleet.delete((err) => {
|
179 | assert.ifError(err, 'should not error');
|
180 | assert.equal(describeSpotFleetRequests.callCount, 1, 'calls describeSpotFleetRequests once');
|
181 | assert.equal(cancelSpotFleetRequests.callCount, 0, 'does not call cancelSpotFleetRequests');
|
182 | AWS.EC2.restore();
|
183 | assert.end();
|
184 | });
|
185 | });
|
186 |
|
187 | test('[spot-fleet] manage parses events and relays LatestStreamLabel through Response', assert => {
|
188 |
|
189 | http.request = (options, cb) => {
|
190 | return {
|
191 | on: function() {
|
192 | return this;
|
193 | },
|
194 | write: () => {},
|
195 | end: () => {
|
196 | return cb();
|
197 | }
|
198 | };
|
199 | };
|
200 | const requestSpotFleet = AWS.stub('EC2', 'requestSpotFleet', (opts, cb) => {
|
201 | assert.deepEquals(opts, {
|
202 | SpotFleetRequestConfig: {
|
203 | AllocationStrategy : 'diversified',
|
204 | ExcessCapacityTerminationPolicy : 'noTermination',
|
205 | IamFleetRole : 'IAM Fleet Role',
|
206 | LaunchSpecifications : [{
|
207 | InstanceType: 'r3.8xlarge',
|
208 | WeightedCapacity: 2.44
|
209 | }],
|
210 | SpotPrice : '20',
|
211 | TargetCapacity : 5,
|
212 | TerminateInstancesWithExpiration : false,
|
213 | ValidFrom : 'start date',
|
214 | ValidUntil : 'end date'
|
215 | },
|
216 | DryRun: false
|
217 | });
|
218 | return cb(null, { SpotFleetRequestId: 'sfr-firstrequestid1111111111111111111111'});
|
219 | });
|
220 |
|
221 | SpotFleet.manage({
|
222 | ResponseURL: 'http://api.mapbox.com/hello',
|
223 | PhysicalResourceId: 'abc',
|
224 | StackId: 'abc',
|
225 | LogicalResourceId: 'abc',
|
226 | ResourceProperties: {
|
227 | SpotFleetRequestConfigData: {
|
228 | AllocationStrategy : 'diversified',
|
229 | ExcessCapacityTerminationPolicy : 'noTermination',
|
230 | IamFleetRole : 'IAM Fleet Role',
|
231 | LaunchSpecifications : [{
|
232 | InstanceType: 'r3.8xlarge',
|
233 | WeightedCapacity: 2.44
|
234 | }],
|
235 | SpotPrice : '20',
|
236 | TargetCapacity : 5,
|
237 | TerminateInstancesWithExpiration : 'false',
|
238 | ValidFrom : 'start date',
|
239 | ValidUntil : 'end date'
|
240 | },
|
241 | Region: 'us-east-1'
|
242 | },
|
243 | RequestId: 'abc',
|
244 | RequestType: 'CREATE'
|
245 | }, {
|
246 | done: (err, body) => {
|
247 | assert.ifError(err, 'should not error');
|
248 | assert.equals(JSON.parse(body).Status, 'SUCCESS', 'status is success');
|
249 | requestSpotFleet.restore();
|
250 | assert.end();
|
251 | }
|
252 | })
|
253 | });
|