diff --git a/src/core/src/bootstrap/ConfigurationFactory.py b/src/core/src/bootstrap/ConfigurationFactory.py index 50129d11..11d93531 100644 --- a/src/core/src/bootstrap/ConfigurationFactory.py +++ b/src/core/src/bootstrap/ConfigurationFactory.py @@ -102,7 +102,7 @@ def get_arguments_configuration(self, argv): 'component_args': ['env_layer', 'composite_logger'], 'component_kwargs': { 'execution_parameters': str(argv), - 'accept_package_eula': self.accept_package_eula(), + 'accept_package_eula': self.is_package_eula_accepted(), } } } @@ -301,7 +301,7 @@ def get_vm_cloud_type(): print("Failed to connect IMDS end point after 5 retries. This is expected in Arc VMs. VMCloudType is set to Arc.\n") return Constants.VMCloudType.ARC - def accept_package_eula(self): + def is_package_eula_accepted(self): """ Reads customer provided config on EULA acceptance from disk and returns a boolean. NOTE: This is a temporary solution and will be deprecated no later than TBD date""" if not os.path.exists(Constants.Paths.EULA_SETTINGS): diff --git a/src/core/tests/Test_AptitudePackageManager.py b/src/core/tests/Test_AptitudePackageManager.py index 00384fd7..ae3d92c9 100644 --- a/src/core/tests/Test_AptitudePackageManager.py +++ b/src/core/tests/Test_AptitudePackageManager.py @@ -17,6 +17,7 @@ import os import unittest from core.src.bootstrap.Constants import Constants +from core.src.core_logic.ExecutionConfig import ExecutionConfig from core.tests.Test_UbuntuProClient import MockVersionResult, MockRebootRequiredResult, MockUpdatesResult from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.LegacyEnvLayerExtensions import LegacyEnvLayerExtensions @@ -562,85 +563,21 @@ def test_check_pro_client_prerequisites_should_return_false(self): LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution = backup_envlayer_platform_linux_distribution package_manager.ubuntu_pro_client.is_pro_working = backup_ubuntu_pro_client_is_pro_working - def test_eula_acceptance_for_patches(self): - package_manager = self.container.get('package_manager') - # EULA not accepted by default - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) - + def test_eula_accepted_for_patches(self): # EULA accepted in settings and commands updated accordingly - eula_settings = { - "AcceptEULAForAllPatches": True, - "AcceptedBy": "TestSetup", - "LastModified": "2023-08-29" - } - self.runtime.env_layer.file_system.write_with_retry(Constants.Paths.EULA_SETTINGS, '{0}'.format(json.dumps(eula_settings)), mode='w+') - self.runtime_for_test = RuntimeCompositor(self.argument_composer, True, Constants.APT) - self.container_for_test = self.runtime_for_test.container - package_manager_for_test = self.container_for_test.get('package_manager') - + self.runtime.execution_config.accept_package_eula = True + package_manager_for_test = AptitudePackageManager.AptitudePackageManager(self.runtime.env_layer, self.runtime.execution_config, self.runtime.composite_logger, self.runtime.telemetry_writer, self.runtime.status_handler) self.assertTrue("ACCEPT_EULA=Y" in package_manager_for_test.single_package_upgrade_simulation_cmd) self.assertTrue("ACCEPT_EULA=Y" in package_manager_for_test.single_package_dependency_resolution_template) self.assertTrue("ACCEPT_EULA=Y" in package_manager_for_test.single_package_upgrade_cmd) def test_eula_not_accepted_for_patches(self): - package_manager = self.container.get('package_manager') - # EULA not accepted by default - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) - - # EULA not accepted in settings and corresponding patch update commands do not contain EULA acceptance - eula_settings = { - "AcceptEULAForAllPatches": False, - "AcceptedBy": "TestSetup", - "LastModified": "2023-08-29" - } - self.runtime.env_layer.file_system.write_with_retry(Constants.Paths.EULA_SETTINGS, '{0}'.format(json.dumps(eula_settings)), mode='w+') - self.runtime_for_test = RuntimeCompositor(ArgumentComposer().get_composed_arguments(), True, Constants.APT) - self.container_for_test = self.runtime_for_test.container - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) - - # EULA not accepted in settings and corresponding patch update commands do not contain EULA acceptance - eula_settings = { - "AcceptEULAForAllPatches": "test incorrect value", - "AcceptedBy": "TestSetup", - "LastModified": "2023-08-29" - } - self.runtime.env_layer.file_system.write_with_retry(Constants.Paths.EULA_SETTINGS, '{0}'.format(json.dumps(eula_settings)), mode='w+') - self.runtime_for_test = RuntimeCompositor(ArgumentComposer().get_composed_arguments(), True, Constants.APT) - self.container_for_test = self.runtime_for_test.container - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) - - def test_empty_eula_settings(self): - package_manager = self.container.get('package_manager') - # EULA not accepted by default - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) - - # Empty eula settings file - eula_settings = '' - self.runtime.env_layer.file_system.write_with_retry(Constants.Paths.EULA_SETTINGS, '{0}'.format(json.dumps(eula_settings)), mode='w+') - self.runtime_for_test = RuntimeCompositor(ArgumentComposer().get_composed_arguments(), True, Constants.APT) - self.container_for_test = self.runtime_for_test.container - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) - - # Empty eula settings file - eula_settings = None - self.runtime.env_layer.file_system.write_with_retry(Constants.Paths.EULA_SETTINGS, '{0}'.format(json.dumps(eula_settings)), mode='w+') - self.runtime_for_test = RuntimeCompositor(ArgumentComposer().get_composed_arguments(), True, Constants.APT) - self.container_for_test = self.runtime_for_test.container - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_simulation_cmd) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_dependency_resolution_template) - self.assertTrue("ACCEPT_EULA=Y" not in package_manager.single_package_upgrade_cmd) + # EULA accepted in settings and commands updated accordingly + self.runtime.execution_config.accept_package_eula = False + package_manager_for_test = AptitudePackageManager.AptitudePackageManager(self.runtime.env_layer, self.runtime.execution_config, self.runtime.composite_logger, self.runtime.telemetry_writer, self.runtime.status_handler) + self.assertTrue("ACCEPT_EULA=Y" not in package_manager_for_test.single_package_upgrade_simulation_cmd) + self.assertTrue("ACCEPT_EULA=Y" not in package_manager_for_test.single_package_dependency_resolution_template) + self.assertTrue("ACCEPT_EULA=Y" not in package_manager_for_test.single_package_upgrade_cmd) if __name__ == '__main__': diff --git a/src/core/tests/Test_ConfigurationFactory.py b/src/core/tests/Test_ConfigurationFactory.py index c341bd51..964529cb 100644 --- a/src/core/tests/Test_ConfigurationFactory.py +++ b/src/core/tests/Test_ConfigurationFactory.py @@ -13,7 +13,8 @@ # limitations under the License. # # Requires Python 2.7+ - +import json +import os import unittest from core.src.bootstrap.Bootstrapper import Bootstrapper from core.src.bootstrap.Constants import Constants @@ -30,6 +31,9 @@ def setUp(self): def tearDown(self): self.runtime.stop() + def mock_read_with_retry_raise_exception(self): + raise Exception + def test_get_prod_config_correctly(self): bootstrapper = Bootstrapper(self.argument_composer, capture_stdout=False) config_factory = bootstrapper.configuration_factory @@ -59,6 +63,84 @@ def test_get_dev_config_correctly(self): self.assertEqual(config['package_manager_name'], Constants.APT) self.assertEqual(config['config_env'], Constants.DEV) + def test_eula_acceptance_file_read_success(self): + bootstrapper = Bootstrapper(self.argument_composer, capture_stdout=False) + config_factory = bootstrapper.configuration_factory + self.assertTrue(config_factory) + + # Accept EULA set to true + eula_settings = { + "AcceptEULAForAllPatches": True, + "AcceptedBy": "TestSetup", + "LastModified": "2023-08-29" + } + f = open(Constants.Paths.EULA_SETTINGS, "w+") + f.write(json.dumps(eula_settings)) + f.close() + config = config_factory.get_arguments_configuration(self.argument_composer) + self.assertEqual(config['execution_config']['component_kwargs']['accept_package_eula'], True) + + # Accept EULA set to true + eula_settings = { + "AcceptEULAForAllPatches": False, + "AcceptedBy": "TestSetup", + "LastModified": "2023-08-29" + } + f = open(Constants.Paths.EULA_SETTINGS, "w+") + f.write(json.dumps(eula_settings)) + f.close() + config = config_factory.get_arguments_configuration(self.argument_composer) + self.assertEqual(config['execution_config']['component_kwargs']['accept_package_eula'], False) + + def test_eula_acceptance_file_read_when_no_data_found(self): + bootstrapper = Bootstrapper(self.argument_composer, capture_stdout=False) + config_factory = bootstrapper.configuration_factory + self.assertTrue(config_factory) + + # EULA file does not exist + config = config_factory.get_arguments_configuration(self.argument_composer) + self.assertEqual(config['execution_config']['component_kwargs']['accept_package_eula'], False) + self.assertFalse(os.path.exists(Constants.Paths.EULA_SETTINGS)) + + # EULA settings set to None + eula_settings = None + f = open(Constants.Paths.EULA_SETTINGS, "w+") + f.write(json.dumps(eula_settings)) + f.close() + config = config_factory.get_arguments_configuration(self.argument_composer) + self.assertEqual(config['execution_config']['component_kwargs']['accept_package_eula'], False) + self.assertTrue(os.path.exists(Constants.Paths.EULA_SETTINGS)) + + # AcceptEULAForAllPatches not set in config + eula_settings = { + "AcceptedBy": "TestSetup", + "LastModified": "2023-08-29" + } + f = open(Constants.Paths.EULA_SETTINGS, "w+") + f.write(json.dumps(eula_settings)) + f.close() + config = config_factory.get_arguments_configuration(self.argument_composer) + self.assertEqual(config['execution_config']['component_kwargs']['accept_package_eula'], False) + self.assertTrue(os.path.exists(Constants.Paths.EULA_SETTINGS)) + + # AcceptEULAForAllPatches not set to a boolean + eula_settings = { + "AcceptEULAForAllPatches": "test", + "AcceptedBy": "TestSetup", + "LastModified": "2023-08-29" + } + f = open(Constants.Paths.EULA_SETTINGS, "w+") + f.write(json.dumps(eula_settings)) + f.close() + config = config_factory.get_arguments_configuration(self.argument_composer) + self.assertEqual(config['execution_config']['component_kwargs']['accept_package_eula'], False) + self.assertTrue(os.path.exists(Constants.Paths.EULA_SETTINGS)) + + self.backup_read_with_retry = config_factory.read_with_retry + config_factory.read_with_retry = self.mock_read_with_retry_raise_exception + self.assertTrue(os.path.exists(Constants.Paths.EULA_SETTINGS)) + self.assertRaises(Exception, config_factory.get_arguments_configuration(self.argument_composer)) + if __name__ == '__main__': unittest.main()