Commit af95196c authored by Helga Velroyen's avatar Helga Velroyen

bootstrap.py: unit tests for setting the usermode helper

This patch factors out the function that checks the DRBD
usermode helper in bootstrap (cluster init) in order to make
it more testable. It also contains the unit tests. Otherwise,
no functional changes.
Signed-off-by: default avatarHelga Velroyen <helgav@google.com>
Reviewed-by: default avatarThomas Thrainer <thomasth@google.com>
parent 46204919
......@@ -472,6 +472,29 @@ def _RestrictIpolicyToEnabledDiskTemplates(ipolicy, enabled_disk_templates):
ipolicy[constants.IPOLICY_DTS] = restricted_disk_templates
def _InitCheckDrbdHelper(drbd_helper):
"""Checks the DRBD usermode helper.
@type drbd_helper: string
@param drbd_helper: name of the DRBD usermode helper that the system should
use
"""
if drbd_helper is not None:
try:
curr_helper = drbd.DRBD8.GetUsermodeHelper()
except errors.BlockDeviceError, err:
raise errors.OpPrereqError("Error while checking drbd helper"
" (specify --no-drbd-storage if you are not"
" using drbd): %s" % str(err),
errors.ECODE_ENVIRON)
if drbd_helper != curr_helper:
raise errors.OpPrereqError("Error: requiring %s as drbd helper but %s"
" is the current helper" % (drbd_helper,
curr_helper),
errors.ECODE_INVAL)
def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
master_netmask, master_netdev, file_storage_dir,
shared_file_storage_dir, candidate_pool_size, secondary_ip=None,
......@@ -569,19 +592,7 @@ def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
if vgstatus:
raise errors.OpPrereqError("Error: %s" % vgstatus, errors.ECODE_INVAL)
if drbd_helper is not None:
try:
curr_helper = drbd.DRBD8.GetUsermodeHelper()
except errors.BlockDeviceError, err:
raise errors.OpPrereqError("Error while checking drbd helper"
" (specify --no-drbd-storage if you are not"
" using drbd): %s" % str(err),
errors.ECODE_ENVIRON)
if drbd_helper != curr_helper:
raise errors.OpPrereqError("Error: requiring %s as drbd helper but %s"
" is the current helper" % (drbd_helper,
curr_helper),
errors.ECODE_INVAL)
_InitCheckDrbdHelper(drbd_helper)
logging.debug("Stopping daemons (if any are running)")
result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"])
......
......@@ -27,6 +27,7 @@ import unittest
from ganeti import bootstrap
from ganeti import constants
from ganeti.storage import drbd
from ganeti import errors
from ganeti import pathutils
......@@ -130,5 +131,37 @@ class TestRestrictIpolicyToEnabledDiskTemplates(unittest.TestCase):
self.assertEqual(ipolicy[constants.IPOLICY_DTS], [constants.DT_PLAIN])
class TestInitCheckDrbdHelper(unittest.TestCase):
@testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
def testHelperNone(self, drbd_mock_get_usermode_helper):
current_helper = "/bin/helper"
drbd_helper = None
drbd_mock_get_usermode_helper.return_value = current_helper
bootstrap._InitCheckDrbdHelper(drbd_helper)
@testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
def testHelperOk(self, drbd_mock_get_usermode_helper):
current_helper = "/bin/helper"
drbd_helper = "/bin/helper"
drbd_mock_get_usermode_helper.return_value = current_helper
bootstrap._InitCheckDrbdHelper(drbd_helper)
@testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
def testWrongHelper(self, drbd_mock_get_usermode_helper):
current_helper = "/bin/otherhelper"
drbd_helper = "/bin/helper"
drbd_mock_get_usermode_helper.return_value = current_helper
self.assertRaises(errors.OpPrereqError,
bootstrap._InitCheckDrbdHelper, drbd_helper)
@testutils.patch_object(drbd.DRBD8, "GetUsermodeHelper")
def testHelperCheckFails(self, drbd_mock_get_usermode_helper):
drbd_helper = "/bin/helper"
drbd_mock_get_usermode_helper.side_effect=errors.BlockDeviceError
self.assertRaises(errors.OpPrereqError,
bootstrap._InitCheckDrbdHelper, drbd_helper)
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment