X-Git-Url: https://gerrit.onap.org/r/gitweb?p=multicloud%2Fframework.git;a=blobdiff_plain;f=multivimbroker%2Fmultivimbroker%2Ftests%2Ftest_fileutil.py;h=14e01d5d6f071d5050f3011e8d65c82987f092fc;hp=8eb4a5240d90b99ae61fbafb71464a56f35ed270;hb=7c194de2d67a1c71b741345a74ed9a493936fdb0;hpb=69433c339b8445665e3ef717604de485c763d7d4 diff --git a/multivimbroker/multivimbroker/tests/test_fileutil.py b/multivimbroker/multivimbroker/tests/test_fileutil.py index 8eb4a52..14e01d5 100644 --- a/multivimbroker/multivimbroker/tests/test_fileutil.py +++ b/multivimbroker/multivimbroker/tests/test_fileutil.py @@ -24,3 +24,56 @@ class TestFileutil(unittest.TestCase): mock_exists.return_value = True fileutil.make_dirs(new_path) mock_mkdir.assert_not_called() + + @mock.patch.object(os.path, "exists") + @mock.patch("os.makedirs") + def test_make_dirs_path_not_exists(self, mock_mkdir, mock_exists): + new_path = "/tmp/test" + mock_exists.return_value = False + fileutil.make_dirs(new_path) + mock_mkdir.assert_called_once_with(new_path, 0o777) + + @mock.patch.object(os.path, "exists") + @mock.patch("shutil.rmtree") + def test_delete_dirs_success(self, mock_rmtree, mock_exists): + mock_exists.return_value = True + new_path = "/tmp/tests" + fileutil.delete_dirs(new_path) + mock_rmtree.assert_called_once_with(new_path) + + @mock.patch.object(os.path, "exists") + @mock.patch("shutil.rmtree") + def test_delete_dirs_failed(self, mock_rmtree, mock_exists): + mock_exists.return_value = True + mock_rmtree.side_effect = [Exception("Fake exception")] + new_path = "/tmp/tests" + fileutil.delete_dirs(new_path) + mock_rmtree.assert_called_once_with(new_path) + + @mock.patch.object(fileutil, "make_dirs") + @mock.patch("urllib.request.urlopen") + def test_download_file_from_http_success(self, mock_urlopen, mock_mkdir): + url = "http://www.example.org/test.dat" + local_dir = "/tmp/" + file_name = "test.dat" + mock_req = mock.Mock() + mock_req.read.return_value = "hello world".encode() + mock_urlopen.return_value = mock_req + m = mock.mock_open() + expect_ret = (True, "/tmp/test.dat") + with mock.patch('{}.open'.format(__name__), m, create=True): + ret = fileutil.download_file_from_http(url, local_dir, file_name) + self.assertEqual(expect_ret, ret) + + @mock.patch.object(fileutil, "make_dirs") + @mock.patch("urllib.request.urlopen") + def test_download_file_from_http_fail(self, mock_urlopen, mock_mkdir): + url = "http://www.example.org/test.dat" + local_dir = "/tmp/" + file_name = "test.dat" + mock_req = mock.Mock() + mock_req.read.return_value = "hello world".encode() + mock_urlopen.side_effect = [Exception("fake exception")] + expect_ret = (False, "/tmp/test.dat") + ret = fileutil.download_file_from_http(url, local_dir, file_name) + self.assertEqual(expect_ret, ret)