9840bde3c97962aecb5f725a7df128caf4f5ea14
[multicloud/framework.git] / multivimbroker / multivimbroker / tests / test_fileutil.py
1 # Copyright (c) 2017-2018 VMware, Inc.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
11 import mock
12 import os
13 import unittest
14
15 from multivimbroker.pub.utils import fileutil
16
17
18 class TestFileutil(unittest.TestCase):
19
20     @mock.patch.object(os.path, "exists")
21     @mock.patch("os.makedirs")
22     def test_make_dirs_path_exists(self, mock_mkdir, mock_exists):
23         new_path = "/tmp/test"
24         mock_exists.return_value = True
25         fileutil.make_dirs(new_path)
26         mock_mkdir.assert_not_called()
27
28     @mock.patch.object(os.path, "exists")
29     @mock.patch("os.makedirs")
30     def test_make_dirs_path_not_exists(self, mock_mkdir, mock_exists):
31         new_path = "/tmp/test"
32         mock_exists.return_value = False
33         fileutil.make_dirs(new_path)
34         mock_mkdir.assert_called_once_with(new_path, 0777)
35
36     @mock.patch.object(os.path, "exists")
37     @mock.patch("shutil.rmtree")
38     def test_delete_dirs_success(self, mock_rmtree, mock_exists):
39         mock_exists.return_value = True
40         new_path = "/tmp/tests"
41         fileutil.delete_dirs(new_path)
42         mock_rmtree.assert_called_once_with(new_path)
43
44     @mock.patch.object(os.path, "exists")
45     @mock.patch("shutil.rmtree")
46     def test_delete_dirs_failed(self, mock_rmtree, mock_exists):
47         mock_exists.return_value = True
48         mock_rmtree.side_effect = [Exception("Fake exception")]
49         new_path = "/tmp/tests"
50         fileutil.delete_dirs(new_path)
51         mock_rmtree.assert_called_once_with(new_path)
52
53     @mock.patch.object(fileutil, "make_dirs")
54     @mock.patch("urllib2.urlopen")
55     def test_download_file_from_http_success(self, mock_urlopen, mock_mkdir):
56         url = "http://www.example.org/test.dat"
57         local_dir = "/tmp/"
58         file_name = "test.dat"
59         mock_req = mock.Mock()
60         mock_req.read.return_value = "hello world"
61         mock_urlopen.return_value = mock_req
62         m = mock.mock_open()
63         expect_ret = (True, "/tmp/test.dat")
64         with mock.patch('{}.open'.format(__name__), m, create=True):
65             ret = fileutil.download_file_from_http(url, local_dir, file_name)
66             self.assertEqual(expect_ret, ret)
67
68     @mock.patch.object(fileutil, "make_dirs")
69     @mock.patch("urllib2.urlopen")
70     def test_download_file_from_http_fail(self, mock_urlopen, mock_mkdir):
71         url = "http://www.example.org/test.dat"
72         local_dir = "/tmp/"
73         file_name = "test.dat"
74         mock_req = mock.Mock()
75         mock_req.read.return_value = "hello world"
76         mock_urlopen.side_effect = [Exception("fake exception")]
77         expect_ret = (False, "/tmp/test.dat")
78         ret = fileutil.download_file_from_http(url, local_dir, file_name)
79         self.assertEqual(expect_ret, ret)