From d22f950cae95350ae0b0518ceefb3bcfc4f17009 Mon Sep 17 00:00:00 2001 From: Cédric Ollivier Date: Thu, 27 Jun 2019 21:33:20 +0200 Subject: Rewrite kubernetes parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It now sets details and result thanks to 1 regex. It also prints the failure summary if needed. Change-Id: Ia89d6825134161cf6513924dac93281d677eada6 Signed-off-by: Cédric Ollivier --- functest_kubernetes/k8stest.py | 71 +++++++++++-------------------------- functest_kubernetes/test_k8stest.py | 22 +++--------- 2 files changed, 24 insertions(+), 69 deletions(-) (limited to 'functest_kubernetes') diff --git a/functest_kubernetes/k8stest.py b/functest_kubernetes/k8stest.py index 8a31d6c1..132cfb04 100644 --- a/functest_kubernetes/k8stest.py +++ b/functest_kubernetes/k8stest.py @@ -49,62 +49,32 @@ class K8sTesting(testcase.TestCase): boutput = process.stdout.read() with open(os.path.join(self.res_dir, 'e2e.log'), 'wb') as foutput: foutput.write(boutput) - output = boutput.decode("utf-8") - if ('Error loading client' in output or - 'Unexpected error' in output): - raise Exception(output) - - remarks = [] - lines = output.split('\n') - success = False - failure = False - i = 0 - while i < len(lines): - if '[Fail]' in lines[i] or 'Failures:' in lines[i]: - self.__logger.error(lines[i]) - if re.search(r'\[(.)*[0-9]+ seconds\]', lines[i]): - self.__logger.debug(lines[i]) - i = i + 1 - while i < len(lines) and lines[i] != '-' * len(lines[i]): - if lines[i].startswith('STEP:') or ('INFO:' in lines[i]): - break - self.__logger.debug(lines[i]) - i = i + 1 - if i >= len(lines): - break - success = 'SUCCESS!' in lines[i] - failure = 'FAIL!' in lines[i] - if success or failure: - if i != 0 and 'seconds' in lines[i - 1]: - remarks.append(lines[i - 1]) - remarks = remarks + lines[i].replace('--', '|').split('|') - break - i = i + 1 - - self.__logger.debug('-' * 10) - self.__logger.info("Remarks:") - for remark in remarks: - if 'seconds' in remark: - self.__logger.debug(remark) - elif 'Passed' in remark: - self.__logger.info("Passed: %s", remark.split()[0]) - elif 'Skipped' in remark: - self.__logger.info("Skipped: %s", remark.split()[0]) - elif 'Failed' in remark: - self.__logger.info("Failed: %s", remark.split()[0]) - - if success: - self.result = 100 - elif failure: - self.result = 0 + grp = re.search( + r'^(FAIL|SUCCESS)!.* ([0-9]+) Passed \| ([0-9]+) Failed \|' + r' ([0-9]+) Pending \| ([0-9]+) Skipped', boutput.decode("utf-8"), + re.MULTILINE | re.DOTALL) + assert grp + self.details['passed'] = int(grp.group(2)) + self.details['failed'] = int(grp.group(3)) + self.details['pending'] = int(grp.group(4)) + self.details['skipped'] = int(grp.group(5)) + self.__logger.debug("details: %s", self.details) + self.result = self.details['passed'] * 100 / ( + self.details['passed'] + self.details['failed'] + + self.details['pending']) + self.__logger.debug("result: %s", self.result) + if grp.group(1) == 'FAIL': + grp2 = re.search( + r'^(Summarizing [0-9]+ Failure.*)Ran', boutput.decode("utf-8"), + re.MULTILINE | re.DOTALL) + if grp2: + self.__logger.error(grp2.group(1)) def run(self, **kwargs): - if not os.path.isfile(self.config): self.__logger.error( "Cannot run k8s testcases. Config file not found") return self.EX_RUN_ERROR - self.start_time = time.time() try: self.run_kubetest() @@ -112,7 +82,6 @@ class K8sTesting(testcase.TestCase): except Exception: # pylint: disable=broad-except self.__logger.exception("Error with running kubetest:") res = self.EX_RUN_ERROR - self.stop_time = time.time() return res diff --git a/functest_kubernetes/test_k8stest.py b/functest_kubernetes/test_k8stest.py index a94fdf88..81d92853 100644 --- a/functest_kubernetes/test_k8stest.py +++ b/functest_kubernetes/test_k8stest.py @@ -49,29 +49,15 @@ class K8sTests(unittest.TestCase): with self.assertRaises(TypeError): self.k8stesting.run_kubetest() - @mock.patch('functest_kubernetes.k8stest.os.path.isfile') - def test_error_logging(self, mock_isfile): - # pylint: disable=unused-argument - with mock.patch('functest_kubernetes.k8stest.' - 'subprocess.Popen') as mock_popen, \ - mock.patch.object(self.k8stesting, - '_K8sTesting__logger') as mock_logger: - mock_stdout = mock.Mock() - attrs = {'stdout.read.return_value': 'Error loading client'} - mock_stdout.configure_mock(**attrs) - mock_popen.return_value = mock_stdout - self.k8stesting.run() - mock_logger.exception.assert_called_with( - "Error with running kubetest:") - + @mock.patch('re.search') @mock.patch('six.moves.builtins.open', mock.mock_open()) @mock.patch('functest_kubernetes.k8stest.os.path.isfile') @mock.patch('functest_kubernetes.k8stest.subprocess.Popen') - def test_run(self, mock_open, mock_isfile): + def test_run(self, *args): self.assertEquals(self.k8stesting.run(), testcase.TestCase.EX_OK) - mock_isfile.assert_called() - mock_open.assert_called() + for loop in range(3): + args[loop].assert_called() if __name__ == "__main__": -- cgit 1.2.3-korg