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_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_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", )