summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCédric Ollivier <cedric.ollivier@orange.com>2018-01-20 14:41:32 +0100
committerCédric Ollivier <cedric.ollivier@orange.com>2018-01-21 01:07:41 +0100
commita6df43da7ab4de653eecbc9b6380d5fc7ce7bc14 (patch)
tree028d5516ead89f61929866382b600598e6dac926
parent45fdfedc5b5573926c53264ff5eb3e48a43345d9 (diff)
Move push_results_to_db to TestCase
It removes the link between TestCase and Functest utils (xtesting). It should be noted that testresults url can only be set by env var. Co-Authored-By: Cédric Ollivier <cedric.ollivier@orange.com> Change-Id: Ie072c675890d0ae2e63619c599d2684a8c7d762d Signed-off-by: Jose Lausuch <jalausuch@suse.com> Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
-rw-r--r--functest/ci/config_functest.yaml6
-rw-r--r--functest/core/testcase.py71
-rw-r--r--functest/tests/unit/core/test_testcase.py157
-rw-r--r--functest/tests/unit/utils/test_decorators.py63
-rw-r--r--functest/tests/unit/utils/test_functest_utils.py103
-rw-r--r--functest/utils/functest_utils.py101
6 files changed, 192 insertions, 309 deletions
diff --git a/functest/ci/config_functest.yaml b/functest/ci/config_functest.yaml
index 4bb7c441a..192a1ee0d 100644
--- a/functest/ci/config_functest.yaml
+++ b/functest/ci/config_functest.yaml
@@ -188,12 +188,6 @@ example:
sg_name: example-sg
sg_desc: Example Security group
-results:
- # you can also set a file (e.g. /home/opnfv/functest/results/dump.txt)
- # to dump results
- # test_db_url: file:///home/opnfv/functest/results/dump.txt
- test_db_url: http://testresults.opnfv.org/test/api/v1/results
-
energy_recorder:
api_url: http://energy.opnfv.fr/resources
api_user: ""
diff --git a/functest/core/testcase.py b/functest/core/testcase.py
index fa3802872..5894f5c0f 100644
--- a/functest/core/testcase.py
+++ b/functest/core/testcase.py
@@ -9,10 +9,15 @@
"""Define the parent class of all Functest TestCases."""
+from datetime import datetime
+import json
import logging
import os
+import re
+import requests
+
+from functest.utils import decorators
-import functest.utils.functest_utils as ft_utils
import prettytable
@@ -35,6 +40,8 @@ class TestCase(object):
EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
"""results are false"""
+ _job_name_rule = "(dai|week)ly-(.+?)-[0-9]*"
+ _headers = {'Content-Type': 'application/json'}
__logger = logging.getLogger(__name__)
def __init__(self, **kwargs):
@@ -139,14 +146,16 @@ class TestCase(object):
self.__logger.error("Run must be implemented")
return TestCase.EX_RUN_ERROR
+ @decorators.can_dump_request_to_file
def push_to_db(self):
"""Push the results of the test case to the DB.
- It allows publishing the results and to check the status.
+ It allows publishing the results and checking the status.
It could be overriden if the common implementation is not
- suitable. The following attributes must be set before pushing
- the results to DB:
+ suitable.
+
+ The following attributes must be set before pushing the results to DB:
* project_name,
* case_name,
@@ -154,6 +163,14 @@ class TestCase(object):
* start_time,
* stop_time.
+ The next vars must be set in env:
+
+ * TEST_DB_URL,
+ * INSTALLER_TYPE,
+ * DEPLOY_SCENARIO,
+ * NODE_NAME,
+ * BUILD_TAG.
+
Returns:
TestCase.EX_OK if results were pushed to DB.
TestCase.EX_PUSH_TO_DB_ERROR otherwise.
@@ -163,20 +180,46 @@ class TestCase(object):
assert self.case_name
assert self.start_time
assert self.stop_time
- pub_result = 'PASS' if self.is_successful(
+ url = os.environ['TEST_DB_URL']
+ data = {"project_name": self.project_name,
+ "case_name": self.case_name,
+ "details": self.details}
+ data["installer"] = os.environ['INSTALLER_TYPE']
+ data["scenario"] = os.environ['DEPLOY_SCENARIO']
+ data["pod_name"] = os.environ['NODE_NAME']
+ data["build_tag"] = os.environ['BUILD_TAG']
+ data["criteria"] = 'PASS' if self.is_successful(
) == TestCase.EX_OK else 'FAIL'
- if ft_utils.push_results_to_db(
- self.project_name, self.case_name, self.start_time,
- self.stop_time, pub_result, self.details):
- self.__logger.info(
- "The results were successfully pushed to DB")
- return TestCase.EX_OK
- else:
- self.__logger.error("The results cannot be pushed to DB")
- return TestCase.EX_PUSH_TO_DB_ERROR
+ data["start_date"] = datetime.fromtimestamp(
+ self.start_time).strftime('%Y-%m-%d %H:%M:%S')
+ data["stop_date"] = datetime.fromtimestamp(
+ self.stop_time).strftime('%Y-%m-%d %H:%M:%S')
+ try:
+ data["version"] = re.search(
+ TestCase._job_name_rule,
+ os.environ['BUILD_TAG']).group(2)
+ except Exception: # pylint: disable=broad-except
+ data["version"] = "unknown"
+ req = requests.post(
+ url, data=json.dumps(data, sort_keys=True),
+ headers=self._headers)
+ req.raise_for_status()
+ self.__logger.info(
+ "The results %s were successfully pushed to DB %s", data, url)
+ except AssertionError:
+ self.__logger.exception(
+ "Please run test before publishing the results")
+ return TestCase.EX_PUSH_TO_DB_ERROR
+ except KeyError as exc:
+ self.__logger.error("Please set env var: " + str(exc))
+ return TestCase.EX_PUSH_TO_DB_ERROR
+ except requests.exceptions.HTTPError:
+ self.__logger.exception("The HTTP request raises issues")
+ return TestCase.EX_PUSH_TO_DB_ERROR
except Exception: # pylint: disable=broad-except
self.__logger.exception("The results cannot be pushed to DB")
return TestCase.EX_PUSH_TO_DB_ERROR
+ return TestCase.EX_OK
def clean(self):
"""Clean the resources.
diff --git a/functest/tests/unit/core/test_testcase.py b/functest/tests/unit/core/test_testcase.py
index 73ed3470a..e11e5ff7b 100644
--- a/functest/tests/unit/core/test_testcase.py
+++ b/functest/tests/unit/core/test_testcase.py
@@ -9,12 +9,16 @@
"""Define the class required to fully cover testcase."""
+from datetime import datetime
+import json
import logging
+import os
import unittest
from functest.core import testcase
import mock
+import requests
__author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
@@ -28,90 +32,131 @@ class TestCaseTesting(unittest.TestCase):
_case_name = "base"
_project_name = "functest"
_published_result = "PASS"
+ _test_db_url = "http://testresults.opnfv.org/test/api/v1/results"
+ _headers = {'Content-Type': 'application/json'}
def setUp(self):
self.test = testcase.TestCase(case_name=self._case_name,
project_name=self._project_name)
- self.test.start_time = "1"
- self.test.stop_time = "2"
+ self.test.start_time = 1
+ self.test.stop_time = 2
self.test.result = 100
self.test.details = {"Hello": "World"}
+ os.environ['TEST_DB_URL'] = TestCaseTesting._test_db_url
+ os.environ['INSTALLER_TYPE'] = "installer_type"
+ os.environ['DEPLOY_SCENARIO'] = "scenario"
+ os.environ['NODE_NAME'] = "node_name"
+ os.environ['BUILD_TAG'] = "foo-daily-master-bar"
def test_run_unimplemented(self):
self.assertEqual(self.test.run(),
testcase.TestCase.EX_RUN_ERROR)
- @mock.patch('functest.utils.functest_utils.push_results_to_db',
- return_value=False)
- def _test_missing_attribute(self, mock_function=None):
+ def _test_pushdb_missing_attribute(self):
self.assertEqual(self.test.push_to_db(),
testcase.TestCase.EX_PUSH_TO_DB_ERROR)
- mock_function.assert_not_called()
- def test_missing_project_name(self):
+ def test_pushdb_no_project_name(self):
self.test.project_name = None
- self._test_missing_attribute()
+ self._test_pushdb_missing_attribute()
- def test_missing_case_name(self):
+ def test_pushdb_no_case_name(self):
self.test.case_name = None
- self._test_missing_attribute()
+ self._test_pushdb_missing_attribute()
- def test_missing_start_time(self):
+ def test_pushdb_no_start_time(self):
self.test.start_time = None
- self._test_missing_attribute()
+ self._test_pushdb_missing_attribute()
- def test_missing_stop_time(self):
+ def test_pushdb_no_stop_time(self):
self.test.stop_time = None
- self._test_missing_attribute()
-
- @mock.patch('functest.utils.functest_utils.push_results_to_db',
- return_value=True)
- def test_missing_details(self, mock_function=None):
- self.test.details = None
- self.assertEqual(self.test.push_to_db(),
- testcase.TestCase.EX_OK)
- mock_function.assert_called_once_with(
- self._project_name, self._case_name, self.test.start_time,
- self.test.stop_time, self._published_result, self.test.details)
+ self._test_pushdb_missing_attribute()
- @mock.patch('functest.utils.functest_utils.push_results_to_db',
- return_value=False)
- def test_push_to_db_failed(self, mock_function=None):
+ def _test_pushdb_missing_env(self, var):
+ del os.environ[var]
self.assertEqual(self.test.push_to_db(),
testcase.TestCase.EX_PUSH_TO_DB_ERROR)
- mock_function.assert_called_once_with(
- self._project_name, self._case_name, self.test.start_time,
- self.test.stop_time, self._published_result, self.test.details)
- @mock.patch('functest.utils.functest_utils.push_results_to_db',
- return_value=True)
- def test_push_to_db(self, mock_function=None):
- self.assertEqual(self.test.push_to_db(),
- testcase.TestCase.EX_OK)
- mock_function.assert_called_once_with(
- self._project_name, self._case_name, self.test.start_time,
- self.test.stop_time, self._published_result, self.test.details)
+ def test_pushdb_no_db_url(self):
+ self._test_pushdb_missing_env('TEST_DB_URL')
- @mock.patch('functest.utils.functest_utils.push_results_to_db',
- return_value=True)
- def test_push_to_db_res_ko(self, mock_function=None):
- self.test.result = 0
- self.assertEqual(self.test.push_to_db(),
- testcase.TestCase.EX_OK)
- mock_function.assert_called_once_with(
- self._project_name, self._case_name, self.test.start_time,
- self.test.stop_time, 'FAIL', self.test.details)
+ def test_pushdb_no_installer_type(self):
+ self._test_pushdb_missing_env('INSTALLER_TYPE')
- @mock.patch('functest.utils.functest_utils.push_results_to_db',
- return_value=True)
- def test_push_to_db_both_ko(self, mock_function=None):
- self.test.result = 0
- self.test.criteria = 0
- self.assertEqual(self.test.push_to_db(),
- testcase.TestCase.EX_OK)
+ def test_pushdb_no_deploy_scenario(self):
+ self._test_pushdb_missing_env('DEPLOY_SCENARIO')
+
+ def test_pushdb_no_node_name(self):
+ self._test_pushdb_missing_env('NODE_NAME')
+
+ def test_pushdb_no_build_tag(self):
+ self._test_pushdb_missing_env('BUILD_TAG')
+
+ @mock.patch('requests.post')
+ def test_pushdb_bad_start_time(self, mock_function=None):
+ self.test.start_time = "1"
+ self.assertEqual(
+ self.test.push_to_db(),
+ testcase.TestCase.EX_PUSH_TO_DB_ERROR)
+ mock_function.assert_not_called()
+
+ @mock.patch('requests.post')
+ def test_pushdb_bad_end_time(self, mock_function=None):
+ self.test.stop_time = "2"
+ self.assertEqual(
+ self.test.push_to_db(),
+ testcase.TestCase.EX_PUSH_TO_DB_ERROR)
+ mock_function.assert_not_called()
+
+ def _get_data(self):
+ return {
+ "build_tag": os.environ['BUILD_TAG'],
+ "case_name": self._case_name,
+ "criteria": 'PASS' if self.test.is_successful(
+ ) == self.test.EX_OK else 'FAIL',
+ "details": self.test.details,
+ "installer": os.environ['INSTALLER_TYPE'],
+ "pod_name": os.environ['NODE_NAME'],
+ "project_name": self.test.project_name,
+ "scenario": os.environ['DEPLOY_SCENARIO'],
+ "start_date": datetime.fromtimestamp(
+ self.test.start_time).strftime('%Y-%m-%d %H:%M:%S'),
+ "stop_date": datetime.fromtimestamp(
+ self.test.stop_time).strftime('%Y-%m-%d %H:%M:%S'),
+ "version": "master"}
+
+ @mock.patch('requests.post')
+ def _test_pushdb_version(self, mock_function=None, **kwargs):
+ payload = self._get_data()
+ payload["version"] = kwargs.get("version", "unknown")
+ self.assertEqual(self.test.push_to_db(), testcase.TestCase.EX_OK)
+ mock_function.assert_called_once_with(
+ os.environ['TEST_DB_URL'],
+ data=json.dumps(payload, sort_keys=True),
+ headers=self._headers)
+
+ def test_pushdb_daily_job(self):
+ self._test_pushdb_version(version="master")
+
+ def test_pushdb_weekly_job(self):
+ os.environ['BUILD_TAG'] = 'foo-weekly-master-bar'
+ self._test_pushdb_version(version="master")
+
+ def test_pushdb_random_build_tag(self):
+ os.environ['BUILD_TAG'] = 'whatever'
+ self._test_pushdb_version(version="unknown")
+
+ @mock.patch('requests.post', return_value=mock.Mock(
+ raise_for_status=mock.Mock(
+ side_effect=requests.exceptions.HTTPError)))
+ def test_pushdb_http_errors(self, mock_function=None):
+ self.assertEqual(
+ self.test.push_to_db(),
+ testcase.TestCase.EX_PUSH_TO_DB_ERROR)
mock_function.assert_called_once_with(
- self._project_name, self._case_name, self.test.start_time,
- self.test.stop_time, 'FAIL', self.test.details)
+ os.environ['TEST_DB_URL'],
+ data=json.dumps(self._get_data(), sort_keys=True),
+ headers=self._headers)
def test_check_criteria_missing(self):
self.test.criteria = None
diff --git a/functest/tests/unit/utils/test_decorators.py b/functest/tests/unit/utils/test_decorators.py
index 82291fa2d..b4cdf6ff3 100644
--- a/functest/tests/unit/utils/test_decorators.py
+++ b/functest/tests/unit/utils/test_decorators.py
@@ -18,13 +18,11 @@ import unittest
import mock
+from functest.core import testcase
from functest.utils import decorators
-from functest.utils import functest_utils
-from functest.utils.constants import CONST
__author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
-VERSION = 'master'
DIR = '/dev'
FILE = '{}/null'.format(DIR)
URL = 'file://{}'.format(FILE)
@@ -38,7 +36,8 @@ class DecoratorsTesting(unittest.TestCase):
_start_time = 1.0
_stop_time = 2.0
_result = 'PASS'
- _build_tag = VERSION
+ _version = 'unknown'
+ _build_tag = 'none'
_node_name = 'bar'
_deploy_scenario = 'foo'
_installer_type = 'debian'
@@ -50,8 +49,8 @@ class DecoratorsTesting(unittest.TestCase):
os.environ['BUILD_TAG'] = self._build_tag
def test_wraps(self):
- self.assertEqual(functest_utils.push_results_to_db.__name__,
- "push_results_to_db")
+ self.assertEqual(testcase.TestCase.push_to_db.__name__,
+ "push_to_db")
def _get_json(self):
stop_time = datetime.fromtimestamp(self._stop_time).strftime(
@@ -62,44 +61,45 @@ class DecoratorsTesting(unittest.TestCase):
'stop_date': stop_time, 'start_date': start_time,
'case_name': self._case_name, 'build_tag': self._build_tag,
'pod_name': self._node_name, 'installer': self._installer_type,
- 'scenario': self._deploy_scenario, 'version': VERSION,
+ 'scenario': self._deploy_scenario, 'version': self._version,
'details': {}, 'criteria': self._result}
return json.dumps(data, sort_keys=True)
- @mock.patch('{}.get_version'.format(functest_utils.__name__),
- return_value=VERSION)
+ def _get_testcase(self):
+ test = testcase.TestCase(
+ project_name=self._project_name, case_name=self._case_name)
+ test.start_time = self._start_time
+ test.stop_time = self._stop_time
+ test.result = 100
+ test.details = {}
+ return test
+
@mock.patch('requests.post')
def test_http_shema(self, *args):
- CONST.__setattr__('results_test_db_url', 'http://127.0.0.1')
- self.assertTrue(functest_utils.push_results_to_db(
- self._project_name, self._case_name, self._start_time,
- self._stop_time, self._result, {}))
- args[1].assert_called_once_with()
+ os.environ['TEST_DB_URL'] = 'http://127.0.0.1'
+ test = self._get_testcase()
+ self.assertEqual(test.push_to_db(), testcase.TestCase.EX_OK)
args[0].assert_called_once_with(
'http://127.0.0.1', data=self._get_json(),
headers={'Content-Type': 'application/json'})
def test_wrong_shema(self):
- CONST.__setattr__('results_test_db_url', '/dev/null')
- self.assertFalse(functest_utils.push_results_to_db(
- self._project_name, self._case_name, self._start_time,
- self._stop_time, self._result, {}))
-
- @mock.patch('{}.get_version'.format(functest_utils.__name__),
- return_value=VERSION)
- def _test_dump(self, *args):
- CONST.__setattr__('results_test_db_url', URL)
+ os.environ['TEST_DB_URL'] = '/dev/null'
+ test = self._get_testcase()
+ self.assertEqual(
+ test.push_to_db(), testcase.TestCase.EX_PUSH_TO_DB_ERROR)
+
+ def _test_dump(self):
+ os.environ['TEST_DB_URL'] = URL
with mock.patch.object(decorators, 'open', mock.mock_open(),
create=True) as mock_open:
- self.assertTrue(functest_utils.push_results_to_db(
- self._project_name, self._case_name, self._start_time,
- self._stop_time, self._result, {}))
+ test = self._get_testcase()
+ self.assertEqual(test.push_to_db(), testcase.TestCase.EX_OK)
mock_open.assert_called_once_with(FILE, 'a')
handle = mock_open()
call_args, _ = handle.write.call_args
self.assertIn('POST', call_args[0])
self.assertIn(self._get_json(), call_args[0])
- args[0].assert_called_once_with()
@mock.patch('os.makedirs')
def test_default_dump(self, mock_method=None):
@@ -113,11 +113,10 @@ class DecoratorsTesting(unittest.TestCase):
@mock.patch('os.makedirs', side_effect=OSError)
def test_makedirs_exc(self, *args):
- CONST.__setattr__('results_test_db_url', URL)
- self.assertFalse(
- functest_utils.push_results_to_db(
- self._project_name, self._case_name, self._start_time,
- self._stop_time, self._result, {}))
+ os.environ['TEST_DB_URL'] = URL
+ test = self._get_testcase()
+ self.assertEqual(
+ test.push_to_db(), testcase.TestCase.EX_PUSH_TO_DB_ERROR)
args[0].assert_called_once_with(DIR)
diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py
index 7a77d25b9..6979932ef 100644
--- a/functest/tests/unit/utils/test_functest_utils.py
+++ b/functest/tests/unit/utils/test_functest_utils.py
@@ -14,16 +14,16 @@ import time
import unittest
import mock
-import requests
from six.moves import urllib
-from functest.tests.unit import test_utils
from functest.utils import functest_utils
-from functest.utils.constants import CONST
class FunctestUtilsTesting(unittest.TestCase):
+ readline = 0
+ test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15']
+
def setUp(self):
self.url = 'http://www.opnfv.org/'
self.timeout = 5
@@ -97,22 +97,6 @@ class FunctestUtilsTesting(unittest.TestCase):
m.assert_called_once_with(dest, 'wb')
self.assertTrue(mock_sh.called)
- def test_get_version_daily_job(self):
- CONST.__setattr__('BUILD_TAG', self.build_tag)
- self.assertEqual(functest_utils.get_version(), self.version)
-
- def test_get_version_weekly_job(self):
- CONST.__setattr__('BUILD_TAG', self.build_tag_week)
- self.assertEqual(functest_utils.get_version(), self.version)
-
- def test_get_version_with_dummy_build_tag(self):
- CONST.__setattr__('BUILD_TAG', 'whatever')
- self.assertEqual(functest_utils.get_version(), 'unknown')
-
- def test_get_version_unknown(self):
- CONST.__setattr__('BUILD_TAG', 'unknown_build_tag')
- self.assertEqual(functest_utils.get_version(), "unknown")
-
def _get_env_dict(self, var):
dic = {'INSTALLER_TYPE': self.installer,
'DEPLOY_SCENARIO': self.scenario,
@@ -121,87 +105,6 @@ class FunctestUtilsTesting(unittest.TestCase):
dic.pop(var, None)
return dic
- def _test_push_results_to_db_missing_env(self, env_var):
- dic = self._get_env_dict(env_var)
- CONST.__setattr__('results_test_db_url', self.db_url)
- with mock.patch.dict(os.environ,
- dic,
- clear=True), \
- mock.patch('functest.utils.functest_utils.logger.error') \
- as mock_logger_error:
- functest_utils.push_results_to_db(self.project, self.case_name,
- self.start_date, self.stop_date,
- self.result, self.details)
- mock_logger_error.assert_called_once_with("Please set env var: " +
- str("\'" + env_var +
- "\'"))
-
- def test_push_results_to_db_missing_installer(self):
- self._test_push_results_to_db_missing_env('INSTALLER_TYPE')
-
- def test_push_results_to_db_missing_scenario(self):
- self._test_push_results_to_db_missing_env('DEPLOY_SCENARIO')
-
- def test_push_results_to_db_missing_nodename(self):
- self._test_push_results_to_db_missing_env('NODE_NAME')
-
- def test_push_results_to_db_missing_buildtag(self):
- self._test_push_results_to_db_missing_env('BUILD_TAG')
-
- def test_push_results_to_db_request_post_failed(self):
- dic = self._get_env_dict(None)
- CONST.__setattr__('results_test_db_url', self.db_url)
- with mock.patch.dict(os.environ,
- dic,
- clear=True), \
- mock.patch('functest.utils.functest_utils.logger.error') \
- as mock_logger_error, \
- mock.patch('functest.utils.functest_utils.requests.post',
- side_effect=requests.RequestException):
- self.assertFalse(functest_utils.
- push_results_to_db(self.project, self.case_name,
- self.start_date,
- self.stop_date,
- self.result, self.details))
- mock_logger_error.assert_called_once_with(test_utils.
- RegexMatch("Pushing "
- "Result to"
- " DB"
- "(\S+\s*) "
- "failed:"))
-
- def test_push_results_to_db_request_post_exception(self):
- dic = self._get_env_dict(None)
- CONST.__setattr__('results_test_db_url', self.db_url)
- with mock.patch.dict(os.environ,
- dic,
- clear=True), \
- mock.patch('functest.utils.functest_utils.logger.error') \
- as mock_logger_error, \
- mock.patch('functest.utils.functest_utils.requests.post',
- side_effect=Exception):
- self.assertFalse(functest_utils.
- push_results_to_db(self.project, self.case_name,
- self.start_date,
- self.stop_date,
- self.result, self.details))
- self.assertTrue(mock_logger_error.called)
-
- def test_push_results_to_db_default(self):
- dic = self._get_env_dict(None)
- CONST.__setattr__('results_test_db_url', self.db_url)
- with mock.patch.dict(os.environ,
- dic,
- clear=True), \
- mock.patch('functest.utils.functest_utils.requests.post'):
- self.assertTrue(functest_utils.
- push_results_to_db(self.project, self.case_name,
- self.start_date,
- self.stop_date,
- self.result, self.details))
- readline = 0
- test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15']
-
@staticmethod
def readline_side():
if FunctestUtilsTesting.readline == \
diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py
index 6d0b345d5..1e88dadb7 100644
--- a/functest/utils/functest_utils.py
+++ b/functest/utils/functest_utils.py
@@ -8,7 +8,6 @@
# http://www.apache.org/licenses/LICENSE-2.0
#
import functools
-import json
import logging
import os
import pkg_resources
@@ -17,16 +16,12 @@ import shutil
import subprocess
import sys
import time
-from datetime import datetime as dt
import dns.resolver
-import requests
from six.moves import urllib
import yaml
from functest.utils import constants
-from functest.utils import decorators
-from functest.utils.constants import CONST
logger = logging.getLogger(__name__)
@@ -68,102 +63,6 @@ def download_url(url, dest_path):
# CI UTILS
#
# -----------------------------------------------------------
-def get_version():
- """
- Get version
- """
- # Use the build tag to retrieve the version
- # By default version is unknown
- # if launched through CI the build tag has the following format
- # jenkins-<project>-<installer>-<pod>-<job>-<branch>-<id>
- # e.g. jenkins-functest-fuel-opnfv-jump-2-daily-master-190
- # jenkins-functest-fuel-baremetal-weekly-master-8
- # use regex to match branch info
- rule = "(dai|week)ly-(.+?)-[0-9]*"
- build_tag = CONST.__getattribute__('BUILD_TAG')
- if not build_tag:
- build_tag = 'none'
- m = re.search(rule, build_tag)
- if m:
- return m.group(2)
- else:
- return "unknown"
-
-
-@decorators.can_dump_request_to_file
-def push_results_to_db(project, case_name,
- start_date, stop_date, result, details):
- """
- POST results to the Result target DB
- """
- # Retrieve params from CI and conf
- if (hasattr(CONST, 'TEST_DB_URL')):
- url = CONST.__getattribute__('TEST_DB_URL')
- else:
- url = CONST.__getattribute__("results_test_db_url")
-
- try:
- installer = os.environ['INSTALLER_TYPE']
- scenario = os.environ['DEPLOY_SCENARIO']
- pod_name = os.environ['NODE_NAME']
- build_tag = os.environ['BUILD_TAG']
- except KeyError as e:
- logger.error("Please set env var: " + str(e))
- return False
- version = get_version()
- test_start = dt.fromtimestamp(start_date).strftime('%Y-%m-%d %H:%M:%S')
- test_stop = dt.fromtimestamp(stop_date).strftime('%Y-%m-%d %H:%M:%S')
-
- params = {"project_name": project, "case_name": case_name,
- "pod_name": pod_name, "installer": installer,
- "version": version, "scenario": scenario, "criteria": result,
- "build_tag": build_tag, "start_date": test_start,
- "stop_date": test_stop, "details": details}
-
- error = None
- headers = {'Content-Type': 'application/json'}
- try:
- r = requests.post(url, data=json.dumps(params, sort_keys=True),
- headers=headers)
- logger.debug(r)
- r.raise_for_status()
- except requests.RequestException as exc:
- if 'r' in locals():
- error = ("Pushing Result to DB(%s) failed: %s" %
- (r.url, r.content))
- else:
- error = ("Pushing Result to DB(%s) failed: %s" % (url, exc))
- except Exception as e:
- error = ("Error [push_results_to_db("
- "DB: '%(db)s', "
- "project: '%(project)s', "
- "case: '%(case)s', "
- "pod: '%(pod)s', "
- "version: '%(v)s', "
- "scenario: '%(s)s', "
- "criteria: '%(c)s', "
- "build_tag: '%(t)s', "
- "details: '%(d)s')]: "
- "%(error)s" %
- {
- 'db': url,
- 'project': project,
- 'case': case_name,
- 'pod': pod_name,
- 'v': version,
- 's': scenario,
- 'c': result,
- 't': build_tag,
- 'd': details,
- 'error': e
- })
- finally:
- if error:
- logger.error(error)
- return False
- return True
-
-
def get_resolvconf_ns():
"""
Get nameservers from current resolv.conf