Exemple #1
0
    def __init__(self, commandCallerIn=None):

        if commandCallerIn is not None:
            self.cc = commandCallerIn
        else:
            from DIRAC.ResourceStatusSystem.Command.CommandCaller import CommandCaller
            self.cc = CommandCaller()

        self.policyInvoker = PolicyInvoker()
Exemple #2
0
    def __init__(self, commandCallerIn=None):

        if commandCallerIn is not None:
            self.cc = commandCallerIn
        else:
            from DIRAC.ResourceStatusSystem.Command.CommandCaller import CommandCaller

            self.cc = CommandCaller()

        self.policyInvoker = PolicyInvoker()
Exemple #3
0
    def setUp(self):
        sys.modules["DIRAC"] = DIRAC.ResourceStatusSystem.test.fake_Logger
        sys.modules[
            "DIRAC.ResourceStatusSystem.Utilities.CS"] = DIRAC.ResourceStatusSystem.test.fake_Logger
        sys.modules[
            "DIRAC.Core.Utilities.SiteCEMapping"] = DIRAC.ResourceStatusSystem.test.fake_Logger
        sys.modules[
            "DIRAC.Core.Utilities.SiteSEMapping"] = DIRAC.ResourceStatusSystem.test.fake_Logger
        sys.modules[
            "DIRAC.Core.Utilities.SitesDIRACGOCDBmapping"] = DIRAC.ResourceStatusSystem.test.fake_Logger
        sys.modules[
            "DIRAC.Interfaces.API.DiracAdmin"] = DIRAC.ResourceStatusSystem.test.fake_Admin
        sys.modules[
            "DIRAC.FrameworkSystem.Client.NotificationClient"] = DIRAC.ResourceStatusSystem.test.fake_NotificationClient

        from DIRAC.ResourceStatusSystem.Utilities.InfoGetter import InfoGetter
        from DIRAC.ResourceStatusSystem.PolicySystem.PolicyBase import PolicyBase
        from DIRAC.ResourceStatusSystem.PolicySystem.PolicyInvoker import PolicyInvoker

        from DIRAC import gConfig
        self.VO = gConfig.getValue("DIRAC/Extensions")
        if 'LHCb' in self.VO:
            self.VO = 'LHCb'

        self.mock_command = Mock()
        self.mock_policy = Mock()
        self.mock_p = Mock()
        self.mock_args = Mock()
        self.pb = PolicyBase()
        self.pi = PolicyInvoker()
        self.mock_pdp = Mock()
        self.mock_rsDB = Mock()
        self.mock_rmDB = Mock()
        self.mock_nc = Mock()
        self.mock_da = Mock()
        self.mock_da.getBannedSites.return_value = {
            'OK': True,
            'Value': ['LCG.APC.fr', 'LCG.Bari.it', 'LCG.Catania.it']
        }
        self.mock_da.addSiteInMask.return_value = {'OK': True, 'Value': ''}
        self.mock_da.banSiteFromMask.return_value = {'OK': True, 'Value': ''}
        self.mock_da.sendMail.return_value = {'OK': True, 'Value': ''}
        self.mock_csAPI = Mock()
        self.mock_csAPI.setOption.return_value = {'OK': True, 'Value': ''}
        self.mock_csAPI.commit.return_value = {'OK': True, 'Value': ''}
        self.ig = InfoGetter(self.VO)
