Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
Joe H. Rahme | 61469fd | 2014-12-02 17:09:17 +0100 | [diff] [blame] | 15 | """Javelin is a tool for creating, verifying, and deleting a small set of |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 16 | resources in a declarative way. |
| 17 | |
Joe H. Rahme | 61469fd | 2014-12-02 17:09:17 +0100 | [diff] [blame] | 18 | Javelin is meant to be used as a way to validate quickly that resources can |
| 19 | survive an upgrade process. |
| 20 | |
| 21 | Authentication |
| 22 | -------------- |
| 23 | |
| 24 | Javelin will be creating (and removing) users and tenants so it needs the admin |
| 25 | credentials of your cloud to operate properly. The corresponding info can be |
| 26 | given the usual way, either through CLI options or environment variables. |
| 27 | |
| 28 | You're probably familiar with these, but just in case:: |
| 29 | |
| 30 | +----------+------------------+----------------------+ |
| 31 | | Param | CLI | Environment Variable | |
| 32 | +----------+------------------+----------------------+ |
| 33 | | Username | --os-username | OS_USERNAME | |
| 34 | | Password | --os-password | OS_PASSWORD | |
| 35 | | Tenant | --os-tenant-name | OS_TENANT_NAME | |
| 36 | +----------+------------------+----------------------+ |
| 37 | |
| 38 | |
| 39 | Runtime Arguments |
| 40 | ----------------- |
| 41 | |
| 42 | **-m/--mode**: (Required) Has to be one of 'check', 'create' or 'destroy'. It |
| 43 | indicates which actions javelin is going to perform. |
| 44 | |
| 45 | **-r/--resources**: (Required) The path to a YAML file describing the resources |
| 46 | used by Javelin. |
| 47 | |
| 48 | **-d/--devstack-base**: (Required) The path to the devstack repo used to |
| 49 | retrieve artefacts (like images) that will be referenced in the resource files. |
| 50 | |
| 51 | **-c/--config-file**: (Optional) The path to a valid Tempest config file |
| 52 | describing your cloud. Javelin may use this to determine if certain services |
| 53 | are enabled and modify its behavior accordingly. |
| 54 | |
| 55 | |
| 56 | Resource file |
| 57 | ------------- |
| 58 | |
| 59 | The resource file is a valid YAML file describing the resources that will be |
| 60 | created, checked and destroyed by javelin. Here's a canonical example of a |
| 61 | resource file:: |
| 62 | |
| 63 | tenants: |
| 64 | - javelin |
| 65 | - discuss |
| 66 | |
| 67 | users: |
| 68 | - name: javelin |
| 69 | pass: gungnir |
| 70 | tenant: javelin |
| 71 | - name: javelin2 |
| 72 | pass: gungnir2 |
| 73 | tenant: discuss |
| 74 | |
| 75 | # resources that we want to create |
| 76 | images: |
| 77 | - name: javelin_cirros |
| 78 | owner: javelin |
| 79 | file: cirros-0.3.2-x86_64-blank.img |
| 80 | format: ami |
| 81 | aki: cirros-0.3.2-x86_64-vmlinuz |
| 82 | ari: cirros-0.3.2-x86_64-initrd |
| 83 | |
| 84 | servers: |
| 85 | - name: peltast |
| 86 | owner: javelin |
| 87 | flavor: m1.small |
| 88 | image: javelin_cirros |
| 89 | - name: hoplite |
| 90 | owner: javelin |
| 91 | flavor: m1.medium |
| 92 | image: javelin_cirros |
| 93 | |
| 94 | |
| 95 | An important piece of the resource definition is the *owner* field, which is |
| 96 | the user (that we've created) that is the owner of that resource. All |
| 97 | operations on that resource will happen as that regular user to ensure that |
| 98 | admin level access does not mask issues. |
| 99 | |
| 100 | The check phase will act like a unit test, using well known assert methods to |
| 101 | verify that the correct resources exist. |
| 102 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 103 | """ |
| 104 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 105 | import argparse |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 106 | import collections |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 107 | import datetime |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 108 | import os |
| 109 | import sys |
| 110 | import unittest |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 111 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 112 | import netaddr |
Masayuki Igawa | d938876 | 2015-01-20 14:56:42 +0900 | [diff] [blame^] | 113 | from tempest_lib import exceptions as lib_exc |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 114 | import yaml |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 115 | |
| 116 | import tempest.auth |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 117 | from tempest import config |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 118 | from tempest import exceptions |
Joe Gordon | 915eb8e | 2014-07-17 11:25:46 +0200 | [diff] [blame] | 119 | from tempest.openstack.common import log as logging |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 120 | from tempest.openstack.common import timeutils |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 121 | from tempest.services.compute.json import flavors_client |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 122 | from tempest.services.compute.json import security_groups_client |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 123 | from tempest.services.compute.json import servers_client |
| 124 | from tempest.services.identity.json import identity_client |
| 125 | from tempest.services.image.v2.json import image_client |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 126 | from tempest.services.network.json import network_client |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 127 | from tempest.services.object_storage import container_client |
| 128 | from tempest.services.object_storage import object_client |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 129 | from tempest.services.telemetry.json import telemetry_client |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 130 | from tempest.services.volume.json import volumes_client |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 131 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 132 | CONF = config.CONF |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 133 | OPTS = {} |
| 134 | USERS = {} |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 135 | RES = collections.defaultdict(list) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 136 | |
| 137 | LOG = None |
| 138 | |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 139 | JAVELIN_START = datetime.datetime.utcnow() |
| 140 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 141 | |
| 142 | class OSClient(object): |
| 143 | _creds = None |
| 144 | identity = None |
| 145 | servers = None |
| 146 | |
| 147 | def __init__(self, user, pw, tenant): |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 148 | default_params = { |
| 149 | 'disable_ssl_certificate_validation': |
| 150 | CONF.identity.disable_ssl_certificate_validation, |
| 151 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 152 | 'trace_requests': CONF.debug.trace_requests |
| 153 | } |
Ken'ichi Ohmichi | d5dba1c | 2015-01-23 02:23:22 +0000 | [diff] [blame] | 154 | default_params_with_timeout_values = { |
| 155 | 'build_interval': CONF.compute.build_interval, |
| 156 | 'build_timeout': CONF.compute.build_timeout |
| 157 | } |
| 158 | default_params_with_timeout_values.update(default_params) |
| 159 | |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 160 | compute_params = { |
| 161 | 'service': CONF.compute.catalog_type, |
| 162 | 'region': CONF.compute.region or CONF.identity.region, |
| 163 | 'endpoint_type': CONF.compute.endpoint_type, |
| 164 | 'build_interval': CONF.compute.build_interval, |
| 165 | 'build_timeout': CONF.compute.build_timeout |
| 166 | } |
| 167 | compute_params.update(default_params) |
| 168 | |
Ken'ichi Ohmichi | 564b2ad | 2015-01-22 02:08:59 +0000 | [diff] [blame] | 169 | object_storage_params = { |
| 170 | 'service': CONF.object_storage.catalog_type, |
| 171 | 'region': CONF.object_storage.region or CONF.identity.region, |
| 172 | 'endpoint_type': CONF.object_storage.endpoint_type |
| 173 | } |
| 174 | object_storage_params.update(default_params) |
| 175 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 176 | _creds = tempest.auth.KeystoneV2Credentials( |
| 177 | username=user, |
| 178 | password=pw, |
| 179 | tenant_name=tenant) |
| 180 | _auth = tempest.auth.KeystoneV2AuthProvider(_creds) |
| 181 | self.identity = identity_client.IdentityClientJSON(_auth) |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 182 | self.servers = servers_client.ServersClientJSON(_auth, |
| 183 | **compute_params) |
| 184 | self.flavors = flavors_client.FlavorsClientJSON(_auth, |
| 185 | **compute_params) |
| 186 | self.secgroups = security_groups_client.SecurityGroupsClientJSON( |
| 187 | _auth, **compute_params) |
Ken'ichi Ohmichi | 564b2ad | 2015-01-22 02:08:59 +0000 | [diff] [blame] | 188 | self.objects = object_client.ObjectClient(_auth, |
| 189 | **object_storage_params) |
| 190 | self.containers = container_client.ContainerClient( |
| 191 | _auth, **object_storage_params) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 192 | self.images = image_client.ImageClientV2JSON(_auth) |
Ken'ichi Ohmichi | d5dba1c | 2015-01-23 02:23:22 +0000 | [diff] [blame] | 193 | self.telemetry = telemetry_client.TelemetryClientJSON( |
| 194 | _auth, |
| 195 | CONF.telemetry.catalog_type, |
| 196 | CONF.identity.region, |
| 197 | endpoint_type=CONF.telemetry.endpoint_type, |
| 198 | **default_params_with_timeout_values) |
Ken'ichi Ohmichi | f85e9bd | 2015-01-27 12:51:47 +0000 | [diff] [blame] | 199 | self.volumes = volumes_client.VolumesClientJSON( |
| 200 | _auth, |
| 201 | CONF.volume.catalog_type, |
| 202 | CONF.volume.region or CONF.identity.region, |
| 203 | endpoint_type=CONF.volume.endpoint_type, |
| 204 | build_interval=CONF.volume.build_interval, |
| 205 | build_timeout=CONF.volume.build_timeout, |
| 206 | **default_params) |
Ken'ichi Ohmichi | a182e86 | 2015-01-21 01:16:37 +0000 | [diff] [blame] | 207 | self.networks = network_client.NetworkClientJSON( |
| 208 | _auth, |
| 209 | CONF.network.catalog_type, |
| 210 | CONF.network.region or CONF.identity.region, |
| 211 | endpoint_type=CONF.network.endpoint_type, |
| 212 | build_interval=CONF.network.build_interval, |
| 213 | build_timeout=CONF.network.build_timeout, |
| 214 | **default_params) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 215 | |
| 216 | |
| 217 | def load_resources(fname): |
Joe H. Rahme | 0273673 | 2015-01-27 18:33:09 +0100 | [diff] [blame] | 218 | """Load the expected resources from a yaml file.""" |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 219 | return yaml.load(open(fname, 'r')) |
| 220 | |
| 221 | |
| 222 | def keystone_admin(): |
| 223 | return OSClient(OPTS.os_username, OPTS.os_password, OPTS.os_tenant_name) |
| 224 | |
| 225 | |
| 226 | def client_for_user(name): |
| 227 | LOG.debug("Entering client_for_user") |
| 228 | if name in USERS: |
| 229 | user = USERS[name] |
| 230 | LOG.debug("Created client for user %s" % user) |
| 231 | return OSClient(user['name'], user['pass'], user['tenant']) |
| 232 | else: |
| 233 | LOG.error("%s not found in USERS: %s" % (name, USERS)) |
| 234 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 235 | |
| 236 | def resp_ok(response): |
| 237 | return 200 >= int(response['status']) < 300 |
| 238 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 239 | ################### |
| 240 | # |
| 241 | # TENANTS |
| 242 | # |
| 243 | ################### |
| 244 | |
| 245 | |
| 246 | def create_tenants(tenants): |
| 247 | """Create tenants from resource definition. |
| 248 | |
| 249 | Don't create the tenants if they already exist. |
| 250 | """ |
| 251 | admin = keystone_admin() |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 252 | body = admin.identity.list_tenants() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 253 | existing = [x['name'] for x in body] |
| 254 | for tenant in tenants: |
| 255 | if tenant not in existing: |
| 256 | admin.identity.create_tenant(tenant) |
| 257 | else: |
| 258 | LOG.warn("Tenant '%s' already exists in this environment" % tenant) |
| 259 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 260 | |
| 261 | def destroy_tenants(tenants): |
| 262 | admin = keystone_admin() |
| 263 | for tenant in tenants: |
| 264 | tenant_id = admin.identity.get_tenant_by_name(tenant)['id'] |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 265 | admin.identity.delete_tenant(tenant_id) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 266 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 267 | ############## |
| 268 | # |
| 269 | # USERS |
| 270 | # |
| 271 | ############## |
| 272 | |
| 273 | |
| 274 | def _users_for_tenant(users, tenant): |
| 275 | u_for_t = [] |
| 276 | for user in users: |
| 277 | for n in user: |
| 278 | if user[n]['tenant'] == tenant: |
| 279 | u_for_t.append(user[n]) |
| 280 | return u_for_t |
| 281 | |
| 282 | |
| 283 | def _tenants_from_users(users): |
| 284 | tenants = set() |
| 285 | for user in users: |
| 286 | for n in user: |
| 287 | tenants.add(user[n]['tenant']) |
| 288 | return tenants |
| 289 | |
| 290 | |
| 291 | def _assign_swift_role(user): |
| 292 | admin = keystone_admin() |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 293 | roles = admin.identity.list_roles() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 294 | role = next(r for r in roles if r['name'] == 'Member') |
| 295 | LOG.debug(USERS[user]) |
| 296 | try: |
| 297 | admin.identity.assign_user_role( |
| 298 | USERS[user]['tenant_id'], |
| 299 | USERS[user]['id'], |
| 300 | role['id']) |
Masayuki Igawa | d938876 | 2015-01-20 14:56:42 +0900 | [diff] [blame^] | 301 | except lib_exc.Conflict: |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 302 | # don't care if it's already assigned |
| 303 | pass |
| 304 | |
| 305 | |
| 306 | def create_users(users): |
| 307 | """Create tenants from resource definition. |
| 308 | |
| 309 | Don't create the tenants if they already exist. |
| 310 | """ |
| 311 | global USERS |
| 312 | LOG.info("Creating users") |
| 313 | admin = keystone_admin() |
| 314 | for u in users: |
| 315 | try: |
| 316 | tenant = admin.identity.get_tenant_by_name(u['tenant']) |
| 317 | except exceptions.NotFound: |
| 318 | LOG.error("Tenant: %s - not found" % u['tenant']) |
| 319 | continue |
| 320 | try: |
| 321 | admin.identity.get_user_by_username(tenant['id'], u['name']) |
| 322 | LOG.warn("User '%s' already exists in this environment" |
| 323 | % u['name']) |
| 324 | except exceptions.NotFound: |
| 325 | admin.identity.create_user( |
| 326 | u['name'], u['pass'], tenant['id'], |
| 327 | "%s@%s" % (u['name'], tenant['id']), |
| 328 | enabled=True) |
| 329 | |
| 330 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 331 | def destroy_users(users): |
| 332 | admin = keystone_admin() |
| 333 | for user in users: |
Emilien Macchi | 436de86 | 2014-09-30 17:09:50 -0400 | [diff] [blame] | 334 | tenant_id = admin.identity.get_tenant_by_name(user['tenant'])['id'] |
| 335 | user_id = admin.identity.get_user_by_username(tenant_id, |
| 336 | user['name'])['id'] |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 337 | admin.identity.delete_user(user_id) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 338 | |
| 339 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 340 | def collect_users(users): |
| 341 | global USERS |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 342 | LOG.info("Collecting users") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 343 | admin = keystone_admin() |
| 344 | for u in users: |
| 345 | tenant = admin.identity.get_tenant_by_name(u['tenant']) |
| 346 | u['tenant_id'] = tenant['id'] |
| 347 | USERS[u['name']] = u |
| 348 | body = admin.identity.get_user_by_username(tenant['id'], u['name']) |
| 349 | USERS[u['name']]['id'] = body['id'] |
| 350 | |
| 351 | |
| 352 | class JavelinCheck(unittest.TestCase): |
| 353 | def __init__(self, users, resources): |
| 354 | super(JavelinCheck, self).__init__() |
| 355 | self.users = users |
| 356 | self.res = resources |
| 357 | |
| 358 | def runTest(self, *args): |
| 359 | pass |
| 360 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 361 | def _ping_ip(self, ip_addr, count, namespace=None): |
| 362 | if namespace is None: |
| 363 | ping_cmd = "ping -c1 " + ip_addr |
| 364 | else: |
| 365 | ping_cmd = "sudo ip netns exec %s ping -c1 %s" % (namespace, |
| 366 | ip_addr) |
| 367 | for current in range(count): |
| 368 | return_code = os.system(ping_cmd) |
| 369 | if return_code is 0: |
| 370 | break |
| 371 | self.assertNotEqual(current, count - 1, |
| 372 | "Server is not pingable at %s" % ip_addr) |
| 373 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 374 | def check(self): |
| 375 | self.check_users() |
| 376 | self.check_objects() |
| 377 | self.check_servers() |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 378 | self.check_volumes() |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 379 | self.check_telemetry() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 380 | self.check_secgroups() |
| 381 | |
| 382 | # validate neutron is enabled and ironic disabled: |
| 383 | # Tenant network isolation is not supported when using ironic. |
| 384 | # "admin" has set up a neutron flat network environment within a shared |
| 385 | # fixed network for all tenants to use. |
| 386 | # In this case, network/subnet/router creation can be skipped and the |
| 387 | # server booted the same as nova network. |
| 388 | if (CONF.service_available.neutron and |
| 389 | not CONF.baremetal.driver_enabled): |
| 390 | self.check_networking() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 391 | |
| 392 | def check_users(self): |
| 393 | """Check that the users we expect to exist, do. |
| 394 | |
| 395 | We don't use the resource list for this because we need to validate |
| 396 | that things like tenantId didn't drift across versions. |
| 397 | """ |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 398 | LOG.info("checking users") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 399 | for name, user in self.users.iteritems(): |
| 400 | client = keystone_admin() |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 401 | found = client.identity.get_user(user['id']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 402 | self.assertEqual(found['name'], user['name']) |
| 403 | self.assertEqual(found['tenantId'], user['tenant_id']) |
| 404 | |
| 405 | # also ensure we can auth with that user, and do something |
| 406 | # on the cloud. We don't care about the results except that it |
| 407 | # remains authorized. |
| 408 | client = client_for_user(user['name']) |
| 409 | resp, body = client.servers.list_servers() |
| 410 | self.assertEqual(resp['status'], '200') |
| 411 | |
| 412 | def check_objects(self): |
| 413 | """Check that the objects created are still there.""" |
Joe Gordon | 22cd6de | 2014-07-25 00:16:17 +0000 | [diff] [blame] | 414 | if not self.res.get('objects'): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 415 | return |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 416 | LOG.info("checking objects") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 417 | for obj in self.res['objects']: |
| 418 | client = client_for_user(obj['owner']) |
| 419 | r, contents = client.objects.get_object( |
| 420 | obj['container'], obj['name']) |
| 421 | source = _file_contents(obj['file']) |
| 422 | self.assertEqual(contents, source) |
| 423 | |
| 424 | def check_servers(self): |
| 425 | """Check that the servers are still up and running.""" |
Joe Gordon | 22cd6de | 2014-07-25 00:16:17 +0000 | [diff] [blame] | 426 | if not self.res.get('servers'): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 427 | return |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 428 | LOG.info("checking servers") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 429 | for server in self.res['servers']: |
| 430 | client = client_for_user(server['owner']) |
| 431 | found = _get_server_by_name(client, server['name']) |
| 432 | self.assertIsNotNone( |
| 433 | found, |
| 434 | "Couldn't find expected server %s" % server['name']) |
| 435 | |
| 436 | r, found = client.servers.get_server(found['id']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 437 | # validate neutron is enabled and ironic disabled: |
| 438 | if (CONF.service_available.neutron and |
| 439 | not CONF.baremetal.driver_enabled): |
| 440 | for network_name, body in found['addresses'].items(): |
| 441 | for addr in body: |
| 442 | ip = addr['addr'] |
| 443 | if addr.get('OS-EXT-IPS:type', 'fixed') == 'fixed': |
| 444 | namespace = _get_router_namespace(client, |
| 445 | network_name) |
| 446 | self._ping_ip(ip, 60, namespace) |
| 447 | else: |
| 448 | self._ping_ip(ip, 60) |
| 449 | else: |
| 450 | addr = found['addresses']['private'][0]['addr'] |
| 451 | self._ping_ip(addr, 60) |
| 452 | |
| 453 | def check_secgroups(self): |
Joe H. Rahme | 0273673 | 2015-01-27 18:33:09 +0100 | [diff] [blame] | 454 | """Check that the security groups still exist.""" |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 455 | LOG.info("Checking security groups") |
| 456 | for secgroup in self.res['secgroups']: |
| 457 | client = client_for_user(secgroup['owner']) |
| 458 | found = _get_resource_by_name(client.secgroups, 'security_groups', |
| 459 | secgroup['name']) |
| 460 | self.assertIsNotNone( |
| 461 | found, |
| 462 | "Couldn't find expected secgroup %s" % secgroup['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 463 | |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 464 | def check_telemetry(self): |
| 465 | """Check that ceilometer provides a sane sample. |
| 466 | |
| 467 | Confirm that there are more than one sample and that they have the |
| 468 | expected metadata. |
| 469 | |
| 470 | If in check mode confirm that the oldest sample available is from |
| 471 | before the upgrade. |
| 472 | """ |
Chris Dent | 0b76f2f | 2014-10-10 14:24:28 +0100 | [diff] [blame] | 473 | if not self.res.get('telemetry'): |
| 474 | return |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 475 | LOG.info("checking telemetry") |
| 476 | for server in self.res['servers']: |
| 477 | client = client_for_user(server['owner']) |
David Kranz | 20d06f4 | 2015-02-09 14:54:15 -0500 | [diff] [blame] | 478 | body = client.telemetry.list_samples( |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 479 | 'instance', |
| 480 | query=('metadata.display_name', 'eq', server['name']) |
| 481 | ) |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 482 | self.assertTrue(len(body) >= 1, 'expecting at least one sample') |
| 483 | self._confirm_telemetry_sample(server, body[-1]) |
| 484 | |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 485 | def check_volumes(self): |
| 486 | """Check that the volumes are still there and attached.""" |
Joe Gordon | 22cd6de | 2014-07-25 00:16:17 +0000 | [diff] [blame] | 487 | if not self.res.get('volumes'): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 488 | return |
Joe Gordon | 618c9fb | 2014-07-16 15:40:01 +0200 | [diff] [blame] | 489 | LOG.info("checking volumes") |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 490 | for volume in self.res['volumes']: |
| 491 | client = client_for_user(volume['owner']) |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 492 | vol_body = _get_volume_by_name(client, volume['name']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 493 | self.assertIsNotNone( |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 494 | vol_body, |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 495 | "Couldn't find expected volume %s" % volume['name']) |
| 496 | |
| 497 | # Verify that a volume's attachment retrieved |
| 498 | server_id = _get_server_by_name(client, volume['server'])['id'] |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 499 | attachment = client.volumes.get_attachment_from_volume(vol_body) |
| 500 | self.assertEqual(vol_body['id'], attachment['volume_id']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 501 | self.assertEqual(server_id, attachment['server_id']) |
| 502 | |
Chris Dent | 878f378 | 2014-06-30 17:04:15 +0100 | [diff] [blame] | 503 | def _confirm_telemetry_sample(self, server, sample): |
| 504 | """Check this sample matches the expected resource metadata.""" |
| 505 | # Confirm display_name |
| 506 | self.assertEqual(server['name'], |
| 507 | sample['resource_metadata']['display_name']) |
| 508 | # Confirm instance_type of flavor |
| 509 | flavor = sample['resource_metadata'].get( |
| 510 | 'flavor.name', |
| 511 | sample['resource_metadata'].get('instance_type') |
| 512 | ) |
| 513 | self.assertEqual(server['flavor'], flavor) |
| 514 | # Confirm the oldest sample was created before upgrade. |
| 515 | if OPTS.mode == 'check': |
| 516 | oldest_timestamp = timeutils.normalize_time( |
| 517 | timeutils.parse_isotime(sample['timestamp'])) |
| 518 | self.assertTrue( |
| 519 | oldest_timestamp < JAVELIN_START, |
| 520 | 'timestamp should come before start of second javelin run' |
| 521 | ) |
| 522 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 523 | def check_networking(self): |
| 524 | """Check that the networks are still there.""" |
| 525 | for res_type in ('networks', 'subnets', 'routers'): |
| 526 | for res in self.res[res_type]: |
| 527 | client = client_for_user(res['owner']) |
| 528 | found = _get_resource_by_name(client.networks, res_type, |
| 529 | res['name']) |
| 530 | self.assertIsNotNone( |
| 531 | found, |
| 532 | "Couldn't find expected resource %s" % res['name']) |
| 533 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 534 | |
| 535 | ####################### |
| 536 | # |
| 537 | # OBJECTS |
| 538 | # |
| 539 | ####################### |
| 540 | |
| 541 | |
| 542 | def _file_contents(fname): |
| 543 | with open(fname, 'r') as f: |
| 544 | return f.read() |
| 545 | |
| 546 | |
| 547 | def create_objects(objects): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 548 | if not objects: |
| 549 | return |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 550 | LOG.info("Creating objects") |
| 551 | for obj in objects: |
| 552 | LOG.debug("Object %s" % obj) |
| 553 | _assign_swift_role(obj['owner']) |
| 554 | client = client_for_user(obj['owner']) |
| 555 | client.containers.create_container(obj['container']) |
| 556 | client.objects.create_object( |
| 557 | obj['container'], obj['name'], |
| 558 | _file_contents(obj['file'])) |
| 559 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 560 | |
| 561 | def destroy_objects(objects): |
| 562 | for obj in objects: |
| 563 | client = client_for_user(obj['owner']) |
| 564 | r, body = client.objects.delete_object(obj['container'], obj['name']) |
Emilien Macchi | d70f510 | 2014-09-10 09:54:49 -0400 | [diff] [blame] | 565 | if not (200 <= int(r['status']) < 299): |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 566 | raise ValueError("unable to destroy object: [%s] %s" % (r, body)) |
| 567 | |
| 568 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 569 | ####################### |
| 570 | # |
| 571 | # IMAGES |
| 572 | # |
| 573 | ####################### |
| 574 | |
| 575 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 576 | def _resolve_image(image, imgtype): |
| 577 | name = image[imgtype] |
| 578 | fname = os.path.join(OPTS.devstack_base, image['imgdir'], name) |
| 579 | return name, fname |
| 580 | |
| 581 | |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 582 | def _get_image_by_name(client, name): |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 583 | body = client.images.image_list() |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 584 | for image in body: |
| 585 | if name == image['name']: |
| 586 | return image |
| 587 | return None |
| 588 | |
| 589 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 590 | def create_images(images): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 591 | if not images: |
| 592 | return |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 593 | LOG.info("Creating images") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 594 | for image in images: |
| 595 | client = client_for_user(image['owner']) |
| 596 | |
| 597 | # only upload a new image if the name isn't there |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 598 | if _get_image_by_name(client, image['name']): |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 599 | LOG.info("Image '%s' already exists" % image['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 600 | continue |
| 601 | |
| 602 | # special handling for 3 part image |
| 603 | extras = {} |
| 604 | if image['format'] == 'ami': |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 605 | name, fname = _resolve_image(image, 'aki') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 606 | aki = client.images.create_image( |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 607 | 'javelin_' + name, 'aki', 'aki') |
| 608 | client.images.store_image(aki.get('id'), open(fname, 'r')) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 609 | extras['kernel_id'] = aki.get('id') |
| 610 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 611 | name, fname = _resolve_image(image, 'ari') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 612 | ari = client.images.create_image( |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 613 | 'javelin_' + name, 'ari', 'ari') |
| 614 | client.images.store_image(ari.get('id'), open(fname, 'r')) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 615 | extras['ramdisk_id'] = ari.get('id') |
| 616 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 617 | _, fname = _resolve_image(image, 'file') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 618 | body = client.images.create_image( |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 619 | image['name'], image['format'], image['format'], **extras) |
| 620 | image_id = body.get('id') |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 621 | client.images.store_image(image_id, open(fname, 'r')) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 622 | |
| 623 | |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 624 | def destroy_images(images): |
| 625 | if not images: |
| 626 | return |
| 627 | LOG.info("Destroying images") |
| 628 | for image in images: |
| 629 | client = client_for_user(image['owner']) |
| 630 | |
| 631 | response = _get_image_by_name(client, image['name']) |
| 632 | if not response: |
| 633 | LOG.info("Image '%s' does not exists" % image['name']) |
| 634 | continue |
| 635 | client.images.delete_image(response['id']) |
| 636 | |
| 637 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 638 | ####################### |
| 639 | # |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 640 | # NETWORKS |
| 641 | # |
| 642 | ####################### |
| 643 | |
| 644 | def _get_router_namespace(client, network): |
| 645 | network_id = _get_resource_by_name(client.networks, |
| 646 | 'networks', network)['id'] |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 647 | n_body = client.networks.list_routers() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 648 | for router in n_body['routers']: |
| 649 | router_id = router['id'] |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 650 | r_body = client.networks.list_router_interfaces(router_id) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 651 | for port in r_body['ports']: |
| 652 | if port['network_id'] == network_id: |
| 653 | return "qrouter-%s" % router_id |
| 654 | |
| 655 | |
| 656 | def _get_resource_by_name(client, resource, name): |
| 657 | get_resources = getattr(client, 'list_%s' % resource) |
| 658 | if get_resources is None: |
| 659 | raise AttributeError("client doesn't have method list_%s" % resource) |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 660 | # Until all tempest client methods are changed to return only one value, |
| 661 | # we cannot assume they all have the same signature so we need to discard |
| 662 | # the unused response first value it two values are being returned. |
| 663 | body = get_resources() |
| 664 | if type(body) == tuple: |
| 665 | body = body[1] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 666 | if isinstance(body, dict): |
| 667 | body = body[resource] |
| 668 | for res in body: |
| 669 | if name == res['name']: |
| 670 | return res |
| 671 | raise ValueError('%s not found in %s resources' % (name, resource)) |
| 672 | |
| 673 | |
| 674 | def create_networks(networks): |
| 675 | LOG.info("Creating networks") |
| 676 | for network in networks: |
| 677 | client = client_for_user(network['owner']) |
| 678 | |
| 679 | # only create a network if the name isn't here |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 680 | body = client.networks.list_networks() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 681 | if any(item['name'] == network['name'] for item in body['networks']): |
| 682 | LOG.warning("Dupplicated network name: %s" % network['name']) |
| 683 | continue |
| 684 | |
| 685 | client.networks.create_network(name=network['name']) |
| 686 | |
| 687 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 688 | def destroy_networks(networks): |
| 689 | LOG.info("Destroying subnets") |
| 690 | for network in networks: |
| 691 | client = client_for_user(network['owner']) |
| 692 | network_id = _get_resource_by_name(client.networks, 'networks', |
| 693 | network['name'])['id'] |
| 694 | client.networks.delete_network(network_id) |
| 695 | |
| 696 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 697 | def create_subnets(subnets): |
| 698 | LOG.info("Creating subnets") |
| 699 | for subnet in subnets: |
| 700 | client = client_for_user(subnet['owner']) |
| 701 | |
| 702 | network = _get_resource_by_name(client.networks, 'networks', |
| 703 | subnet['network']) |
| 704 | ip_version = netaddr.IPNetwork(subnet['range']).version |
| 705 | # ensure we don't overlap with another subnet in the network |
| 706 | try: |
| 707 | client.networks.create_subnet(network_id=network['id'], |
| 708 | cidr=subnet['range'], |
| 709 | name=subnet['name'], |
| 710 | ip_version=ip_version) |
| 711 | except exceptions.BadRequest as e: |
| 712 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 713 | if not is_overlapping_cidr: |
| 714 | raise |
| 715 | |
| 716 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 717 | def destroy_subnets(subnets): |
| 718 | LOG.info("Destroying subnets") |
| 719 | for subnet in subnets: |
| 720 | client = client_for_user(subnet['owner']) |
| 721 | subnet_id = _get_resource_by_name(client.networks, |
| 722 | 'subnets', subnet['name'])['id'] |
| 723 | client.networks.delete_subnet(subnet_id) |
| 724 | |
| 725 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 726 | def create_routers(routers): |
| 727 | LOG.info("Creating routers") |
| 728 | for router in routers: |
| 729 | client = client_for_user(router['owner']) |
| 730 | |
| 731 | # only create a router if the name isn't here |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 732 | body = client.networks.list_routers() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 733 | if any(item['name'] == router['name'] for item in body['routers']): |
| 734 | LOG.warning("Dupplicated router name: %s" % router['name']) |
| 735 | continue |
| 736 | |
| 737 | client.networks.create_router(router['name']) |
| 738 | |
| 739 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 740 | def destroy_routers(routers): |
| 741 | LOG.info("Destroying routers") |
| 742 | for router in routers: |
| 743 | client = client_for_user(router['owner']) |
| 744 | router_id = _get_resource_by_name(client.networks, |
| 745 | 'routers', router['name'])['id'] |
| 746 | for subnet in router['subnet']: |
| 747 | subnet_id = _get_resource_by_name(client.networks, |
| 748 | 'subnets', subnet)['id'] |
| 749 | client.networks.remove_router_interface_with_subnet_id(router_id, |
| 750 | subnet_id) |
| 751 | client.networks.delete_router(router_id) |
| 752 | |
| 753 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 754 | def add_router_interface(routers): |
| 755 | for router in routers: |
| 756 | client = client_for_user(router['owner']) |
| 757 | router_id = _get_resource_by_name(client.networks, |
| 758 | 'routers', router['name'])['id'] |
| 759 | |
| 760 | for subnet in router['subnet']: |
| 761 | subnet_id = _get_resource_by_name(client.networks, |
| 762 | 'subnets', subnet)['id'] |
| 763 | # connect routers to their subnets |
| 764 | client.networks.add_router_interface_with_subnet_id(router_id, |
| 765 | subnet_id) |
| 766 | # connect routers to exteral network if set to "gateway" |
| 767 | if router['gateway']: |
| 768 | if CONF.network.public_network_id: |
| 769 | ext_net = CONF.network.public_network_id |
| 770 | client.networks._update_router( |
| 771 | router_id, set_enable_snat=True, |
| 772 | external_gateway_info={"network_id": ext_net}) |
| 773 | else: |
| 774 | raise ValueError('public_network_id is not configured.') |
| 775 | |
| 776 | |
| 777 | ####################### |
| 778 | # |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 779 | # SERVERS |
| 780 | # |
| 781 | ####################### |
| 782 | |
| 783 | def _get_server_by_name(client, name): |
| 784 | r, body = client.servers.list_servers() |
| 785 | for server in body['servers']: |
| 786 | if name == server['name']: |
| 787 | return server |
| 788 | return None |
| 789 | |
| 790 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 791 | def _get_flavor_by_name(client, name): |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 792 | body = client.flavors.list_flavors() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 793 | for flavor in body: |
| 794 | if name == flavor['name']: |
| 795 | return flavor |
| 796 | return None |
| 797 | |
| 798 | |
| 799 | def create_servers(servers): |
Joe Gordon | b9bcdd8 | 2014-07-17 15:44:57 +0000 | [diff] [blame] | 800 | if not servers: |
| 801 | return |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 802 | LOG.info("Creating servers") |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 803 | for server in servers: |
| 804 | client = client_for_user(server['owner']) |
| 805 | |
| 806 | if _get_server_by_name(client, server['name']): |
Joe Gordon | a18d686 | 2014-07-24 22:55:46 +0000 | [diff] [blame] | 807 | LOG.info("Server '%s' already exists" % server['name']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 808 | continue |
| 809 | |
| 810 | image_id = _get_image_by_name(client, server['image'])['id'] |
| 811 | flavor_id = _get_flavor_by_name(client, server['flavor'])['id'] |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 812 | # validate neutron is enabled and ironic disabled |
| 813 | kwargs = dict() |
| 814 | if (CONF.service_available.neutron and |
| 815 | not CONF.baremetal.driver_enabled and server.get('networks')): |
| 816 | get_net_id = lambda x: (_get_resource_by_name( |
| 817 | client.networks, 'networks', x)['id']) |
| 818 | kwargs['networks'] = [{'uuid': get_net_id(network)} |
| 819 | for network in server['networks']] |
| 820 | resp, body = client.servers.create_server( |
| 821 | server['name'], image_id, flavor_id, **kwargs) |
Joe Gordon | 10f260b | 2014-07-24 23:27:19 +0000 | [diff] [blame] | 822 | server_id = body['id'] |
| 823 | client.servers.wait_for_server_status(server_id, 'ACTIVE') |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 824 | # create to security group(s) after server spawning |
| 825 | for secgroup in server['secgroups']: |
| 826 | client.servers.add_security_group(server_id, secgroup) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 827 | |
| 828 | |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 829 | def destroy_servers(servers): |
| 830 | if not servers: |
| 831 | return |
| 832 | LOG.info("Destroying servers") |
| 833 | for server in servers: |
| 834 | client = client_for_user(server['owner']) |
| 835 | |
| 836 | response = _get_server_by_name(client, server['name']) |
| 837 | if not response: |
| 838 | LOG.info("Server '%s' does not exist" % server['name']) |
| 839 | continue |
| 840 | |
| 841 | client.servers.delete_server(response['id']) |
| 842 | client.servers.wait_for_server_termination(response['id'], |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 843 | ignore_error=True) |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 844 | |
| 845 | |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 846 | def create_secgroups(secgroups): |
| 847 | LOG.info("Creating security groups") |
| 848 | for secgroup in secgroups: |
| 849 | client = client_for_user(secgroup['owner']) |
| 850 | |
| 851 | # only create a security group if the name isn't here |
| 852 | # i.e. a security group may be used by another server |
| 853 | # only create a router if the name isn't here |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 854 | body = client.secgroups.list_security_groups() |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 855 | if any(item['name'] == secgroup['name'] for item in body): |
| 856 | LOG.warning("Security group '%s' already exists" % |
| 857 | secgroup['name']) |
| 858 | continue |
| 859 | |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 860 | body = client.secgroups.create_security_group( |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 861 | secgroup['name'], secgroup['description']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 862 | secgroup_id = body['id'] |
| 863 | # for each security group, create the rules |
| 864 | for rule in secgroup['rules']: |
| 865 | ip_proto, from_port, to_port, cidr = rule.split() |
| 866 | client.secgroups.create_security_group_rule( |
| 867 | secgroup_id, ip_proto, from_port, to_port, cidr=cidr) |
| 868 | |
| 869 | |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 870 | def destroy_secgroups(secgroups): |
| 871 | LOG.info("Destroying security groups") |
| 872 | for secgroup in secgroups: |
| 873 | client = client_for_user(secgroup['owner']) |
| 874 | sg_id = _get_resource_by_name(client.secgroups, |
| 875 | 'security_groups', |
| 876 | secgroup['name']) |
| 877 | # sg rules are deleted automatically |
| 878 | client.secgroups.delete_security_group(sg_id['id']) |
| 879 | |
| 880 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 881 | ####################### |
| 882 | # |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 883 | # VOLUMES |
| 884 | # |
| 885 | ####################### |
| 886 | |
| 887 | def _get_volume_by_name(client, name): |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 888 | body = client.volumes.list_volumes() |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 889 | for volume in body: |
| 890 | if name == volume['display_name']: |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 891 | return volume |
| 892 | return None |
| 893 | |
| 894 | |
| 895 | def create_volumes(volumes): |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 896 | if not volumes: |
| 897 | return |
| 898 | LOG.info("Creating volumes") |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 899 | for volume in volumes: |
| 900 | client = client_for_user(volume['owner']) |
| 901 | |
| 902 | # only create a volume if the name isn't here |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 903 | if _get_volume_by_name(client, volume['name']): |
| 904 | LOG.info("volume '%s' already exists" % volume['name']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 905 | continue |
| 906 | |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 907 | size = volume['gb'] |
| 908 | v_name = volume['name'] |
Joseph Lanoux | 6809bab | 2014-12-18 14:57:18 +0000 | [diff] [blame] | 909 | body = client.volumes.create_volume(size=size, |
| 910 | display_name=v_name) |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 911 | client.volumes.wait_for_volume_status(body['id'], 'available') |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 912 | |
| 913 | |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 914 | def destroy_volumes(volumes): |
| 915 | for volume in volumes: |
| 916 | client = client_for_user(volume['owner']) |
| 917 | volume_id = _get_volume_by_name(client, volume['name'])['id'] |
Emilien Macchi | 5ebc27b | 2014-09-15 14:30:35 -0400 | [diff] [blame] | 918 | client.volumes.detach_volume(volume_id) |
| 919 | client.volumes.delete_volume(volume_id) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 920 | |
| 921 | |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 922 | def attach_volumes(volumes): |
| 923 | for volume in volumes: |
| 924 | client = client_for_user(volume['owner']) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 925 | server_id = _get_server_by_name(client, volume['server'])['id'] |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 926 | volume_id = _get_volume_by_name(client, volume['name'])['id'] |
| 927 | device = volume['device'] |
| 928 | client.volumes.attach_volume(volume_id, server_id, device) |
Emilien Macchi | 626b4f8 | 2014-06-15 21:44:29 +0200 | [diff] [blame] | 929 | |
| 930 | |
| 931 | ####################### |
| 932 | # |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 933 | # MAIN LOGIC |
| 934 | # |
| 935 | ####################### |
| 936 | |
| 937 | def create_resources(): |
| 938 | LOG.info("Creating Resources") |
| 939 | # first create keystone level resources, and we need to be admin |
| 940 | # for those. |
| 941 | create_tenants(RES['tenants']) |
| 942 | create_users(RES['users']) |
| 943 | collect_users(RES['users']) |
| 944 | |
| 945 | # next create resources in a well known order |
| 946 | create_objects(RES['objects']) |
| 947 | create_images(RES['images']) |
Emilien Macchi | 7a2348b | 2014-06-16 07:32:11 +0200 | [diff] [blame] | 948 | |
| 949 | # validate neutron is enabled and ironic is disabled |
| 950 | if CONF.service_available.neutron and not CONF.baremetal.driver_enabled: |
| 951 | create_networks(RES['networks']) |
| 952 | create_subnets(RES['subnets']) |
| 953 | create_routers(RES['routers']) |
| 954 | add_router_interface(RES['routers']) |
| 955 | |
| 956 | create_secgroups(RES['secgroups']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 957 | create_servers(RES['servers']) |
Emilien Macchi | d18fec1 | 2014-09-15 14:32:54 -0400 | [diff] [blame] | 958 | create_volumes(RES['volumes']) |
| 959 | attach_volumes(RES['volumes']) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 960 | |
| 961 | |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 962 | def destroy_resources(): |
| 963 | LOG.info("Destroying Resources") |
| 964 | # Destroy in inverse order of create |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 965 | destroy_servers(RES['servers']) |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 966 | destroy_images(RES['images']) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 967 | destroy_objects(RES['objects']) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 968 | destroy_volumes(RES['volumes']) |
Jakub Libosvar | 3791ac9 | 2014-11-11 13:23:44 +0100 | [diff] [blame] | 969 | if CONF.service_available.neutron and not CONF.baremetal.driver_enabled: |
| 970 | destroy_routers(RES['routers']) |
| 971 | destroy_subnets(RES['subnets']) |
| 972 | destroy_networks(RES['networks']) |
| 973 | destroy_secgroups(RES['secgroups']) |
Emilien Macchi | bb71e07 | 2014-07-05 19:18:52 +0200 | [diff] [blame] | 974 | destroy_users(RES['users']) |
| 975 | destroy_tenants(RES['tenants']) |
Joe Gordon | 6f0426c | 2014-07-25 01:10:28 +0000 | [diff] [blame] | 976 | LOG.warn("Destroy mode incomplete") |
| 977 | |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 978 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 979 | def get_options(): |
| 980 | global OPTS |
| 981 | parser = argparse.ArgumentParser( |
| 982 | description='Create and validate a fixed set of OpenStack resources') |
| 983 | parser.add_argument('-m', '--mode', |
| 984 | metavar='<create|check|destroy>', |
| 985 | required=True, |
| 986 | help=('One of (create, check, destroy)')) |
| 987 | parser.add_argument('-r', '--resources', |
| 988 | required=True, |
| 989 | metavar='resourcefile.yaml', |
| 990 | help='Resources definition yaml file') |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 991 | |
Sean Dague | 319b37a | 2014-07-11 07:28:11 -0400 | [diff] [blame] | 992 | parser.add_argument( |
| 993 | '-d', '--devstack-base', |
| 994 | required=True, |
| 995 | metavar='/opt/stack/old', |
| 996 | help='Devstack base directory for retrieving artifacts') |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 997 | parser.add_argument( |
| 998 | '-c', '--config-file', |
| 999 | metavar='/etc/tempest.conf', |
| 1000 | help='path to javelin2(tempest) config file') |
| 1001 | |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1002 | # auth bits, letting us also just source the devstack openrc |
| 1003 | parser.add_argument('--os-username', |
| 1004 | metavar='<auth-user-name>', |
| 1005 | default=os.environ.get('OS_USERNAME'), |
| 1006 | help=('Defaults to env[OS_USERNAME].')) |
| 1007 | parser.add_argument('--os-password', |
| 1008 | metavar='<auth-password>', |
| 1009 | default=os.environ.get('OS_PASSWORD'), |
| 1010 | help=('Defaults to env[OS_PASSWORD].')) |
| 1011 | parser.add_argument('--os-tenant-name', |
| 1012 | metavar='<auth-tenant-name>', |
| 1013 | default=os.environ.get('OS_TENANT_NAME'), |
| 1014 | help=('Defaults to env[OS_TENANT_NAME].')) |
| 1015 | |
| 1016 | OPTS = parser.parse_args() |
| 1017 | if OPTS.mode not in ('create', 'check', 'destroy'): |
| 1018 | print("ERROR: Unknown mode -m %s\n" % OPTS.mode) |
| 1019 | parser.print_help() |
| 1020 | sys.exit(1) |
Joe Gordon | 28a84ae | 2014-07-17 15:38:28 +0000 | [diff] [blame] | 1021 | if OPTS.config_file: |
| 1022 | config.CONF.set_config_path(OPTS.config_file) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1023 | |
| 1024 | |
Joe Gordon | 915eb8e | 2014-07-17 11:25:46 +0200 | [diff] [blame] | 1025 | def setup_logging(): |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1026 | global LOG |
Joe Gordon | 915eb8e | 2014-07-17 11:25:46 +0200 | [diff] [blame] | 1027 | logging.setup(__name__) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1028 | LOG = logging.getLogger(__name__) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1029 | |
| 1030 | |
| 1031 | def main(): |
| 1032 | global RES |
| 1033 | get_options() |
| 1034 | setup_logging() |
Chris Dent | 51e76de | 2014-10-01 12:07:14 +0100 | [diff] [blame] | 1035 | RES.update(load_resources(OPTS.resources)) |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1036 | |
| 1037 | if OPTS.mode == 'create': |
| 1038 | create_resources() |
Joe Gordon | 1a09700 | 2014-07-24 23:44:08 +0000 | [diff] [blame] | 1039 | # Make sure the resources we just created actually work |
| 1040 | checker = JavelinCheck(USERS, RES) |
| 1041 | checker.check() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1042 | elif OPTS.mode == 'check': |
| 1043 | collect_users(RES['users']) |
| 1044 | checker = JavelinCheck(USERS, RES) |
| 1045 | checker.check() |
| 1046 | elif OPTS.mode == 'destroy': |
Joe Gordon | db63b1c | 2014-07-24 23:21:21 +0000 | [diff] [blame] | 1047 | collect_users(RES['users']) |
| 1048 | destroy_resources() |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1049 | else: |
| 1050 | LOG.error('Unknown mode %s' % OPTS.mode) |
| 1051 | return 1 |
Joe Gordon | 246353a | 2014-07-18 00:10:28 +0200 | [diff] [blame] | 1052 | LOG.info('javelin2 successfully finished') |
Sean Dague | 655e0af | 2014-05-29 09:00:22 -0400 | [diff] [blame] | 1053 | return 0 |
| 1054 | |
| 1055 | if __name__ == "__main__": |
| 1056 | sys.exit(main()) |