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_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_pvc_creation_wait(self, mock_create_stream, _phase, _exception): _kind = 'PersistentVolumeClaim' # Desired state: args = dict(state='present', kind=_kind, wait=True, name='testvmi', namespace='vms', api_version='v1') set_module_args(args) # Mock pre-change state: Resource.get.return_value = None # Resource does NOT initially exist in cluster resource_args = dict(kind=_kind, **RESOURCE_DEFAULT_ARGS) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) # Mock post-change state: stream_obj = dict( status=dict(phase=_phase), metadata=dict( annotations={'cdi.kubevirt.io/storage.pod.phase': 'Succeeded' }), method='create', changed=True, ) mock_watcher = MagicMock() mock_create_stream.return_value = (mock_watcher, [dict(object=stream_obj)]) # Actual test: with pytest.raises(_exception) as result: mymodule.KubeVirtVM().execute_module() if _exception is AnsibleExitJson: assert result.value[0]['method'] == 'create' and result.value[0][ 'changed'] == True
def test_missing_url(self): set_module_args({ 'name': 'fedora-rawhide', }) with pytest.raises(AnsibleFailJson) as exit: koji_external_repo.main() result = exit.value.args[0] assert result['msg'] == 'you must set a url for this external_repo'
def test_docker_arch_fail(self, container_module_args): container_module_args['content_type'] = 'Docker' container_module_args['arch'] = 'x86_64' set_module_args(container_module_args) with pytest.raises(AnsibleFailJson) as exit: main() result = exit.value.args[0] assert result['msg'] == 'arch must be "multi" for Docker repos'
def test_install_already_present(self): set_module_args({ 'name': 'foo', 'state': 'present', }) self.set_side_effect( {'/usr/bin/aura --query --info foo': (0, "Name : foo", None)}) self.execute_module()
def test_missing_arches(self): set_module_args({ 'name': 'builder2', }) with pytest.raises(AnsibleFailJson) as exit: koji_host.main() result = exit.value.args[0] assert result['msg'] == 'missing required arguments: arches'
def test_docker_tps_fail(self, container_module_args): container_module_args['content_type'] = 'Docker' container_module_args['use_for_tps'] = True set_module_args(container_module_args) with pytest.raises(AnsibleFailJson) as ex: main() result = ex.value.args[0] assert result['msg'] == 'do not set "use_for_tps" for Docker repos'
def test_simple(monkeypatch): session = FakeKojiSession() monkeypatch.setattr(koji_btype.common_koji, 'get_session', lambda x: session) set_module_args({'name': 'debian'}) with pytest.raises(AnsibleExitJson) as exit: koji_btype.main() result = exit.value.args[0] assert result['changed'] is True
def test_default_arch(self, container_module_args, fake_ensure_cdn_repo, content_type, expected): container_module_args['arch'] = None container_module_args['content_type'] = content_type set_module_args(container_module_args) with pytest.raises(AnsibleExitJson): main() _, _, params = fake_ensure_cdn_repo.args assert params['arch'] == expected
def test_absent(self): set_module_args({ 'name': 'centos-7-cr', 'state': 'absent', }) with pytest.raises(AnsibleExitJson) as exit: koji_external_repo.main() result = exit.value.args[0] assert result['changed'] is True assert result['stdout'] == 'deleted external repo centos-7-cr'
def test_install_version_present_is_latest(self): set_module_args({ 'name': 'foo', 'state': 'latest', }) self.set_side_effect({ '/usr/bin/aura --query --info foo': (0, "Version : 1", None), '/usr/bin/aura --aursync --info foo': (0, "Version : 1", None), }) self.execute_module()
def test_simple(self): set_module_args({ 'name': 'fedora-rawhide', 'url': 'http://mirror.example.com/fedora/rawhide/x86_64/', }) with pytest.raises(AnsibleExitJson) as exit: koji_external_repo.main() result = exit.value.args[0] assert result['changed'] is True assert result['stdout_lines'] == ['created repo id 2']
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(self): set_module_args({ 'name': 'builder2', 'arches': ['x86_64'], }) with pytest.raises(AnsibleExitJson) as exit: koji_host.main() result = exit.value.args[0] assert result['changed'] is True assert result['stdout_lines'] == ['created host']
def test_absent(monkeypatch): session = FakeKojiSession() monkeypatch.setattr(koji_btype.common_koji, 'get_session', lambda x: session) set_module_args({ 'name': 'debian', 'state': 'absent', }) with pytest.raises(AnsibleFailJson) as exit: koji_btype.main() result = exit.value.args[0] assert result['msg'] == 'Cannot remove Koji build types.'
def test_install_older_version_on_aur(self): set_module_args({ 'name': 'foo', 'state': 'latest', }) self.set_side_effect({ '/usr/bin/aura --query --info foo': (0, "Version : 2", None), '/usr/bin/aura --aursync --info foo': (0, "Version : 1", None), '/usr/bin/aura --aursync --builduser=nobody foo --noconfirm': (0, "Determining dependencies...", None) }) self.execute_module(changed=True)
def test_get_json(self, client): url = 'https://errata.devel.redhat.com/api/v1/user/cooldeveloper' client.adapter.register_uri( 'GET', url, json={'login_name': '*****@*****.**'}) set_module_args({'path': '/api/v1/user/cooldeveloper'}) with pytest.raises(AnsibleExitJson) as exit: main() result = exit.value.args[0] assert result['changed'] is True assert result['status'] == 200 assert result['url'] == url assert result['json'] == {'login_name': '*****@*****.**'}
def test_get_html(self, client): url = 'https://errata.devel.redhat.com/products/new' client.adapter.register_uri('GET', url, text='<html>new products form</html>') set_module_args({'path': '/products/new'}) with pytest.raises(AnsibleExitJson) as exit: main() result = exit.value.args[0] assert result['changed'] is True assert result['status'] == 200 assert result['url'] == url assert 'content' not in result
def test_get_contents(self, client): url = 'https://errata.devel.redhat.com/products/new' client.adapter.register_uri('GET', url, text='<html>new products form</html>') set_module_args({ 'path': '/products/new', 'return_content': True, }) with pytest.raises(AnsibleExitJson) as exit: main() result = exit.value.args[0] assert result['content'] == '<html>new products form</html>'
def setUp(self): super(self.__class__, self).setUp() self.module = nuagex_lab utils.set_module_args({ 'name': 'integration-tests', 'state': 'absent', 'nuagex_auth': { 'username': NUAGEX_USERNAME, 'password': NUAGEX_PASSWORD } }) if not self.is_casette_recording(): self.prevent_sleeping()
def test_invalid_input(self, monkeypatch, module_args): """ When validate_params() raises InvalidInputError, the Ansible module exits with the proper error message. """ module_args['valid_bug_states'] = ['BOGUS_STATE_LOL'] set_module_args(module_args) with pytest.raises(AnsibleFailJson) as ex: main() result = ex.value.args[0] assert result['changed'] is False expected = 'invalid valid_bug_states value "BOGUS_STATE_LOL"' assert result['msg'] == expected
def test_simple_rpms(self): module_args = { 'name': 'rhceph-4-tools-for-rhel-8-x86_64-rpms', 'release_type': 'Primary', 'content_type': 'Binary', 'use_for_tps': True, 'arch': 'x86_64', 'variants': ['8Base-RHCEPH-4.0-Tools', '8Base-RHCEPH-4.1-Tools'], } set_module_args(module_args) with pytest.raises(AnsibleExitJson) as exit: main() result = exit.value.args[0] assert result['changed'] is True
def test_simple_layered(self): module_args = { 'name': '8Base-RHCEPH-4.0-Tools', 'description': 'Red Hat Ceph Storage 4.0 Tools', 'cpe': None, 'rhel_variant': '8Base', 'push_targets': ['cdn'], 'buildroot': False, 'product_version': 'RHCEPH-4.0-RHEL-8', } 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_stream_creation(self, mock_watch): _name = 'test-vmirs' # Desired state: args = dict(name=_name, namespace='vms', replicas=2, wait=True) set_module_args(args) # Mock pre-change state: resource_args = dict( kind='VirtualMachineInstanceReplicaSet', **RESOURCE_DEFAULT_ARGS ) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) res_inst = ResourceInstance('', dict(metadata = {'name': _name}, spec = {'replicas': 3})) Resource.get.return_value = res_inst # Actual test: mock_watch.side_effect = KubernetesException("Test", value=42) with pytest.raises(AnsibleFailJson) as result: mymodule.KubeVirtScaleVMIRS().execute_module()
def test_scale_vmirs_nowait(self, _replicas, _changed): _name = 'test-vmirs' # Desired state: args = dict(name=_name, namespace='vms', replicas=2, wait=False) set_module_args(args) # Mock pre-change state: resource_args = dict( kind='VirtualMachineInstanceReplicaSet', **RESOURCE_DEFAULT_ARGS ) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) res_inst = ResourceInstance('', dict(metadata = {'name': _name}, spec = {'replicas': _replicas})) Resource.get.return_value = res_inst # Actual test: with pytest.raises(AnsibleExitJson) as result: mymodule.KubeVirtScaleVMIRS().execute_module() assert result.value[0]['changed'] == _changed
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_resource_absent(self, _kind, _wait): # Desired state: args = dict(state='absent', kind=_kind, wait=_wait, name='testvmi', namespace='vms', api_version='v1') set_module_args(args) # Current state (mock): Resource.get.return_value = None # Resource does NOT exist in cluster resource_args = dict(kind=_kind, **RESOURCE_DEFAULT_ARGS) K8sAnsibleMixin.find_resource.return_value = Resource(**resource_args) # Actual test: with pytest.raises(AnsibleExitJson) as result: mymodule.KubeVirtVM().execute_module() assert result.value[0]['method'] == 'delete' and result.value[0][ 'changed'] == False