summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Lausuch <jose.lausuch@ericsson.com>2017-02-14 10:31:44 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-02-14 10:31:44 +0000
commit33ee390f8324175ae59775bc2589711a60f03939 (patch)
tree1474e8620dfd367559fcf25e786f5a402c17bb96
parent66d02a85d91c5981ae3c2ebf5fa5b1e51c7ad143 (diff)
parent1e60ed4a402d579675fbf122702a5b8fd3225e3a (diff)
Merge changes from topic 'revert_write_to_file'
* changes: Add can_dump_request_to_file decorator Revert "write test results to a local file"
-rwxr-xr-xfunctest/ci/run_tests.py2
-rw-r--r--functest/core/testcase_base.py47
-rw-r--r--functest/opnfv_tests/openstack/vping/vping_base.py2
-rwxr-xr-xfunctest/opnfv_tests/sdn/odl/odl.py2
-rw-r--r--functest/tests/unit/core/test_testcase_base.py8
-rw-r--r--functest/utils/decorators.py36
-rw-r--r--functest/utils/functest_utils.py36
7 files changed, 56 insertions, 77 deletions
diff --git a/functest/ci/run_tests.py b/functest/ci/run_tests.py
index b1ab9169c..93518de0b 100755
--- a/functest/ci/run_tests.py
+++ b/functest/ci/run_tests.py
@@ -164,7 +164,7 @@ def run_test(test, tier_name, testcases=None):
result = test_case.run()
if result == testcase_base.TestcaseBase.EX_OK:
if GlobalVariables.REPORT_FLAG:
- test_case.publish_report()
+ test_case.push_to_db()
result = test_case.check_criteria()
except ImportError:
logger.exception("Cannot import module {}".format(
diff --git a/functest/core/testcase_base.py b/functest/core/testcase_base.py
index ec46bc643..838b63983 100644
--- a/functest/core/testcase_base.py
+++ b/functest/core/testcase_base.py
@@ -9,7 +9,6 @@
import os
-from functest.utils.constants import CONST
import functest.utils.functest_logger as ft_logger
import functest.utils.functest_utils as ft_utils
@@ -18,7 +17,7 @@ class TestcaseBase(object):
EX_OK = os.EX_OK
EX_RUN_ERROR = os.EX_SOFTWARE
- EX_PUBLISH_RESULT_FAILED = os.EX_SOFTWARE - 1
+ EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1
EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
logger = ft_logger.Logger(__name__).getLogger()
@@ -44,45 +43,21 @@ class TestcaseBase(object):
self.logger.error("Run must be implemented")
return TestcaseBase.EX_RUN_ERROR
- def publish_report(self):
- if "RESULTS_STORE" in os.environ:
- CONST.results_test_db_url = os.environ['RESULTS_STORE']
-
+ def push_to_db(self):
try:
assert self.project_name
assert self.case_name
assert self.criteria
assert self.start_time
assert self.stop_time
- if CONST.results_test_db_url.lower().startswith(
- ("http://", "https://")):
- self.push_to_db()
- elif CONST.results_test_db_url.lower().startswith("file://"):
- self.write_to_file()
+ if ft_utils.push_results_to_db(
+ self.project_name, self.case_name, self.start_time,
+ self.stop_time, self.criteria, self.details):
+ self.logger.info("The results were successfully pushed to DB")
+ return TestcaseBase.EX_OK
else:
- self.logger.error("Please check parameter test_db_url and "
- "OS environ variable RESTULTS_STORE")
- return TestcaseBase.EX_PUBLISH_RESULT_FAILED
+ self.logger.error("The results cannot be pushed to DB")
+ return TestcaseBase.EX_PUSH_TO_DB_ERROR
except Exception:
- self.logger.exception("The results cannot be stored")
- return TestcaseBase.EX_PUBLISH_RESULT_FAILED
-
- def write_to_file(self):
- if ft_utils.write_results_to_file(
- self.project_name, self.case_name, self.start_time,
- self.stop_time, self.criteria, self.details):
- self.logger.info("The results were successfully written to a file")
- return TestcaseBase.EX_OK
- else:
- self.logger.error("write results to a file failed")
- return TestcaseBase.EX_PUBLISH_RESULT_FAILED
-
- def push_to_db(self):
- if ft_utils.push_results_to_db(
- self.project_name, self.case_name, self.start_time,
- self.stop_time, self.criteria, self.details):
- self.logger.info("The results were successfully pushed to DB")
- return TestcaseBase.EX_OK
- else:
- self.logger.error("The results cannot be pushed to DB")
- return TestcaseBase.EX_PUBLISH_RESULT_FAILED
+ self.logger.exception("The results cannot be pushed to DB")
+ return TestcaseBase.EX_PUSH_TO_DB_ERROR
diff --git a/functest/opnfv_tests/openstack/vping/vping_base.py b/functest/opnfv_tests/openstack/vping/vping_base.py
index 8285d93f8..a5309bd44 100644
--- a/functest/opnfv_tests/openstack/vping/vping_base.py
+++ b/functest/opnfv_tests/openstack/vping/vping_base.py
@@ -289,6 +289,6 @@ class VPingMain(object):
if result != VPingBase.EX_OK:
return result
if kwargs['report']:
- return self.vping.publish_report()
+ return self.vping.push_to_db()
except Exception:
return VPingBase.EX_RUN_ERROR
diff --git a/functest/opnfv_tests/sdn/odl/odl.py b/functest/opnfv_tests/sdn/odl/odl.py
index 250759572..9bff324f1 100755
--- a/functest/opnfv_tests/sdn/odl/odl.py
+++ b/functest/opnfv_tests/sdn/odl/odl.py
@@ -237,6 +237,6 @@ if __name__ == '__main__':
if result != testcase_base.TestcaseBase.EX_OK:
sys.exit(result)
if args['pushtodb']:
- sys.exit(odl.publish_report())
+ sys.exit(odl.push_to_db())
except Exception:
sys.exit(testcase_base.TestcaseBase.EX_RUN_ERROR)
diff --git a/functest/tests/unit/core/test_testcase_base.py b/functest/tests/unit/core/test_testcase_base.py
index 94d2e966b..b7c81d87c 100644
--- a/functest/tests/unit/core/test_testcase_base.py
+++ b/functest/tests/unit/core/test_testcase_base.py
@@ -9,7 +9,6 @@
import logging
import mock
-import os
import unittest
from functest.core import testcase_base
@@ -32,12 +31,11 @@ class TestcaseBaseTesting(unittest.TestCase):
self.assertEqual(self.test.run(),
testcase_base.TestcaseBase.EX_RUN_ERROR)
- @mock.patch.dict(os.environ, {})
@mock.patch('functest.utils.functest_utils.push_results_to_db',
return_value=False)
def _test_missing_attribute(self, mock_function):
- self.assertEqual(self.test.publish_report(),
- testcase_base.TestcaseBase.EX_PUBLISH_RESULT_FAILED)
+ self.assertEqual(self.test.push_to_db(),
+ testcase_base.TestcaseBase.EX_PUSH_TO_DB_ERROR)
mock_function.assert_not_called()
def test_missing_case_name(self):
@@ -70,7 +68,7 @@ class TestcaseBaseTesting(unittest.TestCase):
return_value=False)
def test_push_to_db_failed(self, mock_function):
self.assertEqual(self.test.push_to_db(),
- testcase_base.TestcaseBase.EX_PUBLISH_RESULT_FAILED)
+ testcase_base.TestcaseBase.EX_PUSH_TO_DB_ERROR)
mock_function.assert_called_once_with(
self.test.project, self.test.case_name, self.test.start_time,
self.test.stop_time, self.test.criteria, self.test.details)
diff --git a/functest/utils/decorators.py b/functest/utils/decorators.py
new file mode 100644
index 000000000..99bcef3e6
--- /dev/null
+++ b/functest/utils/decorators.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import mock
+import requests.sessions
+import urlparse
+
+
+def can_dump_request_to_file(method):
+
+ def dump_preparedrequest(request, **kwargs):
+ parseresult = urlparse.urlparse(request.url)
+ if parseresult.scheme == "file":
+ with open(parseresult.path.replace('/results', ''), 'a') as f:
+ headers = ""
+ for key in request.headers:
+ headers += key + " " + request.headers[key] + "\n"
+ message = "{} {}\n{}\n{}\n\n\n".format(
+ request.method, request.url, headers, request.body)
+ f.write(message)
+ return mock.Mock()
+
+ def patch_request(method, url, **kwargs):
+ with requests.sessions.Session() as session:
+ parseresult = urlparse.urlparse(url)
+ if parseresult.scheme == "file":
+ with mock.patch.object(
+ session, 'send', side_effect=dump_preparedrequest):
+ return session.request(method=method, url=url, **kwargs)
+ else:
+ return session.request(method=method, url=url, **kwargs)
+
+ def hook(*args, **kwargs):
+ with mock.patch('requests.api.request', side_effect=patch_request):
+ return method(*args, **kwargs)
+
+ return hook
diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py
index 040554642..12d8e902f 100644
--- a/functest/utils/functest_utils.py
+++ b/functest/utils/functest_utils.py
@@ -23,10 +23,9 @@ import requests
import yaml
from git import Repo
-from functest.utils.constants import CONST
+from functest.utils import decorators
import functest.utils.functest_logger as ft_logger
-
logger = ft_logger.Logger("functest_utils").getLogger()
@@ -184,43 +183,14 @@ def logger_test_results(project, case_name, status, details):
'd': details})
-def write_results_to_file(project, case_name, start_date,
- stop_date, criteria, details):
- file_path = re.split(r'://', CONST.results_test_db_url)[1]
-
- try:
- installer = os.environ['INSTALLER_TYPE']
- scenario = os.environ['DEPLOY_SCENARIO']
- pod_name = os.environ['NODE_NAME']
- except KeyError as e:
- logger.error("Please set env var: " + str(e))
- return False
-
- 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,
- "scenario": scenario, "criteria": criteria,
- "start_date": test_start, "stop_date": test_stop,
- "details": details}
- try:
- with open(file_path, "a+w") as outfile:
- json.dump(params, outfile)
- outfile.write("\n")
- return True
- except Exception as e:
- logger.error("write result data into a file failed: %s" % e)
- return False
-
-
+@decorators.can_dump_request_to_file
def push_results_to_db(project, case_name,
start_date, stop_date, criteria, details):
"""
POST results to the Result target DB
"""
# Retrieve params from CI and conf
- url = CONST.results_test_db_url + "/results"
+ url = get_db_url() + "/results"
try:
installer = os.environ['INSTALLER_TYPE']