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