gnt-cluster modify: ipolicy vs enabled disk templates
[ganeti-local] / test / py / ganeti.bootstrap_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2013 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.bootstrap"""
23
24 import shutil
25 import tempfile
26 import unittest
27
28 from ganeti import bootstrap
29 from ganeti import constants
30 from ganeti import errors
31 from ganeti import pathutils
32
33 import testutils
34 import mock
35
36
37 class TestPrepareFileStorage(unittest.TestCase):
38   def setUp(self):
39     unittest.TestCase.setUp(self)
40     self.tmpdir = tempfile.mkdtemp()
41
42   def tearDown(self):
43     shutil.rmtree(self.tmpdir)
44
45   def enableFileStorage(self, enable):
46     self.enabled_disk_templates = []
47     if enable:
48       self.enabled_disk_templates.append(constants.DT_FILE)
49     else:
50       # anything != DT_FILE would do here
51       self.enabled_disk_templates.append(constants.DT_DISKLESS)
52
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,
60         init_fn=init_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)
64
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,
71         init_fn=init_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)
75
76   def testPathAcceptedFileStorageDisabled(self):
77     acceptance_fn = mock.Mock()
78     init_fn = mock.Mock()
79     self.enableFileStorage(False)
80     file_storage_dir = bootstrap._PrepareFileStorage(
81         self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
82         init_fn=init_fn)
83     self.assertEqual(self.tmpdir, file_storage_dir)
84     self.assertFalse(init_fn.called)
85     self.assertFalse(acceptance_fn.called)
86
87   def testPathNotAccepted(self):
88     acceptance_fn = mock.Mock()
89     acceptance_fn.side_effect = errors.FileStoragePathError
90     init_fn = mock.Mock()
91     self.enableFileStorage(True)
92     self.assertRaises(errors.OpPrereqError, bootstrap._PrepareFileStorage,
93         self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
94         init_fn=init_fn)
95     acceptance_fn.assert_called_with(self.tmpdir)
96
97
98 class TestInitCheckEnabledDiskTemplates(unittest.TestCase):
99   def testValidTemplates(self):
100     enabled_disk_templates = list(constants.DISK_TEMPLATES)
101     bootstrap._InitCheckEnabledDiskTemplates(enabled_disk_templates)
102
103   def testInvalidTemplates(self):
104     enabled_disk_templates = ["pinkbunny"]
105     self.assertRaises(errors.OpPrereqError,
106         bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
107
108   def testEmptyTemplates(self):
109     enabled_disk_templates = []
110     self.assertRaises(errors.OpPrereqError,
111         bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
112
113
114 if __name__ == "__main__":
115   testutils.GanetiTestProgram()