def _obtain_insights_report_from_qpc_server(self): """Download report, validate, and write tar.gz.""" # Obtain report ID self.report_id = None if self.args.report_id is None: # Make request to convert scan_job_id to self.report_id scan_job_response = request(parser=self.parser, method=GET, path='%s%s' % (scan.SCAN_JOB_URI, self.args.scan_job_id), payload=None) if scan_job_response.status_code == codes.ok: # pylint: disable=no-member json_data = scan_job_response.json() self.report_id = json_data.get('report_id') if self.report_id is None: print(_(messages.REPORT_NO_DEPLOYMENTS_REPORT_FOR_SJ % self.args.scan_job_id)) sys.exit(1) else: print(_(messages.REPORT_SJ_DOES_NOT_EXIST % self.args.scan_job_id)) sys.exit(1) # Log report ID that was obtained from scanjob print(_(messages.INSIGHTS_SCAN_JOB_ID_PRODUCED % (self.args.scan_job_id, self.report_id))) else: self.report_id = self.args.report_id # Request report from QCP server print(_(messages.INSIGHTS_RETRIEVE_REPORT % self.report_id)) headers = {'Accept': 'application/json+gzip'} report_path = '%s%s%s' % ( insights.REPORT_URI, str(self.report_id), insights.INSIGHTS_PATH_SUFFIX) report_response = request(parser=self.parser, method=GET, path=report_path, headers=headers, payload=None, min_server_version=VERSION) if report_response.status_code != codes.ok: # pylint: disable=no-member print(_(messages.INSIGHTS_REPORT_NOT_FOUND % self.report_id)) sys.exit(1) # Validate insights report insights_report_dict = extract_json_from_tar(report_response.content, print_pretty=False) valid, error = self._verify_report_details(insights_report_dict) if not valid: print(_(messages.INVALID_REPORT_INSIGHTS_UPLOAD % (self.report_id, error))) sys.exit(1) # write file content to disk write_file(self.tmp_tar_name, report_response.content, True)
def _obtain_insights_report_from_local_file(self): """Load local report, validate, and write tar.gz.""" json_file = self.args.json_file if not os.path.isfile(json_file): print(_(messages.INSIGHTS_LOCAL_REPORT_NOT % json_file)) sys.exit(1) insights_report_dict = None with open(json_file) as insights_report_file: try: insights_report_dict = json.load(insights_report_file) except json_exception_class: print(_(messages.INSIGHTS_LOCAL_REPORT_NOT_JSON % json_file)) sys.exit(1) # Validate insights report valid, error = self._verify_report_details(insights_report_dict) if not valid: print(_(messages.INVALID_REPORT_INSIGHTS_UPLOAD % (self.report_id, error))) sys.exit(1) insights_name = 'report_id_%s/%s.%s' % (self.report_id, 'insights', 'json') reports_dict = {} reports_dict[insights_name] = insights_report_dict tar_buffer = create_tar_buffer(reports_dict) # write file content to disk write_file(self.tmp_tar_name, tar_buffer, True)
def _handle_response_success(self): try: write_file(self.args.path, self.response.content, binary=True) print(_(messages.REPORT_SUCCESSFULLY_WRITTEN)) except EnvironmentError as err: err_msg = _(messages.WRITE_FILE_ERROR % (self.args.path, err)) print(err_msg) sys.exit(1)
def _handle_response_success(self): file_content = self.response.content try: write_file(self.args.path, file_content, True) print(_(messages.DOWNLOAD_SUCCESSFULLY_WRITTEN % (self.report_id, self.args.path))) except EnvironmentError as err: err_msg = _(messages.WRITE_FILE_ERROR % (self.args.path, err)) print(err_msg) sys.exit(1)
def _handle_response_success(self): file_content = None file_content = extract_json_from_tar(self.response.content) try: write_file(self.args.path, file_content) print(_(messages.REPORT_SUCCESSFULLY_WRITTEN)) except EnvironmentError as err: err_msg = _(messages.WRITE_FILE_ERROR % (self.args.path, err)) print(err_msg) sys.exit(1)
def _handle_response_success(self): json_data = self.response.json() status = pretty_print(json_data) if self.args.path: try: write_file(self.args.path, status) print(_(messages.STATUS_SUCCESSFULLY_WRITTEN)) except EnvironmentError as err: err_msg = _(messages.WRITE_FILE_ERROR % (self.args.path, err)) print(err_msg) else: print(status)
def _handle_response_success(self): file_content = None if self.args.output_json: file_content = self.response.json() file_content = pretty_print(file_content) else: file_content = self.response.text try: write_file(self.args.path, file_content) print(_(messages.REPORT_SUCCESSFULLY_WRITTEN)) except EnvironmentError as err: err_msg = _(messages.WRITE_FILE_ERROR % (self.args.path, err)) print(err_msg)
def test_insights_upload_not_tar_extension(self, subprocess): """Testing uploading insights report with invalid file extension.""" random_file = '/tmp/insights_random_%s.txt' % ( time.strftime('%Y%m%d_%H%M%S')) write_file(random_file, 'not really tar', False) subprocess.return_value.communicate.side_effect = self.success_effect subprocess.return_value.returncode = 0 report_out = StringIO() nac = InsightsUploadCommand(SUBPARSER) args = Namespace(report_id=None, scan_job=None, input_file=random_file, no_gpg=True) with self.assertRaises(SystemExit): with redirect_stdout(report_out): nac.main(args) self.assertIn(messages.INSIGHTS_LOCAL_REPORT_NOT_TAR_GZ % random_file, report_out.getvalue().strip())
def setUp(self): """Create test setup.""" write_server_config(DEFAULT_CONFIG) # Temporarily disable stderr for these tests, CLI errors clutter up self.orig_stderr = sys.stderr self.success_json = { 'report_id': 1, 'report_type': 'insights', 'report_version': '1.0.0.1b025b8', 'status': 'completed', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'hosts': { '2f2cc1fd-ec66-4c67-be1b-171a595ce319': { 'bios_uuid': 'value' } } } self.json_missing_hosts = { 'report_id': 1, 'report_type': 'insights', 'report_version': '1.0.0.1b025b8', 'status': 'completed', 'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319', 'hosts': {} } sys.stderr = HushUpStderr() # pylint:disable=line-too-long self.success_effect = [ (None, b'Running Connection Tests...\nConnection test config:\n=== Begin Certificate Chain Test ===\ndepth=1\nverify error:num=0\nverify return:1\ndepth=0\nverify error:num=0\nverify return:1\n=== End Certificate Chain Test: SUCCESS ===\n\n=== Begin Upload URL Connection Test ===\nHTTP Status Code: 200\nHTTP Status Text: OK\nHTTP Response Text: \nSuccessfully connected to: https://cert-api.access.redhat.com/r/insights/uploads/\n=== End Upload URL Connection Test: SUCCESS ===\n\n=== Begin API URL Connection Test ===\nHTTP Status Code: 200\nHTTP Status Text: OK\nHTTP Response Text: lub-dub\nSuccessfully connected to: https://cert-api.access.redhat.com/r/insights/\n=== End API URL Connection Test: SUCCESS ===\n\n\nConnectivity tests completed successfully\nSee /var/log/insights-client/insights-client.log for more details.\n' ), (b'Client: 3.0.3-2\nCore: 3.0.72-1\n', b''), (None, b'Uploading Insights data.\nSuccessfully uploaded report for.\n') ] # noqa: E501 self.tmp_json_file = '/tmp/insights_tmp_%s.json' % ( time.strftime('%Y%m%d_%H%M%S')) write_file(self.tmp_json_file, json.dumps(self.success_json), False) self.tmp_not_json_file = '/tmp/insights_tmp_%s.txt' % ( time.strftime('%Y%m%d_%H%M%S')) write_file(self.tmp_not_json_file, 'not really json', False) self.tmp_invalid_insights_json = '/tmp/insights_invalid_tmp_%s.json' % ( time.strftime('%Y%m%d_%H%M%S')) write_file(self.tmp_invalid_insights_json, json.dumps(self.json_missing_hosts), False)