a28231bf1dbb587e0cdd328ca4b506163d547c6b
[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 unittest
17 import shutil
18 import uuid
19
20 from snaps import file_utils
21
22 __author__ = 'spisarski'
23
24
25 class FileUtilsTests(unittest.TestCase):
26     """
27     Tests the methods in file_utils.py
28     """
29
30     def setUp(self):
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)
35
36         self.tmpFile = self.tmpDir + '/bar.txt'
37         if not os.path.exists(self.tmpFile):
38             open(self.tmpFile, 'wb')
39
40     def tearDown(self):
41         if os.path.exists(self.tmpDir) and os.path.isdir(self.tmpDir):
42             shutil.rmtree(self.tmpDir)
43
44     def testFileIsDirectory(self):
45         """
46         Ensure the file_utils.fileExists() method returns false with a directory
47         """
48         result = file_utils.file_exists(self.tmpDir)
49         self.assertFalse(result)
50         # TODO - Cleanup directory
51
52     def testFileNotExist(self):
53         """
54         Ensure the file_utils.fileExists() method returns false with a bogus file
55         """
56         result = file_utils.file_exists('/foo/bar.txt')
57         self.assertFalse(result)
58
59     def testFileExists(self):
60         """
61         Ensure the file_utils.fileExists() method returns false with a directory
62         """
63         if not os.path.exists(self.tmpFile):
64             os.makedirs(self.tmpFile)
65
66         result = file_utils.file_exists(self.tmpFile)
67         self.assertTrue(result)
68
69     def testDownloadBadUrl(self):
70         """
71         Tests the file_utils.download() method when given a bad URL
72         """
73         with self.assertRaises(Exception):
74             file_utils.download('http://bunkUrl.com/foo/bar.iso', self.tmpDir)
75
76     def testDownloadBadDir(self):
77         """
78         Tests the file_utils.download() method when given a bad URL
79         """
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')
82
83     def testCirrosImageDownload(self):
84         """
85         Tests the file_utils.download() method when given a good Cirros QCOW2 URL
86         """
87         image_file = file_utils.download('http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img',
88                                          self.tmpDir)
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))
92
93     def testReadOSEnvFile(self):
94         """
95         Tests that the OS Environment file is correctly parsed
96         :return:
97         """
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'])