def test_attach_virtual_host(self): mock_get_ip = MagicMock() mock_get_ip.return_value = '192.168.15.1' mock_retcode = MagicMock() mock_retcode.return_value = 1 mock_run = MagicMock() mock_run.return_value = 0 with patch.dict( netweavermod.__salt__, { 'hosts.get_ip': mock_get_ip, 'cmd.retcode': mock_retcode, 'cmd.run': mock_run }): ip_address = netweavermod.attach_virtual_host('vhost', 'eth1', 32) mock_get_ip.assert_called_once_with('vhost') mock_retcode.assert_called_once_with('ip a | grep 192.168.15.1/32', python_shell=True) mock_run.assert_called_once_with( 'ip address add 192.168.15.1/32 dev eth1') assert ip_address == '192.168.15.1'
def test_attach_virtual_host_ip_not_found(self): mock_get_ip = MagicMock() mock_get_ip.return_value = '192.168.15.1' mock_retcode = MagicMock() mock_retcode.return_value = 1 mock_run = MagicMock() mock_run.return_value = 1 with pytest.raises(exceptions.CommandExecutionError) as err: with patch.dict( netweavermod.__salt__, { 'hosts.get_ip': mock_get_ip, 'cmd.retcode': mock_retcode, 'cmd.run': mock_run }): netweavermod.attach_virtual_host('vhost') mock_get_ip.assert_called_once_with('vhost') mock_retcode.assert_called_once_with('ip a | grep 192.168.15.1/24', python_shell=True) mock_run.assert_called_once_with( 'ip address add 192.168.15.1/24 dev eth0') assert 'error running "ip address" command' in str(err.value)
def test_directory(): """ Test the etcd_mod.directory state function """ get_mock = MagicMock() set_mock = MagicMock() dunder_salt = { "etcd.get": get_mock, "etcd.set": set_mock, } with patch.dict(etcd_state.__salt__, dunder_salt): # Test new directory creation get_mock.return_value = None set_mock.return_value = "new_dir" expected = { "name": "new_dir", "comment": "New directory created", "result": True, "changes": {"new_dir": "Created"}, } assert etcd_state.directory("new_dir", profile="test") == expected # Test creating an existing directory get_mock.return_value = "new_dir" set_mock.return_value = "new_dir" expected = { "name": "new_dir", "comment": "Directory exists", "result": True, "changes": {}, } assert etcd_state.directory("new_dir", profile="test") == expected
def test_op_version(self): """ Test setting the Glusterfs op-version """ name = "salt" current = 30707 new = 31200 ret = {"name": name, "result": False, "comment": "", "changes": {}} mock_get_version = MagicMock(return_value={}) mock_set_version = MagicMock(return_value={}) with patch.dict( glusterfs.__salt__, { "glusterfs.get_op_version": mock_get_version, "glusterfs.set_op_version": mock_set_version, }, ): mock_get_version.return_value = [False, "some error message"] ret.update({"result": False}) ret.update({"comment": "some error message"}) self.assertDictEqual(glusterfs.op_version(name, current), ret) mock_get_version.return_value = current ret.update({"result": True}) ret.update({ "comment": "Glusterfs cluster.op-version for {0} already set to {1}". format(name, current) }) self.assertDictEqual(glusterfs.op_version(name, current), ret) with patch.dict(glusterfs.__opts__, {"test": True}): mock_set_version.return_value = [ False, "Failed to set version" ] ret.update({"result": None}) ret.update({ "comment": "An attempt would be made to set the cluster.op-version for {0} to {1}." .format(name, new) }) self.assertDictEqual(glusterfs.op_version(name, new), ret) with patch.dict(glusterfs.__opts__, {"test": False}): mock_set_version.return_value = [ False, "Failed to set version" ] ret.update({"result": False}) ret.update({"comment": "Failed to set version"}) self.assertDictEqual(glusterfs.op_version(name, new), ret) mock_set_version.return_value = "some success message" ret.update({"comment": "some success message"}) ret.update({"changes": {"old": current, "new": new}}) ret.update({"result": True}) self.assertDictEqual(glusterfs.op_version(name, new), 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_mod_watch(): """ Test the watch requisite function etcd_mod.mod_watch """ get_mock = MagicMock() set_mock = MagicMock() rm_mock = MagicMock() dunder_salt = { "etcd.get": get_mock, "etcd.set": set_mock, "etcd.rm": rm_mock, } with patch.dict(etcd_state.__salt__, dunder_salt): # Test watch with wait_set get_mock.return_value = None set_mock.return_value = "value" expected = { "name": "key", "comment": "New key created", "result": True, "changes": {"key": "value"}, } assert ( etcd_state.mod_watch("key", value="value", sfun="wait_set", profile={}) == expected ) assert ( etcd_state.mod_watch("key", value="value", sfun="wait_set_key", profile={}) == expected ) # Test watch with wait_rm get_mock.return_value = "value" rm_mock.return_value = True expected = { "name": "key", "comment": "Key removed", "result": True, "changes": {"key": "Deleted"}, } assert etcd_state.mod_watch("key", sfun="wait_rm", profile={}) == expected assert etcd_state.mod_watch("key", sfun="wait_rm_key", profile={}) == expected # Test watch with bad sfun kwargs = {"sfun": "bad_sfun"} expected = { "name": "key", "changes": {}, "comment": ( "etcd.{0[sfun]} does not work with the watch requisite, " "please use etcd.wait_set or etcd.wait_rm".format(kwargs) ), "result": False, } assert etcd_state.mod_watch("key", **kwargs) == expected assert etcd_state.mod_watch("key", **kwargs) == expected
def test_op_version(self): ''' Test setting the Glusterfs op-version ''' name = 'salt' current = 30707 new = 31200 ret = {'name': name, 'result': False, 'comment': '', 'changes': {}} mock_get_version = MagicMock(return_value={}) mock_set_version = MagicMock(return_value={}) with patch.dict( glusterfs.__salt__, { 'glusterfs.get_op_version': mock_get_version, 'glusterfs.set_op_version': mock_set_version }): mock_get_version.return_value = [False, 'some error message'] ret.update({'result': False}) ret.update({'comment': 'some error message'}) self.assertDictEqual(glusterfs.op_version(name, current), ret) mock_get_version.return_value = current ret.update({'result': True}) ret.update({ 'comment': 'Glusterfs cluster.op-version for {0} already set to {1}'. format(name, current) }) self.assertDictEqual(glusterfs.op_version(name, current), ret) with patch.dict(glusterfs.__opts__, {'test': True}): mock_set_version.return_value = [ False, 'Failed to set version' ] ret.update({'result': None}) ret.update({ 'comment': 'An attempt would be made to set the cluster.op-version for {0} to {1}.' .format(name, new) }) self.assertDictEqual(glusterfs.op_version(name, new), ret) with patch.dict(glusterfs.__opts__, {'test': False}): mock_set_version.return_value = [ False, 'Failed to set version' ] ret.update({'result': False}) ret.update({'comment': 'Failed to set version'}) self.assertDictEqual(glusterfs.op_version(name, new), ret) mock_set_version.return_value = 'some success message' ret.update({'comment': 'some success message'}) ret.update({'changes': {'old': current, 'new': new}}) ret.update({'result': True}) self.assertDictEqual(glusterfs.op_version(name, new), ret)
def test_peer(self): ''' Test if gluster peer call is successful. ''' mock_run = MagicMock() with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}): mock_run.return_value = xml_peer_probe_already_member self.assertTrue(glusterfs.peer('salt')) mock_run.return_value = xml_peer_probe_localhost self.assertTrue(glusterfs.peer('salt')) mock_run.return_value = xml_peer_probe_fail_cant_connect self.assertFalse(glusterfs.peer('salt'))
def test_status(self): mock = MagicMock(return_value="123") with patch.dict(debian_service.__salt__, {"status.pid": mock}): assert debian_service.status("foo", "foobar") mock = MagicMock(return_value=0) with patch.dict(debian_service.__salt__, {"cmd.retcode": mock}): # Test successful command (0 retcode) assert debian_service.status("foo") # Confirm expected command was run mock.assert_called_once_with("service foo status", ignore_retcode=True) # Test unsuccessful command (nonzero retcode) mock.return_value = 1 assert not debian_service.enable("foo") mock = MagicMock( side_effect=lambda x, **y: 0 if x == "service bar status" else 1) get_all = MagicMock(return_value=["foo", "bar", "baz"]) with patch.dict(debian_service.__salt__, {"cmd.retcode": mock}), patch.object( debian_service, "get_all", get_all): ret = debian_service.status("b*") expected = {"bar": True, "baz": False} assert ret == expected, ret
def test_delete_volume(self): """ Test if it deletes a gluster volume. """ mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}}) with patch.object(glusterfs, "info", mock_info): # volume doesn't exist self.assertFalse(glusterfs.delete_volume("Newvolume3")) mock_stop_volume = MagicMock(return_value=True) mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}): with patch.object(glusterfs, "stop_volume", mock_stop_volume): # volume exists, should not be stopped, and is started self.assertFalse( glusterfs.delete_volume("Newvolume1", False)) self.assertFalse(mock_run.called) self.assertFalse(mock_stop_volume.called) # volume exists, should be stopped, and is started self.assertTrue(glusterfs.delete_volume("Newvolume1")) self.assertTrue(mock_run.called) self.assertTrue(mock_stop_volume.called) # volume exists and isn't started mock_info = MagicMock(return_value={"Newvolume1": {"status": "2"}}) with patch.object(glusterfs, "info", mock_info): mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}): self.assertTrue(glusterfs.delete_volume("Newvolume1")) mock_run.return_value = xml_command_fail self.assertFalse(glusterfs.delete_volume("Newvolume1"))
def test_highstate(): """Test transactional_update.highstage""" transactional_update_highstate_mock = MagicMock() transactional_update_highstate_mock.return_value = ( transactional_update_highstate_mock) _create_and_execute_salt_state_mock = MagicMock(return_value="result") opts_mock = { "hash_type": "md5", } salt_mock = { "saltutil.is_running": MagicMock(return_value=[]), } get_sls_opts_mock = MagicMock(return_value=opts_mock) with patch.dict(tu.__opts__, opts_mock), patch.dict( statemod.__salt__, salt_mock ), patch("salt.utils.state.get_sls_opts", get_sls_opts_mock), patch( "salt.fileclient.get_file_client", MagicMock() ), patch( "salt.modules.transactional_update.TransactionalUpdateHighstate", transactional_update_highstate_mock, ), patch( "salt.modules.transactional_update._create_and_execute_salt_state", _create_and_execute_salt_state_mock, ): assert tu.highstate() == "result" _create_and_execute_salt_state_mock.assert_called_once()
def test_add_volume_bricks(self): ''' Test if it add brick(s) to an existing volume ''' mock_info = MagicMock(return_value={ 'Newvolume1': { 'status': '1', 'bricks': { 'brick1': {'path': 'host:/path1'}, 'brick2': {'path': 'host:/path2'} } } }) with patch.object(glusterfs, 'info', mock_info): mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}): # Volume does not exist self.assertFalse(glusterfs.add_volume_bricks('nonExisting', ['bricks'])) # Brick already exists self.assertTrue(glusterfs.add_volume_bricks('Newvolume1', ['host:/path2'])) # Already existing brick as a string self.assertTrue(glusterfs.add_volume_bricks('Newvolume1', 'host:/path2')) self.assertFalse(mock_run.called) # A new brick: self.assertTrue(glusterfs.add_volume_bricks('Newvolume1', ['host:/new1'])) self.assertTrue(mock_run.called) # Gluster call fails mock_run.return_value = xml_command_fail self.assertFalse(glusterfs.add_volume_bricks('Newvolume1', ['new:/path']))
def test_single_queue_true(): """Test transactional_update.single""" ssh_state_mock = MagicMock() ssh_state_mock.return_value = ssh_state_mock ssh_state_mock.verify_data.return_value = None _create_and_execute_salt_state_mock = MagicMock(return_value="result") opts_mock = { "hash_type": "md5", } salt_mock = { "saltutil.is_running": MagicMock(side_effect=[ [{ "fun": "state.running", "pid": "4126", "jid": "20150325123407204096", }], [], ]), } get_sls_opts_mock = MagicMock(return_value=opts_mock) with patch.dict(tu.__opts__, opts_mock), patch.dict( statemod.__salt__, salt_mock ), patch("salt.utils.state.get_sls_opts", get_sls_opts_mock), patch( "salt.fileclient.get_file_client", MagicMock() ), patch("salt.client.ssh.state.SSHState", ssh_state_mock), patch( "salt.modules.transactional_update._create_and_execute_salt_state", _create_and_execute_salt_state_mock, ): assert tu.single("pkg.installed", name="emacs", queue=True) == "result" _create_and_execute_salt_state_mock.assert_called_once()
def test_delete_volume(self): ''' Test if it deletes a gluster volume. ''' mock_info = MagicMock(return_value={'Newvolume1': {'status': '1'}}) with patch.object(glusterfs, 'info', mock_info): # volume doesn't exist self.assertFalse(glusterfs.delete_volume('Newvolume3')) mock_stop_volume = MagicMock(return_value=True) mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}): with patch.object(glusterfs, 'stop_volume', mock_stop_volume): # volume exists, should not be stopped, and is started self.assertFalse( glusterfs.delete_volume('Newvolume1', False)) self.assertFalse(mock_run.called) self.assertFalse(mock_stop_volume.called) # volume exists, should be stopped, and is started self.assertTrue(glusterfs.delete_volume('Newvolume1')) self.assertTrue(mock_run.called) self.assertTrue(mock_stop_volume.called) # volume exists and isn't started mock_info = MagicMock(return_value={'Newvolume1': {'status': '2'}}) with patch.object(glusterfs, 'info', mock_info): mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}): self.assertTrue(glusterfs.delete_volume('Newvolume1')) mock_run.return_value = xml_command_fail self.assertFalse(glusterfs.delete_volume('Newvolume1'))
def test_highstate_queue_true(): """Test transactional_update.highstage""" transactional_update_highstate_mock = MagicMock() transactional_update_highstate_mock.return_value = ( transactional_update_highstate_mock) _create_and_execute_salt_state_mock = MagicMock(return_value="result") opts_mock = { "hash_type": "md5", } salt_mock = { "saltutil.is_running": MagicMock(side_effect=[ [{ "fun": "state.running", "pid": "4126", "jid": "20150325123407204096", }], [], ]), } get_sls_opts_mock = MagicMock(return_value=opts_mock) with patch.dict(tu.__opts__, opts_mock), patch.dict( statemod.__salt__, salt_mock ), patch("salt.utils.state.get_sls_opts", get_sls_opts_mock), patch( "salt.fileclient.get_file_client", MagicMock() ), patch( "salt.modules.transactional_update.TransactionalUpdateHighstate", transactional_update_highstate_mock, ), patch( "salt.modules.transactional_update._create_and_execute_salt_state", _create_and_execute_salt_state_mock, ): assert tu.highstate(queue=True) == "result" _create_and_execute_salt_state_mock.assert_called_once()
def test_check_installed(): mock = MagicMock(return_value="/etc/alternatives/salt") with patch("salt.utils.path.readlink", mock): assert alternatives.check_installed("better-world", "/etc/alternatives/salt") mock.return_value = False assert not alternatives.check_installed("help", "/etc/alternatives/salt")
def test_started(self): """ Test to check if volume has been started """ name = "salt" ret = {"name": name, "result": False, "comment": "", "changes": {}} started_info = {name: {"status": "1"}} stopped_info = {name: {"status": "0"}} mock_info = MagicMock(return_value={}) mock_start = MagicMock(return_value=True) with patch.dict( glusterfs.__salt__, { "glusterfs.info": mock_info, "glusterfs.start_volume": mock_start }, ): comt = "Volume {0} does not exist".format(name) ret.update({"comment": comt}) self.assertDictEqual(glusterfs.started(name), ret) mock_info.return_value = started_info comt = "Volume {0} is already started".format(name) ret.update({"comment": comt, "result": True}) self.assertDictEqual(glusterfs.started(name), ret) with patch.dict(glusterfs.__opts__, {"test": True}): mock_info.return_value = stopped_info comt = "Volume {0} will be started".format(name) ret.update({"comment": comt, "result": None}) self.assertDictEqual(glusterfs.started(name), ret) with patch.dict(glusterfs.__opts__, {"test": False}): comt = "Volume {0} is started".format(name) ret.update({ "comment": comt, "result": True, "change": { "new": "started", "old": "stopped" }, }) self.assertDictEqual(glusterfs.started(name), ret)
def test_rm(): """ Test the etcd_mod.set state function """ get_mock = MagicMock() rm_mock = MagicMock() dunder_salt = { "etcd.get": get_mock, "etcd.rm": rm_mock, } with patch.dict(etcd_state.__salt__, dunder_salt): # Test removing a key get_mock.return_value = "value" rm_mock.return_value = True expected = { "name": "key", "comment": "Key removed", "result": True, "changes": { "key": "Deleted" }, } assert etcd_state.rm("key") == expected # Test failing to remove an existing key get_mock.return_value = "value" rm_mock.return_value = False expected = { "name": "key", "comment": "Unable to remove key", "result": True, "changes": {}, } assert etcd_state.rm("key") == expected # Test removing a nonexistent key get_mock.return_value = False expected = { "name": "key", "comment": "Key does not exist", "result": True, "changes": {}, } assert etcd_state.rm("key") == expected
def test_check_installed(self): mock = MagicMock(return_value='/etc/alternatives/salt') with patch('salt.utils.path.readlink', mock): self.assertTrue( alternatives.check_installed('better-world', '/etc/alternatives/salt')) mock.return_value = False self.assertFalse( alternatives.check_installed('help', '/etc/alternatives/salt'))
def test_attach_virtual_host_ip_found(self): mock_get_ip = MagicMock() mock_get_ip.return_value = '192.168.15.1' mock_retcode = MagicMock() mock_retcode.return_value = 0 with patch.dict(netweavermod.__salt__, { 'hosts.get_ip': mock_get_ip, 'cmd.retcode': mock_retcode }): ip_address = netweavermod.attach_virtual_host('vhost') mock_get_ip.assert_called_once_with('vhost') mock_retcode.assert_called_once_with('ip a | grep 192.168.15.1/24', python_shell=True) assert ip_address == '192.168.15.1'
def test_started(self): ''' Test to check if volume has been started ''' name = 'salt' ret = {'name': name, 'result': False, 'comment': '', 'changes': {}} started_info = {name: {'status': '1'}} stopped_info = {name: {'status': '0'}} mock_info = MagicMock(return_value={}) mock_start = MagicMock(return_value=True) with patch.dict(glusterfs.__salt__, { 'glusterfs.info': mock_info, 'glusterfs.start_volume': mock_start }): comt = ('Volume {0} does not exist'.format(name)) ret.update({'comment': comt}) self.assertDictEqual(glusterfs.started(name), ret) mock_info.return_value = started_info comt = ('Volume {0} is already started'.format(name)) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(glusterfs.started(name), ret) with patch.dict(glusterfs.__opts__, {'test': True}): mock_info.return_value = stopped_info comt = ('Volume {0} will be started'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(glusterfs.started(name), ret) with patch.dict(glusterfs.__opts__, {'test': False}): comt = 'Volume {0} is started'.format(name) ret.update({ 'comment': comt, 'result': True, 'change': { 'new': 'started', 'old': 'stopped' } }) self.assertDictEqual(glusterfs.started(name), ret)
def test_reload_(self): mock = MagicMock(return_value=0) with patch.dict(debian_service.__salt__, {"cmd.retcode": mock}): # Test successful command (0 retcode) assert debian_service.reload_("foo") # Confirm expected command was run mock.assert_called_once_with("service foo reload") # Test unsuccessful command (nonzero retcode) mock.return_value = 1 assert not debian_service.reload_("foo")
def test_create_volume(self): ''' Test if it creates a glusterfs volume. ''' mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}): self.assertRaises( SaltInvocationError, glusterfs.create_volume, 'newvolume', 'host1:brick') self.assertRaises( SaltInvocationError, glusterfs.create_volume, 'newvolume', 'host1/brick') self.assertFalse(mock_run.called) mock_start_volume = MagicMock(return_value=True) with patch.object(glusterfs, 'start_volume', mock_start_volume): # Create, do not start self.assertTrue(glusterfs.create_volume('newvolume', 'host1:/brick')) self.assertFalse(mock_start_volume.called) # Create and start self.assertTrue(glusterfs.create_volume('newvolume', 'host1:/brick', start=True)) self.assertTrue(mock_start_volume.called) mock_start_volume.return_value = False # Create and fail start self.assertFalse(glusterfs.create_volume('newvolume', 'host1:/brick', start=True)) mock_run.return_value = xml_command_fail self.assertFalse(glusterfs.create_volume('newvolume', 'host1:/brick', True, True, True, 'tcp', True))
def test_enable(self): mock = MagicMock(return_value=0) with patch.dict(debian_service.__salt__, {"cmd.retcode": mock}): # Test successful command (0 retcode) assert debian_service.enable("foo") # Confirm expected command was run mock.assert_called_once_with( "insserv foo && update-rc.d foo enable", python_shell=True) # Test unsuccessful command (nonzero retcode) mock.return_value = 1 assert not debian_service.enable("foo")
def test_attach_virtual_host_not_ip(self): mock_get_ip = MagicMock() mock_get_ip.return_value = None with pytest.raises(exceptions.CommandExecutionError) as err: with patch.dict(netweavermod.__salt__, {'hosts.get_ip': mock_get_ip}): netweavermod.attach_virtual_host('vhost') mock_get_ip.assert_called_once_with('vhost') assert 'virtual host vhost not available' in str(err.value)
def test_create_volume(self): """ Test if it creates a glusterfs volume. """ mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}): self.assertRaises(SaltInvocationError, glusterfs.create_volume, "newvolume", "host1:brick") self.assertRaises(SaltInvocationError, glusterfs.create_volume, "newvolume", "host1/brick") self.assertFalse(mock_run.called) mock_start_volume = MagicMock(return_value=True) with patch.object(glusterfs, "start_volume", mock_start_volume): # Create, do not start self.assertTrue( glusterfs.create_volume("newvolume", "host1:/brick")) self.assertFalse(mock_start_volume.called) # Create and start self.assertTrue( glusterfs.create_volume("newvolume", "host1:/brick", start=True)) self.assertTrue(mock_start_volume.called) mock_start_volume.return_value = False # Create and fail start self.assertFalse( glusterfs.create_volume("newvolume", "host1:/brick", start=True)) mock_run.return_value = xml_command_fail self.assertFalse( glusterfs.create_volume("newvolume", "host1:/brick", True, True, True, "tcp", True))
def mock_open(data=None): """ Mock "open" function in a simple way. :param data: :return: """ data = io.StringIO(data) mock = MagicMock(spec=io.FileIO) handle = MagicMock(spec=io.FileIO) handle.write.return_value = None handle.__enter__.return_value = data or handle mock.return_value = handle return mock
def test_sls_queue_false_failing(): """Test transactional_update.sls""" transactional_update_highstate_mock = MagicMock() transactional_update_highstate_mock.return_value = ( transactional_update_highstate_mock) transactional_update_highstate_mock.render_highstate.return_value = (None, []) transactional_update_highstate_mock.state.reconcile_extend.return_value = ( None, []) transactional_update_highstate_mock.state.requisite_in.return_value = ( None, []) transactional_update_highstate_mock.state.verify_high.return_value = [] _create_and_execute_salt_state_mock = MagicMock(return_value="result") opts_mock = { "hash_type": "md5", } salt_mock = { "saltutil.is_running": MagicMock(side_effect=[ [{ "fun": "state.running", "pid": "4126", "jid": "20150325123407204096", }], [], ]), } get_sls_opts_mock = MagicMock(return_value=opts_mock) with patch.dict(tu.__opts__, opts_mock), patch.dict( statemod.__salt__, salt_mock ), patch("salt.utils.state.get_sls_opts", get_sls_opts_mock), patch( "salt.fileclient.get_file_client", MagicMock() ), patch( "salt.modules.transactional_update.TransactionalUpdateHighstate", transactional_update_highstate_mock, ), patch( "salt.modules.transactional_update._create_and_execute_salt_state", _create_and_execute_salt_state_mock, ): assert tu.sls("module", queue=False) == [ 'The function "state.running" is running as PID 4126 and was started at 2015, Mar 25 12:34:07.204096 with jid 20150325123407204096' ] _create_and_execute_salt_state_mock.assert_not_called()
def test_set(): """ Test the etcd_mod.set state function """ get_mock = MagicMock() set_mock = MagicMock() dunder_salt = { "etcd.get": get_mock, "etcd.set": set_mock, } with patch.dict(etcd_state.__salt__, dunder_salt): # Test new key creation get_mock.return_value = None set_mock.return_value = "new value" expected = { "name": "new_key", "comment": "New key created", "result": True, "changes": { "new_key": "new value" }, } assert etcd_state.set_("new_key", "new value", profile="test") == expected # Test key updating get_mock.return_value = "old value" set_mock.return_value = "new value" expected = { "name": "new_key", "comment": "Key value updated", "result": True, "changes": { "new_key": "new value" }, } assert etcd_state.set_("new_key", "new value", profile="test") == expected # Test setting the same value to a key get_mock.return_value = "value" set_mock.return_value = "value" expected = { "name": "key", "comment": "Key contains correct value", "result": True, "changes": {}, } assert etcd_state.set_("key", "value", profile="test") == expected
def test_add_volume_bricks(self): """ Test if it add brick(s) to an existing volume """ mock_info = MagicMock( return_value={ "Newvolume1": { "status": "1", "bricks": { "brick1": { "path": "host:/path1" }, "brick2": { "path": "host:/path2" }, }, } }) with patch.object(glusterfs, "info", mock_info): mock_run = MagicMock(return_value=xml_command_success) with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}): # Volume does not exist self.assertFalse( glusterfs.add_volume_bricks("nonExisting", ["bricks"])) # Brick already exists self.assertTrue( glusterfs.add_volume_bricks("Newvolume1", ["host:/path2"])) # Already existing brick as a string self.assertTrue( glusterfs.add_volume_bricks("Newvolume1", "host:/path2")) self.assertFalse(mock_run.called) # A new brick: self.assertTrue( glusterfs.add_volume_bricks("Newvolume1", ["host:/new1"])) self.assertTrue(mock_run.called) # Gluster call fails mock_run.return_value = xml_command_fail self.assertFalse( glusterfs.add_volume_bricks("Newvolume1", ["new:/path"]))