def test_exec_cmd_fail_prompt_opt_yes(mock_subp): # GIVEN a command that exits non-zero cmd_args = ["foobar"] stdout = "This is output" stderr = "This is error" job_mock = _get_job_mock(1, stdout, stderr) mock_subp.return_value = job_mock # WHEN the command is executed with opts_yes = TRUE # THEN a Fatal is raised with pytest.raises(ops_utils.Fatal): ops_utils.exec_cmd_fail_prompt(cmd_args, opt_yes=True)
def test_exec_cmd_fail_prompt_no(mock_subp, mock_readline, mock_select): # GIVEN a command that exits non-zero cmd_args = ["foobar"] stdout = "This is output" stderr = "This is error" job_mock = _get_job_mock(1, stdout, stderr) mock_subp.return_value = job_mock mock_select.return_value = (True, None, None) # WHEN the command is executed and we answer "n" to continue mock_readline.return_value = "n" # THEN a Fatal is raised with pytest.raises(ops_utils.Fatal): ops_utils.exec_cmd_fail_prompt(cmd_args)
def test_exec_cmd_fail_prompt_many_bad(mock_subp, mock_readline, mock_select): # GIVEN a command that exits non-zero cmd_args = ["foobar"] stdout = "This is output" stderr = "This is error" job_mock = _get_job_mock(1, stdout, stderr) mock_subp.return_value = job_mock mock_select.return_value = (True, None, None) # WHEN the command is executed and we give non-y/n answers mock_readline.return_value = "x" # THEN a Fatal is raised after trying five times with pytest.raises(ops_utils.Fatal): ops_utils.exec_cmd_fail_prompt(cmd_args) assert mock_readline.call_count == 5
def test_exec_cmd_fail_prompt_opt_force(mock_subp, mock_readline, mock_select): # GIVEN a command that exits non-zero cmd_args = ["foobar"] stdout = "This is output" stderr = "This is error" job_mock = _get_job_mock(1, stdout, stderr) mock_subp.return_value = job_mock mock_select.return_value = (True, None, None) # WHEN the command is executed with opt_force = True exit_status, _, _ = ops_utils.exec_cmd_fail_prompt(cmd_args, opt_force=True) # THEN we receive the expected results of the command without prompting assert exit_status == 1 assert mock_readline.called is False assert mock_select.called is False
def test_exec_cmd_fail_prompt_yes(mock_subp, mock_readline, mock_select): # GIVEN a command that exits non-zero cmd_args = ["foobar"] stdout = "This is output" stderr = "This is error" job_mock = _get_job_mock(1, stdout, stderr) mock_subp.return_value = job_mock mock_select.return_value = (True, None, None) # WHEN the command is executed and we answer "y" to continue mock_readline.return_value = "y" exit_status, out, err = ops_utils.exec_cmd_fail_prompt(cmd_args) # THEN we receive the expected results of the command assert mock_readline.called assert exit_status == 1 assert out == stdout assert err == stderr