Skip to content

Commit

Permalink
add more unit test in status handler to increase code coverage (#226)
Browse files Browse the repository at this point in the history
* add more unit test in status handler to increase code coverage

* pull master changes and add sequence numb termination utest to status_handler

* ignore scratch folder/content

* remove duplicate assertion

* rename Test sequence number change for installation ut

* remove scratch from gitignore

---------

Co-authored-by: Koshy John <[email protected]>
  • Loading branch information
feng-j678 and kjohn-msft authored Mar 20, 2024
1 parent 8734572 commit c58b9aa
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/tests/Test_StatusHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ def test_sort_packages_by_classification_and_state(self):
self.assertEqual(installation_patches_sorted[12]["name"], "test-package-2") # | Other | Excluded |
self.assertEqual(installation_patches_sorted[13]["name"], "test-package-1") # | Other | NotSelected |

def test_sequence_number_changed_termination_configure_patching_only(self):
""" Test sequence number change for configure patching throws newer operation superseded error message """
self.__assert_sequence_num_changed_termination(config=Constants.CONFIGURE_PATCHING, summary=Constants.CONFIGURE_PATCHING_SUMMARY, status=Constants.STATUS_ERROR)

def test_sequence_number_changed_termination_installation(self):
""" Test sequence number change for installation throws newer operation superseded error message """
self.__assert_sequence_num_changed_termination(config=Constants.INSTALLATION, summary=Constants.PATCH_INSTALLATION_SUMMARY, status=Constants.STATUS_ERROR)

def test_if_status_file_resets_on_load_if_malformed(self):
# Mock complete status file with malformed json
sample_json = '[{"version": 1.0, "timestampUTC": "2023-05-13T07:38:07Z", "statusx": {"name": "Azure Patch Management", "operation": "Installation", "status": "success", "code": 0, "formattedMessage": {"lang": "en-US", "message": ""}, "substatusx": []}}]'
Expand Down Expand Up @@ -581,6 +589,18 @@ def test_remove_old_complete_status_files_throws_exception(self):
self.runtime.env_layer.file_system.delete_files_from_dir(file_path, '*.complete.status')
self.assertFalse(os.path.isfile(os.path.join(file_path, '1.complete_status')))

def __assert_sequence_num_changed_termination(self, config, summary, status):
self.runtime.execution_config.operation = config
self.runtime.status_handler.set_current_operation(config)

self.runtime.status_handler.report_sequence_number_changed_termination()
with self.runtime.env_layer.file_system.open(self.runtime.execution_config.status_file_path, 'r') as file_handle:
substatus_file_data = json.load(file_handle)[0]["status"]["substatus"][0]
self.assertTrue(substatus_file_data["name"] == summary)
self.assertEqual(substatus_file_data["status"], status.lower())
formatted_message = json.loads(substatus_file_data['formattedMessage']['message'])
self.assertTrue(formatted_message["errors"]["details"][0]["code"] == Constants.PatchOperationErrorCodes.NEWER_OPERATION_SUPERSEDED)

# Setup functions to populate packages and versions for truncation
def __set_up_packages_func(self, val):
test_packages = []
Expand Down

0 comments on commit c58b9aa

Please sign in to comment.