Token passthrough for nailgun client in ostf

- Ostf config generator makes api requests to nailgun
- Changed requirements of pecan to version that used in production (0.4.5)

related to blueprint access-control-master-node
Related-Bug: #1339082
Change-Id: I96861ef75adbb813a9c91a88e1db16211dac9c4c
This commit is contained in:
Dima Shulyak 2014-07-02 12:04:55 +03:00
parent 12c347865f
commit 74ecb4a85f
11 changed files with 51 additions and 30 deletions

4
fabfile.py vendored
View File

@ -58,10 +58,8 @@ def createmigration(comment):
def migrate(database='ostf'):
path = 'postgresql+psycopg2://ostf:ostf@localhost/{0}'.format(database)
local(
'ostf-server --after-initialization-environment-hook --dbpath {0}'
.format(path)
'ostf-server --after-initialization-environment-hook'
)

View File

@ -488,9 +488,12 @@ class NailgunConfig(object):
self.nailgun_port = os.environ.get('NAILGUN_PORT', None)
self.nailgun_url = 'http://{0}:{1}'.format(self.nailgun_host,
self.nailgun_port)
token = os.environ.get('NAILGUN_TOKEN')
self.cluster_id = os.environ.get('CLUSTER_ID', None)
self.req_session = requests.Session()
self.req_session.trust_env = False
if token:
self.req_session.headers.update({'X-Auth-Token': token})
if parse:
self.prepare_config()

View File

@ -25,11 +25,6 @@ from fuel_plugin.ostf_adapter.nose_plugin import nose_utils
LOG = logging.getLogger(__name__)
REQ_SES = requests.Session()
REQ_SES.trust_env = False
URL = 'http://{0}:{1}/{2}'
NAILGUN_API_URL = 'api/clusters/{0}'
TEST_REPOSITORY = []
@ -64,8 +59,8 @@ def cache_test_repository(session):
TEST_REPOSITORY.append(data_elem)
def discovery_check(session, cluster):
cluster_deployment_args = _get_cluster_depl_tags(cluster)
def discovery_check(session, cluster, token=None):
cluster_deployment_args = _get_cluster_depl_tags(cluster, token=token)
cluster_data = {
'cluster_id': cluster,
@ -106,7 +101,16 @@ def discovery_check(session, cluster):
session.merge(cluster_state)
def _get_cluster_depl_tags(cluster_id):
def _get_cluster_depl_tags(cluster_id, token=None):
REQ_SES = requests.Session()
REQ_SES.trust_env = False
if token is not None:
REQ_SES.headers.update({'X-Auth-Token': token})
URL = 'http://{0}:{1}/{2}'
NAILGUN_API_URL = 'api/clusters/{0}'
cluster_url = NAILGUN_API_URL.format(cluster_id)
request_url = URL.format(cfg.CONF.adapter.nailgun_host,
cfg.CONF.adapter.nailgun_port,

View File

@ -40,12 +40,12 @@ class NoseDriver(object):
def __init__(self):
LOG.warning('Initializing Nose Driver')
def run(self, test_run, test_set, dbpath, ostf_os_access_creds=None,
tests=None):
def run(self, test_run, test_set, dbpath,
ostf_os_access_creds=None,
tests=None, token=None):
if not ostf_os_access_creds:
ostf_os_access_creds = dict()
tests = tests or test_run.enabled_tests
if tests:
argv_add = [nose_utils.modify_test_name_for_nose(test)
@ -60,10 +60,11 @@ class NoseDriver(object):
test_run.id,
test_run.cluster_id,
ostf_os_access_creds,
argv_add).pid
argv_add,
token).pid
def _run_tests(self, lock_path, dbpath, test_run_id, cluster_id,
ostf_os_access_creds, argv_add):
def _run_tests(self, lock_path, dbpath, test_run_id,
cluster_id, ostf_os_access_creds, argv_add, token):
cleanup_flag = False
def raise_exception_handler(signum, stack_frame):
@ -90,7 +91,7 @@ class NoseDriver(object):
nose_test_runner.SilentTestProgram(
addplugins=[nose_storage_plugin.StoragePlugin(
session, test_run_id, str(cluster_id),
ostf_os_access_creds
ostf_os_access_creds, token
)],
exit=False,
argv=['ostf_tests'] + argv_add)

View File

@ -34,7 +34,8 @@ class StoragePlugin(plugins.Plugin):
score = 15000
def __init__(self, session, test_run_id, cluster_id,
ostf_os_access_creds):
ostf_os_access_creds, token):
self.session = session
self.test_run_id = test_run_id
self.cluster_id = cluster_id
@ -42,10 +43,13 @@ class StoragePlugin(plugins.Plugin):
super(StoragePlugin, self).__init__()
self._start_time = None
self.token = token
def options(self, parser, env=os.environ):
env['NAILGUN_HOST'] = str(CONF.adapter.nailgun_host)
env['NAILGUN_PORT'] = str(CONF.adapter.nailgun_port)
if self.token is not None:
env['NAILGUN_TOKEN'] = self.token
if self.cluster_id:
env['CLUSTER_ID'] = str(self.cluster_id)

