def _run_available_services(plists): if six.PY2: mock_read_plist = MagicMock() mock_read_plist.side_effect = plists with patch("plistlib.readPlist", mock_read_plist): ret = mac_utils._available_services() else: mock_load = MagicMock() mock_load.side_effect = plists with patch("salt.utils.files.fopen", mock_open()): with patch("plistlib.load", mock_load): ret = mac_utils._available_services() return ret
def _run_available_services(plists): mock_load = MagicMock() mock_load.side_effect = plists with patch("salt.utils.files.fopen", mock_open()): with patch("plistlib.load", mock_load): ret = mac_utils._available_services() return ret
def test_add_volume_bricks(self): """ Test to add brick(s) to an existing volume """ name = "salt" bricks = ["host1:/drive1"] old_bricks = ["host1:/drive2"] ret = {"name": name, "result": False, "comment": "", "changes": {}} stopped_volinfo = {"salt": {"status": "0"}} volinfo = { "salt": {"status": "1", "bricks": {"brick1": {"path": old_bricks[0]}}} } new_volinfo = { "salt": { "status": "1", "bricks": { "brick1": {"path": old_bricks[0]}, "brick2": {"path": bricks[0]}, }, } } mock_info = MagicMock(return_value={}) mock_add = MagicMock(side_effect=[False, True]) with patch.dict( glusterfs.__salt__, {"glusterfs.info": mock_info, "glusterfs.add_volume_bricks": mock_add}, ): ret.update({"comment": "Volume salt does not exist"}) self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret) mock_info.return_value = stopped_volinfo ret.update({"comment": "Volume salt is not started"}) self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret) mock_info.return_value = volinfo ret.update({"comment": "Adding bricks to volume salt failed"}) self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret) ret.update({"result": True}) ret.update({"comment": "Bricks already added in volume salt"}) self.assertDictEqual(glusterfs.add_volume_bricks(name, old_bricks), ret) mock_info.side_effect = [volinfo, new_volinfo] ret.update( { "comment": "Bricks successfully added to volume salt", "changes": {"new": bricks + old_bricks, "old": old_bricks}, } ) # Let's sort ourselves because the test under python 3 sometimes fails # just because of the new changes list order result = glusterfs.add_volume_bricks(name, bricks) ret["changes"]["new"] = sorted(ret["changes"]["new"]) result["changes"]["new"] = sorted(result["changes"]["new"]) self.assertDictEqual(result, ret)
def test_set_hibernate_timeout_scheme(self): ''' Test to make sure we can set the hibernate timeout value ''' mock = MagicMock(return_value=0) mock.side_effect = [self.query_output] with patch.dict(powercfg.__salt__, {'cmd.retcode': mock}): powercfg.set_hibernate_timeout(0, 'dc', scheme='SCHEME_MIN') mock.assert_called_once_with( 'powercfg /setdcvalueindex SCHEME_MIN SUB_SLEEP HIBERNATEIDLE 0', python_shell=False)
def test_show_current(): mock = MagicMock(return_value="/etc/alternatives/salt") with patch("salt.utils.path.readlink", mock): ret = alternatives.show_current("better-world") assert "/etc/alternatives/salt" == ret mock.assert_called_once_with("/etc/alternatives/better-world") with TstSuiteLoggingHandler() as handler: mock.side_effect = OSError("Hell was not found!!!") assert not alternatives.show_current("hell") mock.assert_called_with("/etc/alternatives/hell") assert "ERROR:alternative: hell does not exist" in handler.messages
def test_set_hibernate_timeout_scheme(self): ''' Test to make sure we can set the hibernate timeout value ''' mock = MagicMock() mock.side_effect = [self.query_ouput] with patch.dict(powercfg.__salt__, {'cmd.run': mock}): powercfg.set_hibernate_timeout(0, "dc", scheme="SCHEME_MIN") calls = [ call('powercfg /setdcvalueindex SCHEME_MIN SUB_SLEEP HIBERNATEIDLE 0', python_shell=False) ] mock.assert_has_calls(calls)
def test_show_current(self): mock = MagicMock(return_value='/etc/alternatives/salt') with patch('salt.utils.path.readlink', mock): ret = alternatives.show_current('better-world') self.assertEqual('/etc/alternatives/salt', ret) mock.assert_called_once_with('/etc/alternatives/better-world') with TestsLoggingHandler() as handler: mock.side_effect = OSError('Hell was not found!!!') self.assertFalse(alternatives.show_current('hell')) mock.assert_called_with('/etc/alternatives/hell') self.assertIn('ERROR:alternative: hell does not exist', handler.messages)
def test_set_hibernate_timeout_scheme(self): """ Test to make sure we can set the hibernate timeout value """ mock = MagicMock(return_value=0) mock.side_effect = [self.query_output] with patch.dict(powercfg.__salt__, {"cmd.retcode": mock}): powercfg.set_hibernate_timeout(0, "dc", scheme="SCHEME_MIN") mock.assert_called_once_with( "powercfg /setdcvalueindex SCHEME_MIN SUB_SLEEP HIBERNATEIDLE 0", python_shell=False, )
def test_set_hibernate_timeout(self): ''' Test to make sure we can set the hibernate timeout value ''' mock = MagicMock() mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput] with patch.dict(powercfg.__salt__, {'cmd.run': mock}): powercfg.set_hibernate_timeout(0, "dc") calls = [ call('powercfg /getactivescheme', python_shell=False), call('powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP HIBERNATEIDLE 0', python_shell=False) ] mock.assert_has_calls(calls)
def test_get_hibernate_timeout_scheme(self): ''' Test to make sure we can get the hibernate timeout value with a specified scheme ''' mock = MagicMock() mock.side_effect = [self.query_output] with patch.dict(powercfg.__salt__, {'cmd.run': mock}): ret = powercfg.get_hibernate_timeout(scheme='SCHEME_MIN') mock.assert_called_once_with( 'powercfg /q SCHEME_MIN SUB_SLEEP HIBERNATEIDLE', python_shell=False) self.assertEqual({'ac': 30, 'dc': 15}, ret)
def test_get_hibernate_timeout_scheme(self): """ Test to make sure we can get the hibernate timeout value with a specified scheme """ mock = MagicMock() mock.side_effect = [self.query_output] with patch.dict(powercfg.__salt__, {"cmd.run": mock}): ret = powercfg.get_hibernate_timeout(scheme="SCHEME_MIN") mock.assert_called_once_with( "powercfg /q SCHEME_MIN SUB_SLEEP HIBERNATEIDLE", python_shell=False ) self.assertEqual({"ac": 30, "dc": 15}, ret)
def test_get_hibernate_timeout(self): ''' Test to make sure we can get the hibernate timeout value ''' mock = MagicMock() mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput] with patch.dict(powercfg.__salt__, {'cmd.run': mock}): ret = powercfg.get_hibernate_timeout() calls = [ call('powercfg /getactivescheme', python_shell=False), call('powercfg /q 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP HIBERNATEIDLE', python_shell=False) ] mock.assert_has_calls(calls) self.assertEqual({'ac': 30, 'dc': 15}, ret)
def test_available_services_expat_error(self, mock_exists, mock_os_walk, mock_read_plist, mock_run): ''' test available_services excludes files with expat errors. Poorly formed XML will raise an ExpatError on py2. It will also be raised by some almost-correct XML on py3. ''' results = {'/Library/LaunchAgents': ['com.apple.lla1.plist']} mock_os_walk.side_effect = _get_walk_side_effects(results) mock_exists.return_value = True file_path = os.sep + os.path.join('Library', 'LaunchAgents', 'com.apple.lla1.plist') if salt.utils.platform.is_windows(): file_path = 'c:' + file_path if six.PY3: mock_load = MagicMock() mock_load.side_effect = xml.parsers.expat.ExpatError with patch('salt.utils.files.fopen', mock_open()): with patch('plistlib.load', mock_load): ret = mac_utils._available_services() else: attrs = {'cmd.run': MagicMock()} def getitem(name): return attrs[name] mock_run.__getitem__.side_effect = getitem mock_run.configure_mock(**attrs) cmd = '/usr/bin/plutil -convert xml1 -o - -- "{}"'.format(file_path) calls = [call.cmd.run(cmd)] mock_raise_expat_error = MagicMock( side_effect=xml.parsers.expat.ExpatError) with patch('plistlib.readPlist', mock_raise_expat_error): with patch('plistlib.readPlistFromString', mock_raise_expat_error): ret = mac_utils._available_services() mock_run.assert_has_calls(calls, any_order=True) self.assertEqual(len(ret), 0)
def test_available_services_invalid_file(self, mock_exists, mock_os_walk): """ test available_services excludes invalid files. The py3 plistlib raises an InvalidFileException when a plist file cannot be parsed. """ results = {"/Library/LaunchAgents": ["com.apple.lla1.plist"]} mock_os_walk.side_effect = _get_walk_side_effects(results) mock_exists.return_value = True plists = [{"Label": "com.apple.lla1"}] mock_load = MagicMock() mock_load.side_effect = plistlib.InvalidFileException with patch("salt.utils.files.fopen", mock_open()): with patch("plistlib.load", mock_load): ret = mac_utils._available_services() self.assertEqual(len(ret), 0)
def test_set_hibernate_timeout(self): ''' Test to make sure we can set the hibernate timeout value ''' mock = MagicMock(return_value=0) mock.side_effect = [ 'Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)', self.query_output ] mock_retcode = MagicMock(return_value=0) with patch.dict(powercfg.__salt__, {'cmd.run': mock}): with patch.dict(powercfg.__salt__, {'cmd.retcode': mock_retcode}): powercfg.set_hibernate_timeout(0, 'dc') mock.assert_called_once_with('powercfg /getactivescheme', python_shell=False) mock_retcode.assert_called_once_with( 'powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP HIBERNATEIDLE 0', python_shell=False)
def test_available_services_invalid_file(self, mock_exists, mock_os_walk): ''' test available_services excludes invalid files. The py3 plistlib raises an InvalidFileException when a plist file cannot be parsed. This test only asserts things for py3. ''' if six.PY3: results = {'/Library/LaunchAgents': ['com.apple.lla1.plist']} mock_os_walk.side_effect = _get_walk_side_effects(results) mock_exists.return_value = True plists = [{'Label': 'com.apple.lla1'}] mock_load = MagicMock() mock_load.side_effect = plistlib.InvalidFileException with patch('salt.utils.files.fopen', mock_open()): with patch('plistlib.load', mock_load): ret = mac_utils._available_services() self.assertEqual(len(ret), 0)
def test_available_services_value_error(self, mock_exists, mock_os_walk, mock_run): """ test available_services excludes files with ValueErrors. """ results = {"/Library/LaunchAgents": ["com.apple.lla1.plist"]} mock_os_walk.side_effect = _get_walk_side_effects(results) mock_exists.return_value = True file_path = os.sep + os.path.join("Library", "LaunchAgents", "com.apple.lla1.plist") if salt.utils.platform.is_windows(): file_path = "c:" + file_path mock_load = MagicMock() mock_load.side_effect = ValueError with patch("salt.utils.files.fopen", mock_open()): with patch("plistlib.load", mock_load): ret = mac_utils._available_services() self.assertEqual(len(ret), 0)
def test_set_standby_timeout(self): """ Test to make sure we can set the standby timeout value """ mock = MagicMock(return_value=0) mock.side_effect = [ "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_output, ] mock_retcode = MagicMock(return_value=0) with patch.dict(powercfg.__salt__, {"cmd.run": mock}): with patch.dict(powercfg.__salt__, {"cmd.retcode": mock_retcode}): powercfg.set_standby_timeout(0, "dc") mock.assert_called_once_with("powercfg /getactivescheme", python_shell=False) mock_retcode.assert_called_once_with( "powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP STANDBYIDLE 0", python_shell=False, )
def test_get_hibernate_timeout(self): """ Test to make sure we can get the hibernate timeout value """ mock = MagicMock() mock.side_effect = [ "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_output, ] with patch.dict(powercfg.__salt__, {"cmd.run": mock}): ret = powercfg.get_hibernate_timeout() calls = [ call("powercfg /getactivescheme", python_shell=False), call( "powercfg /q 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP HIBERNATEIDLE", python_shell=False, ), ] mock.assert_has_calls(calls) self.assertEqual({"ac": 30, "dc": 15}, ret)
def test_available_services_expat_error(self, mock_exists, mock_os_walk, mock_run): """ test available_services excludes files with expat errors. Poorly formed XML will raise an ExpatError on py2. It will also be raised by some almost-correct XML on py3. """ results = {"/Library/LaunchAgents": ["com.apple.lla1.plist"]} mock_os_walk.side_effect = _get_walk_side_effects(results) mock_exists.return_value = True file_path = os.sep + os.path.join("Library", "LaunchAgents", "com.apple.lla1.plist") if salt.utils.platform.is_windows(): file_path = "c:" + file_path mock_load = MagicMock() mock_load.side_effect = xml.parsers.expat.ExpatError with patch("salt.utils.files.fopen", mock_open()): with patch("plistlib.load", mock_load): ret = mac_utils._available_services() self.assertEqual(len(ret), 0)
def test_enable(self): ''' Test for Enable the named service to start at boot ''' rc_update_mock = MagicMock(return_value=0) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name')) rc_update_mock.assert_called_once_with('rc-update add name', ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service from 'l1' to 'l2' runlevel service_name = 'name' runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_has_calls([call('rc-update delete name l1', ignore_retcode=False, python_shell=False), call('rc-update add name l2', ignore_retcode=False, python_shell=False)]) rc_update_mock.reset_mock() # requested levels are the same as the current ones with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels='l1')) self.assertTrue(rc_update_mock.call_count == 0) rc_update_mock.reset_mock() # same as above with the list instead of the string with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l1'])) self.assertTrue(rc_update_mock.call_count == 0) rc_update_mock.reset_mock() # add service to 'l2' runlevel with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l1'])) rc_update_mock.assert_called_once_with('rc-update add name l2', ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # remove service from 'l1' runlevel runlevels = ['l1', 'l2'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service from 'l2' add to 'l3', leaving at l1 with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_has_calls([call('rc-update delete name l2', ignore_retcode=False, python_shell=False), call('rc-update add name l3', ignore_retcode=False, python_shell=False)]) rc_update_mock.reset_mock() # remove from l1, l3, and add to l2, l4, and leave at l5 runlevels = ['l1', 'l3', 'l5'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l4', 'l5'])) rc_update_mock.assert_has_calls([call('rc-update delete name l1 l3', ignore_retcode=False, python_shell=False), call('rc-update add name l2 l4', ignore_retcode=False, python_shell=False)]) rc_update_mock.reset_mock() # rc-update failed rc_update_mock = MagicMock(return_value=1) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name')) rc_update_mock.assert_called_once_with('rc-update add name', ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service delete failed runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_called_once_with('rc-update delete name l1', ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service delete succeeds. add fails rc_update_mock = MagicMock() rc_update_mock.side_effect = [0, 1] with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_has_calls([call('rc-update delete name l1', ignore_retcode=False, python_shell=False), call('rc-update add name l2', ignore_retcode=False, python_shell=False)]) rc_update_mock.reset_mock()
def test_enable(self): """ Test for Enable the named service to start at boot """ rc_update_mock = MagicMock(return_value=0) with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue(gentoo_service.enable("name")) rc_update_mock.assert_called_once_with("rc-update add name", ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service from 'l1' to 'l2' runlevel service_name = "name" runlevels = ["l1"] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue(gentoo_service.enable("name", runlevels="l2")) rc_update_mock.assert_has_calls([ call("rc-update delete name l1", ignore_retcode=False, python_shell=False), call("rc-update add name l2", ignore_retcode=False, python_shell=False), ]) rc_update_mock.reset_mock() # requested levels are the same as the current ones with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue(gentoo_service.enable("name", runlevels="l1")) self.assertTrue(rc_update_mock.call_count == 0) rc_update_mock.reset_mock() # same as above with the list instead of the string with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue(gentoo_service.enable("name", runlevels=["l1"])) self.assertTrue(rc_update_mock.call_count == 0) rc_update_mock.reset_mock() # add service to 'l2' runlevel with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue( gentoo_service.enable("name", runlevels=["l2", "l1"])) rc_update_mock.assert_called_once_with("rc-update add name l2", ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # remove service from 'l1' runlevel runlevels = ["l1", "l2"] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue(gentoo_service.enable("name", runlevels=["l2"])) rc_update_mock.assert_called_once_with("rc-update delete name l1", ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service from 'l2' add to 'l3', leaving at l1 with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue( gentoo_service.enable("name", runlevels=["l1", "l3"])) rc_update_mock.assert_has_calls([ call("rc-update delete name l2", ignore_retcode=False, python_shell=False), call("rc-update add name l3", ignore_retcode=False, python_shell=False), ]) rc_update_mock.reset_mock() # remove from l1, l3, and add to l2, l4, and leave at l5 runlevels = ["l1", "l3", "l5"] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertTrue( gentoo_service.enable("name", runlevels=["l2", "l4", "l5"])) rc_update_mock.assert_has_calls([ call( "rc-update delete name l1 l3", ignore_retcode=False, python_shell=False, ), call("rc-update add name l2 l4", ignore_retcode=False, python_shell=False), ]) rc_update_mock.reset_mock() # rc-update failed rc_update_mock = MagicMock(return_value=1) with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertFalse(gentoo_service.enable("name")) rc_update_mock.assert_called_once_with("rc-update add name", ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service delete failed runlevels = ["l1"] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertFalse(gentoo_service.enable("name", runlevels="l2")) rc_update_mock.assert_called_once_with("rc-update delete name l1", ignore_retcode=False, python_shell=False) rc_update_mock.reset_mock() # move service delete succeeds. add fails rc_update_mock = MagicMock() rc_update_mock.side_effect = [0, 1] with patch.dict(gentoo_service.__salt__, {"cmd.run": level_list_mock}): with patch.dict(gentoo_service.__salt__, {"cmd.retcode": rc_update_mock}): self.assertFalse(gentoo_service.enable("name", runlevels="l2")) rc_update_mock.assert_has_calls([ call("rc-update delete name l1", ignore_retcode=False, python_shell=False), call("rc-update add name l2", ignore_retcode=False, python_shell=False), ]) rc_update_mock.reset_mock()
def test_volume_present(self): ''' Test to ensure that a volume exists ''' name = 'salt' bricks = ['host1:/brick1'] ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} started_info = {name: {'status': '1'}} stopped_info = {name: {'status': '0'}} mock_info = MagicMock() mock_list = MagicMock() mock_create = MagicMock() mock_start = MagicMock(return_value=True) with patch.dict(glusterfs.__salt__, { 'glusterfs.info': mock_info, 'glusterfs.list_volumes': mock_list, 'glusterfs.create_volume': mock_create, 'glusterfs.start_volume': mock_start}): with patch.dict(glusterfs.__opts__, {'test': False}): mock_list.return_value = [name] mock_info.return_value = started_info comt = ('Volume {0} already exists and is started'.format(name)) ret.update({'comment': comt}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=True), ret) mock_info.return_value = stopped_info comt = ('Volume {0} already exists and is now started'.format(name)) ret.update({'comment': comt, 'changes': {'old': 'stopped', 'new': 'started'}}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=True), ret) comt = ('Volume {0} already exists'.format(name)) ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=False), ret) with patch.dict(glusterfs.__opts__, {'test': True}): comt = ('Volume {0} already exists'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=False), ret) comt = ('Volume {0} already exists' + ' and will be started').format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=True), ret) mock_list.return_value = [] comt = ('Volume {0} will be created'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=False), ret) comt = ('Volume {0} will be created' + ' and started').format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=True), ret) with patch.dict(glusterfs.__opts__, {'test': False}): mock_list.side_effect = [[], [name]] comt = ('Volume {0} is created'.format(name)) ret.update({'comment': comt, 'result': True, 'changes': {'old': [], 'new': [name]}}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=False), ret) mock_list.side_effect = [[], [name]] comt = ('Volume {0} is created and is now started'.format(name)) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(glusterfs.volume_present(name, bricks, start=True), ret) mock_list.side_effect = None mock_list.return_value = [] mock_create.return_value = False comt = 'Creation of volume {0} failed'.format(name) ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(glusterfs.volume_present(name, bricks), ret) with patch.object(salt.utils.cloud, 'check_name', MagicMock(return_value=True)): comt = ('Invalid characters in volume name.') ret.update({'comment': comt, 'result': False}) self.assertDictEqual(glusterfs.volume_present(name, bricks), ret)
def test_volume_present(self): """ Test to ensure that a volume exists """ name = "salt" bricks = ["host1:/brick1"] ret = {"name": name, "result": True, "comment": "", "changes": {}} started_info = {name: {"status": "1"}} stopped_info = {name: {"status": "0"}} mock_info = MagicMock() mock_list = MagicMock() mock_create = MagicMock() mock_start = MagicMock(return_value=True) with patch.dict( glusterfs.__salt__, { "glusterfs.info": mock_info, "glusterfs.list_volumes": mock_list, "glusterfs.create_volume": mock_create, "glusterfs.start_volume": mock_start, }, ): with patch.dict(glusterfs.__opts__, {"test": False}): mock_list.return_value = [name] mock_info.return_value = started_info comt = "Volume {} already exists and is started".format(name) ret.update({"comment": comt}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=True), ret ) mock_info.return_value = stopped_info comt = "Volume {} already exists and is now started".format(name) ret.update( {"comment": comt, "changes": {"old": "stopped", "new": "started"}} ) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=True), ret ) comt = "Volume {} already exists".format(name) ret.update({"comment": comt, "changes": {}}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=False), ret ) with patch.dict(glusterfs.__opts__, {"test": True}): comt = "Volume {} already exists".format(name) ret.update({"comment": comt, "result": None}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=False), ret ) comt = ("Volume {0} already exists" + " and will be started").format( name ) ret.update({"comment": comt, "result": None}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=True), ret ) mock_list.return_value = [] comt = "Volume {} will be created".format(name) ret.update({"comment": comt, "result": None}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=False), ret ) comt = ("Volume {0} will be created" + " and started").format(name) ret.update({"comment": comt, "result": None}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=True), ret ) with patch.dict(glusterfs.__opts__, {"test": False}): mock_list.side_effect = [[], [name]] comt = "Volume {} is created".format(name) ret.update( { "comment": comt, "result": True, "changes": {"old": [], "new": [name]}, } ) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=False), ret ) mock_list.side_effect = [[], [name]] comt = "Volume {} is created and is now started".format(name) ret.update({"comment": comt, "result": True}) self.assertDictEqual( glusterfs.volume_present(name, bricks, start=True), ret ) mock_list.side_effect = None mock_list.return_value = [] mock_create.return_value = False comt = "Creation of volume {} failed".format(name) ret.update({"comment": comt, "result": False, "changes": {}}) self.assertDictEqual(glusterfs.volume_present(name, bricks), ret) with patch.object( salt.utils.cloud, "check_name", MagicMock(return_value=True) ): comt = "Invalid characters in volume name." ret.update({"comment": comt, "result": False}) self.assertDictEqual(glusterfs.volume_present(name, bricks), ret)
def test_add_volume_bricks(self): ''' Test to add brick(s) to an existing volume ''' name = 'salt' bricks = ['host1:/drive1'] old_bricks = ['host1:/drive2'] ret = {'name': name, 'result': False, 'comment': '', 'changes': {}} stopped_volinfo = {'salt': {'status': '0'}} volinfo = { 'salt': { 'status': '1', 'bricks': {'brick1': {'path': old_bricks[0]}} } } new_volinfo = { 'salt': { 'status': '1', 'bricks': { 'brick1': {'path': old_bricks[0]}, 'brick2': {'path': bricks[0]} } } } mock_info = MagicMock(return_value={}) mock_add = MagicMock(side_effect=[False, True]) with patch.dict(glusterfs.__salt__, {'glusterfs.info': mock_info, 'glusterfs.add_volume_bricks': mock_add}): ret.update({'comment': 'Volume salt does not exist'}) self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret) mock_info.return_value = stopped_volinfo ret.update({'comment': 'Volume salt is not started'}) self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret) mock_info.return_value = volinfo ret.update({'comment': 'Adding bricks to volume salt failed'}) self.assertDictEqual(glusterfs.add_volume_bricks(name, bricks), ret) ret.update({'result': True}) ret.update({'comment': 'Bricks already added in volume salt'}) self.assertDictEqual(glusterfs.add_volume_bricks(name, old_bricks), ret) mock_info.side_effect = [volinfo, new_volinfo] ret.update({'comment': 'Bricks successfully added to volume salt', 'changes': {'new': bricks + old_bricks, 'old': old_bricks}}) # Let's sort ourselves because the test under python 3 sometimes fails # just because of the new changes list order result = glusterfs.add_volume_bricks(name, bricks) ret['changes']['new'] = sorted(ret['changes']['new']) result['changes']['new'] = sorted(result['changes']['new']) self.assertDictEqual(result, ret)
def test_peered(self): """ Test to verify if node is peered. """ name = "server1" ret = {"name": name, "result": True, "comment": "", "changes": {}} mock_ip = MagicMock(return_value=["1.2.3.4", "1.2.3.5"]) mock_ip6 = MagicMock(return_value=["2001:db8::1"]) mock_host_ips = MagicMock(return_value=["1.2.3.5"]) mock_peer = MagicMock(return_value=True) mock_status = MagicMock(return_value={"uuid1": {"hostnames": [name]}}) with patch.dict( glusterfs.__salt__, {"glusterfs.peer_status": mock_status, "glusterfs.peer": mock_peer}, ): with patch.object(salt.utils.network, "ip_addrs", mock_ip), patch.object( salt.utils.network, "ip_addrs6", mock_ip6 ), patch.object(salt.utils.network, "host_to_ips", mock_host_ips): comt = "Peering with localhost is not needed" ret.update({"comment": comt}) self.assertDictEqual(glusterfs.peered(name), ret) mock_host_ips.return_value = ["2001:db8::1"] self.assertDictEqual(glusterfs.peered(name), ret) mock_host_ips.return_value = ["1.2.3.42"] comt = "Host {} already peered".format(name) ret.update({"comment": comt}) self.assertDictEqual(glusterfs.peered(name), ret) with patch.dict(glusterfs.__opts__, {"test": False}): old = {"uuid1": {"hostnames": ["other1"]}} new = { "uuid1": {"hostnames": ["other1"]}, "uuid2": {"hostnames": ["someAlias", name]}, } mock_status.side_effect = [old, new] comt = "Host {} successfully peered".format(name) ret.update({"comment": comt, "changes": {"old": old, "new": new}}) self.assertDictEqual(glusterfs.peered(name), ret) mock_status.side_effect = None mock_status.return_value = {"uuid1": {"hostnames": ["other"]}} mock_peer.return_value = False ret.update({"result": False}) comt = ( "Failed to peer with {0}," + " please check logs for errors" ).format(name) ret.update({"comment": comt, "changes": {}}) self.assertDictEqual(glusterfs.peered(name), ret) comt = "Invalid characters in peer name." ret.update({"comment": comt, "name": ":/"}) self.assertDictEqual(glusterfs.peered(":/"), ret) ret.update({"name": name}) with patch.dict(glusterfs.__opts__, {"test": True}): comt = "Peer {} will be added.".format(name) ret.update({"comment": comt, "result": None}) self.assertDictEqual(glusterfs.peered(name), ret)
def test_peered(self): ''' Test to verify if node is peered. ''' name = 'server1' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} mock_ip = MagicMock(return_value=['1.2.3.4', '1.2.3.5']) mock_hostbyname = MagicMock(return_value='1.2.3.5') mock_peer = MagicMock(return_value=True) mock_status = MagicMock(return_value={'uuid1': {'hostnames': [name]}}) with patch.dict(glusterfs.__salt__, {'glusterfs.peer_status': mock_status, 'glusterfs.peer': mock_peer, 'network.ip_addrs': mock_ip}): with patch.object(socket, 'gethostbyname', mock_hostbyname): comt = 'Peering with localhost is not needed' ret.update({'comment': comt}) self.assertDictEqual(glusterfs.peered(name), ret) mock_hostbyname.return_value = '1.2.3.42' comt = ('Host {0} already peered'.format(name)) ret.update({'comment': comt}) self.assertDictEqual(glusterfs.peered(name), ret) with patch.dict(glusterfs.__opts__, {'test': False}): old = {'uuid1': {'hostnames': ['other1']}} new = {'uuid1': {'hostnames': ['other1']}, 'uuid2': {'hostnames': ['someAlias', name]}} mock_status.side_effect = [old, new] comt = 'Host {0} successfully peered'.format(name) ret.update({'comment': comt, 'changes': {'old': old, 'new': new}}) self.assertDictEqual(glusterfs.peered(name), ret) mock_status.side_effect = None mock_status.return_value = { 'uuid1': {'hostnames': ['other']} } mock_peer.return_value = False ret.update({'result': False}) comt = ('Failed to peer with {0},' + ' please check logs for errors').format(name) ret.update({'comment': comt, 'changes': {}}) self.assertDictEqual(glusterfs.peered(name), ret) comt = ('Invalid characters in peer name.') ret.update({'comment': comt, 'name': ':/'}) self.assertDictEqual(glusterfs.peered(':/'), ret) ret.update({'name': name}) with patch.dict(glusterfs.__opts__, {'test': True}): comt = ('Peer {0} will be added.'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(glusterfs.peered(name), ret)