Andrea Frittoli | f5da28b | 2013-12-06 07:08:07 +0000 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 Hewlett-Packard, Ltd. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | from tempest.common.utils import misc |
| 18 | from tempest import config |
| 19 | from tempest.scenario import manager |
| 20 | |
| 21 | import json |
| 22 | import re |
| 23 | import string |
| 24 | import unicodedata |
| 25 | |
| 26 | CONF = config.CONF |
| 27 | |
| 28 | |
| 29 | @misc.singleton |
| 30 | class ImageUtils(object): |
| 31 | |
| 32 | default_ssh_user = 'root' |
| 33 | |
| 34 | def __init__(self): |
| 35 | # Load configuration items |
| 36 | self.ssh_users = json.loads(CONF.input_scenario.ssh_user_regex) |
| 37 | self.non_ssh_image_pattern = \ |
| 38 | CONF.input_scenario.non_ssh_image_regex |
| 39 | # Setup clients |
| 40 | ocm = manager.OfficialClientManager(CONF.identity.username, |
| 41 | CONF.identity.password, |
| 42 | CONF.identity.tenant_name) |
| 43 | self.client = ocm.compute_client |
| 44 | |
| 45 | def ssh_user(self, image_id): |
| 46 | _image = self.client.images.get(image_id) |
| 47 | for regex, user in self.ssh_users: |
| 48 | # First match wins |
| 49 | if re.match(regex, _image.name) is not None: |
| 50 | return user |
| 51 | else: |
| 52 | return self.default_ssh_user |
| 53 | |
| 54 | def _is_sshable_image(self, image): |
| 55 | return not re.search(pattern=self.non_ssh_image_pattern, |
| 56 | string=str(image.name)) |
| 57 | |
| 58 | def is_sshable_image(self, image_id): |
| 59 | _image = self.client.images.get(image_id) |
| 60 | return self._is_sshable_image(_image) |
| 61 | |
| 62 | def _is_flavor_enough(self, flavor, image): |
| 63 | return image.minDisk <= flavor.disk |
| 64 | |
| 65 | def is_flavor_enough(self, flavor_id, image_id): |
| 66 | _image = self.client.images.get(image_id) |
| 67 | _flavor = self.client.flavors.get(flavor_id) |
| 68 | return self._is_flavor_enough(_flavor, _image) |
| 69 | |
| 70 | |
| 71 | @misc.singleton |
| 72 | class InputScenarioUtils(object): |
| 73 | |
| 74 | """ |
| 75 | Example usage: |
| 76 | |
| 77 | import testscenarios |
| 78 | (...) |
| 79 | load_tests = testscenarios.load_tests_apply_scenarios |
| 80 | |
| 81 | |
| 82 | class TestInputScenario(manager.OfficialClientTest): |
| 83 | |
| 84 | scenario_utils = test_utils.InputScenarioUtils() |
| 85 | scenario_flavor = scenario_utils.scenario_flavors |
| 86 | scenario_image = scenario_utils.scenario_images |
| 87 | scenarios = testscenarios.multiply_scenarios(scenario_image, |
| 88 | scenario_flavor) |
| 89 | |
| 90 | def test_create_server_metadata(self): |
| 91 | name = rand_name('instance') |
| 92 | _ = self.compute_client.servers.create(name=name, |
| 93 | flavor=self.flavor_ref, |
| 94 | image=self.image_ref) |
| 95 | """ |
| 96 | validchars = "-_.{ascii}{digit}".format(ascii=string.ascii_letters, |
| 97 | digit=string.digits) |
| 98 | |
| 99 | def __init__(self): |
| 100 | ocm = manager.OfficialClientManager(CONF.identity.username, |
| 101 | CONF.identity.password, |
| 102 | CONF.identity.tenant_name) |
| 103 | self.client = ocm.compute_client |
| 104 | self.image_pattern = CONF.input_scenario.image_regex |
| 105 | self.flavor_pattern = CONF.input_scenario.flavor_regex |
| 106 | |
| 107 | def _normalize_name(self, name): |
| 108 | nname = unicodedata.normalize('NFKD', name).encode('ASCII', 'ignore') |
| 109 | nname = ''.join(c for c in nname if c in self.validchars) |
| 110 | return nname |
| 111 | |
| 112 | @property |
| 113 | def scenario_images(self): |
| 114 | """ |
| 115 | :return: a scenario with name and uuid of images |
| 116 | """ |
| 117 | if not hasattr(self, '_scenario_images'): |
| 118 | images = self.client.images.list(detailed=False) |
| 119 | self._scenario_images = [ |
| 120 | (self._normalize_name(i.name), dict(image_ref=i.id)) |
| 121 | for i in images if re.search(self.image_pattern, str(i.name)) |
| 122 | ] |
| 123 | return self._scenario_images |
| 124 | |
| 125 | @property |
| 126 | def scenario_flavors(self): |
| 127 | """ |
| 128 | :return: a scenario with name and uuid of flavors |
| 129 | """ |
| 130 | if not hasattr(self, '_scenario_flavors'): |
| 131 | flavors = self.client.flavors.list(detailed=False) |
| 132 | self._scenario_flavors = [ |
| 133 | (self._normalize_name(f.name), dict(flavor_ref=f.id)) |
| 134 | for f in flavors if re.search(self.flavor_pattern, str(f.name)) |
| 135 | ] |
| 136 | return self._scenario_flavors |