def test_aws_main(enable_delayed_registration, post_to_hydra, get_aws_identity): ''' Test the flow of the main routine for success and failure ''' # portal access with insights registration conf = InsightsConfig(portal_access=True) assert aws.aws_main(conf) get_aws_identity.assert_called_once() post_to_hydra.assert_called_once() enable_delayed_registration.assert_called_once() get_aws_identity.reset_mock() post_to_hydra.reset_mock() enable_delayed_registration.reset_mock() # portal access, no insights registration conf = InsightsConfig(portal_access_no_insights=True) assert aws.aws_main(conf) get_aws_identity.assert_called_once() post_to_hydra.assert_called_once() enable_delayed_registration.assert_not_called() get_aws_identity.reset_mock() post_to_hydra.reset_mock() enable_delayed_registration.reset_mock() # identity call fails - returns false conf = InsightsConfig(portal_access=True) get_aws_identity.return_value = None result = aws.aws_main(conf) assert not result post_to_hydra.assert_not_called() enable_delayed_registration.assert_not_called() get_aws_identity.reset_mock() post_to_hydra.reset_mock() enable_delayed_registration.reset_mock() # hydra call fails - returns false conf = InsightsConfig(portal_access=True) post_to_hydra.return_value = False result = aws.aws_main(conf) assert not result enable_delayed_registration.assert_not_called()
def collect_and_output(client, config): # last phase, delete PID file on exit atexit.register(write_to_disk, constants.pidfile, delete=True) # register cloud (aws) if config.portal_access or config.portal_access_no_insights: if aws_main(config): sys.exit(constants.sig_kill_ok) else: sys.exit(constants.sig_kill_bad) # --compliance was called if config.compliance: config.payload, config.content_type = ComplianceClient(config).oscap_scan() # default (below) if config.payload: insights_archive = config.payload else: try: insights_archive = client.collect() except RuntimeError as e: logger.error(e) sys.exit(constants.sig_kill_bad) config.content_type = 'application/vnd.redhat.advisor.collection+tgz' if config.no_upload: # output options for which upload is not performed if config.output_dir: client.copy_to_output_dir(insights_archive) elif config.output_file: client.copy_to_output_file(insights_archive) else: # upload the archive if not insights_archive: # no archive to upload, something went wrong sys.exit(constants.sig_kill_bad) resp = None try: resp = client.upload(payload=insights_archive, content_type=config.content_type) except (IOError, ValueError, RuntimeError) as e: logger.error(str(e)) sys.exit(constants.sig_kill_bad) if resp: if config.to_json: print(json.dumps(resp)) client.delete_cached_branch_info() # rotate eggs once client completes all work successfully try: client.rotate_eggs() except IOError: message = ("Failed to rotate %s to %s" % (constants.insights_core_newest, constants.insights_core_last_stable)) logger.debug(message) raise IOError(message)
def collect_and_output(client, config): # last phase, delete PID file on exit atexit.register(write_to_disk, constants.pidfile, delete=True) # register cloud (aws) if config.portal_access or config.portal_access_no_insights: if aws_main(config): sys.exit(constants.sig_kill_ok) else: sys.exit(constants.sig_kill_bad) # --compliance was called if config.compliance: config.payload, config.content_type = ComplianceClient( config).oscap_scan() # default (below) if config.payload: insights_archive = config.payload else: try: insights_archive = client.collect() except RuntimeError as e: logger.error(e) sys.exit(constants.sig_kill_bad) config.content_type = 'application/vnd.redhat.advisor.collection+tgz' if not insights_archive: sys.exit(constants.sig_kill_bad) resp = None if not config.no_upload: try: resp = client.upload(payload=insights_archive, content_type=config.content_type) except IOError as e: logger.error(str(e)) sys.exit(constants.sig_kill_bad) except ValueError as e: logger.error(str(e)) sys.exit(constants.sig_kill_bad) else: logger.info('Archive saved at %s', insights_archive) if resp: if config.to_json: print(json.dumps(resp)) if not config.payload: # delete the archive if config.keep_archive: logger.info('Insights archive retained in ' + insights_archive) else: client.delete_archive(insights_archive, delete_parent_dir=True) client.delete_cached_branch_info() # rotate eggs once client completes all work successfully try: client.rotate_eggs() except IOError: message = ("Failed to rotate %s to %s" % (constants.insights_core_newest, constants.insights_core_last_stable)) logger.debug(message) raise IOError(message)