From cd279829598e83a7bebaaf294253a57db3ad7931 Mon Sep 17 00:00:00 2001 From: jainnikhil30 Date: Wed, 9 Jan 2019 20:01:21 +0530 Subject: [PATCH] Fixing exception import for tower modules (#50447) * fixing the exception import from tower modules * Adding tests for checking tower modules are failing with correct msg * fixed failing tests * fixed failing test in tower_team --- .../ansible_tower/tower_credential.py | 2 +- .../ansible_tower/tower_group.py | 2 +- .../ansible_tower/tower_host.py | 2 +- .../ansible_tower/tower_inventory.py | 2 +- .../ansible_tower/tower_job_cancel.py | 2 +- .../ansible_tower/tower_job_launch.py | 2 +- .../ansible_tower/tower_job_list.py | 2 +- .../ansible_tower/tower_job_template.py | 2 +- .../ansible_tower/tower_job_wait.py | 2 +- .../ansible_tower/tower_label.py | 2 +- .../ansible_tower/tower_organization.py | 2 +- .../ansible_tower/tower_project.py | 2 +- .../ansible_tower/tower_role.py | 2 +- .../ansible_tower/tower_settings.py | 2 +- .../ansible_tower/tower_team.py | 2 +- .../ansible_tower/tower_user.py | 2 +- .../targets/tower_credential/tasks/main.yml | 18 ++++++++++-- .../targets/tower_group/tasks/main.yml | 13 +++++++++ .../targets/tower_host/tasks/main.yml | 13 +++++++++ .../targets/tower_inventory/tasks/main.yml | 13 +++++++++ .../targets/tower_job_cancel/tasks/main.yml | 10 +++++++ .../targets/tower_job_launch/tasks/main.yml | 12 ++++++++ .../targets/tower_job_wait/tasks/main.yml | 10 +++++++ .../targets/tower_label/tasks/main.yml | 12 ++++++++ .../targets/tower_project/tasks/main.yml | 28 +++++++++++++++++++ .../targets/tower_team/tasks/main.yml | 14 +++++++++- .../tower_workflow_template/tasks/main.yml | 12 ++++++++ 27 files changed, 168 insertions(+), 19 deletions(-) diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_credential.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_credential.py index fe912745acd..2b30355363d 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_credential.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_credential.py @@ -154,7 +154,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_group.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_group.py index 1e87a408ecf..50772891ff5 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_group.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_group.py @@ -97,7 +97,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_host.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_host.py index 16a92b11207..492c4842c06 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_host.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_host.py @@ -70,7 +70,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py index 6da39289285..1be24773211 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py @@ -71,7 +71,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_cancel.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_cancel.py index 51258a31de5..f2f5f8eadae 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_cancel.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_cancel.py @@ -59,7 +59,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py index cb997abdb24..c34ec3dd23e 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py @@ -87,7 +87,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_list.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_list.py index 47b4ed6b064..51eda5ecdb4 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_list.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_list.py @@ -82,7 +82,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_template.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_template.py index fb811d11ce8..4f69ec99d55 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_template.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_template.py @@ -197,7 +197,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_wait.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_wait.py index 519adc84037..89742891579 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_wait.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_wait.py @@ -88,7 +88,7 @@ from ansible.module_utils.six.moves import cStringIO as StringIO try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_label.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_label.py index 836cb1b43f8..554d154cb1a 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_label.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_label.py @@ -53,7 +53,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_organization.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_organization.py index 20e5399675d..8acbfd5108d 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_organization.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_organization.py @@ -52,7 +52,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py index 6d6a80260a2..c1b4a0c972c 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_project.py @@ -88,7 +88,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_role.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_role.py index 2279bc925a5..38a56514b96 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_role.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_role.py @@ -75,7 +75,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_settings.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_settings.py index b3d07f99b1d..1a2acbfb9e0 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_settings.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_settings.py @@ -58,7 +58,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_team.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_team.py index cd9854f7ee0..4c49fd54477 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_team.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_team.py @@ -54,7 +54,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_user.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_user.py index 00e8c0fcf07..86baf18e90e 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_user.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_user.py @@ -75,7 +75,7 @@ from ansible.module_utils.ansible_tower import TowerModule, tower_auth_config, t try: import tower_cli - import tower_cli.utils.exceptions as exc + import tower_cli.exceptions as exc from tower_cli.conf import settings except ImportError: diff --git a/test/integration/targets/tower_credential/tasks/main.yml b/test/integration/targets/tower_credential/tasks/main.yml index 9af78d95772..20631bfdd0e 100644 --- a/test/integration/targets/tower_credential/tasks/main.yml +++ b/test/integration/targets/tower_credential/tasks/main.yml @@ -111,7 +111,7 @@ - assert: that: - "result is failed" - - "'must be set when SSH key is encrypted' in result.module_stderr" + - "'must be set when SSH key is encrypted' in result.msg" - name: Create an invalid SSH credential (Organization not found) tower_credential: @@ -126,7 +126,7 @@ - assert: that: - "result is failed" - - "'The requested object could not be found' in result.module_stderr" + - "'The requested object could not be found' in result.msg" - name: Delete an SSH credential tower_credential: @@ -522,3 +522,17 @@ - assert: that: - "result is changed" + +- name: Check module fails with correct msg + tower_credential: + name: test-credential + description: Credential Description + kind: ssh + organization: test-non-existing-org + state: present + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update credential, organization not found: The requested object could not be found.'" diff --git a/test/integration/targets/tower_group/tasks/main.yml b/test/integration/targets/tower_group/tasks/main.yml index d559ba69192..a1e2f118af5 100644 --- a/test/integration/targets/tower_group/tasks/main.yml +++ b/test/integration/targets/tower_group/tasks/main.yml @@ -32,3 +32,16 @@ - assert: that: - "result is changed" + +- name: Check module fails with correct msg + tower_group: + name: test-group + description: Group Description + inventory: test-non-existing-inventory + state: present + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update the group, inventory not found: The requested object could not be found.'" diff --git a/test/integration/targets/tower_host/tasks/main.yml b/test/integration/targets/tower_host/tasks/main.yml index b56553f14ee..ad1a5f04e52 100644 --- a/test/integration/targets/tower_host/tasks/main.yml +++ b/test/integration/targets/tower_host/tasks/main.yml @@ -31,3 +31,16 @@ - assert: that: - "result is changed" + +- name: Check module fails with correct msg + tower_host: + name: test-host + description: Host Description + inventory: test-non-existing-inventory + state: present + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update host, inventory not found: The requested object could not be found.'" diff --git a/test/integration/targets/tower_inventory/tasks/main.yml b/test/integration/targets/tower_inventory/tasks/main.yml index 0d45bb8c723..e1af6d64540 100644 --- a/test/integration/targets/tower_inventory/tasks/main.yml +++ b/test/integration/targets/tower_inventory/tasks/main.yml @@ -19,3 +19,16 @@ - assert: that: - "result is changed" + +- name: Check module fails with correct msg + tower_inventory: + name: test-inventory + description: Inventory Description + organization: test-non-existing-org + state: present + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update inventory, organization not found: The requested object could not be found.'" \ No newline at end of file diff --git a/test/integration/targets/tower_job_cancel/tasks/main.yml b/test/integration/targets/tower_job_cancel/tasks/main.yml index 4d795d6096d..304e71a84ff 100644 --- a/test/integration/targets/tower_job_cancel/tasks/main.yml +++ b/test/integration/targets/tower_job_cancel/tasks/main.yml @@ -13,3 +13,13 @@ - name: Cancel the job tower_job_cancel: job_id: "{{ job.id }}" + +- name: Check module fails with correct msg + tower_job_cancel: + job_id: 9999999999 + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Unable to cancel job_id/9999999999: The requested object could not be found.'" \ No newline at end of file diff --git a/test/integration/targets/tower_job_launch/tasks/main.yml b/test/integration/targets/tower_job_launch/tasks/main.yml index f619368d238..9bf271c105d 100644 --- a/test/integration/targets/tower_job_launch/tasks/main.yml +++ b/test/integration/targets/tower_job_launch/tasks/main.yml @@ -9,3 +9,15 @@ that: - "result is changed" - "result.status == 'pending'" + +- name: Check module fails with correct msg + tower_job_launch: + job_template: "Non Existing Job Template" + inventory: "Test Inventory" + credential: "Test Credential" + 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.'" diff --git a/test/integration/targets/tower_job_wait/tasks/main.yml b/test/integration/targets/tower_job_wait/tasks/main.yml index 904e6a5169f..2ffc5bca3aa 100644 --- a/test/integration/targets/tower_job_wait/tasks/main.yml +++ b/test/integration/targets/tower_job_wait/tasks/main.yml @@ -14,3 +14,13 @@ tower_job_wait: job_id: "{{ job.id }}" timeout: 60 + +- name: Check module fails with correct msg + tower_job_wait: + job_id: "99999999" + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Unable to wait, no job_id 99999999 found: The requested object could not be found.'" \ No newline at end of file diff --git a/test/integration/targets/tower_label/tasks/main.yml b/test/integration/targets/tower_label/tasks/main.yml index f580c7c690b..f86712b96ad 100644 --- a/test/integration/targets/tower_label/tasks/main.yml +++ b/test/integration/targets/tower_label/tasks/main.yml @@ -4,4 +4,16 @@ organization: Default state: present +- name: Check module fails with correct msg + tower_label: + name: "Test Label" + organization: "Non existing org" + state: present + register: result + ignore_errors: true + +- assert: + that: + - "result.msg == 'Failed to update label, organization not found: The requested object could not be found.'" + # TODO: Deleting labels doesn't seem to work currently diff --git a/test/integration/targets/tower_project/tasks/main.yml b/test/integration/targets/tower_project/tasks/main.yml index e965709cbb5..ba9096c20f4 100644 --- a/test/integration/targets/tower_project/tasks/main.yml +++ b/test/integration/targets/tower_project/tasks/main.yml @@ -76,3 +76,31 @@ - assert: that: - "multi_org_cred_project is changed" + +- name: Check module fails with correct msg + tower_project: + name: TestProject + organization: Non Existing Org + scm_type: git + scm_url: "https://github.com/ansible/ansible" + scm_credential: TestCred1 + register: result + ignore_errors: true + +- assert: + that: + - "result.msg == 'Failed to update project, organization not found: Non Existing Org'" + +- name: Check module fails with correct msg + tower_project: + name: TestProject + organization: TestOrg1 + scm_type: git + scm_url: "https://github.com/ansible/ansible" + scm_credential: Non Existing Credential + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update project, credential not found: Non Existing Credential'" \ No newline at end of file diff --git a/test/integration/targets/tower_team/tasks/main.yml b/test/integration/targets/tower_team/tasks/main.yml index 40fa7e05e40..96fb921a815 100644 --- a/test/integration/targets/tower_team/tasks/main.yml +++ b/test/integration/targets/tower_team/tasks/main.yml @@ -9,7 +9,7 @@ - name: Assert a meaningful error was provided for the failed Tower team creation assert: that: - - "'The requested object could not be found.' in result.exception" + - "'The requested object could not be found.' in result.msg" - name: Create a Tower team tower_team: @@ -31,3 +31,15 @@ - assert: that: - "result is changed" + +- name: Check module fails with correct msg + tower_team: + name: Test Team + organization: Non Existing Org + state: present + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update team, organization not found: The requested object could not be found.'" diff --git a/test/integration/targets/tower_workflow_template/tasks/main.yml b/test/integration/targets/tower_workflow_template/tasks/main.yml index 39a03c68d7e..c4bc1607d07 100644 --- a/test/integration/targets/tower_workflow_template/tasks/main.yml +++ b/test/integration/targets/tower_workflow_template/tasks/main.yml @@ -91,3 +91,15 @@ - assert: that: - "result is changed" + +- name: Check module fails with correct msg + tower_workflow_template: + name: my-workflow + organization: Non Existing Organization + schema: '[{"success": [{"job_template": "my-job-1"}], "job_template": "my-job-2"}]' + register: result + ignore_errors: true + +- assert: + that: + - "result.msg =='Failed to update organization source,organization not found: The requested object could not be found.'" \ No newline at end of file