def test_12_bug_9287(self): """Verify that the current exception stack won't be logged unless it is for the same exception the operation ended with.""" h = self.__h # Test that an exception that occurs before an operation # starts won't be recorded if it is not the same exception # the operation ended with. # Clear history completely. h.purge() shutil.rmtree(h.path) # Populate the exception stack. try: d = {} d['nosuchkey'] except KeyError: pass h.log_operation_start("test-exceptions") e = AssertionError() h.log_operation_end(error=e) for entry in sorted(os.listdir(h.path)): # Load the history entry. he = history.History(root_dir=h.root_dir, filename=entry) # Verify that the right exception was logged. for e in he.operation_errors: self.assertNotEqual(e.find("AssertionError"), -1)
def test_07_aborted_operations(self): """Verify that aborted operations are saved properly.""" h = self.__h h.client_name = "pkg-test" for i in range(1, 4): h.log_operation_start("operation-{0:d}".format(i)) h.abort(history.RESULT_FAILED_BAD_REQUEST) # Now load all operation data that's been saved during testing # for comparison and verify the expected result was set for # each. loaded_ops = [] for entry in sorted(os.listdir(h.path)): # Load the history entry. he = history.History(root_dir=h.root_dir, filename=entry) if he.operation_name != "purge-history": loaded_ops.append([he.operation_name, he.operation_result]) # There should be three operations: operation-1, operation-2, # and operation-3. self.assertTrue(len(loaded_ops) == 3) for op in loaded_ops: op_name, op_result = op self.assertTrue(re.match("operation-[123]", op_name)) self.assertEqual(op_result, history.RESULT_FAILED_BAD_REQUEST)
def test_11_bug_8072(self): """Verify that a history file with unexpected start state, end state, and error data won't cause an exception.""" bad_hist = b"""<?xml version="1.0" encoding="ascii"?> <history> <client name="pkg" version="e827313523d8+"> <args> <arg><![CDATA[/usr/bin/pkg]]></arg> <arg><![CDATA[-R]]></arg> <arg><![CDATA[/tmp/image]]></arg> <arg><![CDATA[install]]></arg> <arg><![CDATA[Django]]></arg> </args> </client> <operation end_time="20090409T165520Z" name="install" result="Failed, Out of Memory" start_time="20090409T165520Z" userid="101" username="******"> <start_state></start_state> <end_state></end_state> <errors> <error></error> </errors> </operation> </history>""" (fd1, path1) = tempfile.mkstemp(dir=self.__scratch_dir) os.write(fd1, bad_hist) # Load the history entry. he = history.History(root_dir=self.__scratch_dir, filename=path1) self.assertEqual(he.operation_start_state, "None") self.assertEqual(he.operation_end_state, "None") self.assertEqual(he.operation_errors, [])
def test_04_stacked_operations(self): """Verify that multiple operations can be stacked properly (in other words, that storage and retrieval works as expected). """ op_stack = { "operation-1": { "start_state": "op-1-start", "end_state": "op-1-end", "result": history.RESULT_SUCCEEDED, }, "operation-2": { "start_state": "op-2-start", "end_state": "op-2-end", "result": history.RESULT_FAILED_UNKNOWN, }, "operation-3": { "start_state": "op-3-start", "end_state": "op-3-end", "result": history.RESULT_CANCELED, }, } h = self.__h h.client_name = "pkg-test" for op_name in sorted(op_stack.keys()): h.log_operation_start(op_name) for op_name in sorted(op_stack.keys(), reverse=True): op_data = op_stack[op_name] h.operation_start_state = op_data["start_state"] h.operation_end_state = op_data["end_state"] h.log_operation_end(result=op_data["result"]) # Now load all operation data that's been saved during testing # for comparison. loaded_ops = {} for entry in sorted(os.listdir(h.path)): # Load the history entry. he = history.History(root_dir=h.root_dir, filename=entry) loaded_ops[he.operation_name] = { "start_state": he.operation_start_state, "end_state": he.operation_end_state, "result": he.operation_result } # Now verify that each operation was saved in the stack and # that the correct data was written for each one. for op_name in op_stack.keys(): op_data = op_stack[op_name] loaded_data = loaded_ops[op_name] self.assertEqual(op_data, loaded_data)
def setUp(self): """Prepare the test for execution. """ pkg5unittest.Pkg5TestCase.setUp(self) self.__scratch_dir = tempfile.mkdtemp(dir=self.test_root) # Explicitly convert these to strings as they will be # converted by history to deal with minidom issues. self.__userid = str(portable.get_userid()) self.__username = str(portable.get_username()) self.__h = history.History(root_dir=self.__scratch_dir) self.__h.client_name = "pkg-test"
def test_03_client_load(self): """Verify that the saved history can be retrieved properly. """ h = history.History(root_dir=self.__scratch_dir, filename=self.__filename) # Verify that a valid start time and end time was set. misc.timestamp_to_time(h.operation_start_time) misc.timestamp_to_time(h.operation_end_time) self.assertEqual("install", h.operation_name) self.assertEqual(self.__username, h.operation_username) self.assertEqual(self.__userid, h.operation_userid) self.assertEqual(self.__ip_before, h.operation_start_state) self.assertEqual(self.__ip_after, h.operation_end_state) self.assertEqual(self.__errors, h.operation_errors) self.assertEqual(history.RESULT_SUCCEEDED, h.operation_result)
def test_08_bug_3540(self): """Ensure that corrupt History files raise a HistoryLoadException with parse_failure set to True. """ # Overwrite first entry with bad data. h = self.__h entry = sorted(os.listdir(h.path))[0] f = open(os.path.join(h.path, entry), "w") f.write("<Invalid>") f.close() try: he = history.History(root_dir=h.root_dir, filename=entry) except apx.HistoryLoadException as e: if not e.parse_failure: raise pass
def test_06_purge_history(self): """Verify that purge() removes all history and creates a new history entry. """ h = self.__h h.clear() h.client_name = "pkg-test" h.purge() expected_ops = [["purge-history", history.RESULT_SUCCEEDED]] # Now load all operation data to verify that only an entry # for purge-history remains and that it was successful. loaded_ops = [] for entry in sorted(os.listdir(h.path)): # Load the history entry. he = history.History(root_dir=h.root_dir, filename=entry) loaded_ops.append([he.operation_name, he.operation_result]) self.assertTrue(loaded_ops == expected_ops)
def test_05_discarded_operations(self): """Verify that discarded operations are not saved. """ h = self.__h h.client_name = "pkg-test" for op_name in sorted(history.DISCARDED_OPERATIONS): h.log_operation_start(op_name) h.log_operation_end(history.RESULT_NOTHING_TO_DO) # Now load all operation data that's been saved during testing # for comparison. loaded_ops = [] for entry in sorted(os.listdir(h.path)): # Load the history entry. he = history.History(root_dir=h.root_dir, filename=entry) loaded_ops.append(he.operation_name) # Now verify that none of the saved operations are one that # should have been discarded. for op_name in sorted(history.DISCARDED_OPERATIONS): self.assertTrue(op_name not in loaded_ops)