| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2017 Red Hat, Inc. |
| 4 |
# All Rights Reserved. |
| 5 |
# |
| 6 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 |
# not use this file except in compliance with the License. You may obtain |
| 8 |
# a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, software |
| 13 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
# License for the specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
from unittest import mock |
| 19 |
|
| 20 |
from fixtures import MonkeyPatch |
| 21 |
import json |
| 22 |
import logging |
| 23 |
from oslotest import base |
| 24 |
|
| 25 |
from config_tempest.clients import ClientManager |
| 26 |
from config_tempest.credentials import Credentials |
| 27 |
from config_tempest import tempest_conf |
| 28 |
|
| 29 |
# disable logging when running unit tests |
| 30 |
logging.disable(logging.CRITICAL) |
| 31 |
|
| 32 |
|
| 33 |
class BaseConfigTempestTest(base.BaseTestCase): |
| 34 |
"""Test case base class for all config_tempest unit tests""" |
| 35 |
|
| 36 |
FAKE_V3_VERSIONS = ( |
| 37 |
[{ |
| 38 |
'status': 'stable', |
| 39 |
'id': 'v3.8', |
| 40 |
}, { |
| 41 |
'status': 'deprecated', |
| 42 |
'id': 'v2.0', |
| 43 |
}] |
| 44 |
) |
| 45 |
FAKE_V2_VERSIONS = ( |
| 46 |
[{ |
| 47 |
'status': 'deprecated', |
| 48 |
'id': 'v3.8', |
| 49 |
}, { |
| 50 |
'status': 'stable', |
| 51 |
'id': 'v2.0', |
| 52 |
}] |
| 53 |
) |
| 54 |
|
| 55 |
def _get_conf(self, V2, V3): |
| 56 |
"""Creates fake conf for testing purposes""" |
| 57 |
conf = tempest_conf.TempestConf() |
| 58 |
uri = "http://172.16.52.151:5000/" |
| 59 |
conf.set("identity", "username", "demo") |
| 60 |
conf.set("identity", "password", "secret") |
| 61 |
conf.set("identity", "project_name", "demo") |
| 62 |
conf.set("identity", "domain_name", "Default") |
| 63 |
conf.set("identity", "project_domain_name", "Default") |
| 64 |
conf.set("identity", "user_domain_name", "Default") |
| 65 |
conf.set("identity", "disable_ssl_certificate_validation", "true") |
| 66 |
conf.set("identity", "auth_version", "v3") |
| 67 |
conf.set("identity", "uri", uri + V2, priority=True) |
| 68 |
conf.set("identity", "uri_v3", uri + V3) |
| 69 |
conf.set("auth", "admin_username", "admin") |
| 70 |
conf.set("auth", "admin_project_name", "adminProject") |
| 71 |
conf.set("auth", "admin_password", "adminPass") |
| 72 |
conf.set("auth", "admin_project_domain_name", "Default") |
| 73 |
conf.set("auth", "admin_user_domain_name", "Default") |
| 74 |
conf.set("auth", "use_dynamic_credentials", "False", priority=True) |
| 75 |
return conf |
| 76 |
|
| 77 |
def _get_alt_conf(self, V2, V3): |
| 78 |
"""Contains newer params in place of the deprecated params""" |
| 79 |
conf = tempest_conf.TempestConf() |
| 80 |
uri = "http://172.16.52.151:5000/" |
| 81 |
conf.set("identity", "username", "demo") |
| 82 |
conf.set("identity", "password", "secret") |
| 83 |
conf.set("identity", "project_name", "demo") |
| 84 |
conf.set("identity", "domain_name", "Default") |
| 85 |
conf.set("identity", "project_domain_name", "Default") |
| 86 |
conf.set("identity", "user_domain_name", "Default") |
| 87 |
conf.set("identity", "disable_ssl_certificate_validation", "true") |
| 88 |
conf.set("identity", "auth_version", "v3") |
| 89 |
conf.set("identity", "uri", uri + V2, priority=True) |
| 90 |
conf.set("identity", "uri_v3", uri + V3) |
| 91 |
conf.set("auth", "admin_username", "admin") |
| 92 |
conf.set("auth", "admin_project_name", "adminProject") |
| 93 |
conf.set("auth", "admin_password", "adminPass") |
| 94 |
conf.set("auth", "admin_domain_name", "Default") |
| 95 |
conf.set("auth", "admin_project_domain_name", "Default") |
| 96 |
conf.set("auth", "admin_user_domain_name", "Default") |
| 97 |
conf.set("auth", "use_dynamic_credentials", "True", priority=True) |
| 98 |
return conf |
| 99 |
|
| 100 |
def _get_creds(self, conf, admin=False, v2=False): |
| 101 |
# We return creds configured to v2 or v3 |
| 102 |
func2mock = 'config_tempest.credentials.Credentials._list_versions' |
| 103 |
return_value = self.FAKE_V3_VERSIONS |
| 104 |
if v2: |
| 105 |
return_value = self.FAKE_V2_VERSIONS |
| 106 |
mock_function = mock.Mock(return_value=return_value) |
| 107 |
self.useFixture(MonkeyPatch(func2mock, mock_function)) |
| 108 |
return Credentials(conf, admin) |
| 109 |
|
| 110 |
def _get_clients(self, conf, creds=None): |
| 111 |
"""Returns ClientManager instance""" |
| 112 |
if creds is None: |
| 113 |
creds = self._get_creds(conf, v2=True) |
| 114 |
return ClientManager(conf, creds) |
| 115 |
|
| 116 |
|
| 117 |
class BaseServiceTest(BaseConfigTempestTest): |
| 118 |
"""Test case base class for all api_discovery unit tests""" |
| 119 |
|
| 120 |
FAKE_TOKEN = "s6d5f45sdf4s564f4s6464sdfsd514" |
| 121 |
FAKE_CLIENT_MOCK = 'config_tempest.tests.base.BaseServiceTest' + \ |
| 122 |
'.FakeServiceClient' |
| 123 |
FAKE_HEADERS = { |
| 124 |
'Accept': 'application/json', 'X-Auth-Token': FAKE_TOKEN |
| 125 |
} |
| 126 |
FAKE_URL = "http://10.200.16.10:8774/" |
| 127 |
FAKE_VERSIONS = ( |
| 128 |
{ |
| 129 |
"versions": [{ |
| 130 |
"status": "SUPPORTED", |
| 131 |
"updated": "2011-01-21T11:33:21Z", |
| 132 |
"links": [{ |
| 133 |
"href": "http://10.200.16.10:8774/v2 / ", |
| 134 |
"rel": "self " |
| 135 |
}], |
| 136 |
"min_version": "", |
| 137 |
"version": "", |
| 138 |
"id": "v2.0", |
| 139 |
"values": [ |
| 140 |
{"id": 'v3.8'} |
| 141 |
] |
| 142 |
}, { |
| 143 |
"status": "CURRENT", |
| 144 |
"updated": "2013-07-23T11:33:21Z", |
| 145 |
"links": [{ |
| 146 |
"href": "http://10.200.16.10:8774/v2.1/", |
| 147 |
"rel": "self" |
| 148 |
}], |
| 149 |
"min_version": "2.1", |
| 150 |
"version": "2.41", |
| 151 |
"id": "v2.1", |
| 152 |
"values": [ |
| 153 |
{"id": 'v2.0'} |
| 154 |
] |
| 155 |
}, { |
| 156 |
"status": "DEPRECATED", |
| 157 |
"updated": "2013-07-23T11:33:21Z", |
| 158 |
"links": [{ |
| 159 |
"href": "http://10.200.16.10:8774/v1/", |
| 160 |
"rel": "self" |
| 161 |
}], |
| 162 |
"min_version": "1.0", |
| 163 |
"version": "1.0", |
| 164 |
"id": "v1.0", |
| 165 |
"values": [ |
| 166 |
{"id": 'v1.0'} |
| 167 |
] |
| 168 |
}] |
| 169 |
} |
| 170 |
) |
| 171 |
FAKE_IDENTITY_VERSIONS = ( |
| 172 |
{ |
| 173 |
'versions': { |
| 174 |
'values': [{ |
| 175 |
'status': 'stable', |
| 176 |
'id': 'v3.8', |
| 177 |
}, { |
| 178 |
'status': 'deprecated', |
| 179 |
'id': 'v2.0', |
| 180 |
}] |
| 181 |
} |
| 182 |
} |
| 183 |
) |
| 184 |
FAKE_IDENTITY_VERSION = ( |
| 185 |
{ |
| 186 |
'version': { |
| 187 |
'status': 'stable', |
| 188 |
'id': 'v3.8', |
| 189 |
} |
| 190 |
} |
| 191 |
) |
| 192 |
FAKE_EXTENSIONS = ( |
| 193 |
{ |
| 194 |
"extensions": [{ |
| 195 |
"updated": "2014-12-03T00:00:00Z", |
| 196 |
"name": "Multinic", |
| 197 |
"namespace": "http://docs.openstack.org/compute/ext/fake_xml", |
| 198 |
"alias": "NMN", |
| 199 |
"description": "Multiple network support." |
| 200 |
}, { |
| 201 |
"updated": "2014-12-03T00:00:00Z", |
| 202 |
"name": "DiskConfig", |
| 203 |
"namespace": "http://docs.openstack.org/compute/ext/fake_xml", |
| 204 |
"alias": "OS-DCF", |
| 205 |
"description": "Disk Management Extension." |
| 206 |
}] |
| 207 |
} |
| 208 |
) |
| 209 |
FAKE_IDENTITY_EXTENSIONS = ( |
| 210 |
{ |
| 211 |
"extensions": { |
| 212 |
'values': [{ |
| 213 |
'alias': 'OS-DCF', |
| 214 |
'id': 'v3.8', |
| 215 |
}, { |
| 216 |
'alias': 'NMN', |
| 217 |
'id': 'v2.0', |
| 218 |
}] |
| 219 |
} |
| 220 |
} |
| 221 |
) |
| 222 |
FAKE_LBAAS_PROVIDERS = ( |
| 223 |
{ |
| 224 |
"providers": [{ |
| 225 |
"name": "amphora", |
| 226 |
"description": "The Octavia Amphora driver." |
| 227 |
}, { |
| 228 |
"name": "octavia", |
| 229 |
"description": "Deprecated alias of the Octavia driver." |
| 230 |
}] |
| 231 |
} |
| 232 |
) |
| 233 |
FAKE_STORAGE_EXTENSIONS = ( |
| 234 |
{ |
| 235 |
"formpost": {}, |
| 236 |
"methods": ["GET", "HEAD", "PUT", "POST", "DELETE"], |
| 237 |
"ratelimit": { |
| 238 |
"account_ratelimit": 0.0, |
| 239 |
"max_sleep_time_seconds": 60.0, |
| 240 |
"container_ratelimits": [] |
| 241 |
}, |
| 242 |
"account_quotas": {}, |
| 243 |
"swift": { |
| 244 |
"container_listing_limit": 10000, |
| 245 |
"allow_account_management": True, |
| 246 |
"max_container_name_length": 256 |
| 247 |
} |
| 248 |
} |
| 249 |
) |
| 250 |
FAKE_ROLES = ( |
| 251 |
{ |
| 252 |
"roles": [ |
| 253 |
{ |
| 254 |
"name": "ResellerAdmin", |
| 255 |
"id": "2b4df7a671a443d741c62b4df7a671a443d741c6", |
| 256 |
"domain_id": None |
| 257 |
} |
| 258 |
] |
| 259 |
} |
| 260 |
) |
| 261 |
FAKE_ACCOUNTS = ( |
| 262 |
{ |
| 263 |
'status': '200', |
| 264 |
u'content-length': '2', |
| 265 |
'content-location': 'http://192.168.122.120:8080/healthcheck', |
| 266 |
u'connection': 'close', |
| 267 |
u'x-trans-id': 'txec03483c96cd4958a5c6b-005b17c346', |
| 268 |
u'date': 'Wed, 06 Jun 2018 11:19:34 GMT', |
| 269 |
u'content-type': 'text/plain', |
| 270 |
u'x-openstack-request-id': 'txec03483c96cd4958a5c6b-005b17c346' |
| 271 |
}, |
| 272 |
'OK') |
| 273 |
|
| 274 |
class FakeRequestResponse(object): |
| 275 |
URL = 'https://docs.openstack.org/api/openstack-identity/3/ext/' |
| 276 |
FAKE_V3_EXTENSIONS = ( |
| 277 |
{ |
| 278 |
'resources': { |
| 279 |
URL + 'OS-INHERIT/1.0/rel/domain_user_' + |
| 280 |
'role_inherited_to_projects': "", |
| 281 |
|
| 282 |
URL + 'OS-SIMPLE-CERT/1.0/rel/ca_certificate': "", |
| 283 |
|
| 284 |
URL + 'OS-EP-FILTER/1.0/rel/endpoint_group_to_' + |
| 285 |
'project_association': "", |
| 286 |
|
| 287 |
URL + 'OS-EP-FILTER/1.0/rel/project_endpoint': "", |
| 288 |
|
| 289 |
URL + 'OS-OAUTH1/1.0/rel/user_access_token_roles': "" |
| 290 |
} |
| 291 |
} |
| 292 |
) |
| 293 |
|
| 294 |
def __init__(self, bytes_content=False): |
| 295 |
self.content = json.dumps(self.FAKE_V3_EXTENSIONS) |
| 296 |
if bytes_content: |
| 297 |
self.content = self.content.encode('utf-8') |
| 298 |
|
| 299 |
class FakeServiceClient(object): |
| 300 |
def __init__(self, services=None): |
| 301 |
self.client = mock.Mock() |
| 302 |
self.return_value = mock.Mock() |
| 303 |
self.services = services |
| 304 |
|
| 305 |
def list_networks(self): |
| 306 |
return self.return_value |
| 307 |
|
| 308 |
def list_services(self, **kwargs): |
| 309 |
return self.services |
| 310 |
|
| 311 |
def _fake_service_do_get_method(self, fake_data): |
| 312 |
function2mock = 'config_tempest.services.base.Service.do_get' |
| 313 |
do_get_output = json.dumps(fake_data) |
| 314 |
mocked_do_get = mock.Mock() |
| 315 |
mocked_do_get.return_value = do_get_output |
| 316 |
self.useFixture(MonkeyPatch(function2mock, mocked_do_get)) |
| 317 |
|
| 318 |
def _set_get_extensions(self, service, expected_resp, fake_data): |
| 319 |
# mock do_get response |
| 320 |
self._fake_service_do_get_method(fake_data) |
| 321 |
# set the fake extensions |
| 322 |
service.set_extensions() |
| 323 |
# check if extensions were set |
| 324 |
self.assertCountEqual(service.extensions, expected_resp) |
| 325 |
# check if get method returns the right data |
| 326 |
resp = service.get_extensions() |
| 327 |
self.assertCountEqual(resp, expected_resp) |
| 328 |
|
| 329 |
def _set_get_versions(self, service, expected_resp, fake_data): |
| 330 |
# mock do_get response |
| 331 |
self._fake_service_do_get_method(fake_data) |
| 332 |
# set the fake versions |
| 333 |
service.set_versions() |
| 334 |
# check if versions were set |
| 335 |
self.assertCountEqual(service.versions, expected_resp) |
| 336 |
# check if get method returns the right data |
| 337 |
resp = service.get_versions() |
| 338 |
self.assertCountEqual(resp, expected_resp) |
| 339 |
|
| 340 |
def _test_deserialize_versions(self, service, expected_resp, fake_data): |
| 341 |
resp = service.deserialize_versions(fake_data) |
| 342 |
self.assertCountEqual(resp, expected_resp) |
| 343 |
|
| 344 |
def _assert_conf_get_not_raises(self, exc, section, value): |
| 345 |
try: |
| 346 |
self.conf.get(section, value) |
| 347 |
except exc: |
| 348 |
return |
| 349 |
self.assertTrue(False) |