This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
ec2_si_tests.py
executable file
·405 lines (348 loc) · 18.5 KB
/
ec2_si_tests.py
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import string
import random
import os
import sys
import nose.tools
import boto
from boto.exception import BotoServerError
from boto.ec2.connection import EC2Connection
import boto.ec2
import sys
from ConfigParser import SafeConfigParser
import time
import unittest
import tempfile
import pycb
import pynimbusauthz
from pynimbusauthz.db import *
from pynimbusauthz.user import *
import pycb.test_common
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.connection import S3Connection
import random
import datetime
import re
from nimbusweb.setup.groupauthz import *
def get_nimbus_home():
"""Determines home directory of Nimbus install we are using.
First looks for a NIMBUS_HOME enviroment variable, else assumes that
the home directory is the parent directory of the directory with this
script.
"""
nimbus_home = os.getenv("NIMBUS_HOME")
if not nimbus_home:
script_dir = os.path.dirname(__file__)
nimbus_home = os.path.dirname(script_dir)
if not os.path.exists(nimbus_home):
raise Exception('ENIMBUSHOME', "NIMBUS_HOME must refer to a valid path")
return nimbus_home
class TestEC2Submit(unittest.TestCase):
def cb_random_bucketname(self, len):
chars = string.letters + string.digits
newpasswd = ""
for i in range(len):
newpasswd = newpasswd + random.choice(chars)
return newpasswd
def setUp(self):
host = 'localhost'
cumport = 8888
ec2port = 8444
self.db = DB(pycb.config.authzdb)
self.friendly = self.cb_random_bucketname(21)
self.can_user = User(self.db, friendly=self.friendly, create=True)
self.subject = self.cb_random_bucketname(21)
self.s3id = self.cb_random_bucketname(21)
self.s3pw = self.cb_random_bucketname(42)
self.s3user = self.can_user.create_alias(self.s3id, pynimbusauthz.alias_type_s3, self.friendly, self.s3pw)
self.dnuser = self.can_user.create_alias(self.subject, pynimbusauthz.alias_type_x509, self.friendly)
self.ec2conn = EC2Connection(self.s3id, self.s3pw, host=host, port=ec2port, debug=2)
self.ec2conn.host = host
cf = OrdinaryCallingFormat()
self.s3conn = S3Connection(self.s3id, self.s3pw, host=host, port=cumport, is_secure=False, calling_format=cf)
self.db.commit()
nh = get_nimbus_home()
groupauthz_dir = os.path.join(nh, "services/etc/nimbus/workspace-service/group-authz/")
add_member(groupauthz_dir, self.subject, 4)
def tearDown(self):
if self.s3conn != None:
pass
if self.ec2conn != None:
pass
if self.s3user != None:
self.s3user.remove()
if self.dnuser != None:
self.dnuser.remove()
if self.can_user != None:
self.can_user.destroy_brutally()
if self.db != None:
self.db.close()
def test_error(self):
self.assertRaises(BotoServerError, self.ec2conn.request_spot_instances, '2.0', 'nil', 1, None, None, None, None, None, None, None, None, None, 'm1.large')
def test_simple_requestSI(self):
bucket_name = "Repo"
bucket = self.s3conn.get_bucket(bucket_name)
k = boto.s3.key.Key(bucket)
image_name = self.cb_random_bucketname(10)
k.key = "WHATEVER/" + image_name
k.set_contents_from_filename(os.environ['NIMBUS_SOURCE_TEST_IMAGE'])
url = "cumulus://HOST/" + bucket_name + "/" + k.key
result = self.ec2conn.request_spot_instances("1.0",url)
assert len(result) == 1, 'incorrect result size'
request = result[0]
prev_id = request.id
assert request.launch_specification.instance_type == "m1.small"
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "one-time", 'incorrect type returned'
result = self.ec2conn.get_all_spot_instance_requests()
assert len(result) == 1, 'incorrect result size'
request = result[0]
assert request.id == prev_id, 'returned id is not the same from the request call'
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "one-time", 'incorrect type returned'
assert request.state == "active", 'request should have been in the active status'
allReservations = self.ec2conn.get_all_instances()
assert len(allReservations) == 1, 'incorrect result size'
reservation = allReservations[0]
assert len(reservation.instances) == 1, 'incorrect result size'
instance = reservation.instances[0]
assert instance.id == request.instance_id, 'returned instance id must be equal to spot instance id'
assert instance.spot_instance_request_id == prev_id, 'returned spot instance id is not the same from the request call'
assert instance.instanceLifecycle == "spot", 'instance life cycle is incorrect'
assert instance.instance_type == "m1.small", 'returned instance is not the same as submitted'
request.cancel()
result = self.ec2conn.get_all_spot_instance_requests()
assert len(result) == 1, 'incorrect result size'
request = result[0]
assert request.id == prev_id, 'returned id is not the same from the request call'
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "one-time", 'incorrect type returned'
assert request.state == "canceled", 'request should have been in the canceled status'
assert reservation.instances[0].id == request.instance_id, 'returned instance id must be equal to spot instance id'
reservation.stop_all()
result = self.ec2conn.get_all_spot_instance_requests()
assert len(result) == 1, 'incorrect result size'
request = result[0]
assert request.id == prev_id, 'returned id is not the same from the request call'
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "one-time", 'incorrect type returned'
assert request.state == "canceled", 'request should have been in the canceled status'
assert not request.instance_id, 'there shouldnt be an instance id'
def test_persistent_requestSI(self):
bucket_name = "Repo"
bucket = self.s3conn.get_bucket(bucket_name)
k = boto.s3.key.Key(bucket)
image_name = self.cb_random_bucketname(10)
k.key = "WHATEVER/" + image_name
k.set_contents_from_filename(os.environ['NIMBUS_SOURCE_TEST_IMAGE'])
url = "cumulus://HOST/" + bucket_name + "/" + k.key
result = self.ec2conn.request_spot_instances("1.0",url,count=2,type="persistent")
assert len(result) == 1, 'incorrect result size'
request = result[0]
prev_id = request.id
assert request.launch_specification.instance_type == "m1.small"
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "persistent", 'incorrect type returned'
result = self.ec2conn.get_all_spot_instance_requests()
assert len(result) == 1, 'incorrect result size'
request = result[0]
assert request.id == prev_id, 'returned id is not the same from the request call'
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "persistent", 'incorrect type returned'
assert request.state == "active", 'request should have been in the active status'
allReservations = self.ec2conn.get_all_instances()
assert len(allReservations) == 1, 'incorrect result size'
reservation = allReservations[0]
assert len(reservation.instances) == 2, 'incorrect result size'
instance1 = reservation.instances[0]
assert instance1.id == request.instance_id, 'returned instance id must be equal to spot instance id'
assert instance1.spot_instance_request_id == prev_id, 'returned spot instance id is not the same from the request call'
assert instance1.instanceLifecycle == "spot", 'instance life cycle is incorrect'
assert instance1.instance_type == "m1.small", 'returned instance is not the same as submitted'
instance2 = reservation.instances[1]
assert instance2.spot_instance_request_id == prev_id, 'returned spot instance id is not the same from the request call'
assert instance2.instanceLifecycle == "spot", 'instance life cycle is incorrect'
assert instance2.instance_type == "m1.small", 'returned instance is not the same as submitted'
instance2.stop()
time.sleep(1)
allReservations = self.ec2conn.get_all_instances()
assert len(allReservations) == 1, 'incorrect result size'
reservation = allReservations[0]
assert len(reservation.instances) == 2, 'incorrect result size'
instance3 = reservation.instances[0]
assert instance3.id == instance1.id, 'returned instance is not the same as before'
instance4 = reservation.instances[1]
assert instance4.id != instance2.id
assert instance4.spot_instance_request_id == prev_id, 'returned spot instance id is not the same from the request call'
assert instance4.instanceLifecycle == "spot", 'instance life cycle is incorrect'
assert instance4.instance_type == "m1.small", 'returned instance is not the same as submitted'
request.cancel()
result = self.ec2conn.get_all_spot_instance_requests()
assert len(result) == 1, 'incorrect result size'
request = result[0]
assert request.id == prev_id, 'returned id is not the same from the request call'
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "persistent", 'incorrect type returned'
assert request.state == "canceled", 'request should have been in the canceled status'
assert reservation.instances[0].id == request.instance_id, 'returned instance id must be equal to spot instance id'
reservation.stop_all()
result = self.ec2conn.get_all_spot_instance_requests()
assert len(result) == 1, 'incorrect result size'
request = result[0]
assert request.id == prev_id, 'returned id is not the same from the request call'
assert request.price == 1.0, 'incorrect price returned'
assert request.type == "persistent", 'incorrect type returned'
assert request.state == "canceled", 'request should have been in the canceled status'
assert not request.instance_id, 'there shouldnt be an instance id'
allReservations = self.ec2conn.get_all_instances()
assert len(allReservations) == 0, 'incorrect result size'
def test_multiple_requestSI(self):
bucket_name = "Repo"
bucket = self.s3conn.get_bucket(bucket_name)
k = boto.s3.key.Key(bucket)
image_name = self.cb_random_bucketname(10)
k.key = "WHATEVER/" + image_name
k.set_contents_from_filename(os.environ['NIMBUS_SOURCE_TEST_IMAGE'])
url = "cumulus://HOST/" + bucket_name + "/" + k.key
ts1 = now = datetime.datetime.now()
req1 = self.ec2conn.request_spot_instances("1.0",url,count=2,type="persistent")[0]
req2 = self.ec2conn.request_spot_instances("1.2",url,count=1)[0]
req3 = self.ec2conn.request_spot_instances("1.5",url,count=1)[0]
req1_res = None
req2_res = None
req3_res = None
allReservations = self.ec2conn.get_all_instances()
assert len(allReservations) == 3, 'incorrect result size'
for reservation in allReservations:
assert len(reservation.instances) > 0, 'reservations have no instances!'
inst = reservation.instances[0]
if inst.spot_instance_request_id == req1.id:
req1_res = reservation
elif inst.spot_instance_request_id == req2.id:
req2_res = reservation
elif inst.spot_instance_request_id == req3.id:
req3_res = reservation
assert len(req1_res.instances) == 2
assert len(req2_res.instances) == 1
assert len(req3_res.instances) == 1
price_history = self.ec2conn.get_spot_price_history(start_time=ts1.isoformat())
assert len(price_history) == 0
run_res1 = self.ec2conn.run_instances(url,min_count=2)
time.sleep(1)
price_history = self.ec2conn.get_spot_price_history(start_time=ts1.isoformat())
assert len(price_history) == 1
price1 = price_history[0]
assert price1.price == 1.0
ts2 = now = datetime.datetime.now()
run_res2 = self.ec2conn.run_instances(url)
time.sleep(1)
price_history = self.ec2conn.get_spot_price_history(start_time=ts1.isoformat())
assert len(price_history) == 2
price1 = price_history[0]
assert price1.price == 1.0
price1 = price_history[1]
assert price1.price == 1.2
run_res3 = self.ec2conn.run_instances(url)
time.sleep(1)
price_history = self.ec2conn.get_spot_price_history(start_time=ts1.isoformat())
assert len(price_history) == 3
price1 = price_history[0]
assert price1.price == 1.0
price1 = price_history[1]
assert price1.price == 1.2
price1 = price_history[2]
assert price1.price == 1.5
si_reqs = self.ec2conn.get_all_spot_instance_requests()
assert len(si_reqs) == 3, 'incorrect result size'
for si_req in si_reqs:
if si_req.id == req1.id:
assert si_req.price == 1.0, 'incorrect price returned'
assert si_req.type == "persistent", 'incorrect type returned'
assert si_req.state == "open", 'request should have been in the open status'
assert not si_req.instance_id, 'there shouldnt be an instance id'
elif si_req.id == req2.id:
assert si_req.price == 1.2, 'incorrect price returned'
assert si_req.type == "one-time", 'incorrect type returned'
assert si_req.state == "closed", 'request should have been in the open status'
assert not si_req.instance_id, 'there shouldnt be an instance id'
elif si_req.id == req3.id:
assert si_req.price == 1.5, 'incorrect price returned'
assert si_req.type == "one-time", 'incorrect type returned'
assert si_req.state == "active", 'request should have been in the open status'
assert req3_res.instances[0].id == si_req.instance_id, 'returned instance id must be equal to spot instance id'
else:
self.fail("shouldn't happen")
ts3 = now = datetime.datetime.now()
run_res2.stop_all()
time.sleep(1)
si_reqs = self.ec2conn.get_all_spot_instance_requests()
assert len(si_reqs) == 3, 'incorrect result size'
for si_req in si_reqs:
if si_req.id == req1.id:
assert si_req.price == 1.0, 'incorrect price returned'
assert si_req.type == "persistent", 'incorrect type returned'
assert si_req.state == "active", 'request should have been in the open status'
elif si_req.id == req2.id:
assert si_req.price == 1.2, 'incorrect price returned'
assert si_req.type == "one-time", 'incorrect type returned'
assert si_req.state == "closed", 'request should have been in the open status'
assert not si_req.instance_id, 'there shouldnt be an instance id'
elif si_req.id == req3.id:
assert si_req.price == 1.5, 'incorrect price returned'
assert si_req.type == "one-time", 'incorrect type returned'
assert si_req.state == "active", 'request should have been in the open status'
assert req3_res.instances[0].id == si_req.instance_id, 'returned instance id must be equal to spot instance id'
else:
self.fail("shouldn't happen")
price_history = self.ec2conn.get_spot_price_history(start_time=ts1.isoformat())
assert len(price_history) == 4
price1 = price_history[0]
assert price1.price == 1.0
price1 = price_history[1]
assert price1.price == 1.2
price1 = price_history[2]
assert price1.price == 1.5
price1 = price_history[3]
assert price1.price == 1.0
self.ec2conn.cancel_spot_instance_requests([req1.id,req3.id])
si_reqs = self.ec2conn.get_all_spot_instance_requests()
assert len(si_reqs) == 3, 'incorrect result size'
for si_req in si_reqs:
if si_req.id == req1.id:
assert si_req.price == 1.0, 'incorrect price returned'
assert si_req.type == "persistent", 'incorrect type returned'
assert si_req.state == "canceled", 'request should have been in the open status'
elif si_req.id == req2.id:
assert si_req.price == 1.2, 'incorrect price returned'
assert si_req.type == "one-time", 'incorrect type returned'
assert si_req.state == "closed", 'request should have been in the open status'
assert not si_req.instance_id, 'there shouldnt be an instance id'
elif si_req.id == req3.id:
assert si_req.price == 1.5, 'incorrect price returned'
assert si_req.type == "one-time", 'incorrect type returned'
assert si_req.state == "canceled", 'request should have been in the open status'
assert req3_res.instances[0].id == si_req.instance_id, 'returned instance id must be equal to spot instance id'
else:
self.fail("shouldn't happen")
allReservations = self.ec2conn.get_all_instances()
for reservation in allReservations:
reservation.stop_all()
allReservations = self.ec2conn.get_all_instances()
assert len(allReservations) == 0, 'incorrect result size'
price_history = self.ec2conn.get_spot_price_history(start_time=ts1.isoformat())
assert len(price_history) == 5
price1 = price_history[0]
assert price1.price == 1.0
price1 = price_history[1]
assert price1.price == 1.2
price1 = price_history[2]
assert price1.price == 1.5
price1 = price_history[3]
assert price1.price == 1.0
price1 = price_history[4]
assert price1.price == 0.1
price_history = self.ec2conn.get_spot_price_history(start_time=ts2.isoformat(), end_time=ts3.isoformat())
assert len(price_history) == 2
price1 = price_history[0]
assert price1.price == 1.2
price1 = price_history[1]
assert price1.price == 1.5