Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.bootstrap_unittest.py @ 560ef132

History | View | Annotate | Download (6.5 kB)

1 5030cff3 Helga Velroyen
#!/usr/bin/python
2 5030cff3 Helga Velroyen
#
3 5030cff3 Helga Velroyen
4 5030cff3 Helga Velroyen
# Copyright (C) 2013 Google Inc.
5 5030cff3 Helga Velroyen
#
6 5030cff3 Helga Velroyen
# This program is free software; you can redistribute it and/or modify
7 5030cff3 Helga Velroyen
# it under the terms of the GNU General Public License as published by
8 5030cff3 Helga Velroyen
# the Free Software Foundation; either version 2 of the License, or
9 5030cff3 Helga Velroyen
# (at your option) any later version.
10 5030cff3 Helga Velroyen
#
11 5030cff3 Helga Velroyen
# This program is distributed in the hope that it will be useful, but
12 5030cff3 Helga Velroyen
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 5030cff3 Helga Velroyen
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 5030cff3 Helga Velroyen
# General Public License for more details.
15 5030cff3 Helga Velroyen
#
16 5030cff3 Helga Velroyen
# You should have received a copy of the GNU General Public License
17 5030cff3 Helga Velroyen
# along with this program; if not, write to the Free Software
18 5030cff3 Helga Velroyen
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 5030cff3 Helga Velroyen
# 02110-1301, USA.
20 5030cff3 Helga Velroyen
21 5030cff3 Helga Velroyen
22 5030cff3 Helga Velroyen
"""Script for testing ganeti.bootstrap"""
23 5030cff3 Helga Velroyen
24 5030cff3 Helga Velroyen
import shutil
25 5030cff3 Helga Velroyen
import tempfile
26 5030cff3 Helga Velroyen
import unittest
27 5030cff3 Helga Velroyen
28 5030cff3 Helga Velroyen
from ganeti import bootstrap
29 5030cff3 Helga Velroyen
from ganeti import constants
30 af95196c Helga Velroyen
from ganeti.storage import drbd
31 5030cff3 Helga Velroyen
from ganeti import errors
32 3039e2dc Helga Velroyen
from ganeti import pathutils
33 5030cff3 Helga Velroyen
34 5030cff3 Helga Velroyen
import testutils
35 3039e2dc Helga Velroyen
import mock
36 5030cff3 Helga Velroyen
37 5030cff3 Helga Velroyen
38 5030cff3 Helga Velroyen
class TestPrepareFileStorage(unittest.TestCase):
39 5030cff3 Helga Velroyen
  def setUp(self):
40 3039e2dc Helga Velroyen
    unittest.TestCase.setUp(self)
41 5030cff3 Helga Velroyen
    self.tmpdir = tempfile.mkdtemp()
42 5030cff3 Helga Velroyen
43 5030cff3 Helga Velroyen
  def tearDown(self):
44 5030cff3 Helga Velroyen
    shutil.rmtree(self.tmpdir)
45 5030cff3 Helga Velroyen
46 3039e2dc Helga Velroyen
  def enableFileStorage(self, enable):
47 3039e2dc Helga Velroyen
    self.enabled_disk_templates = []
48 3039e2dc Helga Velroyen
    if enable:
49 3039e2dc Helga Velroyen
      self.enabled_disk_templates.append(constants.DT_FILE)
50 3039e2dc Helga Velroyen
    else:
51 3039e2dc Helga Velroyen
      # anything != DT_FILE would do here
52 3039e2dc Helga Velroyen
      self.enabled_disk_templates.append(constants.DT_DISKLESS)
53 3039e2dc Helga Velroyen
54 3039e2dc Helga Velroyen
  def testFallBackToDefaultPathAcceptedFileStorageEnabled(self):
55 3039e2dc Helga Velroyen
    expected_file_storage_dir = pathutils.DEFAULT_FILE_STORAGE_DIR
56 3039e2dc Helga Velroyen
    acceptance_fn = mock.Mock()
57 3039e2dc Helga Velroyen
    init_fn = mock.Mock(return_value=expected_file_storage_dir)
58 3039e2dc Helga Velroyen
    self.enableFileStorage(True)
59 5030cff3 Helga Velroyen
    file_storage_dir = bootstrap._PrepareFileStorage(
60 3039e2dc Helga Velroyen
        self.enabled_disk_templates, None, acceptance_fn=acceptance_fn,
61 3039e2dc Helga Velroyen
        init_fn=init_fn)
62 3039e2dc Helga Velroyen
    self.assertEqual(expected_file_storage_dir, file_storage_dir)
63 3039e2dc Helga Velroyen
    acceptance_fn.assert_called_with(expected_file_storage_dir)
