def mock_ensure_variant(self, monkeypatch): """ Fake this large method, since we unit-test it individually elsewhere. """ mock_ensure = Mock() mock_ensure.return_value = {'changed': True} monkeypatch.setattr(errata_tool_variant, 'ensure_variant', mock_ensure) return mock_ensure
def test_handle_form_error_500(self, client): mock_response = Mock() mock_response.status_code = 500 mock_response.url = PROD mock_response.text = 'Some error' expected = ( 'The request to https://errata.devel.redhat.com had a status code ' 'of 500 and failed with: Some error') with pytest.raises(RuntimeError, match=expected): handle_form_errors(mock_response)
def test_simple(self, monkeypatch, module_args): """ Create a simple minimal product. """ mock_ensure = Mock() mock_ensure.return_value = {'changed': True} monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure) set_module_args(module_args) with pytest.raises(AnsibleExitJson) as ex: main() result = ex.value.args[0] assert result['changed'] is True
def test_simple_async(self, monkeypatch): mock_ensure = Mock() mock_ensure.return_value = {'changed': True} monkeypatch.setattr(errata_tool_release, 'ensure_release', mock_ensure) module_args = { 'name': 'rhceph-5.0', 'type': 'Async', 'description': 'Red Hat Ceph Storage 5.0', 'program_manager': '*****@*****.**', 'product_versions': [], } set_module_args(module_args) with pytest.raises(AnsibleExitJson) as ex: main() result = ex.value.args[0] assert result['changed'] is True
def test_missing_doc_reviewer(self, monkeypatch, module_args): """ When ensure_product() raises DocsReviewerNotFoundError, the Ansible module exits with the proper error message. """ mock_ensure = Mock() mock_ensure.side_effect = \ DocsReviewerNotFoundError('*****@*****.**') monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure) module_args['default_docs_reviewer'] = '*****@*****.**', set_module_args(module_args) with pytest.raises(AnsibleFailJson) as ex: main() result = ex.value.args[0] assert result['changed'] is False expected = 'default_docs_reviewer [email protected] account not found' assert result['msg'] == expected
def test_missing_program_manager(self, monkeypatch): mock_ensure = Mock() mock_ensure.side_effect = \ ProgramManagerNotFoundError('*****@*****.**') monkeypatch.setattr(errata_tool_release, 'ensure_release', mock_ensure) module_args = { 'name': 'rhceph-5.0', 'type': 'Async', 'description': 'Red Hat Ceph Storage 5.0', 'program_manager': '*****@*****.**', 'product_versions': [], } set_module_args(module_args) with pytest.raises(AnsibleFailJson) as ex: main() result = ex.value.args[0] assert result['changed'] is False expected = 'program_manager [email protected] account not found' assert result['msg'] == expected
def test_strict_user_check_missing_user(self, monkeypatch, module_args): """ Test that the module fails when in strict user check mode and the user doesn't exist. """ monkeypatch.setenv('ANSIBLE_STRICT_USER_CHECK_MODE', 'True') module_args['program_manager'] = '*****@*****.**' module_args['_ansible_check_mode'] = True set_module_args(module_args) mock_ensure = Mock() mock_ensure.return_value = {'changed': True} monkeypatch.setattr(errata_tool_release, 'ensure_release', mock_ensure) mock_get_user = Mock() mock_get_user.side_effect = UserNotFoundError('*****@*****.**') monkeypatch.setattr(common_errata_tool, 'get_user', mock_get_user) with pytest.raises(AnsibleFailJson) as ex: main() result = ex.value.args[0] assert result['changed'] is False expected = 'program_manager [email protected] account not found' assert result['msg'] == expected
def test_strict_user_check_missing_role(self, monkeypatch, module_args): """ Test that the module fails when in strict user check mode and the user doesn't have the 'docs' role. """ monkeypatch.setenv('ANSIBLE_STRICT_USER_CHECK_MODE', 'True') module_args['default_docs_reviewer'] = '*****@*****.**' module_args['_ansible_check_mode'] = True set_module_args(module_args) mock_ensure = Mock() mock_ensure.return_value = {'changed': True} monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure) mock_get_user = Mock() mock_get_user.return_value = {'roles': ['qa']} monkeypatch.setattr(common_errata_tool, 'get_user', mock_get_user) with pytest.raises(AnsibleFailJson) as ex: main() result = ex.value.args[0] assert result['changed'] is False expected = "User [email protected] does not have 'docs' role in ET" assert result['msg'] == expected
def test___mul__(self, matrix_name, input_values, output_values, result_values): mock = Mock(input_values, []) mx_mul.input = mock.mocked_input mx_mul.print = mock.mocked_print matrix = mx_mul.Matrix(matrix_name) matrix.load_values() result = matrix * matrix assert result == result_values assert mock.output == output_values
def test___init__(self, error_message, matrix_name, input_values, output_values): mock = Mock(input_values, []) mx_mul.input = mock.mocked_input mx_mul.print = mock.mocked_print try: matrix = mx_mul.Matrix(matrix_name) except mx_mul.Error as e: assert e.message == error_message else: assert vars(matrix) == {'name': matrix_name, 'width': int(input_values[0]), 'height': int(input_values[1]), 'values': []} finally: assert mock.output == output_values
def test_load_values(self, error_message, matrix_name, input_values, output_values): mock = Mock(input_values, []) mx_mul.input = mock.mocked_input mx_mul.print = mock.mocked_print matrix = mx_mul.Matrix(matrix_name) try: matrix.load_values() except mx_mul.Error as e: assert vars(e) == {'message': error_message, 'error_code': 1} else: attributes = vars(matrix) assert attributes['name'] == matrix_name assert attributes['width'] == int(input_values[0]) assert attributes['height'] == int(input_values[1]) for i in range(len(attributes['values'])): assert attributes['values'][i] == [float(item) for item in input_values[2:][i].split()] finally: assert mock.output == output_values