View File

@ -355,7 +355,7 @@ class TestRun(BASE):
return not bool(test_run) or test_run.is_finished()
@classmethod
def start(cls, session, test_set, metadata, tests, dbpath):
def start(cls, session, test_set, metadata, tests, dbpath, token=None):
plugin = nose_plugin.get_plugin(test_set.driver)
if cls.is_last_running(session, test_set.id,
metadata['cluster_id']):
@ -368,12 +368,13 @@ class TestRun(BASE):
session.commit()
plugin.run(test_run, test_set, dbpath,
metadata.get('ostf_os_access_creds'))
metadata.get('ostf_os_access_creds'), token=token)
return test_run.frontend
return {}
def restart(self, session, dbpath, ostf_os_access_creds, tests=None):
def restart(self, session, dbpath,
ostf_os_access_creds, tests=None, token=None):
"""Restart test run with
if tests given they will be enabled
"""
@ -388,7 +389,7 @@ class TestRun(BASE):
session, self.id, tests)
plugin.run(self, self.test_set, dbpath,
ostf_os_access_creds, tests)
ostf_os_access_creds, tests, token=token)
return self.frontend
return {}

View File

@ -55,6 +55,7 @@ def setup_app(config=None):
pecan.conf.app.root,
debug=pecan.conf.debug,
force_canonical=True,
hooks=[hooks.CustomTransactionalHook(dbpath=pecan.conf.dbpath)]
hooks=[hooks.CustomTransactionalHook(dbpath=pecan.conf.dbpath),
hooks.AddTokenHook()]
)
return access_control.setup(app)

View File

@ -46,7 +46,7 @@ class TestsetsController(BaseRestController):
@expose('json')
def get(self, cluster):
mixins.discovery_check(request.session, cluster)
mixins.discovery_check(request.session, cluster, request.token)
needed_testsets = request.session\
.query(models.ClusterTestingPattern.test_set_id)\
@ -66,7 +66,7 @@ class TestsController(BaseRestController):
@expose('json')
def get(self, cluster):
mixins.discovery_check(request.session, cluster)
mixins.discovery_check(request.session, cluster, request.token)
needed_tests_list = request.session\
.query(models.ClusterTestingPattern.tests)\
.filter_by(cluster_id=cluster)
@ -142,7 +142,8 @@ class TestrunsController(BaseRestController):
test_set,
metadata,
tests,
cfg.CONF.adapter.dbpath
cfg.CONF.adapter.dbpath,
token=request.token
)
res.append(test_run)
@ -170,5 +171,6 @@ class TestrunsController(BaseRestController):
data.append(test_run.restart(request.session,
cfg.CONF.adapter.dbpath,
ostf_os_access_creds,
tests=tests))
tests=tests,
token=request.token))
return data

View File

@ -55,3 +55,10 @@ class CustomTransactionalHook(hooks.TransactionHook):
def on_error(self, state, exc):
super(CustomTransactionalHook, self).on_error(state, exc)
LOG.exception('Pecan state %r', state)
class AddTokenHook(hooks.PecanHook):
def before(self, state):
# (dshulyak) just utility to get token
state.request.token = state.request.headers.get('X-Auth-Token', None)

View File

@ -273,7 +273,7 @@ class TestClusterRedeployment(base.BaseWSGITest):
with patch(
('fuel_plugin.ostf_adapter.mixins._get_cluster_depl_tags'),
lambda *args: cluster_data
lambda *args, **kwargs: cluster_data
):
self.controller.get(self.expected['cluster']['id'])

View File

@ -14,6 +14,6 @@ nose>=1.3.0
SQLAlchemy>=0.7.8,<=0.9.99
alembic>=0.5.0
gevent==0.13.8
pecan>=0.3.0
pecan>=0.3.0,<0.6.0
psycopg2>=2.5.1
stevedore>=0.10