def test_composite_command_success(self): uc1 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=1) uc2 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=2) uc3 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=3) uc4 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=4) ccu1 = CompositeCommand({uc1: []}, stop_on_error=True) ccu2 = CompositeCommand({uc2: [uc3], uc3: [uc4]}, stop_on_error=True) c1 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=ccu1, id_=5) c2 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=ccu2, id_=6) cc = CompositeCommand({ c1: [c2], c2: [] }, stop_on_error=False, stop_undo_on_error=False) res = cc.invoke() self.assertDictEqual( { 5: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 6: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END) }, cc.result) self.assertEqual(True, res) self.assertEqual(2, self.mocked_imp_succ.execute.call_count) self.assertEqual(0, self.mocked_imp_error.execute.call_count)
def test_undo_on_error(self): mocked_imp_succ = mock.Mock() mocked_imp_error = mock.Mock() mocked_imp_succ.execute.return_value = StepExecution( success=True, stdout='stdout', stderr='stderr', rc=0, start_time=get_now(), end_time=get_now() + datetime.timedelta(5 / (24 * 60 * 60))) mocked_imp_error.execute.return_value = StepExecution( success=False, stdout='stdout', stderr='stderr', rc=0, start_time=get_now(), end_time=get_now() + datetime.timedelta(5 / (24 * 60 * 60))) uc1 = UndoCommand(implementation=mocked_imp_succ, id_=1) uc2 = UndoCommand(implementation=mocked_imp_error, id_=2) uc3 = UndoCommand(implementation=mocked_imp_succ, id_=3) c1 = Command(implementation=mocked_imp_succ, undo_implementation=uc1, id_=1) c2 = Command(implementation=mocked_imp_succ, undo_implementation=uc2, id_=2) c3 = Command(implementation=mocked_imp_error, undo_implementation=uc3, undo_on_error=True, id_=3) cc1 = CompositeCommand({ c1: [c2], c2: [c3] }, force_all=True, id_=1, async_operator=self.ao) res = cc1.invoke() self.assertFalse(res) res = cc1.undo() self.assertEqual(3, mocked_imp_succ.execute.call_count) self.assertEqual(2, mocked_imp_error.execute.call_count) self.assertFalse(res)
def test_invoke_undo__step_undo_on_error_false(self): uc1 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=1) uc2 = UndoCommand(implementation=self.mocked_imp_error, var_context=self.mock_context, id_=2) uc3 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=3) c1 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=uc1, id_=1) c2 = Command(implementation=self.mocked_imp_error, var_context=self.mock_context, undo_command=uc2, undo_on_error=False, id_=2) c3 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=uc3, id_=3) cc1 = CompositeCommand({ c1: [c2], c2: [c3] }, stop_on_error=True, stop_undo_on_error=False, id_=1) res = cc1.invoke() self.assertEqual(1, self.mocked_imp_succ.execute.call_count) self.assertEqual(1, self.mocked_imp_error.execute.call_count) self.assertFalse(res) res = cc1.undo() self.assertEqual(2, self.mocked_imp_succ.execute.call_count) self.assertEqual(1, self.mocked_imp_error.execute.call_count) self.assertTrue(res)
def test_composite_command_error2(self): mocked_imp_succ = mock.Mock() mocked_imp_error = mock.Mock() start_time = get_now() end_time = get_now() + datetime.timedelta(5 / (24 * 60 * 60)) mocked_imp_succ.execute.return_value = StepExecution( success=True, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time) mocked_imp_error.execute.return_value = StepExecution( success=False, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time) uc1 = UndoCommand(implementation=mocked_imp_succ, id_=1) uc2 = UndoCommand(implementation=mocked_imp_succ, id_=2) uc3 = UndoCommand(implementation=mocked_imp_succ, id_=3) uc4 = UndoCommand(implementation=mocked_imp_succ, id_=4) uc5 = UndoCommand(implementation=mocked_imp_succ, id_=5) ccu1 = CompositeCommand({uc1: []}, async_operator=self.ao) ccu2 = CompositeCommand({ uc2: [uc3], uc3: [uc4] }, async_operator=self.ao) c1 = Command(implementation=mocked_imp_succ, undo_implementation=ccu1, id_=6) c2 = Command(implementation=mocked_imp_error, undo_implementation=ccu2, id_=7) c3 = Command(implementation=mocked_imp_succ, undo_implementation=uc5, id_=8) cc = CompositeCommand({c1: [c2], c2: [c3]}, async_operator=self.ao) res = cc.invoke() self.assertEqual(False, res) self.assertEqual(1, mocked_imp_succ.execute.call_count) self.assertEqual(1, mocked_imp_error.execute.call_count) res = cc.undo() self.assertEqual(True, res) self.assertEqual(2, mocked_imp_succ.execute.call_count) self.assertEqual(1, mocked_imp_error.execute.call_count)
def test_composite_command_success(self): mocked_imp_succ = mock.Mock() mocked_imp_error = mock.Mock() start_time = get_now() end_time = get_now() + datetime.timedelta(5 / (24 * 60 * 60)) mocked_imp_succ.execute.return_value = StepExecution( success=True, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time) mocked_imp_error.execute.return_value = StepExecution( success=False, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time) uc1 = UndoCommand(implementation=mocked_imp_succ, id_=1) uc2 = UndoCommand(implementation=mocked_imp_succ, id_=2) uc3 = UndoCommand(implementation=mocked_imp_succ, id_=3) uc4 = UndoCommand(implementation=mocked_imp_succ, id_=4) c1 = Command(implementation=mocked_imp_succ, undo_implementation=uc1, id_=5) c2 = Command(implementation=mocked_imp_succ, undo_implementation=uc2, id_=6) c3 = Command(implementation=mocked_imp_error, undo_implementation=uc3, id_=7) c4 = Command(implementation=mocked_imp_succ, undo_implementation=uc4, id_=8) cc = CompositeCommand({c1: [c2, c3, c4]}, async_operator=self.ao) res = cc.invoke() self.assertDictEqual( { 5: StepExecution(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time), 6: StepExecution(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time), 7: StepExecution(success=False, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time), 8: StepExecution(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=start_time, end_time=end_time), }, cc.execution) self.assertEqual(False, res) self.assertEqual(3, mocked_imp_succ.execute.call_count) self.assertEqual(1, mocked_imp_error.execute.call_count)
def test_composite_command_error(self): uc1 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=1) uc2 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=2) uc3 = UndoCommand(implementation=self.mocked_imp_error, var_context=self.mock_context, id_=3) uc4 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=4) uc5 = UndoCommand(implementation=self.mocked_imp_succ, var_context=self.mock_context, id_=5) executor = ThreadPoolExecutor() ccu1 = CompositeCommand({uc1: []}, stop_on_error=True) ccu2 = CompositeCommand({uc2: [uc3, uc4]}, stop_on_error=True, executor=executor) c6 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=ccu1, id_=6) c7 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=ccu2, id_=7) c8 = Command(implementation=self.mocked_imp_succ, var_context=self.mock_context, undo_command=uc5, id_=8) cc = CompositeCommand({ c6: [c7], c7: [c8] }, stop_on_error=False, stop_undo_on_error=False, executor=executor) res = cc.invoke() self.assertTrue(res) self.assertEqual(3, self.mocked_imp_succ.execute.call_count) self.assertEqual(0, self.mocked_imp_error.execute.call_count) res = cc.undo() self.assertFalse(res) self.assertEqual(7, self.mocked_imp_succ.execute.call_count) self.assertEqual(1, self.mocked_imp_error.execute.call_count) self.assertDictEqual( { 1: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 2: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 3: CompletedProcess(success=False, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 4: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 5: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 6: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 7: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END), 8: CompletedProcess(success=True, stdout='stdout', stderr='stderr', rc=0, start_time=START, end_time=END) }, cc.result)