Added support for running tests in parallel.
[snaps.git] / snaps / tests / file_utils_tests.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import os
16 import pkg_resources
17 import unittest
18 import shutil
19 import uuid
20
21 from snaps import file_utils
22 from snaps.openstack.tests import openstack_tests
23
24 __author__ = 'spisarski'
25
26
27 class FileUtilsTests(unittest.TestCase):
28     """
29     Tests the methods in file_utils.py
30     """
31
32     def setUp(self):
33         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
34         self.tmp_dir = 'tmp/'
35         self.test_dir = self.tmp_dir + str(guid)
36         if not os.path.exists(self.test_dir):
37             os.makedirs(self.test_dir)
38
39         self.tmpFile = self.test_dir + '/bar.txt'
40         self.tmp_file_opened = None
41
42     def tearDown(self):
43         if self.tmp_file_opened:
44             self.tmp_file_opened.close()
45
46         if os.path.exists(self.test_dir) and os.path.isdir(self.test_dir):
47             shutil.rmtree(self.test_dir)
48
49     def testFileIsDirectory(self):
50         """
51         Ensure the file_utils.fileExists() method returns false with a
52         directory
53         """
54         result = file_utils.file_exists(self.test_dir)
55         self.assertFalse(result)
56
57     def testFileNotExist(self):
58         """
59         Ensure the file_utils.fileExists() method returns false with a bogus
60         file
61         """
62         result = file_utils.file_exists('/foo/bar.txt')
63         self.assertFalse(result)
64
65     def testFileExists(self):
66         """
67         Ensure the file_utils.fileExists() method returns false with a
68         directory
69         """
70         if not os.path.exists(self.tmpFile):
71             self.tmp_file_opened = open(self.tmpFile, 'wb')
72
73         if not os.path.exists(self.tmpFile):
74             os.makedirs(self.tmpFile)
75
76         result = file_utils.file_exists(self.tmpFile)
77         self.assertTrue(result)
78
79     def testDownloadBadUrl(self):
80         """
81         Tests the file_utils.download() method when given a bad URL
82         """
83         with self.assertRaises(Exception):
84             file_utils.download('http://bunkUrl.com/foo/bar.iso',
85                                 self.test_dir)
86
87     def testDownloadBadDir(self):
88         """
89         Tests the file_utils.download() method when given a bad URL
90         """
91         with self.assertRaises(Exception):
92             file_utils.download(openstack_tests.CIRROS_DEFAULT_IMAGE_URL,
93                                 '/foo/bar')
94
95     def testCirrosImageDownload(self):
96         """
97         Tests the file_utils.download() method when given a good Cirros QCOW2
98         URL
99         """
100         image_file = file_utils.download(
101             openstack_tests.CIRROS_DEFAULT_IMAGE_URL, self.test_dir)
102         self.assertIsNotNone(image_file)
103         self.assertTrue(
104             image_file.name.endswith("cirros-0.3.4-x86_64-disk.img"))
105         self.assertTrue(image_file.name.startswith(self.test_dir))
106
107     def testReadOSEnvFile(self):
108         """
109         Tests that the OS Environment file is correctly parsed
110         :return:
111         """
112         rc_file_path = pkg_resources.resource_filename(
113             'snaps.openstack.tests.conf', 'overcloudrc_test')
114         os_env_dict = file_utils.read_os_env_file(rc_file_path)
115         self.assertEqual('test_pw', os_env_dict['OS_PASSWORD'])
116         self.assertEqual('http://foo:5000/v2.0/', os_env_dict['OS_AUTH_URL'])
117         self.assertEqual('admin', os_env_dict['OS_USERNAME'])
118         self.assertEqual('admin', os_env_dict['OS_TENANT_NAME'])
119
120     def test_write_str_to_file(self):
121         """
122         Ensure the file_utils.fileExists() method returns false with a
123         directory
124         """
125         test_val = 'test string'
126
127         test_file = file_utils.save_string_to_file(
128             test_val, self.tmpFile)
129         result1 = file_utils.file_exists(self.tmpFile)
130         self.assertTrue(result1)
131         result2 = file_utils.file_exists(test_file.name)
132         self.assertTrue(result2)
133
134         file_contents = file_utils.read_file(self.tmpFile)
135         self.assertEqual(test_val, file_contents)