Replace custom HTTPClient by standard keystoneauth1 session
1. Use keystoneauth1 for making session
2. Nailgun/OSTF client uses keystoneauth1 session
3. Drop HTTPClient
4. Support NailgunClient construction by providing session
5. Requests has on-demand authorization and re-authorization
by keystoneauth1.
Pros:
* interface unification, after implementation of bugfix fr 1581024
we could transparently switch the most methods
from our implmentation to python-fuelclient implementation.
* Less decorators magic (@jsonparse is not required)
Blueprint: python-fuel-client-in-system-tests
Change-Id: Idc0c0b3b0039f64d852ea1a08e02a9c3ecd65c46
(cherry picked from commit 92006f3
)
This commit is contained in:
parent
8f903810e6
commit
441542c234
@ -24,8 +24,8 @@ import traceback
|
||||
from proboscis import SkipTest
|
||||
from proboscis.asserts import assert_equal
|
||||
from proboscis.asserts import assert_true
|
||||
import requests
|
||||
# pylint: disable=import-error
|
||||
# noinspection PyUnresolvedReferences
|
||||
from six.moves import urllib
|
||||
# pylint: enable=import-error
|
||||
|
||||
@ -47,13 +47,10 @@ from fuelweb_test.helpers.utils import TimeStat
|
||||
from gates_tests.helpers.exceptions import ConfigurationException
|
||||
|
||||
|
||||
def save_logs(url, path, auth_token=None, chunk_size=1024):
|
||||
def save_logs(session, url, path, chunk_size=1024):
|
||||
logger.info('Saving logs to "%s" file', path)
|
||||
headers = {}
|
||||
if auth_token is not None:
|
||||
headers['X-Auth-Token'] = auth_token
|
||||
|
||||
stream = requests.get(url, headers=headers, stream=True, verify=False)
|
||||
stream = session.get(url, stream=True, verify=False)
|
||||
if stream.status_code != 200:
|
||||
logger.error("%s %s: %s", stream.status_code, stream.reason,
|
||||
stream.content)
|
||||
@ -119,7 +116,7 @@ def log_snapshot_after_test(func):
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
except SkipTest:
|
||||
raise SkipTest()
|
||||
raise
|
||||
except Exception:
|
||||
name = 'error_{:s}'.format(func.__name__)
|
||||
store_error_details(name, args[0].env)
|
||||
@ -346,8 +343,10 @@ def create_diagnostic_snapshot(env, status, name="",
|
||||
status=status,
|
||||
name=name,
|
||||
basename=os.path.basename(task['message']))
|
||||
save_logs(url, os.path.join(settings.LOGS_DIR, log_file_name),
|
||||
auth_token=env.fuel_web.client.client.token)
|
||||
save_logs(
|
||||
session=env.fuel_web.client.session,
|
||||
url=url,
|
||||
path=os.path.join(settings.LOGS_DIR, log_file_name))
|
||||
|
||||
|
||||
def retry(count=3, delay=30):
|
||||
@ -525,6 +524,7 @@ def __getcallargs(func, *positional, **named):
|
||||
if sys.version_info.major < 3:
|
||||
return inspect.getcallargs(func, *positional, **named)
|
||||
else:
|
||||
# noinspection PyUnresolvedReferences
|
||||
return inspect.signature(func).bind(*positional, **named).arguments
|
||||
|
||||
|
||||
@ -549,6 +549,7 @@ def __get_arg_names(func):
|
||||
if sys.version_info.major < 3:
|
||||
return [arg for arg in inspect.getargspec(func=func).args]
|
||||
else:
|
||||
# noinspection PyUnresolvedReferences
|
||||
return list(inspect.signature(obj=func).parameters.keys())
|
||||
# pylint:enable=no-member
|
||||
|
||||
|
@ -13,127 +13,10 @@
|
||||
# under the License.
|
||||
|
||||
import json
|
||||
import traceback
|
||||
|
||||
from keystoneauth1 import exceptions
|
||||
from keystoneauth1.identity import V2Password
|
||||
from keystoneauth1.session import Session as KeystoneSession
|
||||
from keystoneclient.v2_0 import Client as KeystoneClient
|
||||
# pylint: disable=import-error
|
||||
# noinspection PyUnresolvedReferences
|
||||
from six.moves.urllib import request
|
||||
from six.moves.urllib.error import HTTPError
|
||||
# pylint: enable=import-error
|
||||
import requests
|
||||
|
||||
from fuelweb_test import logger
|
||||
|
||||
|
||||
class HTTPClient(object):
|
||||
"""HTTPClient.""" # TODO documentation
|
||||
# TODO: Rewrite using requests library?
|
||||
|
||||
def __init__(self, url, keystone_url, credentials, **kwargs):
|
||||
logger.info('Initiate HTTPClient with url %s', url)
|
||||
self.url = url
|
||||
self.keystone_url = keystone_url
|
||||
self.creds = dict(credentials, **kwargs)
|
||||
self.keystone = None
|
||||
self.session = None
|
||||
self.opener = request.build_opener(request.HTTPHandler)
|
||||
|
||||
def authenticate(self):
|
||||
try:
|
||||
logger.info('Initialize keystoneclient with url %s',
|
||||
self.keystone_url)
|
||||
auth = V2Password(
|
||||
auth_url=self.keystone_url,
|
||||
username=self.creds['username'],
|
||||
password=self.creds['password'],
|
||||
tenant_name=self.creds['tenant_name'])
|
||||
# TODO: in v3 project_name
|
||||
self.session = KeystoneSession(auth=auth, verify=False)
|
||||
self.keystone = KeystoneClient(session=self.session)
|
||||
logger.debug('Authorization token is successfully updated')
|
||||
except exceptions.AuthorizationFailure:
|
||||
logger.warning(
|
||||
'Cant establish connection to keystone with url %s',
|
||||
self.keystone_url)
|
||||
|
||||
@property
|
||||
def token(self):
|
||||
if self.keystone is not None:
|
||||
try:
|
||||
return self.session.get_token()
|
||||
except exceptions.AuthorizationFailure:
|
||||
logger.warning(
|
||||
'Cant establish connection to keystone with url %s',
|
||||
self.keystone_url)
|
||||
except exceptions.Unauthorized:
|
||||
logger.warning("Keystone returned unauthorized error, trying "
|
||||
"to pass authentication.")
|
||||
self.authenticate()
|
||||
return self.session.get_token()
|
||||
return None
|
||||
|
||||
def get(self, endpoint):
|
||||
req = request.Request(self.url + endpoint)
|
||||
return self._open(req)
|
||||
|
||||
def post(self, endpoint, data=None, content_type="application/json"):
|
||||
if not data:
|
||||
data = {}
|
||||
req = request.Request(self.url + endpoint, data=json.dumps(data))
|
||||
req.add_header('Content-Type', content_type)
|
||||
return self._open(req)
|
||||
|
||||
def put(self, endpoint, data=None, content_type="application/json"):
|
||||
if not data:
|
||||
data = {}
|
||||
req = request.Request(self.url + endpoint, data=json.dumps(data))
|
||||
req.add_header('Content-Type', content_type)
|
||||
req.get_method = lambda: 'PUT'
|
||||
return self._open(req)
|
||||
|
||||
def delete(self, endpoint):
|
||||
req = request.Request(self.url + endpoint)
|
||||
req.get_method = lambda: 'DELETE'
|
||||
return self._open(req)
|
||||
|
||||
def _open(self, req):
|
||||
try:
|
||||
return self._get_response(req)
|
||||
except HTTPError as e:
|
||||
if e.code == 308:
|
||||
logger.info(e.read())
|
||||
url = req.get_full_url()
|
||||
req = requests.get(url, headers={'X-Auth-Token': self.token})
|
||||
if req.status_code in [200]:
|
||||
return req.json()
|
||||
else:
|
||||
req.raise_for_status()
|
||||
if e.code == 401:
|
||||
logger.warning('Authorization failure: {0}'.format(e.read()))
|
||||
self.authenticate()
|
||||
return self._get_response(req)
|
||||
elif e.code == 504:
|
||||
logger.error("Got HTTP Error 504: "
|
||||
"Gateway Time-out: {}".format(e.read()))
|
||||
return self._get_response(req)
|
||||
else:
|
||||
logger.error('{} code {} [{}]'.format(e.reason,
|
||||
e.code,
|
||||
e.read()))
|
||||
raise
|
||||
|
||||
def _get_response(self, req):
|
||||
if self.token is not None:
|
||||
try:
|
||||
logger.debug('Set X-Auth-Token to {0}'.format(self.token))
|
||||
req.add_header("X-Auth-Token", self.token)
|
||||
except exceptions.AuthorizationFailure:
|
||||
logger.warning('Failed with auth in http _get_response')
|
||||
logger.warning(traceback.format_exc())
|
||||
return self.opener.open(req)
|
||||
|
||||
|
||||
class HTTPClientZabbix(object):
|
||||
|
@ -12,11 +12,12 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
|
||||
from devops.helpers.helpers import _tcp_ping
|
||||
from devops.helpers.helpers import _wait
|
||||
from devops.helpers.helpers import tcp_ping_
|
||||
from devops.helpers.helpers import wait_pass
|
||||
from devops.helpers.helpers import wait
|
||||
from devops.helpers.ntp import sync_time
|
||||
from devops.models import Environment
|
||||
@ -46,8 +47,8 @@ from fuelweb_test.models.collector_client import CollectorClient
|
||||
from fuelweb_test import settings
|
||||
from fuelweb_test.settings import iface_alias
|
||||
from fuelweb_test import logwrap
|
||||
from fuelweb_test import logger
|
||||
from fuelweb_test import QuietLogger
|
||||
from fuelweb_test import logger
|
||||
|
||||
|
||||
@six.add_metaclass(SingletonMeta)
|
||||
@ -305,9 +306,9 @@ class EnvironmentModel(object):
|
||||
if not skip_timesync:
|
||||
self.sync_time()
|
||||
try:
|
||||
with QuietLogger():
|
||||
with QuietLogger(upper_log_level=logging.CRITICAL):
|
||||
# TODO(astudenov): add timeout_msg
|
||||
_wait(
|
||||
wait_pass(
|
||||
self.fuel_web.client.get_releases,
|
||||
expected=(
|
||||
exceptions.RetriableConnectionFailure,
|
||||
@ -319,7 +320,7 @@ class EnvironmentModel(object):
|
||||
|
||||
if not skip_slaves_check:
|
||||
# TODO(astudenov): add timeout_msg
|
||||
_wait(lambda: self.check_slaves_are_ready(), timeout=60 * 6)
|
||||
wait_pass(lambda: self.check_slaves_are_ready(), timeout=60 * 6)
|
||||
return True
|
||||
|
||||
def set_admin_ssh_password(self):
|
||||
@ -461,7 +462,7 @@ class EnvironmentModel(object):
|
||||
def wait_for_provisioning(self,
|
||||
timeout=settings.WAIT_FOR_PROVISIONING_TIMEOUT):
|
||||
# TODO(astudenov): add timeout_msg
|
||||
_wait(lambda: _tcp_ping(
|
||||
wait_pass(lambda: tcp_ping_(
|
||||
self.d_env.nodes(
|
||||
).admin.get_ip_address_by_network_name
|
||||
(self.d_env.admin_net), 22), timeout=timeout)
|
||||
@ -473,7 +474,7 @@ class EnvironmentModel(object):
|
||||
def check_ssh_connection():
|
||||
"""Try to close fuelmenu and check ssh connection"""
|
||||
try:
|
||||
_tcp_ping(
|
||||
tcp_ping_(
|
||||
self.d_env.nodes(
|
||||
).admin.get_ip_address_by_network_name
|
||||
(self.d_env.admin_net), 22)
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -59,7 +59,7 @@ class OneNodeDeploy(TestBasic):
|
||||
|
||||
"""
|
||||
self.env.revert_snapshot("ready")
|
||||
self.fuel_web.client.get_root()
|
||||
self.fuel_web.client.list_nodes()
|
||||
self.env.bootstrap_nodes(
|
||||
self.env.d_env.nodes().slaves[:1])
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user