Example #1
0
 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)
Example #2
0
 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())
Example #3
0
 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())
Example #4
0
 def test_verify_report_missing_fingerprints(self):
     """Test to verify a QPC report with empty fingerprints is failed."""
     command = InsightsUploadCommand(SUBPARSER)
     status, message = command._verify_report_details(
         self.json_missing_hosts)
     self.assertEqual(status, False)
     self.assertIn(messages.INSIGHTS_REPORT_MISSING_FIELDS % 'hosts',
                   message)
Example #5
0
 def test_invalid_tmp_file(self):
     """Test invalid temp file (is dir)."""
     report_out = StringIO()
     command = InsightsUploadCommand(SUBPARSER)
     command.tmp_tar_name = '/'
     with self.assertRaises(SystemExit):
         with redirect_stdout(report_out):
             command._validate_args()
     cli_error_msg = messages.INSIGHTS_TMP_ERROR % command.tmp_tar_name
     self.assertIn(cli_error_msg, report_out.getvalue())
Example #6
0
 def test_verify_report_fails_not_dict(self):
     """Test to verify a QPC report with not dict."""
     report_json = {
         'report_id': 1,
         'report_type': 'insights',
         'report_version': '1.0.0.1b025b8',
         'status': 'completed',
         'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319',
         'hosts': ['foo']
     }
     command = InsightsUploadCommand(SUBPARSER)
     status, message = command._verify_report_details(report_json)
     self.assertEqual(status, False)
     self.assertIn(messages.INSIGHTS_INVALID_HOST_DICT_TYPE, message)
Example #7
0
 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())
Example #8
0
 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())
Example #9
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())
Example #10
0
 def test_verify_report_fails_no_canonical_facts(self):
     """Test to verify a QPC report without canonical facts fails."""
     report_json = {
         'report_id': 1,
         'report_type': 'insights',
         'report_version': '1.0.0.1b025b8',
         'status': 'completed',
         'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319',
         'hosts': {
             '5f2cc1fd-ec66-4c67-be1b-171a595ce31b': {
                 'name': 'value'
             }
         }
     }
     command = InsightsUploadCommand(SUBPARSER)
     status, message = command._verify_report_details(report_json)
     self.assertEqual(status, False)
     self.assertIn(messages.INSIGHTS_REPORT_NO_VALID_HOST, message)
Example #11
0
 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())
Example #12
0
    def test_verify_report_missing_id(self):
        """Test to verify a QPC report with a missing id is failed."""
        report_json = {
            'report_type': 'insights',
            'report_version': '1.0.0.1b025b8',
            'status': 'completed',
            'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319',
            'hosts': {
                '5f2cc1fd-ec66-4c67-be1b-171a595ce319': {
                    'key': 'value'
                }
            }
        }

        command = InsightsUploadCommand(SUBPARSER)
        status, message = command._verify_report_details(report_json)
        self.assertEqual(status, False)
        self.assertIn(messages.INSIGHTS_REPORT_MISSING_FIELDS % 'report_id',
                      message)
Example #13
0
    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())
Example #14
0
    def test_verify_report_invalid_report_type(self):
        """Test to verify a QPC report with an invalid report_type is failed."""
        report_json = {
            'report_id': 1,
            'report_type': 'details',
            'report_version': '1.0.0.1b025b8',
            'status': 'completed',
            'report_platform_id': '5f2cc1fd-ec66-4c67-be1b-171a595ce319',
            'hosts': {
                '5f2cc1fd-ec66-4c67-be1b-171a595ce319': {
                    'key': 'value'
                }
            }
        }

        command = InsightsUploadCommand(SUBPARSER)
        status, message = command._verify_report_details(report_json)
        self.assertEqual(status, False)
        self.assertIn(messages.INSIGHTS_INVALID_REPORT_TYPE % 'details',
                      message)
Example #15
0
 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())
Example #16
0
 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())
Example #17
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())
Example #18
0
 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())
Example #19
0
 def test_verify_report_success(self):
     """Test to verify a QPC report with the correct structure passes validation."""
     command = InsightsUploadCommand(SUBPARSER)
     status, message = command._verify_report_details(self.success_json)
     self.assertEqual(status, True)
     self.assertEqual(message, None)