Ensure library and tests close all necessary resources.
[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         if not os.path.exists(self.tmpFile):
42             self.tmp_file_opened = open(self.tmpFile, 'wb')
43
44     def tearDown(self):
45         if self.tmp_file_opened:
46             self.tmp_file_opened.close()
47
48         if os.path.exists(self.test_dir) and os.path.isdir(self.test_dir):
49             shutil.rmtree(self.tmp_dir)
50
51     def testFileIsDirectory(self):
52         """
53         Ensure the file_utils.fileExists() method returns false with a
54         directory
55         """
56         result = file_utils.file_exists(self.test_dir)
57         self.assertFalse(result)
58
59     def testFileNotExist(self):
60         """
61         Ensure the file_utils.fileExists() method returns false with a bogus
62         file
63         """
64         result = file_utils.file_exists('/foo/bar.txt')
65         self.assertFalse(result)
66
67     def testFileExists(self):
68         """
69         Ensure the file_utils.fileExists() method returns false with a
70         directory
71         """
72         if not os.path.exists(self.tmpFile):
73             os.makedirs(self.tmpFile)
74
75         result = file_utils.file_exists(self.tmpFile)
76         self.assertTrue(result)
77
78     def testDownloadBadUrl(self):
79         """
80         Tests the file_utils.download() method when given a bad URL
81         """
82         with self.assertRaises(Exception):
83             file_utils.download('http://bunkUrl.com/foo/bar.iso',
84                                 self.test_dir)
85
86     def testDownloadBadDir(self):
87         """
88         Tests the file_utils.download() method when given a bad URL
89         """
90         with self.assertRaises(Exception):
91             file_utils.download(openstack_tests.CIRROS_DEFAULT_IMAGE_URL,
92                                 '/foo/bar')
93
94     def testCirrosImageDownload(self):
95         """
96         Tests the file_utils.download() method when given a good Cirros QCOW2
97         URL
98         """
99         image_file = file_utils.download(
100             openstack_tests.CIRROS_DEFAULT_IMAGE_URL, self.test_dir)
101         self.assertIsNotNone(image_file)
102         self.assertTrue(
103             image_file.name.endswith("cirros-0.3.4-x86_64-disk.img"))
104         self.assertTrue(image_file.name.startswith(self.test_dir))
105
106     def testReadOSEnvFile(self):
107         """
108         Tests that the OS Environment file is correctly parsed
109         :return:
110         """
111         rc_file_path = pkg_resources.resource_filename(
112             'snaps.openstack.tests.conf', 'overcloudrc_test')
113         os_env_dict = file_utils.read_os_env_file(rc_file_path)
114         self.assertEqual('test_pw', os_env_dict['OS_PASSWORD'])
115         self.assertEqual('http://foo:5000/v2.0/', os_env_dict['OS_AUTH_URL'])
116         self.assertEqual('admin', os_env_dict['OS_USERNAME'])
117         self.assertEqual('admin', os_env_dict['OS_TENANT_NAME'])