blob: 216f506941af6bfab4ccdc0b03cf51437a2e2189 [file] [log] [blame]
Daryl Walleckc7251962012-03-12 17:26:54 -05001from tempest import exceptions
2from tempest import openstack
3from tempest.config import TempestConfig
4
5import unittest2 as unittest
6
7
8class BaseComputeTest(unittest.TestCase):
9
10 os = openstack.Manager()
11 servers_client = os.servers_client
12 flavors_client = os.flavors_client
13 images_client = os.images_client
14 extensions_client = os.extensions_client
15 floating_ips_client = os.floating_ips_client
16 keypairs_client = os.keypairs_client
17 floating_ips_client = os.floating_ips_client
18 security_groups_client = os.security_groups_client
19 limits_client = os.limits_client
20 config = os.config
21 build_interval = config.compute.build_interval
22 build_timeout = config.compute.build_timeout
23
24 # Validate reference data exists
25 # If not, attempt to auto-configure
26 try:
27 image_ref = config.compute.image_ref
28 image_ref_alt = config.compute.image_ref_alt
29 images_client.get_image(image_ref)
30 images_client.get_image(image_ref_alt)
31 except:
32 # Make a reasonable attempt to get usable images
33 params = {'status': 'ACTIVE'}
34 _, images = images_client.list_images_with_detail(params)
35 if len(images) is 0:
36 message = "No usable image exists. Upload an image to Glance."
37 raise exceptions.InvalidConfiguration(message=message)
38 if len(images) is 1:
39 image_ref = images[0]['id']
40 image_ref_alt = images[0]['id']
41 else:
42 # Try to determine if this is a devstack environment.
43 # If so, some of the images are not usable
44
45 # For now, the useable image in devstack has this property
46 usable = [i for i in images if 'ramdisk_id' in i['metadata']]
47 if len(usable) > 0:
48 # We've found at least one image we can use
49 image_ref = usable[0]['id']
50 image_ref_alt = usable[0]['id']
51 else:
52 # We've done our due dillegence, take the first two images
53 image_ref = images[0]['id']
54 image_ref_alt = images[1]['id']
55
56 try:
57 flavor_ref = config.compute.flavor_ref
58 flavor_ref_alt = config.compute.flavor_ref_alt
59 flavors_client.get_flavor_details(flavor_ref)
60 flavors_client.get_flavor_details(flavor_ref_alt)
61 except:
62 # Reload both with new values
63 # Sort so the smallest flavors are used. This is for efficiency.
64 _, flavors = flavors_client.list_flavors_with_detail()
65 flavors = sorted(flavors, key=lambda k: k['ram'])
66
67 if len(flavors) is 0:
68 message = "No flavors exists. Add flavors via the admin API."
69 raise exceptions.InvalidConfiguration(message=message)
70 if len(flavors) is 1:
71 flavor_ref = flavors[0]['id']
72 flavor_ref_alt = flavors[0]['id']
73 else:
74 flavor_ref = flavors[0]['id']
75 # Make sure the second flavor does not have the same RAM
76 for i in range(1, len(flavors)):
77 if flavors[i] == flavors[-1]:
78 # We've tried. Take the last flavor
79 flavor_ref_alt = flavors[i]['id']
80 else:
81 if flavors[i]['ram'] > flavors[0]['ram']:
82 flavor_ref_alt = flavors[i]['id']
83 break