Merge lp:~devcamcar/nova/improve_smoketests into lp:~hudson-openstack/nova/trunk

Proposed by Devin Carlen
Status: Merged
Approved by: Soren Hansen
Approved revision: 397
Merged at revision: 456
Proposed branch: lp:~devcamcar/nova/improve_smoketests
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 1321 lines (+575/-706)
6 files modified
smoketests/admin_smoketests.py (+92/-0)
smoketests/base.py (+154/-0)
smoketests/flags.py (+3/-10)
smoketests/novatestcase.py (+0/-130)
smoketests/smoketest.py (+0/-566)
smoketests/user_smoketests.py (+326/-0)
To merge this branch: bzr merge lp:~devcamcar/nova/improve_smoketests
Reviewer Review Type Date Requested Status
Soren Hansen (community) Approve
Vish Ishaya (community) Approve
Jay Pipes (community) Needs Information
Review via email: mp+40155@code.launchpad.net

Commit message

Refactored smoketests to use novarc environment and to separate user and admin specific tests.

Description of the change

Refactored smoketests to use novarc environment and to separate user and admin specific tests.

To post a comment you must log in.
Revision history for this message
Devin Carlen (devcamcar) wrote :

This branch addresses several underlying problems with the smoke tests. Most importantly, the tests had bit rotted. The tests were all grouped together and all relied on having nova admin credentials to run all of the tests, which is not ideal. Now the tests are split into admin and user tests.

The tests require you to source the appropriate novarc file prior to running them. For admin tests, you will need to source the admin user's credentials.

The tests also no longer rely on complicated network configurations. The network is left up to the user. If you need to be connected to a VPN to communicate with nova, then you need to do so for these tests as well.

This allows more specific and fine grained testing to be done, and will allow me to add some tests soon for launching VPN instances, connecting via OpenVPN, and doing deeper testing that way as well.

Revision history for this message
Jay Pipes (jaypipes) wrote :

I don't see the novarc files. Did you bzr add them?

review: Needs Information
Revision history for this message
Devin Carlen (devcamcar) wrote :

No novarc files are included. I may have explained it a bit confusingly, but basically you are supposed to source whatever novarc file you want to use to run the tests. The user tests can be run as whomever you want. The admin tests need to be run using admin's novarc.

Revision history for this message
Vish Ishaya (vishvananda) wrote :

lgtm

review: Approve
Revision history for this message
Soren Hansen (soren) wrote :

