def _prepare(self, action, comps=None, groupby=None, reverse=False, need_unload=False, tunings=None, allservers=False, **kwargs): """ Instanciate all actions for the component list and but them in a graph of ActionGroup(). Action could be local or proxy actions. Components list is filtered, based on action name. """ graph = ActionGroup() comps = comps or self.components first_comps = None last_comps = None localsrv = None modules = set() localcomps = None if groupby: iterable = comps.groupby(attr=groupby, reverse=reverse) else: iterable = [(None, comps)] # Iterate over targets, grouping them by start order and server. for _order, comps in iterable: graph.add(ActionGroup()) compgrp = ActionGroup() proxygrp = ActionGroup() for srv, comps in comps.groupbyserver(allservers=allservers): if srv.action_enabled is True: if srv.is_local(): localsrv = srv localcomps = comps for comp in comps: compgrp.add(getattr(comp, action)(**kwargs)) else: act = self._proxy_action(action, srv.hostname, comps, **kwargs) if tunings and tunings.filename: copy = Install(srv.hostname, self, tunings.filename, comps=comps, **kwargs) act.depends_on(copy) proxygrp.add(copy) proxygrp.add(act) if len(compgrp) > 0: graph[-1].add(compgrp) # Keep track of first comp group if first_comps is None: first_comps = compgrp first_comps.parent = graph[-1] # Keep track of last comp group last_comps = compgrp last_comps.parent = graph[-1] # Build module loading list, if needed for comp_action in compgrp: modules.update(comp_action.needed_modules()) if len(proxygrp) > 0: graph[-1].add(proxygrp) # Add module loading, if needed. if first_comps is not None and len(modules) > 0: modgrp = ActionGroup() for module in modules: modgrp.add(localsrv.load_modules(modname=module, **kwargs)) # Serialize modules loading actions modgrp.sequential() first_comps.parent.add(modgrp) first_comps.depends_on(modgrp) # Apply tuning to last component group, if needed if tunings is not None and last_comps is not None: tune = localsrv.tune(tunings, localcomps, self.fs_name, **kwargs) last_comps.parent.add(tune) tune.depends_on(last_comps) # Add module unloading to last component group, if needed. if need_unload and last_comps is not None: unload = localsrv.unload_modules(**kwargs) last_comps.parent.add(unload) unload.depends_on(last_comps) # Join the different part together graph.sequential() return graph
class ActionGroupTests(unittest.TestCase): def setUp(self): self.grp = ActionGroup() def test_group_len_and_iter(self): """Action group length and __iter__ are correct""" self.assertEqual(len(self.grp), 0) act1 = TestAction('/bin/foo') self.grp.add(act1) self.assertEqual(len(self.grp), 1) self.assertEqual(self.grp[0], act1) self.assertEqual(self.grp[-1], act1) act2 = TestAction('/bin/bar') self.grp.add(act2) # Add it twice does not raise an error self.grp.add(act2) self.assertEqual(len(self.grp), 2) self.assertEqual(self.grp[0], act1) self.assertEqual(self.grp[1], act2) self.assertEqual(self.grp[-1], act2) self.assertEqual(list(iter(self.grp)), [act1, act2]) def test_sequential(self): """A group could be easily sequentialized""" act1 = TestAction('/bin/foo') self.grp.add(act1) act2 = TestAction('/bin/foo') self.grp.add(act2) self.grp.sequential() self.assertEqual(act1.deps, set([])) self.assertEqual(act2.deps, set([act1])) self.assertEqual(act1.followers, set([self.grp, act2])) self.assertEqual(act2.followers, set([self.grp])) def test_empty(self): """Launching an empty group is ok""" self.grp.launch() task_self().run() self.assertEqual(self.grp.status(), ACT_OK) def test_one_action(self): """A group with its only action both run ok""" act = TestAction('/bin/true') self.grp.add(act) self.grp.launch() task_self().run() self.assertEqual(act.status(), ACT_OK) self.assertEqual(self.grp.status(), ACT_OK) def test_two_actions_ok_and_ok(self): """A group with 2 actions all run fine""" act1 = TestAction('/bin/true') act2 = TestAction('/bin/true') self.grp.add(act1) self.grp.add(act2) self.grp.launch() task_self().run() self.assertEqual(act1.status(), ACT_OK) self.assertEqual(act2.status(), ACT_OK) self.assertEqual(self.grp.status(), ACT_OK) def test_one_action_error(self): """A group with only an error action is on error""" act = TestAction('/bin/false') self.grp.add(act) self.grp.launch() task_self().run() self.assertEqual(act.status(), ACT_ERROR) self.assertEqual(self.grp.status(), ACT_ERROR) def test_two_actions_ok_and_error(self): """A group with a ok action and an error action is on error""" act1 = TestAction('/bin/true') act2 = TestAction('/bin/false') self.grp.add(act1) self.grp.add(act2) self.grp.launch() task_self().run() self.assertEqual(act1.status(), ACT_OK) self.assertEqual(act2.status(), ACT_ERROR) self.assertEqual(self.grp.status(), ACT_ERROR) def test_two_actions_2_errors(self): """A group with 2 error actions is on error""" act1 = TestAction('/bin/false') act2 = TestAction('/bin/false') self.grp.add(act1) self.grp.add(act2) self.grp.launch() task_self().run() self.assertEqual(act1.status(), ACT_ERROR) self.assertEqual(act2.status(), ACT_ERROR) self.assertEqual(self.grp.status(), ACT_ERROR) def test_one_dep_ok(self): """A group with one ok dependency is ok""" act = TestAction('/bin/true') act1 = TestAction('/bin/true') self.grp.add(act1) self.grp.depends_on(act) self.grp.launch() task_self().run() self.assertEqual(act.status(), ACT_OK) self.assertEqual(act1.status(), ACT_OK) self.assertEqual(self.grp.status(), ACT_OK) def test_one_dep_error(self): """A group with an error dependency is on error, content is not run""" act = TestAction('/bin/false') act1 = TestAction('/bin/true') self.grp.add(act1) self.grp.depends_on(act) self.grp.launch() task_self().run() self.assertEqual(act.status(), ACT_ERROR) self.assertEqual(act1.status(), ACT_WAITING) self.assertEqual(self.grp.status(), ACT_ERROR) def test_two_deps_ok_and_ok(self): """A group with two ok dependencies is ok""" dep1 = TestAction('/bin/true') dep2 = TestAction('/bin/true') act1 = TestAction('/bin/true') self.grp.add(act1) self.grp.depends_on(dep1) self.grp.depends_on(dep2) self.grp.launch() task_self().run() self.assertEqual(dep1.status(), ACT_OK) self.assertEqual(dep2.status(), ACT_OK) self.assertEqual(act1.status(), ACT_OK) self.assertEqual(self.grp.status(), ACT_OK) def test_two_deps_ok_and_error(self): """A group with one error dep and one ok dep is on error""" dep1 = TestAction('/bin/true') dep2 = TestAction('/bin/false') act1 = TestAction('/bin/true') self.grp.add(act1) self.grp.depends_on(dep1) self.grp.depends_on(dep2) self.grp.launch() task_self().run() self.assertEqual(dep1.status(), ACT_OK) self.assertEqual(dep2.status(), ACT_ERROR) self.assertEqual(act1.status(), ACT_WAITING) self.assertEqual(self.grp.status(), ACT_ERROR) def test_two_deps_error_and_error(self): """A group with 2 error dependencies is on error""" dep1 = TestAction('/bin/false') dep2 = TestAction('/bin/false') act1 = TestAction('/bin/true') self.grp.add(act1) self.grp.depends_on(dep1) self.grp.depends_on(dep2) self.grp.launch() task_self().run() self.assertEqual(dep1.status(), ACT_ERROR) self.assertEqual(dep2.status(), ACT_ERROR) self.assertEqual(act1.status(), ACT_WAITING) self.assertEqual(self.grp.status(), ACT_ERROR) def test_one_dep_after_ok(self): """An action with an ok group dependency is ok""" act = TestAction('/bin/true') self.grp = ActionGroup() act1 = TestAction('/bin/true') self.grp.add(act1) act.depends_on(self.grp) act.launch() task_self().run() self.assertEqual(act1.status(), ACT_OK) self.assertEqual(self.grp.status(), ACT_OK) self.assertEqual(act.status(), ACT_OK) def test_one_dep_after_error(self): """An action with an error group dependency is on error""" act = TestAction('/bin/true') self.grp = ActionGroup() act1 = TestAction('/bin/false') self.grp.add(act1) act.depends_on(self.grp) act.launch() task_self().run() self.assertEqual(act1.status(), ACT_ERROR) self.assertEqual(self.grp.status(), ACT_ERROR) self.assertEqual(act.status(), ACT_ERROR) def test_two_deps_after_ok(self): """2 ok actions with an ok group dependency are ok""" dep1 = TestAction('/bin/true') dep2 = TestAction('/bin/true') self.grp = ActionGroup() act1 = TestAction('/bin/true') self.grp.add(act1) dep1.depends_on(self.grp) dep2.depends_on(self.grp) dep1.launch() task_self().run() self.assertEqual(act1.status(), ACT_OK) self.assertEqual(self.grp.status(), ACT_OK) self.assertEqual(dep1.status(), ACT_OK) self.assertEqual(dep2.status(), ACT_OK) def test_two_deps_after_error(self): """2 ok actions with an error group dependency are on error""" dep1 = TestAction('/bin/true') dep2 = TestAction('/bin/true') self.grp = ActionGroup() act1 = TestAction('/bin/false') self.grp.add(act1) dep1.depends_on(self.grp) dep2.depends_on(self.grp) dep1.launch() task_self().run() self.assertEqual(act1.status(), ACT_ERROR) self.assertEqual(self.grp.status(), ACT_ERROR) self.assertEqual(dep1.status(), ACT_ERROR) self.assertEqual(dep2.status(), ACT_ERROR) def test_group_dep_ok(self): """A ok group which depends on an ok group is ok""" grp1 = ActionGroup() act1 = TestAction('/bin/true') grp1.add(act1) grp2 = ActionGroup() act2 = TestAction('/bin/true') grp2.add(act2) grp1.depends_on(grp2) grp1.launch() task_self().run() self.assertEqual(act1.status(), ACT_OK) self.assertEqual(grp1.status(), ACT_OK) self.assertEqual(act2.status(), ACT_OK) self.assertEqual(grp2.status(), ACT_OK) def test_group_dep_error(self): """A ok group which depends on an error group is on error""" grp1 = ActionGroup() act1 = TestAction('/bin/false') grp1.add(act1) grp2 = ActionGroup() act2 = TestAction('/bin/true') grp2.add(act2) grp2.depends_on(grp1) grp2.launch() task_self().run() self.assertEqual(act1.status(), ACT_ERROR) self.assertEqual(grp1.status(), ACT_ERROR) self.assertEqual(act2.status(), ACT_WAITING) self.assertEqual(grp2.status(), ACT_ERROR)