4 # Copyright (C) 2013 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for testing ganeti.bootstrap"""
28 from ganeti import bootstrap
29 from ganeti import constants
30 from ganeti import errors
31 from ganeti import pathutils
37 class TestPrepareFileStorage(unittest.TestCase):
39 unittest.TestCase.setUp(self)
40 self.tmpdir = tempfile.mkdtemp()
43 shutil.rmtree(self.tmpdir)
45 def enableFileStorage(self, enable):
46 self.enabled_disk_templates = []
48 self.enabled_disk_templates.append(constants.DT_FILE)
50 # anything != DT_FILE would do here
51 self.enabled_disk_templates.append(constants.DT_DISKLESS)
53 def testFallBackToDefaultPathAcceptedFileStorageEnabled(self):
54 expected_file_storage_dir = pathutils.DEFAULT_FILE_STORAGE_DIR
55 acceptance_fn = mock.Mock()
56 init_fn = mock.Mock(return_value=expected_file_storage_dir)
57 self.enableFileStorage(True)
58 file_storage_dir = bootstrap._PrepareFileStorage(
59 self.enabled_disk_templates, None, acceptance_fn=acceptance_fn,
61 self.assertEqual(expected_file_storage_dir, file_storage_dir)
62 acceptance_fn.assert_called_with(expected_file_storage_dir)
63 init_fn.assert_called_with(expected_file_storage_dir)
65 def testPathAcceptedFileStorageEnabled(self):
66 acceptance_fn = mock.Mock()
67 init_fn = mock.Mock(return_value=self.tmpdir)
68 self.enableFileStorage(True)
69 file_storage_dir = bootstrap._PrepareFileStorage(
70 self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
72 self.assertEqual(self.tmpdir, file_storage_dir)
73 acceptance_fn.assert_called_with(self.tmpdir)
74 init_fn.assert_called_with(self.tmpdir)
76 def testPathAcceptedFileStorageDisabled(self):
77 acceptance_fn = mock.Mock()
79 self.enableFileStorage(False)
80 file_storage_dir = bootstrap._PrepareFileStorage(
81 self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
83 self.assertEqual(self.tmpdir, file_storage_dir)
84 self.assertFalse(init_fn.called)
85 self.assertFalse(acceptance_fn.called)
87 def testPathNotAccepted(self):
88 acceptance_fn = mock.Mock()
89 acceptance_fn.side_effect = errors.FileStoragePathError
91 self.enableFileStorage(True)
92 self.assertRaises(errors.OpPrereqError, bootstrap._PrepareFileStorage,
93 self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
95 acceptance_fn.assert_called_with(self.tmpdir)
98 class TestInitCheckEnabledDiskTemplates(unittest.TestCase):
99 def testValidTemplates(self):
100 enabled_disk_templates = list(constants.DISK_TEMPLATES)
101 bootstrap._InitCheckEnabledDiskTemplates(enabled_disk_templates)
103 def testInvalidTemplates(self):
104 enabled_disk_templates = ["pinkbunny"]
105 self.assertRaises(errors.OpPrereqError,
106 bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
108 def testEmptyTemplates(self):
109 enabled_disk_templates = []
110 self.assertRaises(errors.OpPrereqError,
111 bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
114 if __name__ == "__main__":
115 testutils.GanetiTestProgram()