Skip to content

Commit

Permalink
test: mocking a bit more (ResourceStatus)
Browse files Browse the repository at this point in the history
  • Loading branch information
fstagni committed Aug 28, 2024
1 parent fb74146 commit 52e69e4
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 10 deletions.
18 changes: 17 additions & 1 deletion src/DIRAC/DataManagementSystem/Client/test/Test_FTS3Objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import errno
from unittest import mock

import pytest
import tempfile
import errno
Expand Down Expand Up @@ -181,6 +183,20 @@ def monkeypatchForAllTest(monkeypatch):
lambda _self, _seName, _vo: S_OK(),
)

def mock_init(self, useProxy=False, vo=None):
self.proxy = False
self.proxy = useProxy
self.resourceStatus = mock.MagicMock()
self.vo = vo
self.remoteProtocolSections = []
self.localProtocolSections = []
self.name = ""
self.options = {}
self.protocols = {}
self.storages = {}

monkeypatch.setattr(DIRAC.Resources.Storage.StorageFactory.StorageFactory, "__init__", mock_init)


def generateFTS3Job(sourceSE, targetSE, lfns, multiHopSE=None):
"""Utility to create a new FTS3Job object with some FTS3Files
Expand Down
308 changes: 308 additions & 0 deletions src/DIRAC/DataManagementSystem/Client/test/Test_SE.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
import itertools
import os
import tempfile
import pytest
from unittest import mock

from diraccfg import CFG

from DIRAC import S_OK
from DIRAC.ConfigurationSystem.Client.ConfigurationData import gConfigurationData
from DIRAC.ConfigurationSystem.private.ConfigurationClient import ConfigurationClient
from DIRAC.Resources.Storage.StorageBase import StorageBase
from DIRAC.Resources.Storage.StorageElement import StorageElementItem


class fake_SRM2Plugin(StorageBase):
def putFile(self, lfns, sourceSize=0):
return S_OK({"Successful": dict.fromkeys(lfns, "srm:putFile"), "Failed": {}})

def getTransportURL(self, path, protocols=False):
return S_OK({"Successful": dict.fromkeys(path, "srm:getTransportURL"), "Failed": {}})


class fake_XROOTPlugin(StorageBase):
def putFile(self, lfns, sourceSize=0):
return S_OK({"Successful": dict.fromkeys(lfns, "root:putFile"), "Failed": {}})

def getTransportURL(self, path, protocols=False):
return S_OK({"Successful": dict.fromkeys(path, "root:getTransportURL"), "Failed": {}})


class fake_GSIFTPPlugin(StorageBase):
def putFile(self, lfns, sourceSize=0):
return S_OK({"Successful": dict.fromkeys(lfns, "gsiftp:putFile"), "Failed": {}})

def getTransportURL(self, path, protocols=False):
return S_OK({"Successful": dict.fromkeys(path, "gsiftp:getTransportURL"), "Failed": {}})


def mock_StorageFactory_generateStorageObject(
self, storageName, pluginName, parameters, hideExceptions=False
): # pylint: disable=unused-argument
"""Generate fake storage object"""
storageObj = StorageBase(storageName, parameters)
storageObj.pluginName = pluginName

return S_OK(storageObj)

@pytest.fixture(scope="function")
def setup_environment(monkeypatch):
def mock_init(self, useProxy=False, vo=None):
self.proxy = useProxy
self.resourceStatus = mock.MagicMock()
self.vo = vo
self.remoteProtocolSections = []
self.localProtocolSections = []
self.name = ""
self.options = {}
self.protocols = {}
self.storages = {}

monkeypatch.setattr(
"DIRAC.Resources.Storage.StorageFactory.StorageFactory.__init__", mock_init
)

monkeypatch.setattr(
"DIRAC.Resources.Storage.StorageFactory.StorageFactory._StorageFactory__generateStorageObject",
mock_StorageFactory_generateStorageObject
)

monkeypatch.setattr(
"DIRAC.Resources.Storage.StorageElement.StorageElementItem._StorageElementItem__isLocalSE",
lambda self: S_OK(True)
)

monkeypatch.setattr(
"DIRAC.Resources.Storage.StorageElement.StorageElementItem.addAccountingOperation",
lambda self: None
)

# Create test configuration file
testCfgFileName = os.path.join(tempfile.gettempdir(), "test_StorageElement.cfg")
cfgContent = """
DIRAC
{
Setup=TestSetup
}
Resources{
StorageElements{
StorageA
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
AccessProtocol.0
{
Host =
PluginName = File
Protocol = file
Path =
}
}
StorageB
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
AccessProtocol.0
{
Host =
PluginName = SRM2
Protocol = srm
Path =
}
}
StorageC
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
AccessProtocol.0
{
Host =
PluginName = XROOT
Protocol = root
Path =
}
}
StorageD
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
AccessProtocol.0
{
Host =
PluginName = SRM2
Protocol = srm
Path =
}
AccessProtocol.1
{
Host =
PluginName = XROOT
Protocol = root
Path =
}
}
StorageE
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
WriteProtocols = root
WriteProtocols += srm
AccessProtocol.0
{
Host =
PluginName = SRM2
Protocol = srm
Path =
}
AccessProtocol.1
{
Host =
PluginName = XROOT
Protocol = root
Path =
}
}
StorageX
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
WriteProtocols = gsiftp
AccessProtocols = root
AccessProtocol.0
{
Host =
PluginName = GSIFTP
Protocol = gsiftp
Path =
}
AccessProtocol.1
{
Host =
PluginName = XROOT
Protocol = root
Path =
}
}
StorageY
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
AccessProtocols = gsiftp
AccessProtocols += srm
AccessProtocol.0
{
Host =
PluginName = GSIFTP
Protocol = gsiftp
Path =
}
AccessProtocol.1
{
Host =
PluginName = SRM2
Protocol = srm
Path =
}
}
StorageZ
{
BackendType = local
ReadAccess = Active
WriteAccess = Active
AccessProtocols = root
AccessProtocols += srm
WriteProtocols = root
WriteProtocols += srm
AccessProtocol.0
{
Host =
PluginName = ROOT
Protocol = root
Path =
}
AccessProtocol.1
{
Host =
PluginName = SRM2
Protocol = srm
Path =
}
}
}
}
Operations{
Defaults
{
DataManagement{
AccessProtocols = fakeProto
AccessProtocols += root
WriteProtocols = srm
}
}
}
"""

with open(testCfgFileName, "w") as f:
f.write(cfgContent)

gConfigurationData.localCFG = CFG()
gConfigurationData.remoteCFG = CFG()
gConfigurationData.mergedCFG = CFG()
gConfigurationData.generateNewVersion()

gConfig = ConfigurationClient(
fileToLoadList=[testCfgFileName]
)

seA = StorageElementItem("StorageA")
seA.vo = "lhcb"
seB = StorageElementItem("StorageB")
seB.vo = "lhcb"
seC = StorageElementItem("StorageC")
seC.vo = "lhcb"
seD = StorageElementItem("StorageD")
seD.vo = "lhcb"
seE = StorageElementItem("StorageE")
seE.vo = "lhcb"
seX = StorageElementItem("StorageX")
seX.vo = "lhcb"
seY = StorageElementItem("StorageY")
seY.vo = "lhcb"
seZ = StorageElementItem("StorageZ")
seZ.vo = "lhcb"

yield seA, seB, seC, seD, seE, seX, seY, seZ

os.remove(testCfgFileName)
gConfigurationData.localCFG = CFG()
gConfigurationData.remoteCFG = CFG()
gConfigurationData.mergedCFG = CFG()
gConfigurationData.generateNewVersion()


@pytest.mark.usefixtures("setup_environment")
def test_negociateProtocolWithOtherSE(setup_environment):
seA, seB, seC, seD, seE, seX, seY, seZ = setup_environment

res = seA.negociateProtocolWithOtherSE(seB)
assert res["OK"]
assert res["Value"] == ["file"]

res = seB.negociateProtocolWithOtherSE(seA)
assert res["OK"]
assert res["Value"] == ["file"]

res = seA.negociateProtocolWithOtherSE(seC)
assert res["OK"]
assert res["Value"] == []

res = seC.negociateProtocolWith
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import os
import tempfile
import pytest

import mock
import pytest
from diraccfg import CFG

import DIRAC

from DIRAC.ConfigurationSystem.private.ConfigurationClient import ConfigurationClient
from DIRAC import S_OK
from DIRAC.ConfigurationSystem.Client.ConfigurationData import gConfigurationData

from DIRAC.Resources.Storage.StorageBase import StorageBase
from DIRAC.ConfigurationSystem.private.ConfigurationClient import ConfigurationClient
from DIRAC.DataManagementSystem.private.FTS3Plugins.DefaultFTS3Plugin import DefaultFTS3Plugin
from DIRAC import S_OK
from DIRAC.Resources.Storage.StorageBase import StorageBase

# pylint: disable=redefined-outer-name

Expand Down Expand Up @@ -318,6 +317,21 @@ def fts3Plugin(monkeypatch):
monkeypatch.setattr(
DIRAC.Resources.Storage.StorageElement.StorageElementItem, "addAccountingOperation", lambda: None
)

def mock_init(self, useProxy=False, vo=None):
self.proxy = False
self.proxy = useProxy
self.resourceStatus = mock.MagicMock()
self.vo = vo
self.remoteProtocolSections = []
self.localProtocolSections = []
self.name = ""
self.options = {}
self.protocols = {}
self.storages = {}

monkeypatch.setattr(DIRAC.Resources.Storage.StorageFactory.StorageFactory, "__init__", mock_init)

fts3Plugin = DefaultFTS3Plugin()

return fts3Plugin
Expand All @@ -329,8 +343,10 @@ def test_multiHop_specificLink(fts3Plugin):
assert hopName == "CERN-RAW-CNAF-DST"


def test_multiHop_specificSrc_baseSEDst(fts3Plugin):
def test_multiHop_specificSrc_baseSEDst(mocker, fts3Plugin):
"""When we have a specific source and a baseSE dest"""
# mocker.patch("DIRAC.DataManagementSystem.private.FTS3Plugins.DefaultFTS3Plugin.StorageElement", return_value=mock.MagicMock())

hopName = fts3Plugin.findMultiHopSEToCoverUpForWLCGFailure("CERN-RAW", "CNAF_MC-DST")
assert hopName == "CERN-RAW-CNAF-Disk"

Expand Down
Loading

0 comments on commit 52e69e4

Please sign in to comment.