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.
20 from snaps import file_utils
22 __author__ = 'spisarski'
25 class FileUtilsTests(unittest.TestCase):
27 Tests the methods in file_utils.py
31 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
32 self.tmpDir = 'tmp/' + str(guid)
33 if not os.path.exists(self.tmpDir):
34 os.makedirs(self.tmpDir)
36 self.tmpFile = self.tmpDir + '/bar.txt'
37 if not os.path.exists(self.tmpFile):
38 open(self.tmpFile, 'wb')
41 if os.path.exists(self.tmpDir) and os.path.isdir(self.tmpDir):
42 shutil.rmtree(self.tmpDir)
44 def testFileIsDirectory(self):
46 Ensure the file_utils.fileExists() method returns false with a directory
48 result = file_utils.file_exists(self.tmpDir)
49 self.assertFalse(result)
50 # TODO - Cleanup directory
52 def testFileNotExist(self):
54 Ensure the file_utils.fileExists() method returns false with a bogus file
56 result = file_utils.file_exists('/foo/bar.txt')
57 self.assertFalse(result)
59 def testFileExists(self):
61 Ensure the file_utils.fileExists() method returns false with a directory
63 if not os.path.exists(self.tmpFile):
64 os.makedirs(self.tmpFile)
66 result = file_utils.file_exists(self.tmpFile)
67 self.assertTrue(result)
69 def testDownloadBadUrl(self):
71 Tests the file_utils.download() method when given a bad URL
73 with self.assertRaises(Exception):
74 file_utils.download('http://bunkUrl.com/foo/bar.iso', self.tmpDir)
76 def testDownloadBadDir(self):
78 Tests the file_utils.download() method when given a bad URL
80 with self.assertRaises(Exception):
81 file_utils.download('http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img', '/foo/bar')
83 def testCirrosImageDownload(self):
85 Tests the file_utils.download() method when given a good Cirros QCOW2 URL
87 image_file = file_utils.download('http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img',
89 self.assertIsNotNone(image_file)
90 self.assertTrue(image_file.name.endswith("cirros-0.3.4-x86_64-disk.img"))
91 self.assertTrue(image_file.name.startswith(self.tmpDir))
93 def testReadOSEnvFile(self):
95 Tests that the OS Environment file is correctly parsed
98 os_env_dict = file_utils.read_os_env_file('openstack/tests/conf/overcloudrc_test')
99 self.assertEqual('test_pw', os_env_dict['OS_PASSWORD'])
100 self.assertEqual('http://foo:5000/v2.0/', os_env_dict['OS_AUTH_URL'])
101 self.assertEqual('admin', os_env_dict['OS_USERNAME'])
102 self.assertEqual('admin', os_env_dict['OS_TENANT_NAME'])