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