64 3039e2dc Helga Velroyen
    init_fn.assert_called_with(expected_file_storage_dir)
65 3039e2dc Helga Velroyen
66 3039e2dc Helga Velroyen
  def testPathAcceptedFileStorageEnabled(self):
67 3039e2dc Helga Velroyen
    acceptance_fn = mock.Mock()
68 3039e2dc Helga Velroyen
    init_fn = mock.Mock(return_value=self.tmpdir)
69 3039e2dc Helga Velroyen
    self.enableFileStorage(True)
70 3039e2dc Helga Velroyen
    file_storage_dir = bootstrap._PrepareFileStorage(
71 3039e2dc Helga Velroyen
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
72 3039e2dc Helga Velroyen
        init_fn=init_fn)
73 5030cff3 Helga Velroyen
    self.assertEqual(self.tmpdir, file_storage_dir)
74 3039e2dc Helga Velroyen
    acceptance_fn.assert_called_with(self.tmpdir)
75 3039e2dc Helga Velroyen
    init_fn.assert_called_with(self.tmpdir)
76 5030cff3 Helga Velroyen
77 3039e2dc Helga Velroyen
  def testPathAcceptedFileStorageDisabled(self):
78 3039e2dc Helga Velroyen
    acceptance_fn = mock.Mock()
79 3039e2dc Helga Velroyen
    init_fn = mock.Mock()
80 3039e2dc Helga Velroyen
    self.enableFileStorage(False)
81 5030cff3 Helga Velroyen
    file_storage_dir = bootstrap._PrepareFileStorage(
82 3039e2dc Helga Velroyen
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
83 3039e2dc Helga Velroyen
        init_fn=init_fn)
84 3039e2dc Helga Velroyen
    self.assertEqual(self.tmpdir, file_storage_dir)
85 3039e2dc Helga Velroyen
    self.assertFalse(init_fn.called)
86 3039e2dc Helga Velroyen
    self.assertFalse(acceptance_fn.called)
87 3039e2dc Helga Velroyen
88 3039e2dc Helga Velroyen
  def testPathNotAccepted(self):
89 3039e2dc Helga Velroyen
    acceptance_fn = mock.Mock()
90 3039e2dc Helga Velroyen
    acceptance_fn.side_effect = errors.FileStoragePathError
91 3039e2dc Helga Velroyen
    init_fn = mock.Mock()
92 3039e2dc Helga Velroyen
    self.enableFileStorage(True)
93 3039e2dc Helga Velroyen
    self.assertRaises(errors.OpPrereqError, bootstrap._PrepareFileStorage,
94 3039e2dc Helga Velroyen
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
95 3039e2dc Helga Velroyen
        init_fn=init_fn)
96 3039e2dc Helga Velroyen
    acceptance_fn.assert_called_with(self.tmpdir)
97 5030cff3 Helga Velroyen
98 5030cff3 Helga Velroyen
99 5030cff3 Helga Velroyen
class TestInitCheckEnabledDiskTemplates(unittest.TestCase):
100 5030cff3 Helga Velroyen
  def testValidTemplates(self):
101 5030cff3 Helga Velroyen
    enabled_disk_templates = list(constants.DISK_TEMPLATES)
102 5030cff3 Helga Velroyen
    bootstrap._InitCheckEnabledDiskTemplates(enabled_disk_templates)
103 5030cff3 Helga Velroyen
104 5030cff3 Helga Velroyen
  def testInvalidTemplates(self):
105 5030cff3 Helga Velroyen
    enabled_disk_templates = ["pinkbunny"]
