Ilya Menkov | ad23403 | 2019-10-22 13:43:54 +0400 | [diff] [blame] | 1 | from testrail import * |
| 2 | import config |
| 3 | |
| 4 | |
| 5 | class Base: |
| 6 | def __init__(self): |
| 7 | self.client = APIClient(config.URL) |
| 8 | self.client.user = config.USER |
| 9 | self.client.password = config.PASSWORD |
| 10 | self.project = self._get_project(config.PROJECT) |
| 11 | |
| 12 | def _get_project(self, project_name): |
| 13 | projects_uri = 'get_projects' |
Ilya Menkov | 10e16d2 | 2022-07-05 15:06:51 +0400 | [diff] [blame] | 14 | projects = self.client.send_get(uri=projects_uri)['projects'] |
Ilya Menkov | ad23403 | 2019-10-22 13:43:54 +0400 | [diff] [blame] | 15 | for project in projects: |
| 16 | if project['name'] == project_name: |
| 17 | return project |
| 18 | return None |
| 19 | |
| 20 | def send_post_add_result(self, some_id, bug, status_id, add_result): |
| 21 | add_result['status_id'] = status_id |
| 22 | add_result['custom_launchpad_bug'] = bug |
| 23 | send_add_result = 'add_result/' + str(some_id) |
| 24 | return self.client.send_post(send_add_result, add_result) |
| 25 | |
| 26 | def get_plans(self, project_id): # ! |
| 27 | return self.client.send_get('get_plans/{0}'.format(project_id)) |
| 28 | |
| 29 | def get_plan(self, plan_id): # ! |
| 30 | return self.client.send_get('get_plan/{0}'.format(plan_id)) |
| 31 | |
| 32 | def is_test_plan_exist(self, test_plan_name): |
| 33 | runs = self.get_plans(self.project['id']) |
| 34 | if True in map(lambda item: item['name'] == test_plan_name, runs): |
| 35 | return True |
| 36 | return False |
| 37 | |
| 38 | def get_tests(self, plan_id): # ! |
| 39 | return self.client.send_get('get_tests/{0}'.format(plan_id)) |
| 40 | |
| 41 | def get_test_runs(self, plan_id, pattern=None): |
| 42 | plans_runs = self.get_plan(plan_id) # !get_plans |
| 43 | runs = [] |
| 44 | for run in plans_runs['entries']: |
| 45 | if pattern: |
| 46 | if pattern in run['name']: |
| 47 | runs.append(run) |
| 48 | else: |
| 49 | runs.append(run) |
| 50 | return runs |
| 51 | |
| 52 | def get_tempest_runs(self, plan_id): |
| 53 | runs = self.get_plan(plan_id) # !get_plans |
| 54 | tempest_runs = [] |
| 55 | for run in runs['entries']: |
| 56 | if 'Tempest' in run['name']: |
| 57 | tempest_runs.append(run) |
| 58 | return tempest_runs |
| 59 | |
| 60 | def get_id_of_failed_tests(self, run_id): # ! |
| 61 | all_tests = self.get_tests(run_id) |
| 62 | test_ids = [] |
| 63 | for test in all_tests: |
| 64 | if test['status_id'] == 5: |
| 65 | test_ids.append(test['id']) |
| 66 | return test_ids |
| 67 | |
| 68 | def get_test_result(self, test_id): |
| 69 | return self.client.send_get('get_results/{0}'.format(test_id)) |
| 70 | |
| 71 | def get_test_results_for_run(self, run_id): |
| 72 | return self.client.send_get('get_results_for_run/{0}'.format(run_id)) |
| 73 | |
| 74 | def get_results_for_case(self, run_id, case_id): |
| 75 | return self.client.send_get('get_results_for_case/{0}/{1}'. |
| 76 | format(run_id, case_id)) |
| 77 | |
| 78 | def get_test(self, test_id): |
| 79 | return self.client.send_get('get_test/{0}'.format(test_id)) |
| 80 | |
| 81 | def get_runs(self, run_id): |
| 82 | return self.client.send_get('get_runs/{0}'.format(run_id)) |
| 83 | |
| 84 | def get_run(self, run_id): |
| 85 | return self.client.send_get('get_run/{0}'.format(run_id)) |
| 86 | |
| 87 | def get_milestones(self): |
| 88 | milestones_uri = 'get_milestones/{project_id}'.format( |
| 89 | project_id=self.project['id']) |
Ilya Menkov | 10e16d2 | 2022-07-05 15:06:51 +0400 | [diff] [blame] | 90 | return self.client.send_get(uri=milestones_uri)['milestones'] |
Ilya Menkov | ad23403 | 2019-10-22 13:43:54 +0400 | [diff] [blame] | 91 | |
| 92 | def get_milestone(self, milestone_id): |
| 93 | milestone_uri = 'get_milestone/{milestone_id}'.format( |
| 94 | milestone_id=milestone_id) |
| 95 | return self.client.send_get(uri=milestone_uri) |
| 96 | |
| 97 | def get_milestone_by_name(self, name): |
| 98 | for milestone in self.get_milestones(): |
| 99 | if milestone['name'] == name: |
| 100 | return self.get_milestone(milestone_id=milestone['id']) |
| 101 | |
| 102 | def add_plan(self, name, description, milestone_id, entries): |
| 103 | add_plan_uri = 'add_plan/{project_id}'.format( |
| 104 | project_id=self.project['id']) |
| 105 | new_plan = { |
| 106 | 'name': name, |
| 107 | 'description': description, |
| 108 | 'milestone_id': milestone_id, |
| 109 | 'entries': entries # entries=[] |
| 110 | } |
| 111 | return self.client.send_post(add_plan_uri, new_plan) |
| 112 | |
| 113 | def add_plan_entry(self, project_id, new_run): |
| 114 | add_plan_uri = 'add_plan_entry/{project_id}'.format( |
| 115 | project_id=project_id) |
| 116 | return self.client.send_post(add_plan_uri, new_run) |
| 117 | |
| 118 | def get_suites(self): |
| 119 | suites_uri = 'get_suites/{project_id}'.format( |
| 120 | project_id=self.project['id']) |
| 121 | return self.client.send_get(uri=suites_uri) |
| 122 | |
| 123 | def get_suite(self, suite_id): |
| 124 | suite_uri = 'get_suite/{suite_id}'.format(suite_id=suite_id) |
| 125 | return self.client.send_get(uri=suite_uri) |
| 126 | |
| 127 | def get_suite_by_name(self, name): |
| 128 | for suite in self.get_suites(): |
| 129 | if suite['name'] == name: |
| 130 | return self.get_suite(suite_id=suite['id']) |
| 131 | |
| 132 | def get_plan_by_name(self, name): |
| 133 | for plan in self.get_plans(13): |
| 134 | if plan['name'] == name: |
| 135 | return self.get_plan(plan['id']) |
| 136 | |
| 137 | def add_result(self, test_id, result_to_add): |
| 138 | return self.client.send_post('add_result/{0}'.format(test_id['id']), |
| 139 | result_to_add) |
| 140 | |
| 141 | def add_suite(self, name, description=None): |
| 142 | return self.client.send_post('add_suite/' + str(self.project['id']), |
| 143 | dict(name=name, description=description)) |
| 144 | |
| 145 | def get_sections(self, suite_id): |
| 146 | sections_uri = 'get_sections/{project_id}&suite_id={suite_id}'.format( |
| 147 | project_id=self.project['id'], |
| 148 | suite_id=suite_id |
| 149 | ) |
| 150 | return self.client.send_get(sections_uri) |
| 151 | |
| 152 | def get_section(self, section_id): |
| 153 | section_uri = 'get_section/{section_id}'.format(section_id=section_id) |
| 154 | return self.client.send_get(section_uri) |
| 155 | |
| 156 | def get_section_by_name(self, suite_id, section_name): |
| 157 | for section in self.get_sections(suite_id=suite_id): |
| 158 | if section['name'] == section_name: |
| 159 | return self.get_section(section_id=section['id']) |
| 160 | |
| 161 | def add_section(self, suite_id, name, parent_id=None): |
| 162 | return self.client.send_post('add_section/' + str(self.project['id']), |
| 163 | dict(suite_id=suite_id, name=name, |
| 164 | parent_id=parent_id)) |
| 165 | |
| 166 | def delete_section(self, section_id): |
| 167 | # Not working bug in testrail |
| 168 | section = self.get_section(section_id) |
| 169 | print('SECTION', section) |
| 170 | try: |
| 171 | deleted = self.client.send_post('delete_section/{}'.format(section_id), section) |
| 172 | print('DELETED', deleted) |
| 173 | except Exception: |
| 174 | pass |
| 175 | return |
| 176 | |
| 177 | def add_case(self, section_id, case): |
| 178 | add_case_uri = 'add_case/{section_id}'.format(section_id=section_id) |
| 179 | return self.client.send_post(add_case_uri, case) |
| 180 | |
| 181 | @staticmethod |
| 182 | def prepare_common_results(tests, status_id): |
| 183 | results = {"results": []} |
| 184 | |
| 185 | for test in tests: |
| 186 | results["results"].append({ |
| 187 | "test_id": test['id'], |
| 188 | "status_id": status_id, |
| 189 | "comment": 'Deploy failed', |
| 190 | }) |
| 191 | return results |
| 192 | |
| 193 | @staticmethod |
| 194 | def get_result_by_name(): |
| 195 | result = config.RESULT |
| 196 | if result == 'Blocked': |
| 197 | return 2 |
| 198 | elif result == 'Passed': |
| 199 | return 1 |
| 200 | elif result == 'Failed': |
| 201 | return 5 |
| 202 | elif result == 'ProdFailed': |
| 203 | return 8 |
| 204 | elif result == 'Skipped': |
| 205 | return 6 |
| 206 | |
| 207 | @staticmethod |
| 208 | def get_id_of_tempest_runs(tempest_runs): |
| 209 | tempest_runs_ids = {} # [] |
| 210 | for i in tempest_runs: |
| 211 | for item in i['runs']: |
| 212 | tempest_runs_ids.update({item['id']: item['name']}) |
| 213 | return tempest_runs_ids |
| 214 | |
| 215 | @staticmethod |
| 216 | def get_last_tempest_run(get_plans): |
| 217 | for plans in get_plans: |
| 218 | # print dict |
| 219 | if (plans.get(u'passed_count') > 1000 or plans.get( |
| 220 | u'blocked_count') > 1000)and '9.1' in plans.get(u'name'): |
| 221 | return plans.get(u'id') |