def test_syspurpose_empty(self, mock_describe_devices, mock_report_results, mock_sh_blkid): """ Test error handling when syspurpose.json exists but is empty. Note: We have to detect RHEL before we parse the syspurpose.json file. """ rhel_version = "7.4" runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_rhel_release(self.partition_1) helper.prepare_fs_rhel_syspurpose(self.partition_1, content="") result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertIsNone(results["images"][self.aws_image_id]["syspurpose"])
def test_syspurpose_malformed(self, mock_describe_devices, mock_report_results, mock_sh_blkid): """ Test error handling when syspurpose.json exists but has non-JSON content. Note: We have to detect RHEL before we parse the syspurpose.json file. """ rhel_version = "7.4" runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_rhel_release(self.partition_1) helper.prepare_fs_rhel_syspurpose(self.partition_1, content="lol nope!") result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() self.assertIn( f"Parsing system purpose on {self.partition_1} failed because", result.output, )
def test_large_syspurpose(self, mock_describe_devices, mock_report_results, mock_sh_blkid): """ Test skipping of syspurpose.json when the size is larger than 1K bytes. Note: We create a 2K size syspurpose.json file for this test. """ rhel_version = "7.4" runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_rhel_release(self.partition_1) helper.prepare_fs_rhel_syspurpose(self.partition_1, content="x" * 2048) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() self.assertIn( "Skipping system purpose file, file is larger than 1024 bytes", result.output, )
def test_rhel_found_via_enabled_repos( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """Test finding RHEL via enabled yum repos.""" rhel_version = None # Because we detect RHEL without a release file. mock_subprocess_check_output.return_value = RPM_RESULT_NONE runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_with_yum(self.partition_1) helper.prepare_fs_with_yum(self.partition_2, include_optional=False) helper.prepare_fs_with_yum(self.partition_3, use_dnf=True) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) self.assertFoundEnabledRepos(result.output, self.partition_1) self.assertFoundEnabledRepos(result.output, self.partition_2) self.assertFoundEnabledRepos(result.output, self.partition_3) self.assertIn( '{"repo": "rhel7-cdn-internal", "name": "RHEL 7 - $basearch"}', result.output, ) self.assertIn( '{"repo": "rhel7-cdn-internal-extras", "name": "RHEL 7 - $basearch"}', result.output, ) self.assertIn( '{"repo": "rhel7-cdn-internal-optional", "name": "RHEL 7 - $basearch"}', result.output, ) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=True, rhel_signed_packages_found=False, rhel_enabled_repos_found=True, rhel_product_certs_found=False, rhel_release_files_found=False, )
def test_rhel_not_found_with_unreadable_release_file( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """Test not finding RHEL with an unreadable release file.""" runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_with_bad_release_file(self.partition_1) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertIn(f"Error reading release files on {self.partition_1}", result.output) self.assertRhelNotFound(result.output, self.aws_image_id) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=False, rhel_signed_packages_found=False, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=False, ) release_files_status = ( f"Error reading release files on {self.partition_1}: " "'utf-8' codec can't decode byte 0xac in position 0: invalid start byte" ) self.assertEqual( release_files_status, results["images"][self.aws_image_id]["drives"][self.drive_path][ self.partition_1]["facts"]["rhel_release_files"]["status"], )
def test_rhel_found_via_signed_package( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """Test finding RHEL via signed package (RHEL in RPM DB).""" rhel_version = None # Because we detect RHEL without a release file. mock_subprocess_check_output.side_effect = [ RPM_RESULT_FOUND, # result for `rpm` call in partition_1 RPM_RESULT_NONE, # result for `rpm` call in partition_2 ] runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_with_rpm_db(self.partition_1) helper.prepare_fs_with_rpm_db(self.partition_2) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) self.assertFoundSignedPackages(result.output, self.partition_1) self.assertFoundSignedPackages(result.output, self.partition_2, False) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=True, rhel_signed_packages_found=True, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=False, )
def test_rhel_found_via_release_file( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """Test finding RHEL via etc release file.""" rhel_version = "7.4" mock_subprocess_check_output.return_value = RPM_RESULT_NONE runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_rhel_release(self.partition_1) helper.prepare_fs_centos_release(self.partition_2) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) self.assertFoundReleaseFile(result.output, self.partition_1, True) self.assertFoundReleaseFile(result.output, self.partition_2, False) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=True, rhel_signed_packages_found=False, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=True, rhel_version=rhel_version, )
def test_cli_no_version_files( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """Test appropriate error handling when release files are missing.""" mock_subprocess_check_output.return_value = RPM_RESULT_NONE runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_empty(self.partition_1) helper.prepare_fs_empty(self.partition_2) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertNoReleaseFiles(result.output, self.partition_1) self.assertNoReleaseFiles(result.output, self.partition_2) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=False, rhel_signed_packages_found=False, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=False, )
def test_rhel_not_found_with_bad_yum_conf( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """Test not finding RHEL with bad yum.conf.""" mock_subprocess_check_output.return_value = RPM_RESULT_NONE runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_with_bad_yum_conf(self.partition_1) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertNoReleaseFiles(result.output, self.partition_1) self.assertRhelNotFound(result.output, self.aws_image_id) self.assertIn("Error reading yum repo files on", result.output) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=False, rhel_signed_packages_found=False, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=False, )
def test_no_rpm_db_early_return(self, mock_describe_devices, mock_report_results, mock_sh_blkid): """Test error handling when RPM DB does not exist.""" runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_empty(self.partition_1) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=False, rhel_signed_packages_found=False, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=False, ) signed_packages_status = _( "RPM DB directory on {0} has no data for {1}").format( self.partition_1, self.aws_image_id) self.assertEqual( signed_packages_status, results["images"][self.aws_image_id]["drives"][self.drive_path][ self.partition_1]["facts"]["rhel_signed_packages"]["status"], )
def test_rhel_not_found( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """ Test not finding RHEL via normal inspection. This exercises effectively the opposite cases of test_rhel_found_multiple_ways. This should verify not finding RHEL in all currently know ways, which includes: * release file(s) exist but do not have RHEL * release file(s) don't exist * yum repo(s) exist and are enabled but do not have RHEL * yum repo(s) exist with RHEL but are not enabled * no installed product certificate(s) * no RHEL found in the RPM database * rpm command fails to execute """ subprocess_error = CalledProcessError(1, "rpm", stderr="rpm failed.") mock_subprocess_check_output.side_effect = [ RPM_RESULT_NONE, # result for `rpm` call in partition_1 subprocess_error, # result for `rpm` call in partition_2 ] runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_centos_release(self.partition_1) helper.prepare_fs_with_yum(self.partition_1, rhel_enabled=False, include_optional=False) helper.prepare_fs_with_yum(self.partition_2, rhel_enabled=False, include_optional=True) helper.prepare_fs_with_rpm_db(self.partition_1) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertRhelNotFound(result.output, self.aws_image_id) self.assertFoundReleaseFile(result.output, self.partition_1, False) self.assertFoundEnabledRepos(result.output, self.partition_1, False) self.assertFoundProductCertificate(result.output, self.partition_1, False) self.assertFoundSignedPackages(result.output, self.partition_1, False) self.assertNoReleaseFiles(result.output, self.partition_2) self.assertFoundEnabledRepos(result.output, self.partition_2, False) self.assertFoundProductCertificate(result.output, self.partition_2, False) # Skip next assert because the RPM check quietly errors out (correctly). # self.assertFoundSignedPackages(result.output, self.partition_2, False) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=False, rhel_signed_packages_found=False, rhel_enabled_repos_found=False, rhel_product_certs_found=False, rhel_release_files_found=False, )
def test_rhel_found_multiple_ways( self, mock_subprocess_check_output, mock_describe_devices, mock_report_results, mock_sh_blkid, ): """ Test finding RHEL via multiple ways. This should verify finding RHEL in all currently know ways, which includes: * RHEL in at least one partition's release file(s) * RHEL in at least one partition's enabled yum repo(s) * RHEL in at least one partition's installed product certificate(s) * RHEL in at least one partition's RPM database """ rhel_version = "7.4" mock_subprocess_check_output.side_effect = [ RPM_RESULT_FOUND, # result for `rpm` call in partition_1 RPM_RESULT_NONE, # result for `rpm` call in partition_2 ] runner = CliRunner() with runner.isolated_filesystem() as tempdir_path, patch( "cli.mount", helper.fake_mount(tempdir_path)), patch( "cli.INSPECT_PATH", self.inspect_path): helper.prepare_fs_empty(self.drive_path) helper.prepare_fs_rhel_release(self.partition_1) helper.prepare_fs_rhel_syspurpose(self.partition_1) helper.prepare_fs_with_yum(self.partition_1) helper.prepare_fs_with_rhel_product_certificate(self.partition_1) helper.prepare_fs_with_rpm_db(self.partition_1) helper.prepare_fs_centos_release(self.partition_2) helper.prepare_fs_with_yum(self.partition_2, include_optional=False) helper.prepare_fs_with_rpm_db(self.partition_2) result = runner.invoke( main, ["-c", CLOUD_AWS, "-t", self.aws_image_id, self.drive_path]) self.assertEqual(result.exit_code, 0) self.assertIn(f'"cloud": "{CLOUD_AWS}"', result.output) self.assertIn(f'"{self.aws_image_id}"', result.output) self.assertFoundReleaseFile(result.output, self.partition_1) self.assertFoundEnabledRepos(result.output, self.partition_1) self.assertFoundProductCertificate(result.output, self.partition_1) self.assertFoundSignedPackages(result.output, self.partition_1) self.assertFoundReleaseFile(result.output, self.partition_2, False) self.assertFoundEnabledRepos(result.output, self.partition_2) self.assertFoundProductCertificate(result.output, self.partition_2, False) self.assertFoundSignedPackages(result.output, self.partition_2, False) self.assertRhelFound(result.output, rhel_version, self.aws_image_id) self.assertIn( '{"repo": "rhel7-cdn-internal", "name": "RHEL 7 - $basearch"}', result.output, ) self.assertIn( '{"repo": "rhel7-cdn-internal-extras", "name": "RHEL 7 - $basearch"}', result.output, ) self.assertIn( '{"repo": "rhel7-cdn-internal-optional", "name": "RHEL 7 - $basearch"}', result.output, ) self.assertIn('"role": "Red Hat Enterprise Linux Server"', result.output) mock_sh_blkid.assert_called_once() mock_describe_devices.assert_called_once() mock_report_results.assert_called_once() results = mock_report_results.call_args[0][0] self.assertReportResultsStructure(results, image_ids=[self.aws_image_id]) self.assertReportResultsImageDetails( results, image_id=self.aws_image_id, rhel_found=True, rhel_signed_packages_found=True, rhel_enabled_repos_found=True, rhel_product_certs_found=True, rhel_release_files_found=True, rhel_version=rhel_version, syspurpose_role="Red Hat Enterprise Linux Server", )