def setup_runs(self): action_names = ['action_name', 'second_name', 'cleanup'] action_graph = [ mock.Mock(name=name, required_actions=[]) for name in action_names ] self.action_graph = actiongraph.ActionGraph( action_graph, dict((a.name, a) for a in action_graph)) self.output_path = filehandler.OutputPath(tempfile.mkdtemp()) self.command = "do command" self.action_runs = [self._build_run(name) for name in action_names] self.run_map = dict((a.action_name, a) for a in self.action_runs) self.run_map['cleanup'].is_cleanup = True self.collection = ActionRunCollection(self.action_graph, self.run_map)
def setup_collection(self): action_names = ['action_name', 'second_name', 'cleanup'] action_graph = [ Turtle(name=name, required_actions=[]) for name in action_names ] self.second_act = second_act = action_graph.pop(1) second_act.required_actions.append(action_graph[0]) action_map = dict((a.name, a) for a in action_graph) action_map['second_name'] = second_act self.action_graph = actiongraph.ActionGraph(action_graph, action_map) self.output_path = filehandler.OutputPath(tempfile.mkdtemp()) self.command = "do command" self.action_runs = [self._build_run(name) for name in action_names] self.run_map = dict((a.action_name, a) for a in self.action_runs) self.run_map['cleanup'].is_cleanup = True self.collection = ActionRunCollection(self.action_graph, self.run_map)
class ActionRunCollectionIsRunBlockedTestCase(TestCase): def _build_run(self, name): mock_node = mock.create_autospec(node.Node) return ActionRun("id", name, mock_node, self.command, output_path=self.output_path) @setup def setup_collection(self): action_names = ['action_name', 'second_name', 'cleanup'] action_graph = [ Turtle(name=name, required_actions=[]) for name in action_names ] self.second_act = second_act = action_graph.pop(1) second_act.required_actions.append(action_graph[0]) action_map = dict((a.name, a) for a in action_graph) action_map['second_name'] = second_act self.action_graph = actiongraph.ActionGraph(action_graph, action_map) self.output_path = filehandler.OutputPath(tempfile.mkdtemp()) self.command = "do command" self.action_runs = [self._build_run(name) for name in action_names] self.run_map = dict((a.action_name, a) for a in self.action_runs) self.run_map['cleanup'].is_cleanup = True self.collection = ActionRunCollection(self.action_graph, self.run_map) @teardown def teardown_action_run(self): shutil.rmtree(self.output_path.base, ignore_errors=True) def test_is_run_blocked_no_required_actions(self): assert not self.collection._is_run_blocked(self.run_map['action_name']) def test_is_run_blocked_completed_run(self): self.run_map['second_name'].machine.state = ActionRun.STATE_FAILED assert not self.collection._is_run_blocked(self.run_map['second_name']) self.run_map['second_name'].machine.state = ActionRun.STATE_RUNNING assert not self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_completed(self): self.run_map['action_name'].machine.state = ActionRun.STATE_SKIPPED assert not self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_blocked(self): third_act = Turtle(name='third_act', required_actions=[self.second_act]) self.action_graph.action_map['third_act'] = third_act self.run_map['third_act'] = self._build_run('third_act') self.run_map['action_name'].machine.state = ActionRun.STATE_FAILED assert self.collection._is_run_blocked(self.run_map['third_act']) def test_is_run_blocked_required_actions_scheduled(self): self.run_map['action_name'].machine.state = ActionRun.STATE_SCHEDULED assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_starting(self): self.run_map['action_name'].machine.state = ActionRun.STATE_STARTING assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_queued(self): self.run_map['action_name'].machine.state = ActionRun.STATE_QUEUED assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_failed(self): self.run_map['action_name'].machine.state = ActionRun.STATE_FAILED assert self.collection._is_run_blocked(self.run_map['second_name'])
class ActionRunCollectionTestCase(TestCase): def _build_run(self, name): mock_node = mock.create_autospec(node.Node) return ActionRun("id", name, mock_node, self.command, output_path=self.output_path) @setup def setup_runs(self): action_names = ['action_name', 'second_name', 'cleanup'] action_graph = [ mock.Mock(name=name, required_actions=[]) for name in action_names ] self.action_graph = actiongraph.ActionGraph( action_graph, dict((a.name, a) for a in action_graph)) self.output_path = filehandler.OutputPath(tempfile.mkdtemp()) self.command = "do command" self.action_runs = [self._build_run(name) for name in action_names] self.run_map = dict((a.action_name, a) for a in self.action_runs) self.run_map['cleanup'].is_cleanup = True self.collection = ActionRunCollection(self.action_graph, self.run_map) @teardown def teardown_action_run(self): shutil.rmtree(self.output_path.base, ignore_errors=True) def test__init__(self): assert_equal(self.collection.action_graph, self.action_graph) assert_equal(self.collection.run_map, self.run_map) assert self.collection.proxy_action_runs_with_cleanup def test_action_runs_for_actions(self): actions = [Turtle(name='action_name')] action_runs = self.collection.action_runs_for_actions(actions) assert_equal(list(action_runs), self.action_runs[:1]) def test_get_action_runs_with_cleanup(self): runs = self.collection.get_action_runs_with_cleanup() assert_equal(set(runs), set(self.action_runs)) def test_get_action_runs(self): runs = self.collection.get_action_runs() assert_equal(set(runs), set(self.action_runs[:2])) def test_cleanup_action_run(self): assert_equal(self.action_runs[2], self.collection.cleanup_action_run) def test_state_data(self): state_data = self.collection.state_data assert_length(state_data, len(self.action_runs[:2])) def test_cleanup_action_state_data(self): state_data = self.collection.cleanup_action_state_data assert_equal(state_data['action_name'], 'cleanup') def test_cleanup_action_state_data_no_cleanup_action(self): del self.collection.run_map['cleanup'] assert not self.collection.cleanup_action_state_data def test_get_startable_action_runs(self): action_runs = self.collection.get_startable_action_runs() assert_equal(set(action_runs), set(self.action_runs[:2])) def test_get_startable_action_runs_none(self): self.collection.run_map.clear() action_runs = self.collection.get_startable_action_runs() assert_equal(set(action_runs), set()) def test_has_startable_action_runs(self): assert self.collection.has_startable_action_runs def test_has_startable_action_runs_false(self): self.collection.run_map.clear() assert not self.collection.has_startable_action_runs def test_is_complete_false(self): assert not self.collection.is_complete def test_is_complete_true(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.STATE_SKIPPED assert self.collection.is_complete def test_is_done_false(self): assert not self.collection.is_done def test_is_done_false_because_of_running(self): action_run = self.collection.run_map['action_name'] action_run.machine.state = ActionRun.STATE_RUNNING assert not self.collection.is_done def test_is_done_true_because_blocked(self): self.run_map['action_name'].machine.state = ActionRun.STATE_FAILED self.run_map['second_name'].machine.state = ActionRun.STATE_QUEUED autospec_method(self.collection._is_run_blocked) blocked_second_action_run = lambda ar: ar == self.run_map['second_name'] self.collection._is_run_blocked.side_effect = blocked_second_action_run assert self.collection.is_done assert self.collection.is_failed def test_is_done_true(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.STATE_FAILED assert self.collection.is_done def test_is_failed_false_not_done(self): self.run_map['action_name'].machine.state = ActionRun.STATE_FAILED assert not self.collection.is_failed def test_is_failed_false_no_failed(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.STATE_SUCCEEDED assert not self.collection.is_failed def test_is_failed_true(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.STATE_FAILED assert self.collection.is_failed def test__getattr__(self): assert self.collection.is_scheduled assert not self.collection.is_cancelled assert not self.collection.is_running assert self.collection.ready() def test__str__(self): self.collection._is_run_blocked = lambda r: r.action_name != 'cleanup' expected = [ "ActionRunCollection", "second_name(scheduled:blocked)", "action_name(scheduled:blocked)", "cleanup(scheduled)" ] for expectation in expected: assert_in(expectation, str(self.collection)) def test_end_time(self): max_end_time = datetime.datetime(2013, 6, 15) self.run_map['action_name'].machine.state = ActionRun.STATE_FAILED self.run_map['action_name'].end_time = datetime.datetime(2013, 5, 12) self.run_map['second_name'].machine.state = ActionRun.STATE_SUCCEEDED self.run_map['second_name'].end_time = max_end_time assert_equal(self.collection.end_time, max_end_time) def test_end_time_not_done(self): self.run_map['action_name'].end_time = datetime.datetime(2013, 5, 12) self.run_map['action_name'].machine.state = ActionRun.STATE_FAILED self.run_map['second_name'].end_time = None self.run_map['second_name'].machine.state = ActionRun.STATE_RUNNING assert_equal(self.collection.end_time, None) def test_end_time_not_started(self): assert_equal(self.collection.end_time, None)
class TestActionRunCollectionIsRunBlocked(TestCase): def _build_run(self, name): mock_node = mock.create_autospec(node.Node) return ActionRun( "id", name, mock_node, self.command, output_path=self.output_path, ) @setup def setup_collection(self): action_names = ['action_name', 'second_name', 'cleanup'] action_graph = [] for name in action_names: m = MagicMock(name=name, required_actions=[]) m.name = name action_graph.append(m) self.second_act = second_act = action_graph.pop(1) second_act.required_actions.append(action_graph[0]) action_map = {a.name: a for a in action_graph} action_map['second_name'] = second_act self.action_graph = actiongraph.ActionGraph(action_graph, action_map) self.output_path = filehandler.OutputPath(tempfile.mkdtemp()) self.command = "do command" self.action_runs = [self._build_run(name) for name in action_names] self.run_map = {a.action_name: a for a in self.action_runs} self.run_map['cleanup'].is_cleanup = True self.collection = ActionRunCollection(self.action_graph, self.run_map) @teardown def teardown_action_run(self): shutil.rmtree(self.output_path.base, ignore_errors=True) def test_is_run_blocked_no_required_actions(self): assert not self.collection._is_run_blocked(self.run_map['action_name']) def test_is_run_blocked_completed_run(self): self.run_map['second_name'].machine.state = ActionRun.FAILED assert not self.collection._is_run_blocked(self.run_map['second_name']) self.run_map['second_name'].machine.state = ActionRun.RUNNING assert not self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_completed(self): self.run_map['action_name'].machine.state = ActionRun.SKIPPED assert not self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_blocked(self): third_act = MagicMock(required_actions=[self.second_act], ) third_act.name = 'third_act' self.action_graph.action_map['third_act'] = third_act self.run_map['third_act'] = self._build_run('third_act') self.run_map['action_name'].machine.state = ActionRun.FAILED assert self.collection._is_run_blocked(self.run_map['third_act']) def test_is_run_blocked_required_actions_scheduled(self): self.run_map['action_name'].machine.state = ActionRun.SCHEDULED assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_starting(self): self.run_map['action_name'].machine.state = ActionRun.STARTING assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_queued(self): self.run_map['action_name'].machine.state = ActionRun.QUEUED assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_failed(self): self.run_map['action_name'].machine.state = ActionRun.FAILED assert self.collection._is_run_blocked(self.run_map['second_name']) def test_is_run_blocked_required_actions_missing(self): del self.run_map['action_name'] assert not self.collection._is_run_blocked(self.run_map['second_name'])
class TestActionRunCollection(TestCase): def _build_run(self, name): mock_node = mock.create_autospec(node.Node) return ActionRun( "id", name, mock_node, self.command, output_path=self.output_path, ) @setup def setup_runs(self): action_names = ['action_name', 'second_name', 'cleanup'] action_graph = [] for name in action_names: m = mock.Mock(name=name, required_actions=[]) m.name = name action_graph.append(m) self.action_graph = actiongraph.ActionGraph( action_graph, {a.name: a for a in action_graph}, ) self.output_path = filehandler.OutputPath(tempfile.mkdtemp()) self.command = "do command" self.action_runs = [self._build_run(name) for name in action_names] self.run_map = {a.action_name: a for a in self.action_runs} self.run_map['cleanup'].is_cleanup = True self.collection = ActionRunCollection(self.action_graph, self.run_map) @teardown def teardown_action_run(self): shutil.rmtree(self.output_path.base, ignore_errors=True) def test__init__(self): assert_equal(self.collection.action_graph, self.action_graph) assert_equal(self.collection.run_map, self.run_map) assert self.collection.proxy_action_runs_with_cleanup def test_action_runs_for_actions(self): m = MagicMock() m.name = 'action_name' actions = [m] action_runs = self.collection.action_runs_for_actions(actions) assert_equal(list(action_runs), self.action_runs[:1]) def test_get_action_runs_with_cleanup(self): runs = self.collection.get_action_runs_with_cleanup() assert_equal(set(runs), set(self.action_runs)) def test_get_action_runs(self): runs = self.collection.get_action_runs() assert_equal(set(runs), set(self.action_runs[:2])) def test_cleanup_action_run(self): assert_equal(self.action_runs[2], self.collection.cleanup_action_run) def test_state_data(self): state_data = self.collection.state_data assert_length(state_data, len(self.action_runs[:2])) def test_cleanup_action_state_data(self): state_data = self.collection.cleanup_action_state_data assert_equal(state_data['action_name'], 'cleanup') def test_cleanup_action_state_data_no_cleanup_action(self): del self.collection.run_map['cleanup'] assert not self.collection.cleanup_action_state_data def test_get_startable_action_runs(self): action_runs = self.collection.get_startable_action_runs() assert_equal(set(action_runs), set(self.action_runs[:2])) def test_get_startable_action_runs_none(self): self.collection.run_map.clear() action_runs = self.collection.get_startable_action_runs() assert_equal(set(action_runs), set()) def test_has_startable_action_runs(self): assert self.collection.has_startable_action_runs def test_has_startable_action_runs_false(self): self.collection.run_map.clear() assert not self.collection.has_startable_action_runs def test_is_complete_false(self): assert not self.collection.is_complete def test_is_complete_true(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.SKIPPED assert self.collection.is_complete def test_is_done_false(self): assert not self.collection.is_done def test_is_done_false_because_of_running(self): action_run = self.collection.run_map['action_name'] action_run.machine.state = ActionRun.RUNNING assert not self.collection.is_done def test_is_done_true_because_blocked(self): self.run_map['action_name'].machine.state = ActionRun.FAILED self.run_map['second_name'].machine.state = ActionRun.QUEUED autospec_method(self.collection._is_run_blocked) def blocked_second_action_run(ar, ): return ar == self.run_map['second_name'] self.collection._is_run_blocked.side_effect = blocked_second_action_run assert self.collection.is_done assert self.collection.is_failed def test_is_done_true(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.FAILED assert self.collection.is_done def test_is_failed_false_not_done(self): self.run_map['action_name'].machine.state = ActionRun.FAILED assert not self.collection.is_failed def test_is_failed_false_no_failed(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.SUCCEEDED assert not self.collection.is_failed def test_is_failed_true(self): for action_run in self.collection.action_runs_with_cleanup: action_run.machine.state = ActionRun.FAILED assert self.collection.is_failed def test__getattr__(self): assert self.collection.is_scheduled assert not self.collection.is_cancelled assert not self.collection.is_running assert self.collection.ready() def test__str__(self): self.collection._is_run_blocked = lambda r: r.action_name != 'cleanup' expected = [ "ActionRunCollection", "second_name(scheduled:blocked)", "action_name(scheduled:blocked)", "cleanup(scheduled)", ] for expectation in expected: assert_in(expectation, str(self.collection)) def test_end_time(self): max_end_time = datetime.datetime(2013, 6, 15) self.run_map['action_name'].machine.state = ActionRun.FAILED self.run_map['action_name'].end_time = datetime.datetime(2013, 5, 12) self.run_map['second_name'].machine.state = ActionRun.SUCCEEDED self.run_map['second_name'].end_time = max_end_time assert_equal(self.collection.end_time, max_end_time) def test_end_time_not_done(self): self.run_map['action_name'].end_time = datetime.datetime(2013, 5, 12) self.run_map['action_name'].machine.state = ActionRun.FAILED self.run_map['second_name'].end_time = None self.run_map['second_name'].machine.state = ActionRun.RUNNING assert_equal(self.collection.end_time, None) def test_end_time_not_started(self): assert_equal(self.collection.end_time, None)