Yay! Now we just need a way to run these from Hudson.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'smoketests/admin_smoketests.py'
2--- smoketests/admin_smoketests.py 1970-01-01 00:00:00 +0000
3+++ smoketests/admin_smoketests.py 2010-11-04 22:56:08 +0000
4@@ -0,0 +1,92 @@
5+# vim: tabstop=4 shiftwidth=4 softtabstop=4
6+
7+# Copyright 2010 United States Government as represented by the
8+# Administrator of the National Aeronautics and Space Administration.
9+# All Rights Reserved.
10+#
11+# Licensed under the Apache License, Version 2.0 (the "License"); you may
12+# not use this file except in compliance with the License. You may obtain
13+# a copy of the License at
14+#
15+# http://www.apache.org/licenses/LICENSE-2.0
16+#
17+# Unless required by applicable law or agreed to in writing, software
18+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20+# License for the specific language governing permissions and limitations
21+# under the License.
22+
23+import os
24+import random
25+import sys
26+import time
27+import unittest
28+import zipfile
29+
30+from nova import adminclient
31+from smoketests import flags
32+from smoketests import base
33+
34+
35+SUITE_NAMES = '[user]'
36+
37+FLAGS = flags.FLAGS
38+flags.DEFINE_string('suite', None, 'Specific test suite to run ' + SUITE_NAMES)
39+
40+# TODO(devamcar): Use random tempfile
41+ZIP_FILENAME = '/tmp/nova-me-x509.zip'
42+
43+TEST_PREFIX = 'test%s' % int(random.random()*1000000)
44+TEST_USERNAME = '%suser' % TEST_PREFIX
45+TEST_PROJECTNAME = '%sproject' % TEST_PREFIX
46+
47+
48+class AdminSmokeTestCase(base.SmokeTestCase):
49+ def setUp(self):
50+ self.admin = adminclient.NovaAdminClient(
51+ access_key=os.getenv('EC2_ACCESS_KEY'),
52+ secret_key=os.getenv('EC2_SECRET_KEY'),
53+ clc_url=os.getenv('EC2_URL'),
54+ region=FLAGS.region)
55+
56+
57+class UserTests(AdminSmokeTestCase):
58+ """ Test admin credentials and user creation. """
59+
60+ def test_001_admin_can_connect(self):
61+ conn = self.admin.connection_for('admin', 'admin')
62+ self.assert_(conn)
63+
64+ def test_002_admin_can_create_user(self):
65+ user = self.admin.create_user(TEST_USERNAME)
66+ self.assertEqual(user.username, TEST_USERNAME)
67+
68+ def test_003_admin_can_create_project(self):
69+ project = self.admin.create_project(TEST_PROJECTNAME,
70+ TEST_USERNAME)
71+ self.assertEqual(project.projectname, TEST_PROJECTNAME)
72+
73+ def test_004_user_can_download_credentials(self):
74+ buf = self.admin.get_zip(TEST_USERNAME, TEST_PROJECTNAME)
75+ output = open(ZIP_FILENAME, 'w')
76+ output.write(buf)
77+ output.close()
78+
79+ zip = zipfile.ZipFile(ZIP_FILENAME, 'a', zipfile.ZIP_DEFLATED)
80+ bad = zip.testzip()
81+ zip.close()
82+
83+ self.failIf(bad)
84+
85+ def test_999_tearDown(self):
86+ self.admin.delete_project(TEST_PROJECTNAME)
87+ self.admin.delete_user(TEST_USERNAME)
88+ try:
89+ os.remove(ZIP_FILENAME)
90+ except:
91+ pass
92+
93+if __name__ == "__main__":
94+ suites = {'user': unittest.makeSuite(UserTests)}
95+ sys.exit(base.run_tests(suites))
96+
97
98=== added file 'smoketests/base.py'
99--- smoketests/base.py 1970-01-01 00:00:00 +0000
100+++ smoketests/base.py 2010-11-04 22:56:08 +0000
101@@ -0,0 +1,154 @@
102+# vim: tabstop=4 shiftwidth=4 softtabstop=4
103+
104+# Copyright 2010 United States Government as represented by the
105+# Administrator of the National Aeronautics and Space Administration.
106+# All Rights Reserved.
107+#
108+# Licensed under the Apache License, Version 2.0 (the "License"); you may
109+# not use this file except in compliance with the License. You may obtain
110+# a copy of the License at
111+#
112+# http://www.apache.org/licenses/LICENSE-2.0
113+#
114+# Unless required by applicable law or agreed to in writing, software
115+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
116+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
117+# License for the specific language governing permissions and limitations
118+# under the License.
119+
120+import boto
121+import commands
122+import httplib
123+import os
124+import paramiko
125+import random
126+import sys
127+import unittest
128+from boto.ec2.regioninfo import RegionInfo
129+
130+from smoketests import flags
131+
132+FLAGS = flags.FLAGS
133+
134+
135+class SmokeTestCase(unittest.TestCase):
136+ def connect_ssh(self, ip, key_name):
137+ # TODO(devcamcar): set a more reasonable connection timeout time
138+ key = paramiko.RSAKey.from_private_key_file('/tmp/%s.pem' % key_name)
139+ client = paramiko.SSHClient()
140+ client.set_missing_host_key_policy(paramiko.WarningPolicy())
141+ client.connect(ip, username='root', pkey=key)
142+ stdin, stdout, stderr = client.exec_command('uptime')
143+ print 'uptime: ', stdout.read()
144+ return client
145+
146+ def can_ping(self, ip):
147+ """ Attempt to ping the specified IP, and give up after 1 second. """
148+
149+ # NOTE(devcamcar): ping timeout flag is different in OSX.
150+ if sys.platform == 'darwin':
151+ timeout_flag = 't'
152+ else:
153+ timeout_flag = 'w'
154+
155+ status, output = commands.getstatusoutput('ping -c1 -%s1 %s' %
156+ (timeout_flag, ip))
157+ return status == 0
158+
159+ def connection_for_env(self, **kwargs):
160+ """
161+ Returns a boto ec2 connection for the current environment.
162+ """
163+ access_key = os.getenv('EC2_ACCESS_KEY')
164+ secret_key = os.getenv('EC2_SECRET_KEY')
165+ clc_url = os.getenv('EC2_URL')
166+
167+ if not access_key or not secret_key or not clc_url:
168+ raise Exception('Missing EC2 environment variables. Please source '
169+ 'the appropriate novarc file before running this '
170+ 'test.')
171+
172+ parts = self.split_clc_url(clc_url)
173+ return boto.connect_ec2(aws_access_key_id=access_key,
174+ aws_secret_access_key=secret_key,
175+ is_secure=parts['is_secure'],
176+ region=RegionInfo(None,
177+ 'nova',
178+ parts['ip']),
179+ port=parts['port'],
180+ path='/services/Cloud',
181+ **kwargs)
182+
183+ def split_clc_url(self, clc_url):
184+ """
185+ Splits a cloud controller endpoint url.
186+ """
187+ parts = httplib.urlsplit(clc_url)
188+ is_secure = parts.scheme == 'https'
189+ ip, port = parts.netloc.split(':')
190+ return {'ip': ip, 'port': int(port), 'is_secure': is_secure}
191+
192+ def create_key_pair(self, conn, key_name):
193+ try:
194+ os.remove('/tmp/%s.pem' % key_name)
195+ except:
196+ pass
197+ key = conn.create_key_pair(key_name)
198+ key.save('/tmp/')
199+ return key
200+
201+ def delete_key_pair(self, conn, key_name):
202+ conn.delete_key_pair(key_name)
203+ try:
204+ os.remove('/tmp/%s.pem' % key_name)
205+ except:
206+ pass
207+
208+ def bundle_image(self, image, kernel=False):
209+ cmd = 'euca-bundle-image -i %s' % image
210+ if kernel:
211+ cmd += ' --kernel true'
212+ status, output = commands.getstatusoutput(cmd)
213+ if status != 0:
214+ print '%s -> \n %s' % (cmd, output)
215+ raise Exception(output)
216+ return True
217+
218+ def upload_image(self, bucket_name, image):
219+ cmd = 'euca-upload-bundle -b %s -m /tmp/%s.manifest.xml' % (bucket_name, image)
220+ status, output = commands.getstatusoutput(cmd)
221+ if status != 0:
222+ print '%s -> \n %s' % (cmd, output)
223+ raise Exception(output)
224+ return True
225+
226+ def delete_bundle_bucket(self, bucket_name):
227+ cmd = 'euca-delete-bundle --clear -b %s' % (bucket_name)
228+ status, output = commands.getstatusoutput(cmd)
229+ if status != 0:
230+ print '%s -> \n%s' % (cmd, output)
231+ raise Exception(output)
232+ return True
233+
234+def run_tests(suites):
235+ argv = FLAGS(sys.argv)
236+
237+ if not os.getenv('EC2_ACCESS_KEY'):
238+ print >> sys.stderr, 'Missing EC2 environment variables. Please ' \
239+ 'source the appropriate novarc file before ' \
240+ 'running this test.'
241+ return 1
242+
243+ if FLAGS.suite:
244+ try:
245+ suite = suites[FLAGS.suite]
246+ except KeyError:
247+ print >> sys.stderr, 'Available test suites:', \
248+ ', '.join(suites.keys())
249+ return 1
250+
251+ unittest.TextTestRunner(verbosity=2).run(suite)
252+ else:
253+ for suite in suites.itervalues():
254+ unittest.TextTestRunner(verbosity=2).run(suite)
255+
256
257=== modified file 'smoketests/flags.py'
258--- smoketests/flags.py 2010-07-15 16:07:40 +0000
259+++ smoketests/flags.py 2010-11-04 22:56:08 +0000
260@@ -1,7 +1,7 @@
261 # vim: tabstop=4 shiftwidth=4 softtabstop=4
262
263 # Copyright 2010 United States Government as represented by the
264-# Administrator of the National Aeronautics and Space Administration.
265+# Administrator of the National Aeronautics and Space Administration.
266 # All Rights Reserved.
267 #
268 # Licensed under the Apache License, Version 2.0 (the "License"); you may
269@@ -33,13 +33,6 @@
270 # __GLOBAL FLAGS ONLY__
271 # Define any app-specific flags in their own files, docs at:
272 # http://code.google.com/p/python-gflags/source/browse/trunk/gflags.py#39
273-DEFINE_string('admin_access_key', 'admin', 'Access key for admin user')
274-DEFINE_string('admin_secret_key', 'admin', 'Secret key for admin user')
275-DEFINE_string('clc_ip', '127.0.0.1', 'IP of cloud controller API')
276-DEFINE_string('bundle_kernel', 'openwrt-x86-vmlinuz',
277- 'Local kernel file to use for bundling tests')
278-DEFINE_string('bundle_image', 'openwrt-x86-ext2.image',
279- 'Local image file to use for bundling tests')
280-#DEFINE_string('vpn_image_id', 'ami-CLOUDPIPE',
281-# 'AMI for cloudpipe vpn server')
282+DEFINE_string('region', 'nova', 'Region to use')
283+DEFINE_string('test_image', 'ami-tiny', 'Image to use for launch tests')
284
285
286=== removed file 'smoketests/novatestcase.py'
287--- smoketests/novatestcase.py 2010-07-15 16:07:40 +0000
288+++ smoketests/novatestcase.py 1970-01-01 00:00:00 +0000
289@@ -1,130 +0,0 @@
290-# vim: tabstop=4 shiftwidth=4 softtabstop=4
291-
292-# Copyright 2010 United States Government as represented by the
293-# Administrator of the National Aeronautics and Space Administration.
294-# All Rights Reserved.
295-#
296-# Licensed under the Apache License, Version 2.0 (the "License"); you may
297-# not use this file except in compliance with the License. You may obtain
298-# a copy of the License at
299-#
300-# http://www.apache.org/licenses/LICENSE-2.0
301-#
302-# Unless required by applicable law or agreed to in writing, software
303-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
304-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
305-# License for the specific language governing permissions and limitations
306-# under the License.
307-
308-import commands
309-import os
310-import random
311-import sys
312-import unittest
313-
314-
315-import paramiko
316-
317-from nova import adminclient
318-from smoketests import flags
319-
320-FLAGS = flags.FLAGS
321-
322-
323-class NovaTestCase(unittest.TestCase):
324- def setUp(self):
325- self.nova_admin = adminclient.NovaAdminClient(
326- access_key=FLAGS.admin_access_key,
327- secret_key=FLAGS.admin_secret_key,
328- clc_ip=FLAGS.clc_ip)
329-
330- def tearDown(self):
331- pass
332-
333- def connect_ssh(self, ip, key_name):
334- # TODO(devcamcar): set a more reasonable connection timeout time
335- key = paramiko.RSAKey.from_private_key_file('/tmp/%s.pem' % key_name)
336- client = paramiko.SSHClient()
337- client.load_system_host_keys()
338- client.set_missing_host_key_policy(paramiko.WarningPolicy())
339- client.connect(ip, username='root', pkey=key)
340- stdin, stdout, stderr = client.exec_command('uptime')
341- print 'uptime: ', stdout.read()
342- return client
343-
344- def can_ping(self, ip):
345- return commands.getstatusoutput('ping -c 1 %s' % ip)[0] == 0
346-
347- @property
348- def admin(self):
349- return self.nova_admin.connection_for('admin')
350-
351- def connection_for(self, username):
352- return self.nova_admin.connection_for(username)
353-
354- def create_user(self, username):
355- return self.nova_admin.create_user(username)
356-
357- def get_user(self, username):
358- return self.nova_admin.get_user(username)
359-
360- def delete_user(self, username):
361- return self.nova_admin.delete_user(username)
362-
363- def get_signed_zip(self, username):
364- return self.nova_admin.get_zip(username)
365-
366- def create_key_pair(self, conn, key_name):
367- try:
368- os.remove('/tmp/%s.pem' % key_name)
369- except:
370- pass
371- key = conn.create_key_pair(key_name)
372- key.save('/tmp/')
373- return key
374-
375- def delete_key_pair(self, conn, key_name):
376- conn.delete_key_pair(key_name)
377- try:
378- os.remove('/tmp/%s.pem' % key_name)
379- except:
380- pass
381-
382- def bundle_image(self, image, kernel=False):
383- cmd = 'euca-bundle-image -i %s' % image
384- if kernel:
385- cmd += ' --kernel true'
386- status, output = commands.getstatusoutput(cmd)
387- if status != 0:
388- print '%s -> \n %s' % (cmd, output)
389- raise Exception(output)
390- return True
391-
392- def upload_image(self, bucket_name, image):
393- cmd = 'euca-upload-bundle -b %s -m /tmp/%s.manifest.xml' % (bucket_name, image)
394- status, output = commands.getstatusoutput(cmd)
395- if status != 0:
396- print '%s -> \n %s' % (cmd, output)
397- raise Exception(output)
398- return True
399-
400- def delete_bundle_bucket(self, bucket_name):
401- cmd = 'euca-delete-bundle --clear -b %s' % (bucket_name)
402- status, output = commands.getstatusoutput(cmd)
403- if status != 0:
404- print '%s -> \n%s' % (cmd, output)
405- raise Exception(output)
406- return True
407-
408- def register_image(self, bucket_name, manifest):
409- conn = nova_admin.connection_for('admin')
410- return conn.register_image("%s/%s.manifest.xml" % (bucket_name, manifest))
411-
412- def setUp_test_image(self, image, kernel=False):
413- self.bundle_image(image, kernel=kernel)
414- bucket = "auto_test_%s" % int(random.random() * 1000000)
415- self.upload_image(bucket, image)
416- return self.register_image(bucket, image)
417-
418- def tearDown_test_image(self, conn, image_id):
419- conn.deregister_image(image_id)
420
421=== removed file 'smoketests/smoketest.py'
422--- smoketests/smoketest.py 2010-07-15 16:07:40 +0000
423+++ smoketests/smoketest.py 1970-01-01 00:00:00 +0000
424@@ -1,566 +0,0 @@
425-# vim: tabstop=4 shiftwidth=4 softtabstop=4
426-
427-# Copyright 2010 United States Government as represented by the
428-# Administrator of the National Aeronautics and Space Administration.
429-# All Rights Reserved.
430-#
431-# Licensed under the Apache License, Version 2.0 (the "License"); you may
432-# not use this file except in compliance with the License. You may obtain
433-# a copy of the License at
434-#
435-# http://www.apache.org/licenses/LICENSE-2.0
436-#
437-# Unless required by applicable law or agreed to in writing, software
438-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
439-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
440-# License for the specific language governing permissions and limitations
441-# under the License.
442-
443-import commands
444-import os
445-import random
446-import re
447-import sys
448-import time
449-import unittest
450-import zipfile
451-
452-
453-import paramiko
454-
455-from smoketests import flags
456-from smoketests import novatestcase
457-
458-SUITE_NAMES = '[user, image, security, public_network, volume]'
459-
460-FLAGS = flags.FLAGS
461-flags.DEFINE_string('suite', None, 'Specific test suite to run ' + SUITE_NAMES)
462-
463-# TODO(devamcar): Use random tempfile
464-ZIP_FILENAME = '/tmp/nova-me-x509.zip'
465-
466-data = {}
467-
468-test_prefix = 'test%s' % int(random.random()*1000000)
469-test_username = '%suser' % test_prefix
470-test_bucket = '%s_bucket' % test_prefix
471-test_key = '%s_key' % test_prefix
472-
473-# Test admin credentials and user creation
474-class UserTests(novatestcase.NovaTestCase):
475- def test_001_admin_can_connect(self):
476- conn = self.connection_for('admin')
477- self.assert_(conn)
478-
479- def test_002_admin_can_create_user(self):
480- userinfo = self.create_user(test_username)
481- self.assertEqual(userinfo.username, test_username)
482-
483- def test_003_user_can_download_credentials(self):
484- buf = self.get_signed_zip(test_username)
485- output = open(ZIP_FILENAME, 'w')
486- output.write(buf)
487- output.close()
488-
489- zip = zipfile.ZipFile(ZIP_FILENAME, 'a', zipfile.ZIP_DEFLATED)
490- bad = zip.testzip()
491- zip.close()
492-
493- self.failIf(bad)
494-
495- def test_999_tearDown(self):
496- self.delete_user(test_username)
497- user = self.get_user(test_username)
498- self.assert_(user is None)
499- try:
500- os.remove(ZIP_FILENAME)
501- except:
502- pass
503-
504-# Test image bundling, registration, and launching
505-class ImageTests(novatestcase.NovaTestCase):
506- def test_000_setUp(self):
507- self.create_user(test_username)
508-
509- def test_001_admin_can_bundle_image(self):
510- self.assertTrue(self.bundle_image(FLAGS.bundle_image))
511-
512- def test_002_admin_can_upload_image(self):
513- self.assertTrue(self.upload_image(test_bucket, FLAGS.bundle_image))
514-
515- def test_003_admin_can_register_image(self):
516- image_id = self.register_image(test_bucket, FLAGS.bundle_image)
517- self.assert_(image_id is not None)
518- data['image_id'] = image_id
519-
520- def test_004_admin_can_bundle_kernel(self):
521- self.assertTrue(self.bundle_image(FLAGS.bundle_kernel, kernel=True))
522-
523- def test_005_admin_can_upload_kernel(self):
524- self.assertTrue(self.upload_image(test_bucket, FLAGS.bundle_kernel))
525-
526- def test_006_admin_can_register_kernel(self):
527- # FIXME(devcamcar): registration should verify that bucket/manifest
528- # exists before returning successfully.
529- kernel_id = self.register_image(test_bucket, FLAGS.bundle_kernel)
530- self.assert_(kernel_id is not None)
531- data['kernel_id'] = kernel_id
532-
533- def test_007_admin_images_are_available_within_10_seconds(self):
534- for i in xrange(10):
535- image = self.admin.get_image(data['image_id'])
536- if image and image.state == 'available':
537- break
538- time.sleep(1)
539- else:
540- print image.state
541- self.assert_(False) # wasn't available within 10 seconds
542- self.assert_(image.type == 'machine')
543-
544- for i in xrange(10):
545- kernel = self.admin.get_image(data['kernel_id'])
546- if kernel and kernel.state == 'available':
547- break
548- time.sleep(1)
549- else:
550- self.assert_(False) # wasn't available within 10 seconds
551- self.assert_(kernel.type == 'kernel')
552-
553- def test_008_admin_can_describe_image_attribute(self):
554- attrs = self.admin.get_image_attribute(data['image_id'],
555- 'launchPermission')
556- self.assert_(attrs.name, 'launch_permission')
557-
558- def test_009_me_cannot_see_non_public_images(self):
559- conn = self.connection_for(test_username)
560- images = conn.get_all_images(image_ids=[data['image_id']])
561- self.assertEqual(len(images), 0)
562-
563- def test_010_admin_can_modify_image_launch_permission(self):
564- conn = self.connection_for(test_username)
565-
566- self.admin.modify_image_attribute(image_id=data['image_id'],
567- operation='add',
568- attribute='launchPermission',
569- groups='all')
570-
571- image = conn.get_image(data['image_id'])
572- self.assertEqual(image.id, data['image_id'])
573-
574- def test_011_me_can_list_public_images(self):
575- conn = self.connection_for(test_username)
576- images = conn.get_all_images(image_ids=[data['image_id']])
577- self.assertEqual(len(images), 1)
578- pass
579-
580- def test_012_me_can_see_launch_permission(self):
581- attrs = self.admin.get_image_attribute(data['image_id'],
582- 'launchPermission')
583- self.assert_(attrs.name, 'launch_permission')
584- self.assert_(attrs.groups[0], 'all')
585-
586- # FIXME: add tests that user can launch image
587-
588-# def test_013_user_can_launch_admin_public_image(self):
589-# # TODO: Use openwrt kernel instead of default kernel
590-# conn = self.connection_for(test_username)
591-# reservation = conn.run_instances(data['image_id'])
592-# self.assertEqual(len(reservation.instances), 1)
593-# data['my_instance_id'] = reservation.instances[0].id
594-
595-# def test_014_instances_launch_within_30_seconds(self):
596-# pass
597-
598-# def test_015_user_can_terminate(self):
599-# conn = self.connection_for(test_username)
600-# terminated = conn.terminate_instances(
601-# instance_ids=[data['my_instance_id']])
602-# self.assertEqual(len(terminated), 1)
603-
604- def test_016_admin_can_deregister_kernel(self):
605- self.assertTrue(self.admin.deregister_image(data['kernel_id']))
606-
607- def test_017_admin_can_deregister_image(self):
608- self.assertTrue(self.admin.deregister_image(data['image_id']))
609-
610- def test_018_admin_can_delete_bundle(self):
611- self.assertTrue(self.delete_bundle_bucket(test_bucket))
612-
613- def test_999_tearDown(self):
614- data = {}
615- self.delete_user(test_username)
616-
617-
618-# Test key pairs and security groups
619-class SecurityTests(novatestcase.NovaTestCase):
620- def test_000_setUp(self):
621- self.create_user(test_username + '_me')
622- self.create_user(test_username + '_you')
623- data['image_id'] = 'ami-tiny'
624-
625- def test_001_me_can_create_keypair(self):
626- conn = self.connection_for(test_username + '_me')
627- key = self.create_key_pair(conn, test_key)
628- self.assertEqual(key.name, test_key)
629-
630- def test_002_you_can_create_keypair(self):
631- conn = self.connection_for(test_username + '_you')
632- key = self.create_key_pair(conn, test_key+ 'yourkey')
633- self.assertEqual(key.name, test_key+'yourkey')
634-
635- def test_003_me_can_create_instance_with_keypair(self):
636- conn = self.connection_for(test_username + '_me')
637- reservation = conn.run_instances(data['image_id'], key_name=test_key)
638- self.assertEqual(len(reservation.instances), 1)
639- data['my_instance_id'] = reservation.instances[0].id
640-
641- def test_004_me_can_obtain_private_ip_within_60_seconds(self):
642- conn = self.connection_for(test_username + '_me')
643- reservations = conn.get_all_instances([data['my_instance_id']])
644- instance = reservations[0].instances[0]
645- # allow 60 seconds to exit pending with IP
646- for x in xrange(60):
647- instance.update()
648- if instance.state != u'pending':
649- break
650- time.sleep(1)
651- else:
652- self.assert_(False)
653- # self.assertEqual(instance.state, u'running')
654- ip = reservations[0].instances[0].private_dns_name
655- self.failIf(ip == '0.0.0.0')
656- data['my_private_ip'] = ip
657- print data['my_private_ip'],
658-
659- def test_005_can_ping_private_ip(self):
660- for x in xrange(120):
661- # ping waits for 1 second
662- status, output = commands.getstatusoutput(
663- 'ping -c1 -w1 %s' % data['my_private_ip'])
664- if status == 0:
665- break
666- else:
667- self.assert_('could not ping instance')
668- #def test_005_me_cannot_ssh_when_unauthorized(self):
669- # self.assertRaises(paramiko.SSHException, self.connect_ssh,
670- # data['my_private_ip'], 'mykey')
671-
672- #def test_006_me_can_authorize_ssh(self):
673- # conn = self.connection_for(test_username + '_me')
674- # self.assertTrue(
675- # conn.authorize_security_group(
676- # 'default',
677- # ip_protocol='tcp',
678- # from_port=22,
679- # to_port=22,
680- # cidr_ip='0.0.0.0/0'
681- # )
682- # )
683-
684- def test_007_me_can_ssh_when_authorized(self):
685- conn = self.connect_ssh(data['my_private_ip'], test_key)
686- conn.close()
687-
688- #def test_008_me_can_revoke_ssh_authorization(self):
689- # conn = self.connection_for('me')
690- # self.assertTrue(
691- # conn.revoke_security_group(
692- # 'default',
693- # ip_protocol='tcp',
694- # from_port=22,
695- # to_port=22,
696- # cidr_ip='0.0.0.0/0'
697- # )
698- # )
699-
700- #def test_009_you_cannot_ping_my_instance(self):
701- # TODO: should ping my_private_ip from with an instance started by you.
702- #self.assertFalse(self.can_ping(data['my_private_ip']))
703-
704- def test_010_you_cannot_ssh_to_my_instance(self):
705- try:
706- conn = self.connect_ssh(data['my_private_ip'],
707- test_key + 'yourkey')
708- conn.close()
709- except paramiko.SSHException:
710- pass
711- else:
712- self.fail("expected SSHException")
713-
714- def test_999_tearDown(self):
715- conn = self.connection_for(test_username + '_me')
716- self.delete_key_pair(conn, test_key)
717- if data.has_key('my_instance_id'):
718- conn.terminate_instances([data['my_instance_id']])
719-
720- conn = self.connection_for(test_username + '_you')
721- self.delete_key_pair(conn, test_key + 'yourkey')
722-
723- conn = self.connection_for('admin')
724- self.delete_user(test_username + '_me')
725- self.delete_user(test_username + '_you')
726- #self.tearDown_test_image(conn, data['image_id'])
727-
728-# TODO: verify wrt image boots
729-# build python into wrt image
730-# build boto/m2crypto into wrt image
731-# build euca2ools into wrt image
732-# build a script to download and unpack credentials
733-# - return "ok" to stdout for comparison in self.assertEqual()
734-# build a script to bundle the instance
735-# build a script to upload the bundle
736-
737-# status, output = commands.getstatusoutput('cmd')
738-# if status == 0:
739-# print 'ok'
740-# else:
741-# print output
742-
743-# Testing rebundling
744-class RebundlingTests(novatestcase.NovaTestCase):
745- def test_000_setUp(self):
746- self.create_user('me')
747- self.create_user('you')
748- # TODO: create keypair for me
749- # upload smoketest img
750- # run instance
751-
752- def test_001_me_can_download_credentials_within_instance(self):
753- conn = self.connect_ssh(data['my_private_ip'], 'mykey')
754- stdin, stdout = conn.exec_command(
755- 'python ~/smoketests/install-credentials.py')
756- conn.close()
757- self.assertEqual(stdout, 'ok')
758-
759- def test_002_me_can_rebundle_within_instance(self):
760- conn = self.connect_ssh(data['my_private_ip'], 'mykey')
761- stdin, stdout = conn.exec_command(
762- 'python ~/smoketests/rebundle-instance.py')
763- conn.close()
764- self.assertEqual(stdout, 'ok')
765-
766- def test_003_me_can_upload_image_within_instance(self):
767- conn = self.connect_ssh(data['my_private_ip'], 'mykey')
768- stdin, stdout = conn.exec_command(
769- 'python ~/smoketests/upload-bundle.py')
770- conn.close()
771- self.assertEqual(stdout, 'ok')
772-
773- def test_004_me_can_register_image_within_instance(self):
774- conn = self.connect_ssh(data['my_private_ip'], 'mykey')
775- stdin, stdout = conn.exec_command(
776- 'python ~/smoketests/register-image.py')
777- conn.close()
778- if re.matches('ami-{\w+}', stdout):
779- data['my_image_id'] = stdout.strip()
780- else:
781- self.fail('expected ami-nnnnnn, got:\n ' + stdout)
782-
783- def test_005_you_cannot_see_my_private_image(self):
784- conn = self.connection_for('you')
785- image = conn.get_image(data['my_image_id'])
786- self.assertEqual(image, None)
787-
788- def test_006_me_can_make_image_public(self):
789- conn = self.connection_for(test_username)
790- conn.modify_image_attribute(image_id=data['my_image_id'],
791- operation='add',
792- attribute='launchPermission',
793- groups='all')
794-
795- def test_007_you_can_see_my_public_image(self):
796- conn = self.connection_for('you')
797- image = conn.get_image(data['my_image_id'])
798- self.assertEqual(image.id, data['my_image_id'])
799-
800- def test_999_tearDown(self):
801- self.delete_user('me')
802- self.delete_user('you')
803-
804- #if data.has_key('image_id'):
805- # deregister rebundled image
806-
807- # TODO: tear down instance
808- # delete keypairs
809- data = {}
810-
811-# Test elastic IPs
812-class ElasticIPTests(novatestcase.NovaTestCase):
813- def test_000_setUp(self):
814- data['image_id'] = 'ami-tiny'
815-
816- self.create_user('me')
817- conn = self.connection_for('me')
818- self.create_key_pair(conn, 'mykey')
819-
820- conn = self.connection_for('admin')
821- #data['image_id'] = self.setUp_test_image(FLAGS.bundle_image)
822-
823- def test_001_me_can_launch_image_with_keypair(self):
824- conn = self.connection_for('me')
825- reservation = conn.run_instances(data['image_id'], key_name='mykey')
826- self.assertEqual(len(reservation.instances), 1)
827- data['my_instance_id'] = reservation.instances[0].id
828-
829- def test_002_me_can_allocate_elastic_ip(self):
830- conn = self.connection_for('me')
831- data['my_public_ip'] = conn.allocate_address()
832- self.assert_(data['my_public_ip'].public_ip)
833-
834- def test_003_me_can_associate_ip_with_instance(self):
835- self.assertTrue(data['my_public_ip'].associate(data['my_instance_id']))
836-
837- def test_004_me_can_ssh_with_public_ip(self):
838- conn = self.connect_ssh(data['my_public_ip'].public_ip, 'mykey')
839- conn.close()
840-
841- def test_005_me_can_disassociate_ip_from_instance(self):
842- self.assertTrue(data['my_public_ip'].disassociate())
843-
844- def test_006_me_can_deallocate_elastic_ip(self):
845- self.assertTrue(data['my_public_ip'].delete())
846-
847- def test_999_tearDown(self):
848- conn = self.connection_for('me')
849- self.delete_key_pair(conn, 'mykey')
850-
851- conn = self.connection_for('admin')
852- #self.tearDown_test_image(conn, data['image_id'])
853- data = {}
854-
855-ZONE = 'nova'
856-DEVICE = 'vdb'
857-# Test iscsi volumes
858-class VolumeTests(novatestcase.NovaTestCase):
859- def test_000_setUp(self):
860- self.create_user(test_username)
861- data['image_id'] = 'ami-tiny' # A7370FE3
862-
863- conn = self.connection_for(test_username)
864- self.create_key_pair(conn, test_key)
865- reservation = conn.run_instances(data['image_id'],
866- instance_type='m1.tiny',
867- key_name=test_key)
868- data['instance_id'] = reservation.instances[0].id
869- data['private_ip'] = reservation.instances[0].private_dns_name
870- # wait for instance to show up
871- for x in xrange(120):
872- # ping waits for 1 second
873- status, output = commands.getstatusoutput(
874- 'ping -c1 -w1 %s' % data['private_ip'])
875- if status == 0:
876- break
877- else:
878- self.fail('unable to ping instance')
879-
880- def test_001_me_can_create_volume(self):
881- conn = self.connection_for(test_username)
882- volume = conn.create_volume(1, ZONE)
883- self.assertEqual(volume.size, 1)
884- data['volume_id'] = volume.id
885- # give network time to find volume
886- time.sleep(5)
887-
888- def test_002_me_can_attach_volume(self):
889- conn = self.connection_for(test_username)
890- conn.attach_volume(
891- volume_id = data['volume_id'],
892- instance_id = data['instance_id'],
893- device = '/dev/%s' % DEVICE
894- )
895- # give instance time to recognize volume
896- time.sleep(5)
897-
898- def test_003_me_can_mount_volume(self):
899- conn = self.connect_ssh(data['private_ip'], test_key)
900- # FIXME(devcamcar): the tiny image doesn't create the node properly
901- # this will make /dev/vd* if it doesn't exist
902- stdin, stdout, stderr = conn.exec_command(
903- 'grep %s /proc/partitions |' + \
904- '`awk \'{print "mknod /dev/"$4" b "$1" "$2}\'`' % DEVICE)
905- commands = []
906- commands.append('mkdir -p /mnt/vol')
907- commands.append('mkfs.ext2 /dev/%s' % DEVICE)
908- commands.append('mount /dev/%s /mnt/vol' % DEVICE)
909- commands.append('echo success')
910- stdin, stdout, stderr = conn.exec_command(' && '.join(commands))
911- out = stdout.read()
912- conn.close()
913- if not out.strip().endswith('success'):
914- self.fail('Unable to mount: %s %s' % (out, stderr.read()))
915-
916- def test_004_me_can_write_to_volume(self):
917- conn = self.connect_ssh(data['private_ip'], test_key)
918- # FIXME(devcamcar): This doesn't fail if the volume hasn't been mounted
919- stdin, stdout, stderr = conn.exec_command(
920- 'echo hello > /mnt/vol/test.txt')
921- err = stderr.read()
922- conn.close()
923- if len(err) > 0:
924- self.fail('Unable to write to mount: %s' % (err))
925-
926- def test_005_volume_is_correct_size(self):
927- conn = self.connect_ssh(data['private_ip'], test_key)
928- stdin, stdout, stderr = conn.exec_command(
929- "df -h | grep %s | awk {'print $2'}" % DEVICE)
930- out = stdout.read()
931- conn.close()
932- if not out.strip() == '1007.9M':
933- self.fail('Volume is not the right size: %s %s' % (out, stderr.read()))
934-
935- def test_006_me_can_umount_volume(self):
936- conn = self.connect_ssh(data['private_ip'], test_key)
937- stdin, stdout, stderr = conn.exec_command('umount /mnt/vol')
938- err = stderr.read()
939- conn.close()
940- if len(err) > 0:
941- self.fail('Unable to unmount: %s' % (err))
942-
943- def test_007_me_can_detach_volume(self):
944- conn = self.connection_for(test_username)
945- self.assertTrue(conn.detach_volume(volume_id = data['volume_id']))
946-
947- def test_008_me_can_delete_volume(self):
948- conn = self.connection_for(test_username)
949- self.assertTrue(conn.delete_volume(data['volume_id']))
950-
951- def test_009_volume_size_must_be_int(self):
952- conn = self.connection_for(test_username)
953- self.assertRaises(Exception, conn.create_volume, 'foo', ZONE)
954-
955- def test_999_tearDown(self):
956- global data
957- conn = self.connection_for(test_username)
958- self.delete_key_pair(conn, test_key)
959- if data.has_key('instance_id'):
960- conn.terminate_instances([data['instance_id']])
961- self.delete_user(test_username)
962- data = {}
963-
964-def build_suites():
965- return {
966- 'user': unittest.makeSuite(UserTests),
967- 'image': unittest.makeSuite(ImageTests),
968- 'security': unittest.makeSuite(SecurityTests),
969- 'public_network': unittest.makeSuite(ElasticIPTests),
970- 'volume': unittest.makeSuite(VolumeTests),
971- }
972-
973-def main():
974- argv = FLAGS(sys.argv)
975- suites = build_suites()
976-
977- if FLAGS.suite:
978- try:
979- suite = suites[FLAGS.suite]
980- except KeyError:
981- print >> sys.stderr, 'Available test suites:', SUITE_NAMES
982- return 1
983-
984- unittest.TextTestRunner(verbosity=2).run(suite)
985- else:
986- for suite in suites.itervalues():
987- unittest.TextTestRunner(verbosity=2).run(suite)
988-
989-if __name__ == "__main__":
990- sys.exit(main())
991
992=== added file 'smoketests/user_smoketests.py'
993--- smoketests/user_smoketests.py 1970-01-01 00:00:00 +0000
994+++ smoketests/user_smoketests.py 2010-11-04 22:56:08 +0000
995@@ -0,0 +1,326 @@
996+# vim: tabstop=4 shiftwidth=4 softtabstop=4
997+
998+# Copyright 2010 United States Government as represented by the
999+# Administrator of the National Aeronautics and Space Administration.
1000+# All Rights Reserved.
1001+#
1002+# Licensed under the Apache License, Version 2.0 (the "License"); you may
1003+# not use this file except in compliance with the License. You may obtain
1004+# a copy of the License at
1005+#
1006+# http://www.apache.org/licenses/LICENSE-2.0
1007+#
1008+# Unless required by applicable law or agreed to in writing, software
1009+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1010+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1011+# License for the specific language governing permissions and limitations
1012+# under the License.
1013+
1014+import commands
1015+import os
1016+import random
1017+import socket
1018+import sys
1019+import time
1020+import unittest
1021+
1022+from smoketests import flags
1023+from smoketests import base
1024+
1025+
1026+SUITE_NAMES = '[image, instance, volume]'
1027+
1028+FLAGS = flags.FLAGS
1029+flags.DEFINE_string('suite', None, 'Specific test suite to run ' + SUITE_NAMES)
1030+flags.DEFINE_string('bundle_kernel', 'openwrt-x86-vmlinuz',
1031+ 'Local kernel file to use for bundling tests')
1032+flags.DEFINE_string('bundle_image', 'openwrt-x86-ext2.image',
1033+ 'Local image file to use for bundling tests')
1034+
1035+TEST_PREFIX = 'test%s' % int (random.random()*1000000)
1036+TEST_BUCKET = '%s_bucket' % TEST_PREFIX
1037+TEST_KEY = '%s_key' % TEST_PREFIX
1038+TEST_DATA = {}
1039+
1040+
1041+class UserSmokeTestCase(base.SmokeTestCase):
1042+ def setUp(self):
1043+ global TEST_DATA
1044+ self.conn = self.connection_for_env()
1045+ self.data = TEST_DATA
1046+
1047+
1048+class ImageTests(UserSmokeTestCase):
1049+ def test_001_can_bundle_image(self):
1050+ self.assertTrue(self.bundle_image(FLAGS.bundle_image))
1051+
1052+ def test_002_can_upload_image(self):
1053+ self.assertTrue(self.upload_image(TEST_BUCKET, FLAGS.bundle_image))
1054+
1055+ def test_003_can_register_image(self):
1056+ image_id = self.conn.register_image('%s/%s.manifest.xml' %
1057+ (TEST_BUCKET, FLAGS.bundle_image))
1058+ self.assert_(image_id is not None)
1059+ self.data['image_id'] = image_id
1060+
1061+ def test_004_can_bundle_kernel(self):
1062+ self.assertTrue(self.bundle_image(FLAGS.bundle_kernel, kernel=True))
1063+
1064+ def test_005_can_upload_kernel(self):
1065+ self.assertTrue(self.upload_image(TEST_BUCKET, FLAGS.bundle_kernel))
1066+
1067+ def test_006_can_register_kernel(self):
1068+ kernel_id = self.conn.register_image('%s/%s.manifest.xml' %
1069+ (TEST_BUCKET, FLAGS.bundle_kernel))
1070+ self.assert_(kernel_id is not None)
1071+ self.data['kernel_id'] = kernel_id
1072+
1073+ def test_007_images_are_available_within_10_seconds(self):
1074+ for i in xrange(10):
1075+ image = self.conn.get_image(self.data['image_id'])
1076+ if image and image.state == 'available':
1077+ break
1078+ time.sleep(1)
1079+ else:
1080+ print image.state
1081+ self.assert_(False) # wasn't available within 10 seconds
1082+ self.assert_(image.type == 'machine')
1083+
1084+ for i in xrange(10):
1085+ kernel = self.conn.get_image(self.data['kernel_id'])
1086+ if kernel and kernel.state == 'available':
1087+ break
1088+ time.sleep(1)
1089+ else:
1090+ self.assert_(False) # wasn't available within 10 seconds
1091+ self.assert_(kernel.type == 'kernel')
1092+
1093+ def test_008_can_describe_image_attribute(self):
1094+ attrs = self.conn.get_image_attribute(self.data['image_id'],
1095+ 'launchPermission')
1096+ self.assert_(attrs.name, 'launch_permission')
1097+
1098+ def test_009_can_modify_image_launch_permission(self):
1099+ self.conn.modify_image_attribute(image_id=self.data['image_id'],
1100+ operation='add',
1101+ attribute='launchPermission',
1102+ groups='all')
1103+ image = self.conn.get_image(self.data['image_id'])
1104+ self.assertEqual(image.id, self.data['image_id'])
1105+
1106+ def test_010_can_see_launch_permission(self):
1107+ attrs = self.conn.get_image_attribute(self.data['image_id'],
1108+ 'launchPermission')
1109+ self.assert_(attrs.name, 'launch_permission')
1110+ self.assert_(attrs.attrs['groups'][0], 'all')
1111+
1112+ def test_011_user_can_deregister_kernel(self):
1113+ self.assertTrue(self.conn.deregister_image(self.data['kernel_id']))
1114+
1115+ def test_012_can_deregister_image(self):
1116+ self.assertTrue(self.conn.deregister_image(self.data['image_id']))
1117+
1118+ def test_013_can_delete_bundle(self):
1119+ self.assertTrue(self.delete_bundle_bucket(TEST_BUCKET))
1120+
1121+
1122+class InstanceTests(UserSmokeTestCase):
1123+ def test_001_can_create_keypair(self):
1124+ key = self.create_key_pair(self.conn, TEST_KEY)
1125+ self.assertEqual(key.name, TEST_KEY)
1126+
1127+ def test_002_can_create_instance_with_keypair(self):
1128+ reservation = self.conn.run_instances(FLAGS.test_image,
1129+ key_name=TEST_KEY,
1130+ instance_type='m1.tiny')
1131+ self.assertEqual(len(reservation.instances), 1)
1132+ self.data['instance_id'] = reservation.instances[0].id
1133+
1134+ def test_003_instance_runs_within_60_seconds(self):
1135+ reservations = self.conn.get_all_instances([data['instance_id']])
1136+ instance = reservations[0].instances[0]
1137+ # allow 60 seconds to exit pending with IP
1138+ for x in xrange(60):
1139+ instance.update()
1140+ if instance.state == u'running':
1141+ break
1142+ time.sleep(1)
1143+ else:
1144+ self.fail('instance failed to start')
1145+ ip = reservations[0].instances[0].private_dns_name
1146+ self.failIf(ip == '0.0.0.0')
1147+ self.data['private_ip'] = ip
1148+ print self.data['private_ip']
1149+
1150+ def test_004_can_ping_private_ip(self):
1151+ for x in xrange(120):
1152+ # ping waits for 1 second
1153+ status, output = commands.getstatusoutput(
1154+ 'ping -c1 %s' % self.data['private_ip'])
1155+ if status == 0:
1156+ break
1157+ else:
1158+ self.fail('could not ping instance')
1159+
1160+ def test_005_can_ssh_to_private_ip(self):
1161+ for x in xrange(30):
1162+ try:
1163+ conn = self.connect_ssh(self.data['private_ip'], TEST_KEY)
1164+ conn.close()
1165+ except Exception:
1166+ time.sleep(1)
1167+ else:
1168+ break
1169+ else:
1170+ self.fail('could not ssh to instance')
1171+
1172+ def test_006_can_allocate_elastic_ip(self):
1173+ result = self.conn.allocate_address()
1174+ self.assertTrue(hasattr(result, 'public_ip'))
1175+ self.data['public_ip'] = result.public_ip
1176+
1177+ def test_007_can_associate_ip_with_instance(self):
1178+ result = self.conn.associate_address(self.data['instance_id'],
1179+ self.data['public_ip'])
1180+ self.assertTrue(result)
1181+
1182+ def test_008_can_ssh_with_public_ip(self):
1183+ for x in xrange(30):
1184+ try:
1185+ conn = self.connect_ssh(self.data['public_ip'], TEST_KEY)
1186+ conn.close()
1187+ except socket.error:
1188+ time.sleep(1)
1189+ else:
1190+ break
1191+ else:
1192+ self.fail('could not ssh to instance')
1193+
1194+ def test_009_can_disassociate_ip_from_instance(self):
1195+ result = self.conn.disassociate_address(self.data['public_ip'])
1196+ self.assertTrue(result)
1197+
1198+ def test_010_can_deallocate_elastic_ip(self):
1199+ result = self.conn.release_address(self.data['public_ip'])
1200+ self.assertTrue(result)
1201+
1202+ def test_999_tearDown(self):
1203+ self.delete_key_pair(self.conn, TEST_KEY)
1204+ if self.data.has_key('instance_id'):
1205+ self.conn.terminate_instances([data['instance_id']])
1206+
1207+
1208+class VolumeTests(UserSmokeTestCase):
1209+ def setUp(self):
1210+ super(VolumeTests, self).setUp()
1211+ self.device = '/dev/vdb'
1212+
1213+ def test_000_setUp(self):
1214+ self.create_key_pair(self.conn, TEST_KEY)
1215+ reservation = self.conn.run_instances(FLAGS.test_image,
1216+ instance_type='m1.tiny',
1217+ key_name=TEST_KEY)
1218+ instance = reservation.instances[0]
1219+ self.data['instance'] = instance
1220+ for x in xrange(120):
1221+ if self.can_ping(instance.private_dns_name):
1222+ break
1223+ else:
1224+ self.fail('unable to start instance')
1225+
1226+ def test_001_can_create_volume(self):
1227+ volume = self.conn.create_volume(1, 'nova')
1228+ self.assertEqual(volume.size, 1)
1229+ self.data['volume'] = volume
1230+ # Give network time to find volume.
1231+ time.sleep(5)
1232+
1233+ def test_002_can_attach_volume(self):
1234+ volume = self.data['volume']
1235+
1236+ for x in xrange(10):
1237+ if volume.status == u'available':
1238+ break
1239+ time.sleep(5)
1240+ volume.update()
1241+ else:
1242+ self.fail('cannot attach volume with state %s' % volume.status)
1243+
1244+ volume.attach(self.data['instance'].id, self.device)
1245+
1246+ # Volumes seems to report "available" too soon.
1247+ for x in xrange(10):
1248+ if volume.status == u'in-use':
1249+ break
1250+ time.sleep(5)
1251+ volume.update()
1252+
1253+ self.assertEqual(volume.status, u'in-use')
1254+
1255+ # Give instance time to recognize volume.
1256+ time.sleep(5)
1257+
1258+ def test_003_can_mount_volume(self):
1259+ ip = self.data['instance'].private_dns_name
1260+ conn = self.connect_ssh(ip, TEST_KEY)
1261+ commands = []
1262+ commands.append('mkdir -p /mnt/vol')
1263+ commands.append('mkfs.ext2 %s' % self.device)
1264+ commands.append('mount %s /mnt/vol' % self.device)
1265+ commands.append('echo success')
1266+ stdin, stdout, stderr = conn.exec_command(' && '.join(commands))
1267+ out = stdout.read()
1268+ conn.close()
1269+ if not out.strip().endswith('success'):
1270+ self.fail('Unable to mount: %s %s' % (out, stderr.read()))
1271+
1272+ def test_004_can_write_to_volume(self):
1273+ ip = self.data['instance'].private_dns_name
1274+ conn = self.connect_ssh(ip, TEST_KEY)
1275+ # FIXME(devcamcar): This doesn't fail if the volume hasn't been mounted
1276+ stdin, stdout, stderr = conn.exec_command(
1277+ 'echo hello > /mnt/vol/test.txt')
1278+ err = stderr.read()
1279+ conn.close()
1280+ if len(err) > 0:
1281+ self.fail('Unable to write to mount: %s' % (err))
1282+
1283+ def test_005_volume_is_correct_size(self):
1284+ ip = self.data['instance'].private_dns_name
1285+ conn = self.connect_ssh(ip, TEST_KEY)
1286+ stdin, stdout, stderr = conn.exec_command(
1287+ "df -h | grep %s | awk {'print $2'}" % self.device)
1288+ out = stdout.read()
1289+ conn.close()
1290+ if not out.strip() == '1008M':
1291+ self.fail('Volume is not the right size: %s %s' %
1292+ (out, stderr.read()))
1293+
1294+ def test_006_me_can_umount_volume(self):
1295+ ip = self.data['instance'].private_dns_name
1296+ conn = self.connect_ssh(ip, TEST_KEY)
1297+ stdin, stdout, stderr = conn.exec_command('umount /mnt/vol')
1298+ err = stderr.read()
1299+ conn.close()
1300+ if len(err) > 0:
1301+ self.fail('Unable to unmount: %s' % (err))
1302+
1303+ def test_007_me_can_detach_volume(self):
1304+ result = self.conn.detach_volume(volume_id=self.data['volume'].id)
1305+ self.assertTrue(result)
1306+ time.sleep(5)
1307+
1308+ def test_008_me_can_delete_volume(self):
1309+ result = self.conn.delete_volume(self.data['volume'].id)
1310+ self.assertTrue(result)
1311+
1312+ def test_999_tearDown(self):
1313+ self.conn.terminate_instances([self.data['instance'].id])
1314+ self.conn.delete_key_pair(TEST_KEY)
1315+
1316+
1317+if __name__ == "__main__":
1318+ suites = {'image': unittest.makeSuite(ImageTests),
1319+ 'instance': unittest.makeSuite(InstanceTests),
1320+ 'volume': unittest.makeSuite(VolumeTests)}
1321+ sys.exit(base.run_tests(suites))