Exemple #4
0
class PolicyCaller:

    #############################################################################

    def __init__(self, commandCallerIn=None):

        if commandCallerIn is not None:
            self.cc = commandCallerIn
        else:
            from DIRAC.ResourceStatusSystem.Command.CommandCaller import CommandCaller

            self.cc = CommandCaller()

        self.policyInvoker = PolicyInvoker()

    #############################################################################

    def policyInvocation(
        self,
        VOExtension,
        granularity=None,
        name=None,
        status=None,
        policy=None,
        args=None,
        pName=None,
        pModule=None,
        extraArgs=None,
        commandIn=None,
    ):
        """
    Invokes a policy:

    1. If :attr:`policy` is not None, import the right policy module,
    specified with :attr:`VOExtension` (e.g.: 'LHCb') and
    :attr:`pModule` (e.g. 'DT_Policy').

      1.1. Create a policy object.

    2. Set the policy arguments (usually :attr:`granularity`,
    :attr:`name`) + :attr:`extraArgs`.

    3. If commandIn is specified (normally it is), use
    :meth:`DIRAC.ResourceStatusSystem.Command.CommandCaller.CommandCaller.setCommandObject`
    to get a command object
    """

        p = policy
        a = args

        moduleBase = VOExtension + "DIRAC.ResourceStatusSystem.Policy."

        if p is None:
            try:
                module = moduleBase + pModule
                policyModule = __import__(module, globals(), locals(), ["*"])
            except ImportError:
                pModule = "AlwaysFalse_Policy"
                module = moduleBase + pModule
                policyModule = __import__(module, globals(), locals(), ["*"])
            p = getattr(policyModule, pModule)()

        if a is None:
            a = (granularity, name)

        if extraArgs is not None:
            if isinstance(extraArgs, tuple):
                a = a + extraArgs
            elif isinstance(extraArgs, list):
                argsList = []
                for argsTuple in extraArgs:
                    argsList.append(a + argsTuple)
                a = argsList

        if commandIn is not None:
            commandIn = self.cc.setCommandObject(commandIn)

        res = self._innerEval(p, a, commandIn=commandIn)

        res["PolicyName"] = pName

        return res

    #############################################################################

    def _innerEval(self, p, a, commandIn=None, knownInfo=None):
        """ policy evaluation
    """

        self.policyInvoker.setPolicy(p)

        p.setArgs(a)
        p.setCommand(commandIn)
        #    p.setInfoName('Result')

        res = self.policyInvoker.evaluatePolicy()
        return res
Exemple #5
0
class PolicyCaller:

    #############################################################################

    def __init__(self, commandCallerIn=None):

        if commandCallerIn is not None:
            self.cc = commandCallerIn
        else:
            from DIRAC.ResourceStatusSystem.Command.CommandCaller import CommandCaller
            self.cc = CommandCaller()

        self.policyInvoker = PolicyInvoker()

#############################################################################

    def policyInvocation(self,
                         VOExtension,
                         granularity=None,
                         name=None,
                         status=None,
                         policy=None,
                         args=None,
                         pName=None,
                         pModule=None,
                         extraArgs=None,
                         commandIn=None):
        """
    Invokes a policy:

    1. If :attr:`policy` is not None, import the right policy module,
    specified with :attr:`VOExtension` (e.g.: 'LHCb') and
    :attr:`pModule` (e.g. 'DT_Policy').

      1.1. Create a policy object.

    2. Set the policy arguments (usually :attr:`granularity`,
    :attr:`name`) + :attr:`extraArgs`.

    3. If commandIn is specified (normally it is), use
    :meth:`DIRAC.ResourceStatusSystem.Command.CommandCaller.CommandCaller.setCommandObject`
    to get a command object
    """

        p = policy
        a = args

        moduleBase = VOExtension + "DIRAC.ResourceStatusSystem.Policy."

        if p is None:
            try:
                module = moduleBase + pModule
                policyModule = __import__(module, globals(), locals(), ['*'])
            except ImportError:
                pModule = "AlwaysFalse_Policy"
                module = moduleBase + pModule
                policyModule = __import__(module, globals(), locals(), ['*'])
            p = getattr(policyModule, pModule)()

        if a is None:
            a = (granularity, name)

        if extraArgs is not None:
            if isinstance(extraArgs, tuple):
                a = a + extraArgs
            elif isinstance(extraArgs, list):
                argsList = []
                for argsTuple in extraArgs:
                    argsList.append(a + argsTuple)
                a = argsList

        if commandIn is not None:
            commandIn = self.cc.setCommandObject(commandIn)

        res = self._innerEval(p, a, commandIn=commandIn)

        res['PolicyName'] = pName

        return res

#############################################################################

    def _innerEval(self, p, a, commandIn=None, knownInfo=None):
        """ policy evaluation
    """

        self.policyInvoker.setPolicy(p)

        p.setArgs(a)
        p.setCommand(commandIn)
        #    p.setInfoName('Result')

        res = self.policyInvoker.evaluatePolicy()
        return res