1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 from snaps import file_utils
22 from snaps.openstack.tests import openstack_tests
24 __author__ = 'spisarski'
27 class FileUtilsTests(unittest.TestCase):
29 Tests the methods in file_utils.py
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)
39 self.tmpFile = self.test_dir + '/bar.txt'
40 self.tmp_file_opened = None
43 if self.tmp_file_opened:
44 self.tmp_file_opened.close()
46 if os.path.exists(self.test_dir) and os.path.isdir(self.test_dir):
47 shutil.rmtree(self.tmp_dir)
49 def testFileIsDirectory(self):
51 Ensure the file_utils.fileExists() method returns false with a
54 result = file_utils.file_exists(self.test_dir)
55 self.assertFalse(result)
57 def testFileNotExist(self):
59 Ensure the file_utils.fileExists() method returns false with a bogus
62 result = file_utils.file_exists('/foo/bar.txt')
63 self.assertFalse(result)
65 def testFileExists(self):
67 Ensure the file_utils.fileExists() method returns false with a
70 if not os.path.exists(self.tmpFile):
71 self.tmp_file_opened = open(self.tmpFile, 'wb')
73 if not os.path.exists(self.tmpFile):
74 os.makedirs(self.tmpFile)
76 result = file_utils.file_exists(self.tmpFile)
77 self.assertTrue(result)
79 def testDownloadBadUrl(self):
81 Tests the file_utils.download() method when given a bad URL
83 with self.assertRaises(Exception):
84 file_utils.download('http://bunkUrl.com/foo/bar.iso',
87 def testDownloadBadDir(self):
89 Tests the file_utils.download() method when given a bad URL
91 with self.assertRaises(Exception):
92 file_utils.download(openstack_tests.CIRROS_DEFAULT_IMAGE_URL,
95 def testCirrosImageDownload(self):
97 Tests the file_utils.download() method when given a good Cirros QCOW2
100 image_file = file_utils.download(
101 openstack_tests.CIRROS_DEFAULT_IMAGE_URL, self.test_dir)
102 self.assertIsNotNone(image_file)
104 image_file.name.endswith("cirros-0.3.4-x86_64-disk.img"))
105 self.assertTrue(image_file.name.startswith(self.test_dir))
107 def testReadOSEnvFile(self):
109 Tests that the OS Environment file is correctly parsed
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'])
120 def test_write_str_to_file(self):
122 Ensure the file_utils.fileExists() method returns false with a
125 test_val = 'test string'
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)
134 file_contents = file_utils.read_file(self.tmpFile)
135 self.assertEqual(test_val, file_contents)