def test_outdated_version_response(self, subprocess):
     """Testing error response with unexpected response version."""
     # pylint:disable=line-too-long
     subprocess.return_value.communicate.side_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.1\nCore: 3.0.8\n', b'')]  # noqa: E501
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1/insights/'
     with requests_mock.Mocker() as mocker:
         mocker.get(get_report_url,
                    status_code=400,
                    headers={'X-Server-Version': VERSION},
                    json=None)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(report_id='1',
                          scan_job_id=None,
                          input_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         std_out = report_out.getvalue()
         cli_error_msg = (messages.BAD_CLIENT_VERSION %
                          ('3.0.1', CLIENT_VERSION))
         self.assertIn(cli_error_msg, std_out)
         cli_error_msg = (messages.BAD_CORE_VERSION %
                          ('3.0.8', CORE_VERSION))
         self.assertIn(cli_error_msg, std_out)
 def test_insights_upload_valid_scan_job(self, subprocess):
     """Testing response with a valid scan job id."""
     subprocess.return_value.communicate.side_effect = self.success_effect
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_scanjob_url = get_server_location() + \
         SCAN_JOB_URI + '1'
     get_scanjob_json_data = {'id': 1, 'report_id': 1}
     get_report_url = get_server_location() + \
         REPORT_URI + '1/insights/'
     buffer_content = create_tar_buffer(
         {'insights.json': self.success_json})
     with requests_mock.Mocker() as mocker:
         mocker.get(get_scanjob_url,
                    status_code=200,
                    json=get_scanjob_json_data)
         mocker.get(get_report_url,
                    status_code=200,
                    headers={'X-Server-Version': VERSION},
                    content=buffer_content)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(scan_job_id='1',
                          report_id=None,
                          input_file=None,
                          no_gpg=None)
         with redirect_stdout(report_out):
             nac.main(args)
             self.assertIn(
                 messages.REPORT_INSIGHTS_REPORT_SUCCESSFULLY_UPLOADED,
                 report_out.getvalue().strip())
 def test_unexpected_response_upload(self, subprocess):
     """Testing error response with unexpected upload."""
     # pylint:disable=line-too-long
     subprocess.return_value.communicate.side_effect = [
         (None, b''),
         (('Client: %s\nCore: %s\n' %
           (CLIENT_VERSION, CORE_VERSION)).encode(), b''),
         (None, b'Unknown Response')
     ]  # noqa: E501
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1/insights/'
     buffer_content = create_tar_buffer(
         {'insights.json': self.success_json})
     with requests_mock.Mocker() as mocker:
         mocker.get(get_report_url,
                    status_code=200,
                    headers={'X-Server-Version': VERSION},
                    content=buffer_content)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(report_id='1',
                          scan_job_id=None,
                          input_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         self.assertIn((messages.BAD_INSIGHTS_UPLOAD % ('1', ''))[:10],
                       report_out.getvalue())
 def test_insights_upload_invalid_local_tar_path(self, subprocess):
     """Testing uploading insights report with invalid path."""
     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='your_face_is_a',
                      no_gpg=True)
     with self.assertRaises(SystemExit):
         with redirect_stdout(report_out):
             nac.main(args)
     self.assertIn(messages.INSIGHTS_LOCAL_REPORT_NOT % 'your_face_is_a',
                   report_out.getvalue().strip())
 def test_insights_upload_valid_report_from_file(self, subprocess):
     """Testing uploading insights report from local json file."""
     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=self.dest_tar_file,
                      no_gpg=True)
     with redirect_stdout(report_out):
         nac.main(args)
         self.assertIn(
             messages.REPORT_INSIGHTS_REPORT_SUCCESSFULLY_UPLOADED,
             report_out.getvalue().strip())
Exemple #6
0
 def test_insights_upload_invalid_no_hosts(self, subprocess):
     """Testing uploading insights report with no hosts."""
     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,
                      json_file=self.tmp_invalid_insights_json,
                      no_gpg=True)
     with self.assertRaises(SystemExit):
         with redirect_stdout(report_out):
             nac.main(args)
     self.assertIn(messages.INVALID_REPORT_INSIGHTS_UPLOAD % ('1', ''),
                   report_out.getvalue().strip())
    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 test_insights_upload_invalid_scan_job(self, subprocess):
     """Testing scan_job id not found."""
     subprocess.return_value.communicate.side_effect = self.success_effect
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_scanjob_url = get_server_location() + \
         SCAN_JOB_URI + '1'
     with requests_mock.Mocker() as mocker:
         mocker.get(get_scanjob_url, status_code=404, json=None)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(scan_job_id='1',
                          report_id=None,
                          input_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         self.assertIn(messages.REPORT_SJ_DOES_NOT_EXIST % '1',
                       report_out.getvalue().strip())
 def test_insights_upload_nonexistent_report(self, subprocess):
     """Testing error response with an invalid report id."""
     subprocess.return_value.communicate.side_effect = self.success_effect
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1/insights/'
     with requests_mock.Mocker() as mocker:
         mocker.get(get_report_url,
                    status_code=404,
                    headers={'X-Server-Version': VERSION},
                    content=None)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(report_id='1',
                          scan_job_id=None,
                          input_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         self.assertIn(messages.INSIGHTS_REPORT_NOT_FOUND % ('1'),
                       report_out.getvalue().strip())
 def test_insights_upload_valid_scan_job_no_report_id(self, subprocess):
     """Testing response with a valid scan job id but no report_id."""
     subprocess.return_value.communicate.side_effect = self.success_effect
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_scanjob_url = get_server_location() + \
         SCAN_JOB_URI + '1'
     get_scanjob_json_data = {'id': 1}
     with requests_mock.Mocker() as mocker:
         mocker.get(get_scanjob_url,
                    status_code=200,
                    json=get_scanjob_json_data)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(scan_job_id='1',
                          report_id=None,
                          input_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         self.assertIn(messages.REPORT_NO_DEPLOYMENTS_REPORT_FOR_SJ % '1',
                       report_out.getvalue().strip())
Exemple #11
0
 def test_insights_upload_invalid_report(self, subprocess):
     """Testing response with an invaild report id."""
     subprocess.return_value.communicate.side_effect = self.success_effect
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_report_url = get_server_location() + REPORT_URI + '1/insights/'
     buffer_content = create_tar_buffer(
         {'insights.json': self.json_missing_hosts})
     with requests_mock.Mocker() as mocker:
         mocker.get(get_report_url,
                    status_code=200,
                    headers={'X-Server-Version': VERSION},
                    content=buffer_content)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(report_id='1',
                          scan_job=None,
                          json_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         self.assertIn(messages.INSIGHTS_REPORT_MISSING_FIELDS % 'hosts',
                       report_out.getvalue().strip())
 def test_cmd_not_found_response(self, subprocess):
     """Testing error response with unexpected response version."""
     subprocess.return_value.communicate.side_effect = \
         [(None, b'insights-client: command not found')]
     subprocess.return_value.returncode = 0
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1/insights/'
     with requests_mock.Mocker() as mocker:
         mocker.get(get_report_url,
                    status_code=400,
                    headers={'X-Server-Version': VERSION},
                    json=None)
         nac = InsightsUploadCommand(SUBPARSER)
         args = Namespace(report_id='1',
                          scan_job_id=None,
                          input_file=None,
                          no_gpg=None)
         with self.assertRaises(SystemExit):
             with redirect_stdout(report_out):
                 nac.main(args)
         cli_error_msg = (messages.BAD_INSIGHTS_INSTALL %
                          ('sudo insights-client --test-connection'))
         self.assertIn(cli_error_msg, report_out.getvalue())