Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.bootstrap_unittest.py @ 14933c17

History | View | Annotate | Download (6.5 kB)

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.storage import drbd
31
from ganeti import errors
32
from ganeti import pathutils
33

    
34
import testutils
35
import mock
36

    
37

    
38
class TestPrepareFileStorage(unittest.TestCase):
39
  def setUp(self):
40
    unittest.TestCase.setUp(self)
41
    self.tmpdir = tempfile.mkdtemp()
42

    
43
  def tearDown(self):
44
    shutil.rmtree(self.tmpdir)
45

    
46
  def enableFileStorage(self, enable):
47
    self.enabled_disk_templates = []
48
    if enable:
49
      self.enabled_disk_templates.append(constants.DT_FILE)
50
    else:
51
      # anything != DT_FILE would do here
52
      self.enabled_disk_templates.append(constants.DT_DISKLESS)
53

    
54
  def testFallBackToDefaultPathAcceptedFileStorageEnabled(self):
55
    expected_file_storage_dir = pathutils.DEFAULT_FILE_STORAGE_DIR
56
    acceptance_fn = mock.Mock()
57
    init_fn = mock.Mock(return_value=expected_file_storage_dir)
58
    self.enableFileStorage(True)
59
    file_storage_dir = bootstrap._PrepareFileStorage(
60
        self.enabled_disk_templates, None, acceptance_fn=acceptance_fn,
61
        init_fn=init_fn)
62
    self.assertEqual(expected_file_storage_dir, file_storage_dir)
63
    acceptance_fn.assert_called_with(expected_file_storage_dir)
64
    init_fn.assert_called_with(expected_file_storage_dir)
65

    
66
  def testPathAcceptedFileStorageEnabled(self):
67
    acceptance_fn = mock.Mock()
68
    init_fn = mock.Mock(return_value=self.tmpdir)
69
    self.enableFileStorage(True)
70
    file_storage_dir = bootstrap._PrepareFileStorage(
71
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
72
        init_fn=init_fn)
73
    self.assertEqual(self.tmpdir, file_storage_dir)
74
    acceptance_fn.assert_called_with(self.tmpdir)
75
    init_fn.assert_called_with(self.tmpdir)
76

    
77
  def testPathAcceptedFileStorageDisabled(self):
78
    acceptance_fn = mock.Mock()
79
    init_fn = mock.Mock()
80
    self.enableFileStorage(False)
81
    file_storage_dir = bootstrap._PrepareFileStorage(
82
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
83
        init_fn=init_fn)
84
    self.assertEqual(self.tmpdir, file_storage_dir)
85
    self.assertFalse(init_fn.called)
86
    self.assertFalse(acceptance_fn.called)
87

    
88
  def testPathNotAccepted(self):
89
    acceptance_fn = mock.Mock()
90
    acceptance_fn.side_effect = errors.FileStoragePathError
91
    init_fn = mock.Mock()
92
    self.enableFileStorage(True)
93
    self.assertRaises(errors.OpPrereqError, bootstrap._PrepareFileStorage,
94
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
95
        init_fn=init_fn)
96
    acceptance_fn.assert_called_with(self.tmpdir)
97

    
98

    
99
class TestInitCheckEnabledDiskTemplates(unittest.TestCase):
100
  def testValidTemplates(self):
101
    enabled_disk_templates = list(constants.DISK_TEMPLATES)
102
    bootstrap._InitCheckEnabledDiskTemplates(enabled_disk_templates)
103

    
104
  def testInvalidTemplates(self):
105
    enabled_disk_templates = ["pinkbunny"]
106
    self.assertRaises(errors.OpPrereqError,
107
        bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
108

    
109
  def testEmptyTemplates(self):
110
    enabled_disk_templates = []
111
    self.assertRaises(errors.OpPrereqError,
112
        bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
113

    
114

    
115
class TestRestrictIpolicyToEnabledDiskTemplates(unittest.TestCase):
116

    
117
  def testNoRestriction(self):
118
    allowed_disk_templates = list(constants.DISK_TEMPLATES)
119
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
120
    enabled_disk_templates = list(constants.DISK_TEMPLATES)
121
    bootstrap._RestrictIpolicyToEnabledDiskTemplates(
122
        ipolicy, enabled_disk_templates)
123
    self.assertEqual(ipolicy[constants.IPOLICY_DTS], allowed_disk_templates)
124

    
125
  def testRestriction(self):
126
    allowed_disk_templates = [constants.DT_DRBD8, constants.DT_PLAIN]
127
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
128
    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_FILE]
129
    bootstrap._RestrictIpolicyToEnabledDiskTemplates(
130
        ipolicy, enabled_disk_templates)
131
    self.assertEqual(ipolicy[constants.IPOLICY_DTS], [constants.DT_PLAIN])
132

    
133

    
134
class TestInitCheckDrbdHelper(unittest.TestCase):
135

    
136
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
137
  def testNoDrbd(self, drbd_mock_get_usermode_helper):
138
    drbd_enabled = False
139
    drbd_helper = None
140
    bootstrap._InitCheckDrbdHelper(drbd_helper, drbd_enabled)
141

    
142
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
143
  def testHelperNone(self, drbd_mock_get_usermode_helper):
144
    drbd_enabled = True
145
    current_helper = "/bin/helper"
146
    drbd_helper = None
147
    drbd_mock_get_usermode_helper.return_value = current_helper
148
    bootstrap._InitCheckDrbdHelper(drbd_helper, drbd_enabled)
149

    
150
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
151
  def testHelperOk(self, drbd_mock_get_usermode_helper):
152
    drbd_enabled = True
153
    current_helper = "/bin/helper"
154
    drbd_helper = "/bin/helper"
155
    drbd_mock_get_usermode_helper.return_value = current_helper
156
    bootstrap._InitCheckDrbdHelper(drbd_helper, drbd_enabled)
157

    
158
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
159
  def testWrongHelper(self, drbd_mock_get_usermode_helper):
160
    drbd_enabled = True
161
    current_helper = "/bin/otherhelper"
162
    drbd_helper = "/bin/helper"
163
    drbd_mock_get_usermode_helper.return_value = current_helper
164
    self.assertRaises(errors.OpPrereqError,
165
        bootstrap._InitCheckDrbdHelper, drbd_helper, drbd_enabled)
166

    
167
  @testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
168
  def testHelperCheckFails(self, drbd_mock_get_usermode_helper):
169
    drbd_enabled = True
170
    drbd_helper = "/bin/helper"
171
    drbd_mock_get_usermode_helper.side_effect=errors.BlockDeviceError
172
    self.assertRaises(errors.OpPrereqError,
173
        bootstrap._InitCheckDrbdHelper, drbd_helper, drbd_enabled)
174

    
175

    
176
if __name__ == "__main__":
177
  testutils.GanetiTestProgram()