def test_chgroups(self): ''' Test if user groups changed ''' mock = MagicMock(return_value=['wheel', 'root']) with patch.object(useradd, 'list_groups', mock): self.assertTrue(useradd.chgroups('foo', 'wheel,root')) mock = MagicMock(return_value=['wheel', 'root']) with patch.object(useradd, 'list_groups', mock): with patch.dict(useradd.__grains__, {'kernel': 'OpenBSD'}): mock_runall = MagicMock(return_value={ 'retcode': False, 'stderr': '' }) with patch.dict(useradd.__salt__, {'cmd.run_all': mock_runall}): self.assertTrue(useradd.chgroups('foo', 'wheel,test,root')) mock_runall = MagicMock(return_value={ 'retcode': True, 'stderr': '' }) with patch.dict(useradd.__salt__, {'cmd.run_all': mock_runall}): self.assertFalse(useradd.chgroups('foo', 'wheel,test,root'))
def test_chgroups(self): """ Test if user groups changed """ mock = MagicMock(return_value=["wheel", "root"]) with patch.object(useradd, "list_groups", mock): self.assertTrue(useradd.chgroups("foo", "wheel,root")) mock = MagicMock(return_value=["wheel", "root"]) with patch.object(useradd, "list_groups", mock): with patch.dict(useradd.__grains__, {"kernel": "OpenBSD"}): mock_runall = MagicMock(return_value={ "retcode": False, "stderr": "" }) with patch.dict(useradd.__salt__, {"cmd.run_all": mock_runall}): self.assertTrue(useradd.chgroups("foo", "wheel,test,root")) mock_runall = MagicMock(return_value={ "retcode": True, "stderr": "" }) with patch.dict(useradd.__salt__, {"cmd.run_all": mock_runall}): self.assertFalse(useradd.chgroups("foo", "wheel,test,root"))
def test_chgroups(): # groups matched - no command run mock = MagicMock() with patch.object(useradd, "list_groups", MagicMock(return_value=["wheel", "root"])), patch.dict( useradd.__salt__, {"cmd.run_all": mock}): assert useradd.chgroups("Salt", "wheel,root") is True mock.assert_not_called() # command found and successful run mock = MagicMock(return_value={"retcode": 0}) with patch("salt.utils.path.which", MagicMock(return_value="/sbin/usermod")), patch.dict( useradd.__salt__, {"cmd.run_all": mock}): assert useradd.chgroups("Salt", "wheel,root") is True mock.assert_called_once_with(["/sbin/usermod", "-G", "wheel,root", "Salt"], python_shell=False) # command found and unsuccessful run mock = MagicMock(return_value={"retcode": 1, "stderr": ""}) with patch("salt.utils.path.which", MagicMock(return_value="/sbin/usermod")), patch.dict( useradd.__salt__, {"cmd.run_all": mock}): assert useradd.chgroups("Salt", "wheel,root") is False mock.assert_called_once_with(["/sbin/usermod", "-G", "wheel,root", "Salt"], python_shell=False) # command not found mock = MagicMock() with patch("salt.utils.path.which", MagicMock(return_value=None)), patch.dict( useradd.__salt__, {"cmd.run_all": mock}): with pytest.raises(CommandExecutionError): useradd.chgroups("Salt", "wheel,root") mock.assert_not_called()
def test_chgroups(self): ''' Test if user groups changed ''' mock = MagicMock(return_value=['wheel', 'root']) with patch.object(useradd, 'list_groups', mock): self.assertTrue(useradd.chgroups('foo', 'wheel,root')) mock = MagicMock(return_value=['wheel', 'root']) with patch.object(useradd, 'list_groups', mock): with patch.dict(useradd.__grains__, {'kernel': 'OpenBSD'}): mock_runall = MagicMock(return_value={'retcode': False, 'stderr': ''}) with patch.dict(useradd.__salt__, {'cmd.run_all': mock_runall}): self.assertTrue(useradd.chgroups('foo', 'wheel,test,root')) mock_runall = MagicMock(return_value={'retcode': True, 'stderr': ''}) with patch.dict(useradd.__salt__, {'cmd.run_all': mock_runall}): self.assertFalse(useradd.chgroups('foo', 'wheel,test,root'))