Example #1
0
    def _create_simple_pred(self, cname=None, met=None):
        if cname is None:
            cname = self.comp_name
        s = SimplePredicate(cname, parent='foo')
        if met is not None:
            s.set_met(met)

        return s
Example #2
0
    def setUp(self):
        self.mox = mox.Mox()
        self.comp_name = "Test Predicate Or"

        self.predat = SimplePredicate("a", ThreadSafeObject({}))
        self.predat.set_met(True)
        self.predbt = SimplePredicate("b", ThreadSafeObject({}))
        self.predbt.set_met(True)

        self.predaf = SimplePredicate("a", ThreadSafeObject({}))
        self.predbf = SimplePredicate("b", ThreadSafeObject({}))

        self.list = [self.predaf, self.predbf, self.predat, self.predbt]

        self.factory = PredicateFactory(component_name="factory",
                                        zkclient=None,
                                        proc_client=None,
                                        system=None,
                                        pred_list=self.list,
                                        settings={})
Example #3
0
    def create(self, xmlpart, callback=None, parent=None):
        """
        :type xmlpart: xml.etree.ElementTree.Element
        :type callback: types.FunctionType or None
        :type parent: str or None
        """
        if xmlpart is None:
            # A dummy predicate will be returned if there are no predicates
            # met is true b/c we don't want to block on no predicates
            return create_dummy(comp=self._component_name,
                                parent=self._action,
                                met=True)

        if isinstance(xmlpart, str):
            root = ElementTree.fromstring(xmlpart)
        else:
            root = xmlpart

        if parent is None:
            parent = self._action

        ptype = verify_attribute(root, 'type').lower()
        operational = bool(
            verify_attribute(root, 'operational', none_allowed=True))

        if ptype == 'simple':
            return self._ensure_new(SimplePredicate(self._component_name,
                                                    operational=operational,
                                                    parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.ZOOKEEPERNODEEXISTS:
            return self._ensure_new(ZookeeperNodeExists(
                self._component_name,
                self.zkclient,
                verify_attribute(root, 'path'),
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.ZOOKEEPERHASCHILDREN:
            return self._ensure_new(ZookeeperHasChildren(
                self._component_name,
                self.zkclient,
                verify_attribute(root, 'path'),
                ephemeral_only=verify_attribute(root,
                                                'ephemeral_only',
                                                none_allowed=True,
                                                default=True),
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.ZOOKEEPERHASGRANDCHILDREN:
            return self._ensure_new(ZookeeperHasGrandChildren(
                self._component_name,
                self.zkclient,
                verify_attribute(root, 'path'),
                ephemeral_only=verify_attribute(root,
                                                'ephemeral_only',
                                                none_allowed=True,
                                                default=True),
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.ZOOKEEPERGLOB:
            return self._ensure_new(ZookeeperGlob(
                self._component_name,
                self.zkclient,
                verify_attribute(root, 'path'),
                ephemeral_only=verify_attribute(root,
                                                'ephemeral_only',
                                                none_allowed=True,
                                                default=True),
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.ZOOKEEPERGOODUNTILTIME:
            return self._ensure_new(ZookeeperGoodUntilTime(
                self._component_name,
                self.zkclient,
                verify_attribute(root, 'path'),
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.PROCESS:
            return self._ensure_new(PredicateProcess(self._component_name,
                                                     self._proc_client,
                                                     verify_attribute(
                                                         root,
                                                         'interval',
                                                         cast=float),
                                                     operational=operational,
                                                     parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.API:
            return self._ensure_new(APIPredicate(
                self._component_name,
                verify_attribute(root, 'url'),
                verb=verify_attribute(root,
                                      'verb',
                                      none_allowed=True,
                                      default='GET'),
                expected_code=verify_attribute(root,
                                               'expected_code',
                                               none_allowed=True,
                                               cast=int,
                                               default=200),
                interval=verify_attribute(root, 'interval', cast=float),
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.HEALTH:
            return self._ensure_new(PredicateHealth(
                self._component_name,
                verify_attribute(root, 'command'),
                verify_attribute(root, 'interval', cast=float),
                self._system,
                operational=operational,
                parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.HOLIDAY:
            return self._ensure_new(PredicateHoliday(self._component_name,
                                                     self.zkclient,
                                                     path=self._holiday_path,
                                                     operational=operational,
                                                     parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.WEEKEND:
            return self._ensure_new(PredicateWeekend(self._component_name,
                                                     operational=operational,
                                                     parent=parent),
                                    callback=callback)
        elif ptype == PredicateType.TIMEWINDOW:
            return self._ensure_new(TimeWindow(
                self._component_name,
                begin=verify_attribute(root, 'begin', none_allowed=True),
                end=verify_attribute(root, 'end', none_allowed=True),
                weekdays=verify_attribute(root, 'weekdays', none_allowed=True),
                operational=operational,
                parent=parent),
                                    callback=callback)

        # below, use recursion to get nested predicates
        elif ptype == PredicateType.NOT:
            for element in root.findall('Predicate'):
                dep = self.create(element, callback=callback)
                return self._ensure_new(
                    PredicateNot(self._component_name,
                                 dep,
                                 parent=self._parent_name(parent, 'not')))
        elif ptype == PredicateType.AND:
            deps = list()
            for element in root.findall('Predicate'):
                deps.append(
                    self.create(element,
                                callback=callback,
                                parent=self._parent_name(parent, 'and')))
            return self._ensure_new(
                PredicateAnd(self._component_name, deps, parent=parent))
        elif ptype == PredicateType.OR:
            deps = list()
            for element in root.findall('Predicate'):
                deps.append(
                    self.create(element,
                                callback=callback,
                                parent=self._parent_name(parent, 'or')))
            return self._ensure_new(
                PredicateOr(self._component_name, deps, parent=parent))
        else:
            self._log.error(
                'Unknown predicate type "{0}". Ignoring'.format(ptype))
            # create dummy if it is an unknown predicate type.
            # met is set to true b/c we don't want to block on error
            return create_dummy(comp=self._component_name,
                                parent=self._action,
                                met=True)
Example #4
0
 def test_no_match(self):
     new = SimplePredicate("c", ThreadSafeObject({}))
     ret = self.factory._ensure_new(new)
     self.assertTrue(new is ret)