diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index 55917cacf2..24a14e5713 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -123,12 +123,12 @@ class TowerAPIModule(TowerModule): if name_field in new_data: self.fail_json(msg="You can't specify the field {0} in your search data if using the name_or_id field".format(name_field)) - new_data['or__{0}'.format(name_field)] = name_or_id try: new_data['or__id'] = int(name_or_id) + new_data['or__{0}'.format(name_field)] = name_or_id except ValueError: # If we get a value error, then we didn't have an integer so we can just pass and fall down to the fail - pass + new_data[name_field] = name_or_id new_kwargs['data'] = new_data response = self.get_endpoint(endpoint, **new_kwargs) @@ -160,11 +160,13 @@ class TowerAPIModule(TowerModule): def fail_wanted_one(self, response, endpoint, query_params): sample = response.copy() - if len(sample['json']['results']) < 1: + if len(sample['json']['results']) > 1: sample['json']['results'] = sample['json']['results'][:2] + ['...more results snipped...'] + url = self.build_url(endpoint, query_params) + display_endpoint = url.geturl()[len(self.host):] # truncate to not include the base URL self.fail_json( msg="Request to {0} returned {1} items, expected 1".format( - self.build_url(endpoint, query_params).geturl(), response['json']['count'] + display_endpoint, response['json']['count'] ), query=query_params, response=sample, diff --git a/awx_collection/test/awx/test_module_utils.py b/awx_collection/test/awx/test_module_utils.py index 36e5d2509b..a1f0b77594 100644 --- a/awx_collection/test/awx/test_module_utils.py +++ b/awx_collection/test/awx/test_module_utils.py @@ -126,6 +126,7 @@ def test_conflicting_name_and_id(run_module, admin_user): ) assert team.organization.name == 'foo' + def test_multiple_lookup(run_module, admin_user): org1 = Organization.objects.create(name='foo') org2 = Organization.objects.create(name='bar') @@ -140,4 +141,3 @@ def test_multiple_lookup(run_module, admin_user): assert 'foo' in result['msg'] assert 'returned 2 items, expected 1' in result['msg'] assert 'query' in result - diff --git a/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml b/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml index 16eac5b66a..b3278944e9 100644 --- a/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml @@ -288,7 +288,7 @@ - name: Create an invalid SSH credential (Organization not found) tower_credential: name: SSH Credential - organization: Missing Organization + organization: Missing_Organization state: present kind: ssh username: joe @@ -298,7 +298,9 @@ - assert: that: - "result is failed" - - "'The organizations Missing Organization was not found on the Tower server' in result.msg" + - "result is not changed" + - "'Missing_Organization' in result.msg" + - "result.total_results == 0" - name: Delete an SSH credential tower_credential: @@ -750,5 +752,7 @@ - assert: that: - - result is failed - - "result.msg =='The organizations test-non-existing-org was not found on the Tower server'" + - "result is failed" + - "result is not changed" + - "'test-non-existing-org' in result.msg" + - "result.total_results == 0" diff --git a/awx_collection/tests/integration/targets/tower_group/tasks/main.yml b/awx_collection/tests/integration/targets/tower_group/tasks/main.yml index e59d3c82db..c0f17dbb40 100644 --- a/awx_collection/tests/integration/targets/tower_group/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_group/tasks/main.yml @@ -51,8 +51,10 @@ - assert: that: - - "result.msg =='Failed to update the group, inventory not found: The requested object could not be found.' or - result.msg =='The inventories test-non-existing-inventory was not found on the Tower server'" + - "result is failed" + - "result is not changed" + - "'test-non-existing-inventory' in result.msg" + - "result.total_results == 0" - name: add hosts tower_host: diff --git a/awx_collection/tests/integration/targets/tower_host/tasks/main.yml b/awx_collection/tests/integration/targets/tower_host/tasks/main.yml index 34e02220a7..4135815553 100644 --- a/awx_collection/tests/integration/targets/tower_host/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_host/tasks/main.yml @@ -46,5 +46,6 @@ - assert: that: - - "result.msg =='The inventories test-non-existing-inventory was not found on the Tower server' or - result.msg =='Failed to update host, inventory not found: The requested object could not be found.'" + - "result is failed" + - "'test-non-existing-inventory' in result.msg" + - "result.total_results == 0" diff --git a/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml b/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml index 43dfe39a04..a92fb9c426 100644 --- a/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml @@ -119,9 +119,11 @@ - assert: that: + - "result is failed" - "result is not changed" - - "result.msg =='Failed to update inventory, organization not found: The requested object could not be found.' - or result.msg =='The organizations test-non-existing-org was not found on the Tower server'" + - "'test-non-existing-org' in result.msg" + - "result.total_results == 0" + always: - name: Delete Inventories tower_inventory: diff --git a/awx_collection/tests/integration/targets/tower_job_launch/tasks/main.yml b/awx_collection/tests/integration/targets/tower_job_launch/tasks/main.yml index 86eb196a8e..c74f6a8bd5 100644 --- a/awx_collection/tests/integration/targets/tower_job_launch/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_job_launch/tasks/main.yml @@ -29,16 +29,16 @@ - name: Check module fails with correct msg tower_job_launch: - job_template: "Non Existing Job Template" - inventory: "Test Inventory" - credential: "Test Credential" + job_template: "Non_Existing_Job_Template" + inventory: "Demo Inventory" register: result ignore_errors: true - assert: that: - - "result.msg =='Unable to launch job, job_template/Non Existing Job Template was not found: The requested object could not be found.' - or result.msg == 'The inventories Test Inventory was not found on the Tower server'" + - "result is failed" + - "result is not changed" + - "'Non_Existing_Job_Template' in result.msg" - name: Create a Job Template for testing prompt on launch tower_job_template: diff --git a/awx_collection/tests/integration/targets/tower_label/tasks/main.yml b/awx_collection/tests/integration/targets/tower_label/tasks/main.yml index d354376cb7..994fcb04b8 100644 --- a/awx_collection/tests/integration/targets/tower_label/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_label/tasks/main.yml @@ -12,13 +12,16 @@ - name: Check module fails with correct msg tower_label: name: "Test Label" - organization: "Non existing org" + organization: "Non_existing_org" state: present register: result ignore_errors: true - assert: that: - - "'Non existing org was not found on the Tower server' in result.msg" + - "result is failed" + - "result is not changed" + - "'Non_existing_org' in result.msg" + - "result.total_results == 0" # TODO: Deleting labels doesn't seem to work currently diff --git a/awx_collection/tests/integration/targets/tower_project/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project/tasks/main.yml index cece78acd5..b46e1cab43 100644 --- a/awx_collection/tests/integration/targets/tower_project/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_project/tasks/main.yml @@ -93,7 +93,7 @@ - name: Check module fails with correct msg when given non-existing org as param tower_project: name: "{{ project_name2 }}" - organization: Non Existing Org + organization: Non_Existing_Org scm_type: git scm_url: https://github.com/ansible/test-playbooks scm_credential: "{{ cred_name }}" @@ -102,8 +102,10 @@ - assert: that: - - "result.msg == 'The organizations Non Existing Org was not found on the Tower server' or - result.msg == 'Failed to update project, organization not found: Non Existing Org'" + - "result is failed" + - "result is not changed" + - "'Non_Existing_Org' in result.msg" + - "result.total_results == 0" - name: Check module fails with correct msg when given non-existing credential as param tower_project: @@ -111,14 +113,16 @@ organization: "{{ org_name }}" scm_type: git scm_url: https://github.com/ansible/test-playbooks - scm_credential: Non Existing Credential + scm_credential: Non_Existing_Credential register: result ignore_errors: true - assert: that: - - "result.msg =='The credentials Non Existing Credential was not found on the Tower server' or - result.msg =='Failed to update project, credential not found: Non Existing Credential'" + - "result is failed" + - "result is not changed" + - "'Non_Existing_Credential' in result.msg" + - "result.total_results == 0" - name: Create a git project without credentials without waiting tower_project: diff --git a/awx_collection/tests/integration/targets/tower_team/tasks/main.yml b/awx_collection/tests/integration/targets/tower_team/tasks/main.yml index 0bb9047f78..0284caeaf1 100644 --- a/awx_collection/tests/integration/targets/tower_team/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_team/tasks/main.yml @@ -6,7 +6,7 @@ - name: Attempt to add a Tower team to a non-existant Organization tower_team: name: Test Team - organization: Missing Organization + organization: Missing_Organization state: present register: result ignore_errors: true @@ -14,9 +14,10 @@ - name: Assert a meaningful error was provided for the failed Tower team creation assert: that: - - result is failed - - "result.msg =='Failed to update team, organization not found: The requested object could not be found.' or - result.msg =='The organizations Missing Organization was not found on the Tower server'" + - "result is failed" + - "result is not changed" + - "'Missing_Organization' in result.msg" + - "result.total_results == 0" - name: Create a Tower team tower_team: @@ -42,12 +43,15 @@ - name: Check module fails with correct msg tower_team: name: "{{ team_name }}" - organization: Non Existing Org + organization: Non_Existing_Org state: present register: result ignore_errors: true -- assert: +- name: Lookup of the related organization should cause a failure + assert: that: - - "result.msg =='Failed to update team, organization not found: The requested object could not be found.' or - result.msg =='The organizations Non Existing Org was not found on the Tower server'" + - "result is failed" + - "result is not changed" + - "'Non_Existing_Org' in result.msg" + - "result.total_results == 0" diff --git a/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml b/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml index 1f25f5e0a5..393fbe33da 100644 --- a/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_workflow_job_template/tasks/main.yml @@ -207,13 +207,16 @@ - name: Check module fails with correct msg tower_workflow_job_template: name: "{{ wfjt_name }}" - organization: Non Existing Organization + organization: Non_Existing_Organization register: result ignore_errors: true - assert: that: - - "'The organizations Non Existing Organization was not found' in result.msg" + - "result is failed" + - "result is not changed" + - "'Non_Existing_Organization' in result.msg" + - "result.total_results == 0" - name: Delete the Job Template tower_job_template: