Coverage for suhteita/suhteita.py: 26.34%

212 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-03 13:32:01 +00:00

1"""Load the JIRA instance.""" 

2 

3import argparse 

4import datetime as dti 

5import json 

6import logging 

7import secrets 

8from typing import Dict, Union, no_type_check 

9 

10import suhteita.ticket_system_actions as actions 

11from suhteita import ( 

12 APP_ALIAS, 

13 APP_ENV, 

14 BASE_URL, 

15 IDENTITY, 

16 IS_CLOUD, 

17 NODE_INDICATOR, 

18 PROJECT, 

19 STORE, 

20 TOKEN, 

21 TS_FORMAT_PAYLOADS, 

22 USER, 

23 __version__ as version, 

24 extract_fields, 

25 log, 

26 two_sentences, 

27) 

28from suhteita.store import Store 

29 

30Context = Dict[str, Union[str, dti.datetime]] 

31 

32 

33@no_type_check 

34def setup_twenty_seven(options: argparse.Namespace) -> object: 

35 """Set up the scenario adn return the parameters as members of an object.""" 

36 

37 class Setup: 

38 pass 

39 

40 setup = Setup() 

41 

42 setup.user = options.user if options.user else USER 

43 setup.token = options.token if options.token else TOKEN 

44 setup.target_url = options.target_url if options.target_url else BASE_URL 

45 setup.is_cloud = options.is_cloud if options.is_cloud else IS_CLOUD 

46 setup.target_project = options.target_project if options.target_project else PROJECT 

47 setup.scenario = options.scenario if options.scenario else 'unknown' 

48 setup.identity = options.identity if options.identity else IDENTITY 

49 setup.storage_path = options.out_path if options.out_path else STORE 

50 

51 log.info('=' * 84) 

52 log.info(f'Generator {APP_ALIAS} version {version}') 

53 log.info('# Prelude of a 27-steps scenario test execution') 

54 

55 setup.c_rand, setup.d_rand = two_sentences() 

56 log.info(f'- Setup <01> Random sentence of original ({setup.c_rand})') 

57 log.info(f'- Setup <02> Random sentence of duplicate ({setup.d_rand})') 

58 

59 setup.random_component = secrets.token_urlsafe() 

60 log.info(f'- Setup <03> Random component name ({setup.random_component})') 

61 

62 setup.todo, setup.in_progress, setup.done = ('to do', 'in progress', 'done') 

63 log.info( 

64 f'- Setup <04> The test workflow assumes the (case insensitive) states' 

65 f' ({setup.todo}, {setup.in_progress}, {setup.done})' 

66 ) 

67 

68 setup.ts = dti.datetime.now(tz=dti.timezone.utc).strftime(TS_FORMAT_PAYLOADS) 

69 log.info(f'- Setup <05> Timestamp marker in summaries will be ({setup.ts})') 

70 

71 setup.desc_core = '... and short description we dictate.' 

72 log.info(f'- Setup <06> Common description part - of twin issues / pair - will be ({setup.desc_core})') 

73 

74 setup.amendment = 'No, no, no. They duplicated me, help!' 

75 log.info(f'- Setup <07> Amendment for original description will be ({setup.amendment})') 

76 

77 setup.fake_comment = 'I am the original, surely!' 

78 log.info(f'- Setup <08> Fake comment for duplicate will be ({setup.fake_comment})') 

79 

80 setup.duplicate_labels = ['du', 'pli', 'ca', 'te'] 

81 log.info(f'- Setup <09> Labels for duplicate will be ({setup.duplicate_labels})') 

82 

83 setup.original_labels = ['for', 'real', 'highlander'] 

84 log.info(f'- Setup <10> Labels for original will be ({setup.original_labels})') 

85 

86 setup.hours_value = 42 

87 log.info(f'- Setup <11> Hours value for original estimate will be ({setup.hours_value})') 

88 

89 setup.purge_me = 'SUHTEITA_PURGE_ME_ORIGINAL' 

90 log.info(f'- Setup <12> Purge indicator comment will be ({setup.purge_me})') 

91 

92 setup.node_indicator = NODE_INDICATOR 

93 log.info(f'- Setup <13> Node indicator ({setup.node_indicator})') 

94 

95 log.info( 

96 f'- Setup <14> Connect will be to upstream ({"cloud" if setup.is_cloud else "on-site"})' 

97 f' service ({setup.target_url}) per login ({setup.user})' 

98 ) 

99 log.info('-' * 84) 

100 

101 return setup 

102 

103 

104def main(options: argparse.Namespace) -> int: 

105 """Drive the transactions.""" 

106 

107 if not options.token and not TOKEN: 107 ↛ 111line 107 didn't jump to line 111 because the condition on line 107 was always true

108 log.error(f'No secret token or pass phrase given, please set {APP_ENV}_TOKEN accordingly') 

109 return 2 

110 

111 if options.trace: 

112 logging.getLogger().setLevel(logging.DEBUG) 

113 elif options.debug: 

114 log.setLevel(logging.DEBUG) 

115 cfg = setup_twenty_seven(options=options) 

116 

117 # Belt and braces: 

118 has_failures = False 

119 

120 # Here we start the timer for the session: 

121 start_time = dti.datetime.now(tz=dti.timezone.utc) 

122 start_ts = start_time.strftime(TS_FORMAT_PAYLOADS) 

123 context: Context = { 

124 'target': cfg.target_url, 

125 'mode': f'{"cloud" if cfg.is_cloud else "on-site"}', 

126 'project': cfg.target_project, 

127 'scenario': cfg.scenario, 

128 'identity': cfg.identity, 

129 'start_time': start_time, 

130 } 

131 store = Store(context=context, setup=cfg, folder_path=cfg.storage_path) 

132 log.info(f'# Starting 27-steps scenario test execution at at ({start_ts})') 

133 log.info('- Step <01> LOGIN') 

134 clk, service = actions.login(cfg.target_url, cfg.user, password=cfg.token, is_cloud=cfg.is_cloud) 

135 log.info(f'^ Connected to upstream service; CLK={clk}') 

136 store.add('LOGIN', True, clk) 

137 

138 log.info('- Step <02> SERVER_INFO') 

139 clk, server_info = actions.get_server_info(service) 

140 log.info(f'^ Retrieved upstream server info cf. [SRV]; CLK={clk}') 

141 store.add('SERVER_INFO', True, clk, str(server_info)) 

142 

143 log.info('- Step <03> PROJECTS') 

144 clk, projects = actions.get_all_projects(service) 

145 log.info(f'^ Retrieved {len(projects)} unarchived projects; CLK={clk}') 

146 store.add('PROJECTS', True, clk, f'count({len(projects)})') 

147 

148 proj_env_ok = False 

149 if cfg.target_project: 

150 proj_env_ok = any((cfg.target_project == project['key'] for project in projects)) 

151 

152 if not proj_env_ok: 

153 log.error('Belt and braces - verify project selection:') 

154 log.info(json.dumps(sorted([project['key'] for project in projects]), indent=2)) 

155 return 1 

156 

157 first_proj_key = cfg.target_project if proj_env_ok else projects[0]['key'] 

158 log.info( 

159 f'Verified target project from request ({cfg.target_project}) to be' 

160 f' {"" if proj_env_ok else "not "}present and set target project to ({first_proj_key})' 

161 ) 

162 

163 log.info('- Step <04> CREATE_ISSUE') 

164 clk, c_key = actions.create_issue( 

165 service, first_proj_key, cfg.ts, description=f'{cfg.c_rand}\n{cfg.desc_core}\nCAUSALITY={cfg.node_indicator}' 

166 ) 

167 log.info(f'^ Created original ({c_key}); CLK={clk}') 

168 store.add('CREATE_ISSUE', True, clk, 'original') 

169 

170 log.info('- Step <05> ISSUE_EXISTS') 

171 clk, c_e = actions.issue_exists(service, c_key) 

172 log.info(f'^ Existence of original ({c_key}) verified with result ({c_e}); CLK={clk}') 

173 store.add('ISSUE_EXISTS', bool(c_e), clk, 'original') 

174 

175 log.info('- Step <06> CREATE_ISSUE') 

176 clk, d_key = actions.create_issue( 

177 service, first_proj_key, cfg.ts, description=f'{cfg.d_rand}\n{cfg.desc_core}\nCAUSALITY={cfg.node_indicator}' 

178 ) 

179 log.info(f'^ Created duplicate ({d_key}); CLK={clk}') 

180 store.add('CREATE_ISSUE', True, clk, 'duplicate') 

181 

182 log.info('- Step <07> ISSUE_EXISTS') 

183 clk, d_e = actions.issue_exists(service, d_key) 

184 log.info(f'^ Existence of duplicate ({d_key}) verified with result ({d_e}); CLK={clk}') 

185 store.add('ISSUE_EXISTS', bool(d_e), clk, 'duplicate') 

186 

187 query = f'issue = {c_key}' 

188 log.info('- Step <08> EXECUTE_JQL') 

189 clk, c_q = actions.execute_jql(service=service, query=query) 

190 log.info(f'^ Executed JQL({query}); CLK={clk}') 

191 store.add('EXECUTE_JQL', True, clk, f'query({query.replace(c_key, "original-key")})') 

192 

193 log.info('- Step <09> AMEND_ISSUE_DESCRIPTION') 

194 clk = actions.amend_issue_description(service, c_key, amendment=cfg.amendment, issue_context=c_q) 

195 log.info(f'^ Amended description of original {d_key} with ({cfg.amendment}); CLK={clk}') 

196 store.add('AMEND_ISSUE_DESCRIPTION', True, clk, 'original') 

197 

198 log.info('- Step <10> ADD_COMMENT') 

199 clk, _ = actions.add_comment(service=service, issue_key=d_key, comment=cfg.fake_comment) 

200 log.info(f'^ Added comment ({cfg.fake_comment}) to duplicate {d_key}; CLK={clk}') 

201 store.add('ADD_COMMENT', True, clk, 'duplicate') 

202 

203 log.info('- Step <11> UPDATE_ISSUE_FIELD') 

204 clk = actions.update_issue_field(service, d_key, labels=cfg.duplicate_labels) 

205 log.info(f'^ Updated duplicate {d_key} issue field of labels to ({cfg.duplicate_labels}); CLK={clk}') 

206 store.add('UPDATE_ISSUE_FIELD', True, clk, 'duplicate') 

207 

208 log.info('- Step <12> UPDATE_ISSUE_FIELD') 

209 clk = actions.update_issue_field(service, c_key, labels=cfg.original_labels) 

210 log.info(f'^ Updated original {c_key} issue field of labels to ({cfg.original_labels}); CLK={clk}') 

211 store.add('UPDATE_ISSUE_FIELD', True, clk, 'original') 

212 

213 log.info('- Step <13> CREATE_DUPLICATES_ISSUE_LINK') 

214 clk, _ = actions.create_duplicates_issue_link(service, c_key, d_key) 

215 log.info(f'^ Created link on duplicate stating it duplicates the original; CLK={clk}') 

216 store.add('CREATE_DUPLICATES_ISSUE_LINK', True, clk, 'dublicate duplicates original') 

217 

218 log.info('- Step <14> GET_ISSUE_STATUS') 

219 clk, d_iss_state = actions.get_issue_status(service, d_key) 

220 d_is_todo = d_iss_state.lower() == cfg.todo 

221 log.info( 

222 f'^ Retrieved status of the duplicate {d_key} as ({d_iss_state})' 

223 f' with result (is_todo == {d_is_todo}); CLK={clk}' 

224 ) 

225 store.add('GET_ISSUE_STATUS', d_is_todo, clk, f'duplicate({d_iss_state})') 

226 

227 log.info('- Step <15> SET_ISSUE_STATUS') 

228 clk, _ = actions.set_issue_status(service, d_key, cfg.in_progress) 

229 log.info(f'^ Transitioned the duplicate {d_key} to ({cfg.in_progress}); CLK={clk}') 

230 store.add('SET_ISSUE_STATUS', True, clk, f'duplicate ({cfg.todo})->({cfg.in_progress})') 

231 

232 log.info('- Step <16> SET_ISSUE_STATUS') 

233 clk, _ = actions.set_issue_status(service, d_key, cfg.done) 

234 log.info(f'^ Transitioned the duplicate {d_key} to ({cfg.done}); CLK={clk}') 

235 store.add('SET_ISSUE_STATUS', True, clk, f'duplicate ({cfg.in_progress})->({cfg.done})') 

236 

237 log.info('- Step <17> GET_ISSUE_STATUS') 

238 clk, d_iss_state_done = actions.get_issue_status(service, d_key) 

239 d_is_done = d_iss_state_done.lower() == cfg.done 

240 log.info( 

241 f'^ Retrieved status of the duplicate {d_key} as ({d_iss_state_done})' 

242 f' with result (d_is_done == {d_is_done}); CLK={clk}' 

243 ) 

244 store.add('GET_ISSUE_STATUS', d_is_done, clk, f'duplicate({d_iss_state_done})') 

245 

246 log.info('- Step <18> ADD_COMMENT') 

247 clk, response_step_18_add_comment = actions.add_comment(service, d_key, 'Closed as duplicate.') 

248 log.info(f'^ Added comment on {d_key} with response extract cf. [RESP-STEP-18]; CLK={clk}') 

249 store.add('ADD_COMMENT', True, clk, f'duplicate({response_step_18_add_comment["body"]})') 

250 

251 log.info('- Step <19> SET_ORIGINAL_ESTIMATE') 

252 clk, ok = actions.set_original_estimate(service, c_key, hours=cfg.hours_value) 

253 log.info( 

254 f'^ Added ({cfg.hours_value}) hours as original estimate to original {c_key} with result ({ok}); CLK={clk}' 

255 ) 

256 store.add('SET_ORIGINAL_ESTIMATE', ok, clk, 'original') 

257 

258 log.info('- Step <20> GET_ISSUE_STATUS') 

259 clk, c_iss_state = actions.get_issue_status(service, c_key) 

260 c_is_todo = c_iss_state.lower() == cfg.todo 

261 log.info( 

262 f'^ Retrieved status of the original {c_key} as ({c_iss_state})' 

263 f' with result (c_is_todo == {c_is_todo}); CLK={clk}' 

264 ) 

265 store.add('GET_ISSUE_STATUS', c_is_todo, clk, f'original({c_iss_state})') 

266 

267 log.info('- Step <21> SET_ISSUE_STATUS') 

268 clk, _ = actions.set_issue_status(service, c_key, cfg.in_progress) 

269 log.info(f'^ Transitioned the original {c_key} to ({cfg.in_progress}); CLK={clk}') 

270 store.add('SET_ISSUE_STATUS', True, clk, f'original ({cfg.todo})->({cfg.in_progress})') 

271 

272 log.info('- Step <22> GET_ISSUE_STATUS') 

273 clk, c_iss_state_in_progress = actions.get_issue_status(service, c_key) 

274 c_is_in_progress = c_iss_state_in_progress.lower() == cfg.in_progress 

275 log.info( 

276 f'^ Retrieved status of the original {c_key} as ({c_iss_state_in_progress})' 

277 f' with result (c_is_in_progress == {c_is_in_progress}); CLK={clk}' 

278 ) 

279 store.add('GET_ISSUE_STATUS', c_is_in_progress, clk, f'original({c_iss_state_in_progress})') 

280 

281 log.info('- Step <23> CREATE_COMPONENT') 

282 clk, comp_id, a_component, comp_resp = actions.create_component( 

283 service=service, project=first_proj_key, name=cfg.random_component, description=cfg.c_rand 

284 ) 

285 log.info(f'^ Created component ({a_component}) with response extract cf. [RESP-STEP-23]; CLK={clk}') 

286 store.add('CREATE_COMPONENT', True, clk, f'component({comp_resp["description"]})') # type: ignore 

287 

288 log.info('- Step <24> RELATE_ISSUE_TO_COMPONENT') 

289 clk, ok = actions.relate_issue_to_component(service, c_key, comp_id, a_component) 

290 log.info( 

291 f'^ Attempted relation of original {c_key} issue to component ({a_component}) with result ({ok}); CLK={clk}' 

292 ) 

293 store.add('RELATE_ISSUE_TO_COMPONENT', ok, clk, 'original') 

294 if not ok: 

295 has_failures = True 

296 

297 log.info('- Step <25> LOAD_ISSUE') 

298 clk, x_iss = actions.load_issue(service, c_key) 

299 log.info(f'^ Loaded issue {c_key}; CLK={clk}') 

300 log.debug(json.dumps(x_iss, indent=2)) 

301 store.add('LOAD_ISSUE', True, clk, 'original') 

302 

303 log.info('- Step <26> ADD_COMMENT') 

304 clk, response_step_26_add_comment = actions.add_comment(service=service, issue_key=c_key, comment=cfg.purge_me) 

305 log.info(f'^ Added purge tag comment on original {c_key} with response extract cf. [RESP-STEP-26]; CLK={clk}') 

306 store.add('ADD_COMMENT', True, clk, f'original({response_step_26_add_comment["body"]})') 

307 

308 log.info('- Step <27> ADD_COMMENT') 

309 clk, response_step_27_add_comment = actions.add_comment(service=service, issue_key=d_key, comment=cfg.purge_me) 

310 log.info( 

311 f'^ Added purge tag comment on duplicate issue {d_key} with response extract cf. [RESP-STEP-27]; CLK={clk}' 

312 ) 

313 store.add('ADD_COMMENT', True, clk, f'duplicate({response_step_27_add_comment["body"]})') 

314 

315 # Here we stop the timer for the session: 

316 end_time = dti.datetime.now(tz=dti.timezone.utc) 

317 end_ts = end_time.strftime(TS_FORMAT_PAYLOADS) 

318 log.info(f'# Ended execution of 27-steps scenario test at ({end_ts})') 

319 log.info(f'Execution of 27-steps scenario test took {(end_time - start_time)} h:mm:ss.uuuuuu') 

320 log.info('-' * 84) 

321 

322 log.info('# References:') 

323 log.info(f'[SRV] Server info is ({server_info})') 

324 log.info( 

325 f'[RESP-STEP-18] Add comment response is' 

326 f' ({extract_fields(response_step_18_add_comment, fields=("self", "body"))})' 

327 ) 

328 log.info( 

329 f'[RESP-STEP-23] Create component response is ({extract_fields(comp_resp, fields=("self", "description"))})' 

330 ) 

331 log.info( 

332 f'[RESP-STEP-26] Add comment response is' 

333 f' ({extract_fields(response_step_26_add_comment, fields=("self", "body"))})' 

334 ) 

335 log.info( 

336 f'[RESP-STEP-27] Add comment response is' 

337 f' ({extract_fields(response_step_27_add_comment, fields=("self", "body"))})' 

338 ) 

339 log.info('-' * 84) 

340 

341 log.info('Dumping records to store...') 

342 store.dump(end_time=end_time, has_failures=has_failures) 

343 log.info('-' * 84) 

344 

345 log.info('OK') 

346 log.info('=' * 84) 

347 

348 return 0