def test_assign_cmd_failed(): """ Tests if the assignment was successful or not """ with patch("os.path.exists", MagicMock(return_value=True)): cmd = { "pid": 1337, "retcode": 0, "stderr": 'sysctl: setting key "net.ipv4.ip_forward": Invalid argument', "stdout": "net.ipv4.ip_forward = backward", } mock_cmd = MagicMock(return_value=cmd) with patch.dict(linux_sysctl.__salt__, {"cmd.run_all": mock_cmd}): with pytest.raises(CommandExecutionError): linux_sysctl.assign("net.ipv4.ip_forward", "backward")
def test_assign_proc_sys_failed(): """ Tests if /proc/sys/<kernel-subsystem> exists or not """ with patch("os.path.exists", MagicMock(return_value=False)): cmd = { "pid": 1337, "retcode": 0, "stderr": "", "stdout": "net.ipv4.ip_forward = 1", } mock_cmd = MagicMock(return_value=cmd) with patch.dict(linux_sysctl.__salt__, {"cmd.run_all": mock_cmd}): with pytest.raises(CommandExecutionError): linux_sysctl.assign("net.ipv4.ip_forward", 1)
def test_assign_success(self): ''' Tests the return of successful assign function ''' cmd = {'pid': 1337, 'retcode': 0, 'stderr': '', 'stdout': 'net.ipv4.ip_forward = 1'} ret = {'net.ipv4.ip_forward': '1'} mock_cmd = MagicMock(return_value=cmd) with patch.dict(linux_sysctl.__salt__, {'cmd.run_all': mock_cmd}): self.assertEqual(linux_sysctl.assign( 'net.ipv4.ip_forward', 1), ret)
def test_assign_ignore(self): """ Tests the ignore assign function """ with patch("os.path.exists", MagicMock(return_value=True)): cmd = {"pid": 1337, "retcode": 0, "stderr": "", "stdout": ""} ret = {"net.ipv4.ip_forward": "ignored"} mock_cmd = MagicMock(return_value=cmd) with patch.dict(linux_sysctl.__salt__, {"cmd.run_all": mock_cmd}): self.assertEqual( linux_sysctl.assign("net.ipv4.ip_forward", 1, ignore=True), ret)
def test_assign_success(): """ Tests the return of successful assign function """ with patch("os.path.exists", MagicMock(return_value=True)): cmd = { "pid": 1337, "retcode": 0, "stderr": "", "stdout": "net.ipv4.ip_forward = 1", } ret = {"net.ipv4.ip_forward": "1"} mock_cmd = MagicMock(return_value=cmd) with patch.dict(linux_sysctl.__salt__, {"cmd.run_all": mock_cmd}): assert linux_sysctl.assign("net.ipv4.ip_forward", 1) == ret