diff options
Diffstat (limited to 'utils/test/testapi')
33 files changed, 399 insertions, 383 deletions
diff --git a/utils/test/testapi/.gitignore b/utils/test/testapi/.gitignore index 00f8a03d0..a3d6e01ec 100644 --- a/utils/test/testapi/.gitignore +++ b/utils/test/testapi/.gitignore @@ -4,4 +4,5 @@ setup.cfg-e opnfv_testapi/static build *.egg-info - +3rd_party/static/static +*.pyc diff --git a/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/pods.html b/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/pods.html index cdfcfaf36..e366670a9 100644 --- a/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/pods.html +++ b/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/pods.html @@ -1,8 +1,12 @@ -<h3>{{ctrl.pageHeader}}</h3> -<p>{{ctrl.pageParagraph}}</p> +<h3>Pods</h3> +<p>This page is used to create or query pods.<br> + Querying pods is open to everybody.<br> + But only login users are granted the privilege to create the new pod. +</p> + <div class="row" style="margin-bottom:24px;"></div> -<div class="pod-create"> +<div class="pod-create" ng-class="{ 'hidden': ! auth.isAuthenticated }"> <h4>Create</h4> <div class="row"> <div ng-repeat="require in ctrl.createRequirements"> @@ -63,7 +67,7 @@ </tbody> </table> </div> - +<br> <div ng-show="ctrl.showError" class="alert alert-danger" role="alert"> <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> <span class="sr-only">Error:</span> diff --git a/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/podsController.js b/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/podsController.js index 53e8b1eff..489fa8a8d 100644 --- a/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/podsController.js +++ b/utils/test/testapi/3rd_party/static/testapi-ui/components/pods/podsController.js @@ -31,7 +31,6 @@ function PodsController($scope, $http, $filter, $state, testapiApiUrl, raiseAlert) { var ctrl = this; - ctrl.url = testapiApiUrl + '/pods'; ctrl.create = create; @@ -53,9 +52,6 @@ ctrl.mode = 'metal'; ctrl.details = ''; - ctrl.pageHeader = 'Pods'; - ctrl.pageParagraph = 'This page is used to create or query pods.'; - /** * This is called when the date filter calendar is opened. It * does some event handling, and sets a scope variable so the UI @@ -82,21 +78,28 @@ */ function create() { ctrl.showError = false; - var pods_url = ctrl.url; - var body = { - name: ctrl.name, - mode: ctrl.mode, - role: ctrl.role, - details: ctrl.details - }; - ctrl.podsRequest = - $http.post(pods_url, body).error(function (error) { - ctrl.showError = true; - ctrl.error = - 'Error creating the new pod from server: ' + - angular.toJson(error); - }); + if(ctrl.name != ""){ + var pods_url = ctrl.url; + var body = { + name: ctrl.name, + mode: ctrl.mode, + role: ctrl.role, + details: ctrl.details + }; + ctrl.podsRequest = + $http.post(pods_url, body).error(function (data, status) { + ctrl.showError = true; + if(status == 403){ + ctrl.error = + 'Error creating the new pod from server: Pod\'s name already exists' + } + }); + } + else{ + ctrl.showError = true; + ctrl.error = 'Name is missing.' + } } /** diff --git a/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profile.html b/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profile.html index dc97c41e2..763f5d120 100644 --- a/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profile.html +++ b/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profile.html @@ -3,9 +3,16 @@ <div> <table class="table table-striped table-hover"> <tbody> - <tr> <td>User name</td> <td>{{auth.currentUser.fullname}}</td> </tr> - <tr> <td>User OpenId</td> <td>{{auth.currentUser.openid}}</td> </tr> + <tr> <td>User</td> <td>{{auth.currentUser.user}}</td> </tr> + <tr> <td>Fullname</td> <td>{{auth.currentUser.fullname}}</td> </tr> <tr> <td>Email</td> <td>{{auth.currentUser.email}}</td> </tr> + <tr> <td>Groups</td> + <td> + <div ng-repeat="group in auth.currentUser.groups"> + {{group}}</br> + </div> + </td> + </tr> </tbody> </table> </div> diff --git a/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profileController.js b/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profileController.js index 0660e19f6..5dbdf7b1a 100644 --- a/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profileController.js +++ b/utils/test/testapi/3rd_party/static/testapi-ui/components/profile/profileController.js @@ -26,7 +26,7 @@ * This is a provider for the user's uploaded public keys. */ function PubKeys($resource, testapiApiUrl) { - return $resource(testapiApiUrl + '/profile/pubkeys/:id', null, null); + return $resource(testapiApiUrl + '/user/pubkeys/:id', null, null); } angular diff --git a/utils/test/testapi/MANIFEST.in b/utils/test/testapi/MANIFEST.in new file mode 100644 index 000000000..0ba1808ba --- /dev/null +++ b/utils/test/testapi/MANIFEST.in @@ -0,0 +1 @@ +recursive-include 3rd_party
\ No newline at end of file diff --git a/utils/test/testapi/README.rst b/utils/test/testapi/README.rst index 44ab2a475..0d18b7e93 100644 --- a/utils/test/testapi/README.rst +++ b/utils/test/testapi/README.rst @@ -2,63 +2,92 @@ opnfv-testapi ============= -Test Results Collector of OPNFV Test Projects +**Test Results Collector of OPNFV Test Projects**: -Start Server -============== +This project aims to provide a common way of gathering all the test results for OPNFV +testing projects into a single place, and a unified way of getting those results for +testing analysis projects, such as Reporting/Bitergia Dashboard/Qtip. It exposes +Restful APIs for collecting results and pushing them into a MongoDB database. + +If you are interested in how TestAPI looks like, please visit OPNFV's official `TestAPI Server`__ + +.. __: http://testresults.opnfv.org/test + +Pre-requsites +============= -Getting setup -^^^^^^^^^^^^^ +TestAPI leverages MongoDB as the data warehouse, in order to let it work +successfully, a MongoDB must be provided, the official MongoDB version in OPNFV +TestAPI is 3.2.6. And the database is **test_results_collection**, the five +collections are *users/pods/projects/testcases/scenarioes/results*. And thanks to +MongoDB's very user-friendly property, they don't need to be created in advance. -Requirements for opnfv-testapi: +Running locally +=============== -* tornado -* epydoc +Requirements +^^^^^^^^^^^^ -These requirements are expressed in the requirements.txt file and may be -installed by running the following (from within a virtual environment):: +All requirements are listed in requirements.txt and should be +installed by 'pip install': - pip install -r requirements.txt + *pip install -r requirements.txt* + +Installation +^^^^^^^^^^^^ + + *python setup.py install* + +After installation, configuration file etc/config.ini will be put under +/etc/opnfv_testapi/. And all the web relevant files under 3rd_party/static +will be in /user/local/share/opnfv_testapi as a totality. + +Start Server +^^^^^^^^^^^^ -How to install -^^^^^^^^^^^^^^ + *opnfv-testapi [--config-file <config.ini>]* -From within your environment, just run: +If --config-file is provided, the specified configuration file will be employed +when starting the server, or else /etc/opnfv_testapi/config.ini will be utilized +by default. - ./install.sh +After executing the command successfully, a TestAPI server will be started on +port 8000, to visit web portal, please access http://hostname:8000 -How to run -^^^^^^^^^^ +Regarding swagger-ui website, please visit http://hostname:8000/swagger/spec.html -From within your environment, just run: +Running with container +====================== - opnfv-testapi +TestAPI has already containerized, the docker image is opnfv/testapi:latest. -This will start a server on port 8000. Just visit http://localhost:8000 +**Running the container not behind nginx:** -For swagger website, just visit http://localhost:8000/swagger/spec.html +.. code-block:: shell -Unittest -===================== + docker run -dti --name testapi -p expose_port:8000 + -e "mongodb_url=mongodb://mongodb_ip:27017/" + -e "base_url=http://host_name:expose_port" + opnfv/testapi:latest -Getting setup -^^^^^^^^^^^^^ +**Running the container behind nginx:** -Requirements for unittest: +.. code-block:: shell -* testtools -* discover -* futures + docker run -dti --name testapi -p expose_port:8000 + -e "mongodb_url=mongodb://mongodb_ip:27017/" + -e "base_url=http://nginx_url" + opnfv/testapi:latest -These requirements are expressed in the test-requirements.txt file and may be -installed by running the following (from within a virtual environment):: +Unittest & pep8 +=============== - pip install -r test-requirements.txt +All requirements for unit tests are outlined in test-requirements.txt, and in TestAPI project, tox is leveraged to drive the executing of unit tests and pep8 check -How to run -^^^^^^^^^^ +**Running unit tests** -From within your environment, just run:: + *tox -e py27* - bash run_test.sh +**Running pep8 check** + *tox -e pep8* diff --git a/utils/test/testapi/docker/prepare-env.sh b/utils/test/testapi/docker/prepare-env.sh index b14bc2448..92a0c9fd7 100755 --- a/utils/test/testapi/docker/prepare-env.sh +++ b/utils/test/testapi/docker/prepare-env.sh @@ -10,5 +10,5 @@ if [ "$base_url" != "" ]; then sudo crudini --set --existing $FILE api url $base_url/api/v1 sudo crudini --set --existing $FILE ui url $base_url sudo echo "{\"testapiApiUrl\": \"$base_url/api/v1\"}" > \ - /usr/local/lib/python2.7/dist-packages/opnfv_testapi/static/testapi-ui/config.json + /usr/local/share/opnfv_testapi/testapi-ui/config.json fi diff --git a/utils/test/testapi/etc/config.ini b/utils/test/testapi/etc/config.ini index 1ec899fcb..8d0bde20b 100644 --- a/utils/test/testapi/etc/config.ini +++ b/utils/test/testapi/etc/config.ini @@ -21,48 +21,11 @@ authenticate = False [ui] url = http://localhost:8000 -[osid] - -# OpenStackID Auth Server URI. (string value) -openstack_openid_endpoint = https://openstackid.org/accounts/openid2 - -# OpenStackID logout URI. (string value) -openid_logout_endpoint = https://openstackid.org/accounts/user/logout - -# Interaction mode. Specifies whether Openstack Id IdP may interact -# with the user to determine the outcome of the request. (string -# value) -openid_mode = checkid_setup - -# Protocol version. Value identifying the OpenID protocol version -# being used. This value should be "http://specs.openid.net/auth/2.0". -# (string value) -openid_ns = http://specs.openid.net/auth/2.0 - -# Return endpoint in Refstack's API. Value indicating the endpoint -# where the user should be returned to after signing in. Openstack Id -# Idp only supports HTTPS address types. (string value) -openid_return_to = v1/auth/signin_return - -# Claimed identifier. This value must be set to -# "http://specs.openid.net/auth/2.0/identifier_select". or to user -# claimed identity (user local identifier or user owned identity [ex: -# custom html hosted on a owned domain set to html discover]). (string -# value) -openid_claimed_id = http://specs.openid.net/auth/2.0/identifier_select - -# Alternate identifier. This value must be set to -# http://specs.openid.net/auth/2.0/identifier_select. (string value) -openid_identity = http://specs.openid.net/auth/2.0/identifier_select - -# Indicates request for user attribute information. This value must be -# set to "http://openid.net/extensions/sreg/1.1". (string value) -openid_ns_sreg = http://openid.net/extensions/sreg/1.1 - -# Comma-separated list of field names which, if absent from the -# response, will prevent the Consumer from completing the registration -# without End User interation. The field names are those that are -# specified in the Response Format, with the "openid.sreg." prefix -# removed. Valid values include: "country", "email", "firstname", -# "language", "lastname" (string value) -openid_sreg_required = email,fullname +# this path should be the seem with data_files installation path +static_path = /usr/local/share/opnfv_testapi + +[lfid] +# Linux Foundation cas URL +cas_url = https://identity.linuxfoundation.org/cas/ +#service url used to authenticate to cas +signin_return = api/v1/auth/signin_return diff --git a/utils/test/testapi/opnfv_testapi/cmd/server.py b/utils/test/testapi/opnfv_testapi/cmd/server.py index 50ac049a0..b7d3caa20 100644 --- a/utils/test/testapi/opnfv_testapi/cmd/server.py +++ b/utils/test/testapi/opnfv_testapi/cmd/server.py @@ -38,7 +38,7 @@ from opnfv_testapi.tornado_swagger import swagger def make_app(): swagger.docs(base_url=CONF.ui_url, - static_path=CONF.static_path) + static_path=CONF.ui_static_path) return swagger.Application( url_mappings.mappings, debug=CONF.api_debug, diff --git a/utils/test/testapi/opnfv_testapi/common/check.py b/utils/test/testapi/opnfv_testapi/common/check.py index 24ba876a9..acd331784 100644 --- a/utils/test/testapi/opnfv_testapi/common/check.py +++ b/utils/test/testapi/opnfv_testapi/common/check.py @@ -7,6 +7,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import functools +import re from tornado import gen from tornado import web @@ -92,7 +93,12 @@ def new_not_exists(xstep): def wrap(self, *args, **kwargs): query = kwargs.get('query') if query: - to_data = yield dbapi.db_find_one(self.table, query()) + query_data = query() + if self.table == 'pods': + if query_data.get('name') is not None: + query_data['name'] = re.compile(query_data.get('name'), + re.IGNORECASE) + to_data = yield dbapi.db_find_one(self.table, query_data) if to_data: raises.Forbidden(message.exist(self.table, query())) ret = yield gen.coroutine(xstep)(self, *args, **kwargs) diff --git a/utils/test/testapi/opnfv_testapi/common/config.py b/utils/test/testapi/opnfv_testapi/common/config.py index 4cd53c619..140e49283 100644 --- a/utils/test/testapi/opnfv_testapi/common/config.py +++ b/utils/test/testapi/opnfv_testapi/common/config.py @@ -16,14 +16,10 @@ import sys class Config(object): def __init__(self): - self.config_file = None + self.config_file = '/etc/opnfv_testapi/config.ini' self._set_config_file() self._parse() self._parse_per_page() - self.static_path = os.path.join( - os.path.dirname(os.path.normpath(__file__)), - os.pardir, - 'static') def _parse(self): if not os.path.exists(self.config_file): @@ -56,23 +52,12 @@ class Config(object): return value def _set_config_file(self): - if not self._set_sys_config_file(): - self._set_default_config_file() - - def _set_sys_config_file(self): parser = argparse.ArgumentParser() parser.add_argument("-c", "--config-file", dest='config_file', help="Config file location", metavar="FILE") args, _ = parser.parse_known_args(sys.argv) - try: + if hasattr(args, 'config_file') and args.config_file: self.config_file = args.config_file - finally: - return self.config_file is not None - - def _set_default_config_file(self): - is_venv = os.getenv('VIRTUAL_ENV') - self.config_file = os.path.join('/' if not is_venv else is_venv, - 'etc/opnfv_testapi/config.ini') CONF = Config() diff --git a/utils/test/testapi/opnfv_testapi/common/constants.py b/utils/test/testapi/opnfv_testapi/common/constants.py new file mode 100644 index 000000000..70c922383 --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/common/constants.py @@ -0,0 +1,4 @@ +TESTAPI_ID = 'testapi_id' +CSRF_TOKEN = 'csrf_token' +ROLE = 'role' +TESTAPI_USERS = ['opnfv-testapi-users'] diff --git a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py index 9389d266d..e202f5c2c 100644 --- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py @@ -6,20 +6,20 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import logging -from datetime import datetime -from datetime import timedelta import json +import logging from bson import objectid +from datetime import datetime +from datetime import timedelta -from opnfv_testapi.common.config import CONF +from opnfv_testapi.common import constants from opnfv_testapi.common import message from opnfv_testapi.common import raises +from opnfv_testapi.common.config import CONF from opnfv_testapi.resources import handlers from opnfv_testapi.resources import result_models from opnfv_testapi.tornado_swagger import swagger -from opnfv_testapi.ui.auth import constants as auth_const class GenericResultHandler(handlers.GenericApiHandler): @@ -59,13 +59,12 @@ class GenericResultHandler(handlers.GenericApiHandler): elif k == 'to': date_range.update({'$lt': str(v)}) elif k == 'signed': - openid = self.get_secure_cookie(auth_const.OPENID) - role = self.get_secure_cookie(auth_const.ROLE) - logging.info('role:%s', role) + username = self.get_secure_cookie(constants.TESTAPI_ID) + role = self.get_secure_cookie(constants.ROLE) if role: del query['public'] if role != "reviewer": - query['user'] = openid + query['user'] = username elif k not in ['last', 'page', 'descend']: query[k] = v if date_range: @@ -246,7 +245,7 @@ class ResultsUploadHandler(ResultsCLHandler): self.json_args = json.loads(fileinfo['body']).copy() self.json_args['public'] = is_public - openid = self.get_secure_cookie(auth_const.OPENID) + openid = self.get_secure_cookie(constants.TESTAPI_ID) if openid: self.json_args['user'] = openid diff --git a/utils/test/testapi/opnfv_testapi/router/url_mappings.py b/utils/test/testapi/opnfv_testapi/router/url_mappings.py index 3e3ab87aa..ce0a3eeb3 100644 --- a/utils/test/testapi/opnfv_testapi/router/url_mappings.py +++ b/utils/test/testapi/opnfv_testapi/router/url_mappings.py @@ -72,12 +72,12 @@ mappings = [ # static path (r'/(.*\.(css|png|gif|js|html|json|map|woff2|woff|ttf))', tornado.web.StaticFileHandler, - {'path': CONF.static_path}), + {'path': CONF.ui_static_path}), (r'/', root.RootHandler), (r'/api/v1/auth/signin', sign.SigninHandler), - (r'/api/v1/auth/signin_return', sign.SigninReturnHandler), + (r'/{}'.format(CONF.lfid_signin_return), sign.SigninReturnHandler), (r'/api/v1/auth/signout', sign.SignoutHandler), - (r'/api/v1/profile', user.ProfileHandler), + (r'/api/v1/profile', user.UserHandler), ] diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/common/noparam.ini b/utils/test/testapi/opnfv_testapi/tests/unit/common/noparam.ini deleted file mode 100644 index be7f2b9f8..000000000 --- a/utils/test/testapi/opnfv_testapi/tests/unit/common/noparam.ini +++ /dev/null @@ -1,16 +0,0 @@ -# to add a new parameter in the config file, -# the CONF object in config.ini must be updated -[mongo] -# URL of the mongo DB -# Mongo auth url => mongodb://user1:pwd1@host1/?authSource=db1 -url = mongodb://127.0.0.1:27017/ - -[api] -# Listening port -port = 8000 -# With debug_on set to true, error traces will be shown in HTTP responses -debug = True -authenticate = False - -[ui] -url = http://localhost:8000 diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/common/normal.ini b/utils/test/testapi/opnfv_testapi/tests/unit/common/normal.ini deleted file mode 100644 index c81c6c56a..000000000 --- a/utils/test/testapi/opnfv_testapi/tests/unit/common/normal.ini +++ /dev/null @@ -1,17 +0,0 @@ -# to add a new parameter in the config file, -# the CONF object in config.ini must be updated -[mongo] -# URL of the mongo DB -# Mongo auth url => mongodb://user1:pwd1@host1/?authSource=db1 -url = mongodb://127.0.0.1:27017/ -dbname = test_results_collection - -[api] -# Listening port -port = 8000 -# With debug_on set to true, error traces will be shown in HTTP responses -debug = True -authenticate = False - -[ui] -url = http://localhost:8000 diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/common/nosection.ini b/utils/test/testapi/opnfv_testapi/tests/unit/common/nosection.ini deleted file mode 100644 index a9ed49c5c..000000000 --- a/utils/test/testapi/opnfv_testapi/tests/unit/common/nosection.ini +++ /dev/null @@ -1,11 +0,0 @@ -# to add a new parameter in the config file, -# the CONF object in config.ini must be updated -[api] -# Listening port -port = 8000 -# With debug_on set to true, error traces will be shown in HTTP responses -debug = True -authenticate = False - -[ui] -url = http://localhost:8000 diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/common/notboolean.ini b/utils/test/testapi/opnfv_testapi/tests/unit/common/notboolean.ini deleted file mode 100644 index 3a11f9dd3..000000000 --- a/utils/test/testapi/opnfv_testapi/tests/unit/common/notboolean.ini +++ /dev/null @@ -1,17 +0,0 @@ -# to add a new parameter in the config file, -# the CONF object in config.ini must be updated -[mongo] -# URL of the mongo DB -# Mongo auth url => mongodb://user1:pwd1@host1/?authSource=db1 -url = mongodb://127.0.0.1:27017/ -dbname = test_results_collection - -[api] -# Listening port -port = 8000 -# With debug_on set to true, error traces will be shown in HTTP responses -debug = True -authenticate = notboolean - -[ui] -url = http://localhost:8000 diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/common/notint.ini b/utils/test/testapi/opnfv_testapi/tests/unit/common/notint.ini deleted file mode 100644 index 8180719b8..000000000 --- a/utils/test/testapi/opnfv_testapi/tests/unit/common/notint.ini +++ /dev/null @@ -1,17 +0,0 @@ -# to add a new parameter in the config file, -# the CONF object in config.ini must be updated -[mongo] -# URL of the mongo DB -# Mongo auth url => mongodb://user1:pwd1@host1/?authSource=db1 -url = mongodb://127.0.0.1:27017/ -dbname = test_results_collection - -[api] -# Listening port -port = notint -# With debug_on set to true, error traces will be shown in HTTP responses -debug = True -authenticate = False - -[ui] -url = http://localhost:8000 diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/conftest.py b/utils/test/testapi/opnfv_testapi/tests/unit/conftest.py index feff1daaa..75e621d0e 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/conftest.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/conftest.py @@ -5,4 +5,4 @@ import pytest @pytest.fixture def config_normal(): - return path.join(path.dirname(__file__), 'common/normal.ini') + return path.join(path.dirname(__file__), '../../../etc/config.ini') diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py index 0ca83df62..3320a866a 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py @@ -6,6 +6,8 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import re + from operator import itemgetter from bson.objectid import ObjectId @@ -190,8 +192,13 @@ class MemDb(object): elif k == 'trust_indicator.current': if content.get('trust_indicator').get('current') != v: return False - elif not isinstance(v, dict) and content.get(k, None) != v: - return False + elif not isinstance(v, dict): + if isinstance(v, re._pattern_type): + if v.match(content.get(k, None)) is None: + return False + else: + if content.get(k, None) != v: + return False return True def _find(self, *args): @@ -199,7 +206,6 @@ class MemDb(object): for content in self.contents: if self._in(content, *args): res.append(content) - return res def find(self, *args): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_base.py b/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_base.py index 77a8d18c1..39633e5f5 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_base.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_base.py @@ -37,7 +37,8 @@ class TestBase(testing.AsyncHTTPTestCase): def _patch_server(self): import argparse - config = path.join(path.dirname(__file__), '../common/normal.ini') + config = path.join(path.dirname(__file__), + '../../../../etc/config.ini') self.config_patcher = mock.patch( 'argparse.ArgumentParser.parse_known_args', return_value=(argparse.Namespace(config_file=config), None)) @@ -46,9 +47,6 @@ class TestBase(testing.AsyncHTTPTestCase): self.config_patcher.start() self.db_patcher.start() - def set_config_file(self): - self.config_file = 'normal.ini' - def get_app(self): from opnfv_testapi.cmd import server return server.make_app() diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_pod.py b/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_pod.py index cb4f1d92c..d1a19f7f0 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_pod.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_pod.py @@ -21,6 +21,8 @@ class TestPodBase(base.TestBase): self.req_d = pod_models.PodCreateRequest('zte-1', 'virtual', 'zte pod 1', 'ci-pod') self.req_e = pod_models.PodCreateRequest('zte-2', 'metal', 'zte pod 2') + self.req_f = pod_models.PodCreateRequest('Zte-1', 'virtual', + 'zte pod 1', 'ci-pod') self.get_res = pod_models.Pod self.list_res = pod_models.Pods self.basePath = '/api/v1/pods' @@ -58,6 +60,11 @@ class TestPodCreate(TestPodBase): self.create_d() return self.req_d + @executor.create(httplib.FORBIDDEN, message.exist_base) + def test_alreadyExistCaseInsensitive(self): + self.create(self.req_f) + return self.req_d + class TestPodGet(TestPodBase): def setUp(self): diff --git a/utils/test/testapi/opnfv_testapi/ui/auth/base.py b/utils/test/testapi/opnfv_testapi/ui/auth/base.py deleted file mode 100644 index bea87c4d9..000000000 --- a/utils/test/testapi/opnfv_testapi/ui/auth/base.py +++ /dev/null @@ -1,35 +0,0 @@ -import random -import string - -from six.moves.urllib import parse - -from opnfv_testapi.resources import handlers - - -class BaseHandler(handlers.GenericApiHandler): - def __init__(self, application, request, **kwargs): - super(BaseHandler, self).__init__(application, request, **kwargs) - self.table = 'users' - - def set_cookies(self, cookies): - for cookie_n, cookie_v in cookies: - self.set_secure_cookie(cookie_n, cookie_v) - - -def get_token(length=30): - """Get random token.""" - return ''.join(random.choice(string.ascii_lowercase) - for i in range(length)) - - -def set_query_params(url, params): - """Set params in given query.""" - url_parts = parse.urlparse(url) - url = parse.urlunparse(( - url_parts.scheme, - url_parts.netloc, - url_parts.path, - url_parts.params, - parse.urlencode(params), - url_parts.fragment)) - return url diff --git a/utils/test/testapi/opnfv_testapi/ui/auth/constants.py b/utils/test/testapi/opnfv_testapi/ui/auth/constants.py deleted file mode 100644 index 44ccb46d7..000000000 --- a/utils/test/testapi/opnfv_testapi/ui/auth/constants.py +++ /dev/null @@ -1,18 +0,0 @@ -OPENID = 'openid' -ROLE = 'role' -DEFAULT_ROLE = 'user' - -# OpenID parameters -OPENID_MODE = 'openid.mode' -OPENID_NS = 'openid.ns' -OPENID_RETURN_TO = 'openid.return_to' -OPENID_CLAIMED_ID = 'openid.claimed_id' -OPENID_IDENTITY = 'openid.identity' -OPENID_REALM = 'openid.realm' -OPENID_NS_SREG = 'openid.ns.sreg' -OPENID_NS_SREG_REQUIRED = 'openid.sreg.required' -OPENID_NS_SREG_EMAIL = 'openid.sreg.email' -OPENID_NS_SREG_FULLNAME = 'openid.sreg.fullname' -OPENID_ERROR = 'openid.error' - -CSRF_TOKEN = 'csrf_token' diff --git a/utils/test/testapi/opnfv_testapi/ui/auth/sign.py b/utils/test/testapi/opnfv_testapi/ui/auth/sign.py index 462395225..318473ea2 100644 --- a/utils/test/testapi/opnfv_testapi/ui/auth/sign.py +++ b/utils/test/testapi/opnfv_testapi/ui/auth/sign.py @@ -1,76 +1,59 @@ -from six.moves.urllib import parse +from cas import CASClient from tornado import gen from tornado import web +from opnfv_testapi.common import constants from opnfv_testapi.common.config import CONF from opnfv_testapi.db import api as dbapi -from opnfv_testapi.ui.auth import base -from opnfv_testapi.ui.auth import constants as const +from opnfv_testapi.resources import handlers -class SigninHandler(base.BaseHandler): +class SignBaseHandler(handlers.GenericApiHandler): + def __init__(self, application, request, **kwargs): + super(SignBaseHandler, self).__init__(application, request, **kwargs) + self.table = 'users' + self.cas_client = CASClient(version='2', + server_url=CONF.lfid_cas_url, + service_url='{}/{}'.format( + CONF.ui_url, + CONF.lfid_signin_return)) + + +class SigninHandler(SignBaseHandler): def get(self): - csrf_token = base.get_token() - return_endpoint = parse.urljoin(CONF.api_url, - CONF.osid_openid_return_to) - return_to = base.set_query_params(return_endpoint, - {const.CSRF_TOKEN: csrf_token}) + self.redirect(url=(self.cas_client.get_login_url())) - params = { - const.OPENID_MODE: CONF.osid_openid_mode, - const.OPENID_NS: CONF.osid_openid_ns, - const.OPENID_RETURN_TO: return_to, - const.OPENID_CLAIMED_ID: CONF.osid_openid_claimed_id, - const.OPENID_IDENTITY: CONF.osid_openid_identity, - const.OPENID_REALM: CONF.api_url, - const.OPENID_NS_SREG: CONF.osid_openid_ns_sreg, - const.OPENID_NS_SREG_REQUIRED: CONF.osid_openid_sreg_required, - } - url = CONF.osid_openstack_openid_endpoint - url = base.set_query_params(url, params) - self.redirect(url=url, permanent=False) +class SigninReturnHandler(SignBaseHandler): -class SigninReturnHandler(base.BaseHandler): @web.asynchronous @gen.coroutine def get(self): - if self.get_query_argument(const.OPENID_MODE) == 'cancel': - self._auth_failure('Authentication canceled.') - - openid = self.get_query_argument(const.OPENID_CLAIMED_ID) - role = const.DEFAULT_ROLE - new_user_info = { - 'openid': openid, - 'email': self.get_query_argument(const.OPENID_NS_SREG_EMAIL), - 'fullname': self.get_query_argument(const.OPENID_NS_SREG_FULLNAME), - const.ROLE: role - } - user = yield dbapi.db_find_one(self.table, {'openid': openid}) - if not user: - dbapi.db_save(self.table, new_user_info) - else: - role = user.get(const.ROLE) - - self.clear_cookie(const.OPENID) - self.clear_cookie(const.ROLE) - self.set_secure_cookie(const.OPENID, openid) - self.set_secure_cookie(const.ROLE, role) - self.redirect(url=CONF.ui_url) - - def _auth_failure(self, message): - params = {'message': message} - url = parse.urljoin(CONF.ui_url, - '/#/auth_failure?' + parse.urlencode(params)) - self.redirect(url) - - -class SignoutHandler(base.BaseHandler): + ticket = self.get_query_argument('ticket', default=None) + if ticket: + (user, attrs, _) = self.cas_client.verify_ticket(ticket=ticket) + login_user = { + 'user': user, + 'email': attrs.get('mail'), + 'fullname': attrs.get('field_lf_full_name'), + 'groups': constants.TESTAPI_USERS + attrs.get('group', []) + } + q_user = {'user': user} + db_user = yield dbapi.db_find_one(self.table, q_user) + if not db_user: + dbapi.db_save(self.table, login_user) + else: + dbapi.db_update(self.table, q_user, login_user) + + self.clear_cookie(constants.TESTAPI_ID) + self.set_secure_cookie(constants.TESTAPI_ID, user) + + self.redirect(url=CONF.ui_url) + + +class SignoutHandler(SignBaseHandler): def get(self): """Handle signout request.""" - self.clear_cookie(const.OPENID) - self.clear_cookie(const.ROLE) - params = {'openid_logout': CONF.osid_openid_logout_endpoint} - url = parse.urljoin(CONF.ui_url, - '/#/logout?' + parse.urlencode(params)) - self.redirect(url) + self.clear_cookie(constants.TESTAPI_ID) + logout_url = self.cas_client.get_logout_url(redirect_url=CONF.ui_url) + self.redirect(url=logout_url) diff --git a/utils/test/testapi/opnfv_testapi/ui/auth/user.py b/utils/test/testapi/opnfv_testapi/ui/auth/user.py index 955cdeead..ab86007f1 100644 --- a/utils/test/testapi/opnfv_testapi/ui/auth/user.py +++ b/utils/test/testapi/opnfv_testapi/ui/auth/user.py @@ -1,25 +1,26 @@ -from tornado import gen -from tornado import web - +from opnfv_testapi.common import constants from opnfv_testapi.common import raises -from opnfv_testapi.db import api as dbapi -from opnfv_testapi.ui.auth import base +from opnfv_testapi.resources import handlers +from opnfv_testapi.resources import models + + +class User(models.ModelBase): + def __init__(self, user=None, email=None, fullname=None, groups=None): + self.user = user + self.email = email + self.fullname = fullname + self.groups = groups + +class UserHandler(handlers.GenericApiHandler): + def __init__(self, application, request, **kwargs): + super(UserHandler, self).__init__(application, request, **kwargs) + self.table = 'users' + self.table_cls = User -class ProfileHandler(base.BaseHandler): - @web.asynchronous - @gen.coroutine def get(self): - openid = self.get_secure_cookie('openid') - if openid: - try: - user = yield dbapi.db_find_one(self.table, {'openid': openid}) - self.finish_request({ - "openid": user.get('openid'), - "email": user.get('email'), - "fullname": user.get('fullname'), - "role": user.get('role', 'user') - }) - except Exception: - pass - raises.Unauthorized('Unauthorized') + username = self.get_secure_cookie(constants.TESTAPI_ID) + if username: + self._get_one(query={'user': username}) + else: + raises.Unauthorized('Unauthorized') diff --git a/utils/test/testapi/opnfv_testapi/ui/root.py b/utils/test/testapi/opnfv_testapi/ui/root.py index 5b2c922d7..286a6b097 100644 --- a/utils/test/testapi/opnfv_testapi/ui/root.py +++ b/utils/test/testapi/opnfv_testapi/ui/root.py @@ -1,10 +1,10 @@ -from opnfv_testapi.resources.handlers import GenericApiHandler from opnfv_testapi.common.config import CONF +from opnfv_testapi.resources import handlers -class RootHandler(GenericApiHandler): +class RootHandler(handlers.GenericApiHandler): def get_template_path(self): - return CONF.static_path + return CONF.ui_static_path def get(self): self.render('testapi-ui/index.html') diff --git a/utils/test/testapi/requirements.txt b/utils/test/testapi/requirements.txt index 4b6f75c10..fbd2e0ede 100644 --- a/utils/test/testapi/requirements.txt +++ b/utils/test/testapi/requirements.txt @@ -8,3 +8,4 @@ tornado>=3.1,<=4.3 # Apache-2.0 epydoc>=0.3.1 six>=1.9.0 # MIT motor # Apache-2.0 +python-cas diff --git a/utils/test/testapi/setup.cfg b/utils/test/testapi/setup.cfg index ab1ef553e..d9aa6762e 100644 --- a/utils/test/testapi/setup.cfg +++ b/utils/test/testapi/setup.cfg @@ -23,18 +23,10 @@ setup-hooks = [files] packages = opnfv_testapi -package_data = - opnfv_testapi = - static/*.* - static/*/*.* - static/*/*/*.* - static/*/*/*/*.* - static/*/*/*/*/*.* - static/*/*/*/*/*/*.* - static/*/*/*/*/*/*/*.* + data_files = - /etc/opnfv_testapi = - etc/config.ini + /etc/opnfv_testapi = etc/config.ini + /usr/local/share/opnfv_testapi = 3rd_party/static/* [entry_points] console_scripts = @@ -44,4 +36,3 @@ console_scripts = tag_build = tag_date = 0 tag_svn_revision = 0 - diff --git a/utils/test/testapi/setup.py b/utils/test/testapi/setup.py index dd52373fd..f9d95a32d 100644 --- a/utils/test/testapi/setup.py +++ b/utils/test/testapi/setup.py @@ -1,6 +1,3 @@ -import os -import subprocess - import setuptools __author__ = 'serena' @@ -10,11 +7,7 @@ try: except ImportError: pass -dirpath = os.path.dirname(os.path.abspath(__file__)) -subprocess.call(['ln', '-s', - '{}/3rd_party/static'.format(dirpath), - '{}/opnfv_testapi/static'.format(dirpath)]) setuptools.setup( - setup_requires=['pbr==2.0.0'], + setup_requires=['pbr>=2.0.0'], pbr=True) diff --git a/utils/test/testapi/tools/watchdog/docker_watch.sh b/utils/test/testapi/tools/watchdog/docker_watch.sh new file mode 100644 index 000000000..f1d8946b6 --- /dev/null +++ b/utils/test/testapi/tools/watchdog/docker_watch.sh @@ -0,0 +1,165 @@ +# * +# http://www.apache.org/licenses/LICENSE-2.0 * +# * +# Unless required by applicable law or agreed to in writing, * +# software distributed under the License is distributed on an * +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * +# KIND, either express or implied. See the License for the * +# specific language governing permissions and limitations * +# under the License. * + +# This script checks if deployments are working or and then +# starts the specified containers in case one of the containers +# crash. The only solution is restarting docker as of now. + +#!/bin/bash + +## List of modules +modules=(testapi reporting) + +## Ports of the modules +declare -A ports=( ["testapi"]="8082" ["reporting"]="8084") + +## Urls to check if the modules are deployed or not ? +declare -A urls=( ["testapi"]="http://testresults.opnfv.org/test/" \ + ["reporting"]="http://testresults.opnfv.org/reporting/index.html") + +### Functions related to checking. + +function is_deploying() { + xml=$(curl -m10 "https://build.opnfv.org/ci/job/${1}-automate-master/lastBuild/api/xml?depth=1") + building=$(grep -oPm1 "(?<=<building>)[^<]+" <<< "$xml") + if [[ $building == "false" ]] + then + false + else + true + fi +} + +function get_docker_status() { + status=$(service docker status | sed -n 3p | cut -d ' ' -f5) + echo -e "Docker status: $status" + if [ $status = "active" ] + then + true + else + false + fi +} + +function check_connectivity() { + echo "Checking $1 connection : $2" + cmd=`curl --head -m10 --request GET ${2} | grep '200 OK' > /dev/null` + rc=$? + if [[ $rc == 0 ]]; then + true + else + false + fi +} + +function check_modules() { + echo -e "Checking modules" + failed_modules=() + for module in "${modules[@]}" + do + if is_deploying $module; then + continue + fi + if ! check_connectivity $module "${urls[$module]}"; then + echo -e "$module failed" + failed_modules+=($module) + fi + done + if [ ! -z "$failed_modules" ]; then + echo -e "Failed Modules: $failed_modules" + false + else + echo -e "All modules working good" + exit 0 + fi +} + +### Functions related fixes. + +function restart_docker_fix() { + echo -e "Running restart_docker_fix" + service docker restart + start_containers_fix "${modules[@]}" +} + +function docker_proxy_fix() { + echo -e "Running docker_proxy_fix" + fix_modules=("${@}") + for module in "${fix_modules[@]}" + do + echo -e "Kill docker proxy and restart containers" + pid=$(netstat -nlp | grep :${ports[$module]} | awk '{print $7}' | cut -d'/' -f1) + echo $pid + if [ ! -z "$pid" ]; then + kill $pid + start_container_fix $module + fi + done +} + +function start_containers_fix() { + start_modules=("${@}") + for module in "${start_modules[@]}" + do + start_container_fix $module + done +} + +function start_container_fix() { + echo -e "Starting a container $module" + sudo docker stop $module + sudo docker start $module + sleep 5 + if ! check_connectivity $module "${urls[$module]}"; then + echo -e "Starting an old container $module_old" + sudo docker stop $module + sudo docker start $module"_old" + sleep 5 + fi +} + +### Main Flow + +echo -e +echo -e "WatchDog Started" +echo -e +echo -e `date "+%Y-%m-%d %H:%M:%S.%N"` +echo -e + +## If the problem is related to docker daemon + +if ! get_docker_status; then + restart_docker_fix + if ! check_modules; then + echo -e "Watchdog failed while restart_docker_fix" + fi + exit +fi + +## If the problem is related to docker proxy + +if ! check_modules; then + docker_proxy_fix "${failed_modules[@]}" +fi + +## If any other problem : restart docker + +if ! check_modules; then + restart_docker_fix +fi + +## If nothing works out + +if ! check_modules; then + echo -e "Watchdog failed" +fi + +sudo docker ps +sudo docker images
\ No newline at end of file |