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
41 if not os.path.exists(self.tmpFile):
42 self.tmp_file_opened = open(self.tmpFile, 'wb')
45 if self.tmp_file_opened:
46 self.tmp_file_opened.close()
48 if os.path.exists(self.test_dir) and os.path.isdir(self.test_dir):
49 shutil.rmtree(self.tmp_dir)
51 def testFileIsDirectory(self):
53 Ensure the file_utils.fileExists() method returns false with a
56 result = file_utils.file_exists(self.test_dir)
57 self.assertFalse(result)
59 def testFileNotExist(self):
61 Ensure the file_utils.fileExists() method returns false with a bogus
64 result = file_utils.file_exists('/foo/bar.txt')
65 self.assertFalse(result)
67 def testFileExists(self):
69 Ensure the file_utils.fileExists() method returns false with a
72 if not os.path.exists(self.tmpFile):
73 os.makedirs(self.tmpFile)
75 result = file_utils.file_exists(self.tmpFile)
76 self.assertTrue(result)
78 def testDownloadBadUrl(self):
80 Tests the file_utils.download() method when given a bad URL
82 with self.assertRaises(Exception):
83 file_utils.download('http://bunkUrl.com/foo/bar.iso',
86 def testDownloadBadDir(self):
88 Tests the file_utils.download() method when given a bad URL
90 with self.assertRaises(Exception):
91 file_utils.download(openstack_tests.CIRROS_DEFAULT_IMAGE_URL,
94 def testCirrosImageDownload(self):
96 Tests the file_utils.download() method when given a good Cirros QCOW2
99 image_file = file_utils.download(
100 openstack_tests.CIRROS_DEFAULT_IMAGE_URL, self.test_dir)
101 self.assertIsNotNone(image_file)
103 image_file.name.endswith("cirros-0.3.4-x86_64-disk.img"))
104 self.assertTrue(image_file.name.startswith(self.test_dir))
106 def testReadOSEnvFile(self):
108 Tests that the OS Environment file is correctly parsed
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'])