From 0d39b466bf9530b1bc1c814b7b0db4ab287bf0f5 Mon Sep 17 00:00:00 2001 From: Venkata Harshavardhan Reddy Allu Date: Tue, 4 Sep 2018 23:23:31 +0530 Subject: Refactor unit tests in 'test_test_utils.py' JIRA: SFC-126 JIRA: SFC-129 Some of the unit tests I wrote earlier needs to be separated into individual unit tests, which Mr Dimitrios has mentioned in one of my previous commits. Unit tests are supposed to be independent & should only test one test case at a time. This patch fixes them. Change-Id: I1ccecf049645fe4996d8b4fa5425d485727e1ae0 Signed-off-by: Venkata Harshavardhan Reddy Allu --- sfc/unit_tests/unit/lib/test_test_utils.py | 67 ++++++++++++++++++------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/sfc/unit_tests/unit/lib/test_test_utils.py b/sfc/unit_tests/unit/lib/test_test_utils.py index f973b094..f26f8df9 100644 --- a/sfc/unit_tests/unit/lib/test_test_utils.py +++ b/sfc/unit_tests/unit/lib/test_test_utils.py @@ -191,46 +191,61 @@ class SfcTestUtilsTesting(unittest.TestCase): @patch('time.sleep', autospec=True) @patch('sfc.lib.test_utils.logger', autospec=True) @patch('sfc.lib.test_utils.run_cmd_remote', autospec=True) - def test_start_http_server_returned_false(self, - mock_run_cmd_remote, - mock_log, - mock_sleep): + def test_start_http_server_returned_false_failed_to_start( + self, mock_run_cmd_remote, mock_log, mock_sleep): """ Checks the proper functionality of start_http_server - function when port 80 is down + function when http_server is failed to start """ cmd = "\'python -m SimpleHTTPServer 80 " + \ "> /dev/null 2>&1 &\'" - sleep_calls = [[call(3)], - [call(5)]] + rcr_calls = [call(self.ip, cmd), + call(self.ip, 'ps aux | grep SimpleHTTPServer')] + log_calls = [call('Failed to start http server')] - rcr_calls = [[call(self.ip, cmd), - call(self.ip, 'ps aux | grep SimpleHTTPServer')], - [call(self.ip, 'netstat -pntl | grep :80')]] + mock_run_cmd_remote.side_effect = [('', '', ''), + ('', '', '')] - log_calls = [[call('Failed to start http server')], - [call('output')], - [call('Port 80 is not up yet')]] + result = test_utils.start_http_server(self.ip, 1) + self.assertFalse(result) + mock_run_cmd_remote.assert_has_calls(rcr_calls) + mock_sleep.assert_called_once_with(3) + mock_log.error.assert_has_calls(log_calls) + mock_log.info.assert_not_called() + mock_log.debug.assert_not_called() + + @patch('time.sleep', autospec=True) + @patch('sfc.lib.test_utils.logger', autospec=True) + @patch('sfc.lib.test_utils.run_cmd_remote', autospec=True) + def test_start_http_server_returned_false_port_is_down( + self, mock_run_cmd_remote, mock_log, mock_sleep): + """ + Checks the proper functionality of start_http_server + function when port 80 is down + """ + + cmd = "\'python -m SimpleHTTPServer 80 " + \ + "> /dev/null 2>&1 &\'" + + rcr_calls = [call(self.ip, cmd), + call(self.ip, 'ps aux | grep SimpleHTTPServer'), + call(self.ip, 'netstat -pntl | grep :80')] + + log_calls = [call('output'), + call('Port 80 is not up yet')] mock_run_cmd_remote.side_effect = [('', '', ''), - ('', '', ''), - ('', '', ''), ('', 'output', ''), ('', '', '')] - self.assertFalse(test_utils.start_http_server(self.ip, 1)) - mock_run_cmd_remote.assert_has_calls(rcr_calls[0]) - mock_sleep.assert_has_calls(sleep_calls[0]) - mock_log.error.assert_has_calls(log_calls[0]) - mock_log.info.assert_not_called() - mock_log.debug.assert_not_called() - self.assertFalse(test_utils.start_http_server(self.ip, 1)) - mock_run_cmd_remote.assert_has_calls(rcr_calls[0] + rcr_calls[1]) - mock_sleep.assert_has_calls(sleep_calls[0] + sleep_calls[1]) - mock_log.info.assert_has_calls(log_calls[1]) - mock_log.debug.assert_has_calls(log_calls[2]) + result = test_utils.start_http_server(self.ip, 1) + self.assertFalse(result) + mock_run_cmd_remote.assert_has_calls(rcr_calls) + mock_sleep.assert_called_with(5) + mock_log.info.assert_has_calls(log_calls[:1]) + mock_log.debug.assert_has_calls(log_calls[1:]) @patch('time.sleep', autospec=True) @patch('sfc.lib.test_utils.logger', autospec=True) -- cgit 1.2.3-korg