Skip to content

Commit

Permalink
Adding EULA acceptance: Adding unit tests on recent code change
Browse files Browse the repository at this point in the history
  • Loading branch information
rane-rajasi committed Sep 11, 2023
1 parent a271126 commit 9e1dd29
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/core/src/bootstrap/ConfigurationFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -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):
Expand Down
83 changes: 10 additions & 73 deletions src/core/tests/Test_AptitudePackageManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__':
Expand Down
84 changes: 83 additions & 1 deletion src/core/tests/Test_ConfigurationFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,9 @@ def setUp(self):
def tearDown(self):
self.runtime.stop()

def mock_read_with_retry_raise_exception(self):
raise Exception

Check warning on line 35 in src/core/tests/Test_ConfigurationFactory.py

View check run for this annotation

Codecov / codecov/patch

src/core/tests/Test_ConfigurationFactory.py#L35

Added line #L35 was not covered by tests

def test_get_prod_config_correctly(self):
bootstrapper = Bootstrapper(self.argument_composer, capture_stdout=False)
config_factory = bootstrapper.configuration_factory
Expand Down Expand Up @@ -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()

0 comments on commit 9e1dd29

Please sign in to comment.