106 5030cff3 Helga Velroyen
    self.assertRaises(errors.OpPrereqError,
107 5030cff3 Helga Velroyen
        bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
108 5030cff3 Helga Velroyen
109 5030cff3 Helga Velroyen
  def testEmptyTemplates(self):
110 5030cff3 Helga Velroyen
    enabled_disk_templates = []
111 5030cff3 Helga Velroyen
    self.assertRaises(errors.OpPrereqError,
112 5030cff3 Helga Velroyen
        bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
113 5030cff3 Helga Velroyen
114 5030cff3 Helga Velroyen
115 d514e18b Helga Velroyen
class TestRestrictIpolicyToEnabledDiskTemplates(unittest.TestCase):
116 d514e18b Helga Velroyen
117 d514e18b Helga Velroyen
  def testNoRestriction(self):
118 d514e18b Helga Velroyen
    allowed_disk_templates = list(constants.DISK_TEMPLATES)
119 d514e18b Helga Velroyen
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
120 d514e18b Helga Velroyen
    enabled_disk_templates = list(constants.DISK_TEMPLATES)
121 d514e18b Helga Velroyen
    bootstrap._RestrictIpolicyToEnabledDiskTemplates(
122 d514e18b Helga Velroyen
        ipolicy, enabled_disk_templates)
123 d514e18b Helga Velroyen
    self.assertEqual(ipolicy[constants.IPOLICY_DTS], allowed_disk_templates)
124 d514e18b Helga Velroyen
125 d514e18b Helga Velroyen
  def testRestriction(self):
126 d514e18b Helga Velroyen
    allowed_disk_templates = [constants.DT_DRBD8, constants.DT_PLAIN]
127 d514e18b Helga Velroyen
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
128 d514e18b Helga Velroyen
    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_FILE]
129 d514e18b Helga Velroyen
    bootstrap._RestrictIpolicyToEnabledDiskTemplates(
130 d514e18b Helga Velroyen
        ipolicy, enabled_disk_templates)
131 d514e18b Helga Velroyen
    self.assertEqual(ipolicy[constants.IPOLICY_DTS], [constants.DT_PLAIN])
132 d514e18b Helga Velroyen
133 d514e18b Helga Velroyen
134 af95196c Helga Velroyen
class TestInitCheckDrbdHelper(unittest.TestCase):
135 af95196c Helga Velroyen
136 af95196c Helga Velroyen
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
137 7796e1f8 Helga Velroyen
  def testNoDrbd(self, drbd_mock_get_usermode_helper):
138 7796e1f8 Helga Velroyen
    drbd_enabled = False
139 7796e1f8 Helga Velroyen
    drbd_helper = None
140 7796e1f8 Helga Velroyen
    bootstrap._InitCheckDrbdHelper(drbd_helper, drbd_enabled)
141 7796e1f8 Helga Velroyen
142 7796e1f8 Helga Velroyen
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
143 af95196c Helga Velroyen
  def testHelperNone(self, drbd_mock_get_usermode_helper):
144 7796e1f8 Helga Velroyen
    drbd_enabled = True
145 af95196c Helga Velroyen
    current_helper = "/bin/helper"
146 af95196c Helga Velroyen
    drbd_helper = None
147 af95196c Helga Velroyen
    drbd_mock_get_usermode_helper.return_value = current_helper
148 7796e1f8 Helga Velroyen
    bootstrap._InitCheckDrbdHelper(drbd_helper, drbd_enabled)
149 af95196c Helga Velroyen
150 af95196c Helga Velroyen
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
151 af95196c Helga Velroyen
  def testHelperOk(self, drbd_mock_get_usermode_helper):
152 7796e1f8 Helga Velroyen
    drbd_enabled = True
153 af95196c Helga Velroyen
    current_helper = "/bin/helper"
154 af95196c Helga Velroyen
    drbd_helper = "/bin/helper"
155 af95196c Helga Velroyen
    drbd_mock_get_usermode_helper.return_value = current_helper
156 7796e1f8 Helga Velroyen
    bootstrap._InitCheckDrbdHelper(drbd_helper, drbd_enabled)
157 af95196c Helga Velroyen
158 af95196c Helga Velroyen
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
159 af95196c Helga Velroyen
  def testWrongHelper(self, drbd_mock_get_usermode_helper):
160 7796e1f8 Helga Velroyen
    drbd_enabled = True
161 af95196c Helga Velroyen
    current_helper = "/bin/otherhelper"
162 af95196c Helga Velroyen
    drbd_helper = "/bin/helper"
163 af95196c Helga Velroyen
    drbd_mock_get_usermode_helper.return_value = current_helper
164 af95196c Helga Velroyen
    self.assertRaises(errors.OpPrereqError,
165 7796e1f8 Helga Velroyen
        bootstrap._InitCheckDrbdHelper, drbd_helper, drbd_enabled)
166 af95196c Helga Velroyen
167 af95196c Helga Velroyen
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
168 af95196c Helga Velroyen
  def testHelperCheckFails(self, drbd_mock_get_usermode_helper):
169 7796e1f8 Helga Velroyen
    drbd_enabled = True
170 af95196c Helga Velroyen
    drbd_helper = "/bin/helper"
171 af95196c Helga Velroyen
    drbd_mock_get_usermode_helper.side_effect=errors.BlockDeviceError
172 af95196c Helga Velroyen
    self.assertRaises(errors.OpPrereqError,
173 7796e1f8 Helga Velroyen
        bootstrap._InitCheckDrbdHelper, drbd_helper, drbd_enabled)
174 af95196c Helga Velroyen
175 af95196c Helga Velroyen
176 5030cff3 Helga Velroyen
if __name__ == "__main__":
177 5030cff3 Helga Velroyen
  testutils.GanetiTestProgram()