|
14 | 14 | #
|
15 | 15 | # Requires Python 2.7+
|
16 | 16 | import datetime
|
| 17 | +import glob |
17 | 18 | import json
|
18 | 19 | import os
|
19 | 20 | import re
|
| 21 | +import shutil |
20 | 22 | import time
|
21 | 23 | import unittest
|
22 | 24 | import uuid
|
@@ -46,6 +48,12 @@ def mock_linux_distribution_to_return_centos(self):
|
46 | 48 | def mock_linux_distribution_to_return_redhat(self):
|
47 | 49 | return ['Red Hat Enterprise Linux Server', '7.5', 'Maipo']
|
48 | 50 |
|
| 51 | + def mock_os_remove(self, file_to_remove): |
| 52 | + raise Exception("File could not be deleted") |
| 53 | + |
| 54 | + def mock_os_path_exists(self, patch_to_validate): |
| 55 | + return False |
| 56 | + |
49 | 57 | def test_operation_fail_for_non_autopatching_request(self):
|
50 | 58 | # Test for non auto patching request
|
51 | 59 | argument_composer = ArgumentComposer()
|
@@ -880,7 +888,7 @@ def test_assessment_superseded(self):
|
880 | 888 | scratch_path = os.path.join(os.path.curdir, "scratch")
|
881 | 889 |
|
882 | 890 | # Step 2: Set 1.status to Transitioning
|
883 |
| - with open(os.path.join(scratch_path, "1.status"), 'r+') as f: |
| 891 | + with open(os.path.join(scratch_path, "status", "1.status"), 'r+') as f: |
884 | 892 | status = json.load(f)
|
885 | 893 | status[0]["status"]["status"] = "transitioning"
|
886 | 894 | status[0]["status"]["substatus"][0]["status"] = "transitioning"
|
@@ -919,6 +927,98 @@ def test_assessment_superseded(self):
|
919 | 927 |
|
920 | 928 | runtime.stop()
|
921 | 929 |
|
| 930 | + def test_temp_folder_created_during_execution_config_init(self): |
| 931 | + # temp_folder is set with a path in environment settings but the dir does not exist |
| 932 | + argument_composer = ArgumentComposer() |
| 933 | + shutil.rmtree(argument_composer.temp_folder) |
| 934 | + argument_composer.operation = Constants.ASSESSMENT |
| 935 | + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.APT) |
| 936 | + # validate temp_folder is created |
| 937 | + self.assertTrue(runtime.execution_config.temp_folder is not None) |
| 938 | + self.assertTrue(os.path.exists(runtime.execution_config.temp_folder)) |
| 939 | + runtime.stop() |
| 940 | + |
| 941 | + # temp_folder is set to None in ExecutionConfig with a valid config_folder location |
| 942 | + argument_composer = ArgumentComposer() |
| 943 | + shutil.rmtree(argument_composer.temp_folder) |
| 944 | + argument_composer.temp_folder = None |
| 945 | + argument_composer.operation = Constants.ASSESSMENT |
| 946 | + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.APT) |
| 947 | + # validate temp_folder is created |
| 948 | + self.assertTrue(runtime.execution_config.temp_folder is not None) |
| 949 | + self.assertTrue(os.path.exists(runtime.execution_config.temp_folder)) |
| 950 | + runtime.stop() |
| 951 | + |
| 952 | + # temp_folder is set to None in ExecutionConfig with an invalid config_folder location, throws exception |
| 953 | + argument_composer = ArgumentComposer() |
| 954 | + shutil.rmtree(argument_composer.temp_folder) |
| 955 | + argument_composer.temp_folder = None |
| 956 | + argument_composer.operation = Constants.ASSESSMENT |
| 957 | + # mock path exists check to return False on config_folder exists check |
| 958 | + backup_os_path_exists = os.path.exists |
| 959 | + os.path.exists = self.mock_os_path_exists |
| 960 | + self.assertRaises(Exception, lambda: RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.APT)) |
| 961 | + # validate temp_folder is not created |
| 962 | + self.assertFalse(os.path.exists(os.path.join(os.path.curdir, "scratch", "tmp"))) |
| 963 | + os.path.exists = backup_os_path_exists |
| 964 | + runtime.stop() |
| 965 | + |
| 966 | + def test_delete_temp_folder_contents_success(self): |
| 967 | + argument_composer = ArgumentComposer() |
| 968 | + self.assertTrue(argument_composer.temp_folder is not None) |
| 969 | + self.assertEqual(argument_composer.temp_folder, os.path.abspath(os.path.join(os.path.curdir, "scratch", "tmp"))) |
| 970 | + |
| 971 | + # delete temp content |
| 972 | + argument_composer.operation = Constants.ASSESSMENT |
| 973 | + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.APT) |
| 974 | + runtime.set_legacy_test_type('HappyPath') |
| 975 | + CoreMain(argument_composer.get_composed_arguments()) |
| 976 | + |
| 977 | + # validate files are deleted |
| 978 | + self.assertTrue(argument_composer.temp_folder is not None) |
| 979 | + files_matched = glob.glob(str(argument_composer.temp_folder) + "/" + str(Constants.TEMP_FOLDER_CLEANUP_ARTIFACT_LIST)) |
| 980 | + self.assertTrue(len(files_matched) == 0) |
| 981 | + runtime.stop() |
| 982 | + |
| 983 | + def test_delete_temp_folder_contents_when_none_exists(self): |
| 984 | + argument_composer = ArgumentComposer() |
| 985 | + argument_composer.operation = Constants.ASSESSMENT |
| 986 | + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.APT) |
| 987 | + shutil.rmtree(runtime.execution_config.temp_folder) |
| 988 | + |
| 989 | + # attempt to delete temp content |
| 990 | + runtime.env_layer.file_system.delete_files_from_dir(runtime.execution_config.temp_folder, Constants.TEMP_FOLDER_CLEANUP_ARTIFACT_LIST) |
| 991 | + |
| 992 | + # validate files are deleted |
| 993 | + self.assertTrue(runtime.execution_config.temp_folder is not None) |
| 994 | + files_matched = glob.glob(str(runtime.execution_config.temp_folder) + "/" + str(Constants.TEMP_FOLDER_CLEANUP_ARTIFACT_LIST)) |
| 995 | + self.assertTrue(len(files_matched) == 0) |
| 996 | + runtime.stop() |
| 997 | + |
| 998 | + def test_delete_temp_folder_contents_failure(self): |
| 999 | + argument_composer = ArgumentComposer() |
| 1000 | + self.assertTrue(argument_composer.temp_folder is not None) |
| 1001 | + self.assertEqual(argument_composer.temp_folder, os.path.abspath(os.path.join(os.path.curdir, "scratch", "tmp"))) |
| 1002 | + |
| 1003 | + # mock os.remove() |
| 1004 | + self.backup_os_remove = os.remove |
| 1005 | + os.remove = self.mock_os_remove |
| 1006 | + |
| 1007 | + argument_composer.operation = Constants.ASSESSMENT |
| 1008 | + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.APT) |
| 1009 | + |
| 1010 | + # delete temp content attempt #1, throws exception |
| 1011 | + self.assertRaises(Exception, lambda: runtime.env_layer.file_system.delete_files_from_dir(runtime.execution_config.temp_folder, Constants.TEMP_FOLDER_CLEANUP_ARTIFACT_LIST, raise_if_delete_failed=True)) |
| 1012 | + self.assertTrue(os.path.isfile(os.path.join(runtime.execution_config.temp_folder, "temp1.list"))) |
| 1013 | + |
| 1014 | + # delete temp content attempt #2, does not throws exception |
| 1015 | + runtime.env_layer.file_system.delete_files_from_dir(runtime.execution_config.temp_folder, Constants.TEMP_FOLDER_CLEANUP_ARTIFACT_LIST) |
| 1016 | + self.assertTrue(os.path.isfile(os.path.join(runtime.execution_config.temp_folder, "temp1.list"))) |
| 1017 | + |
| 1018 | + # reset os.remove() mock |
| 1019 | + os.remove = self.backup_os_remove |
| 1020 | + runtime.stop() |
| 1021 | + |
922 | 1022 | def __check_telemetry_events(self, runtime):
|
923 | 1023 | all_events = os.listdir(runtime.telemetry_writer.events_folder_path)
|
924 | 1024 | self.assertTrue(len(all_events) > 0)
|
|
0 commit comments