|
| 1 | +"""This module contains tests for the (DIP) StorageElement |
| 2 | +
|
| 3 | +The tests upload gets a directory |
| 4 | +
|
| 5 | +Requirements: |
| 6 | +
|
| 7 | +* Running StorageElement instance, containing an LFN folder '/Jenkins', writable by the jenkins_user proxy |
| 8 | +* A jenkins_user proxy |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import print_function |
| 13 | +from __future__ import absolute_import |
| 14 | +from __future__ import division |
| 15 | + |
| 16 | +import os |
| 17 | +import sys |
| 18 | +import tempfile |
| 19 | +import time |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from pprint import pprint, pformat |
| 24 | + |
| 25 | +from DIRAC.Core.Base.Script import parseCommandLine |
| 26 | + |
| 27 | +parseCommandLine() |
| 28 | + |
| 29 | +from DIRAC.Core.Security.ProxyInfo import getProxyInfo # noqa: module-import-not-at-top-of-file |
| 30 | +from DIRAC.ConfigurationSystem.Client.Helpers.Registry import getVOForGroup # noqa: module-import-not-at-top-of-file |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module") |
| 34 | +def se(): |
| 35 | + from DIRAC.Resources.Storage.StorageElement import StorageElementItem as StorageElement |
| 36 | + |
| 37 | + return StorageElement("SE-1") |
| 38 | + |
| 39 | + |
| 40 | +try: |
| 41 | + res = getProxyInfo() |
| 42 | + if not res["OK"]: |
| 43 | + raise Exception(res["Message"]) |
| 44 | + |
| 45 | + proxyInfo = res["Value"] |
| 46 | + username = proxyInfo["username"] |
| 47 | + vo = getVOForGroup(proxyInfo["group"]) |
| 48 | + |
| 49 | + if not vo: |
| 50 | + raise ValueError("Proxy has no VO") |
| 51 | + |
| 52 | + LFN_PATH = "/%s/test/unit-test/StorageElement/" % vo |
| 53 | + |
| 54 | +except Exception as e: # pylint: disable=broad-except |
| 55 | + print(repr(e)) |
| 56 | + sys.exit(2) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def tempFile(): |
| 61 | + """Create unique temprary file, will be cleaned up at the end.""" |
| 62 | + with tempfile.NamedTemporaryFile("w") as theTemp: |
| 63 | + theTemp.write(str(time.time())) |
| 64 | + theTemp.flush() |
| 65 | + yield theTemp.name |
| 66 | + |
| 67 | + |
| 68 | +def assertResult(res, lfn, sub="Successful"): |
| 69 | + """Check if lfn is in Successful or Failed, given by sub.""" |
| 70 | + assert res["OK"], res.get("Message", "All OK") |
| 71 | + assert sub in res["Value"] |
| 72 | + assert res["Value"][sub], "Did not find %r in result" % sub |
| 73 | + assert lfn in res["Value"][sub] |
| 74 | + assert "Files" in res["Value"][sub][lfn] |
| 75 | + assert "Size" in res["Value"][sub][lfn] |
| 76 | + |
| 77 | + |
| 78 | +def test_getDirectory(se, tempFile, tmp_path): |
| 79 | + print("\n\n#########################################################################\n\n\t\t\tSE.GetDirectory\n") |
| 80 | + targetDir = os.path.join(LFN_PATH, "myGetDir") |
| 81 | + targetLFN = os.path.join(targetDir, os.path.basename(tempFile)) |
| 82 | + putFileRes = se.putFile({targetLFN: tempFile}) |
| 83 | + pprint(putFileRes) |
| 84 | + localDir = os.path.join(tmp_path, "myGetDir") |
| 85 | + print("is not a dir?", localDir, os.path.isdir(localDir)) |
| 86 | + assert not os.path.isdir(localDir) |
| 87 | + getRes = se.getDirectory(targetDir, localPath=localDir) |
| 88 | + assertResult(getRes, lfn=targetDir) |
| 89 | + assert os.path.isdir(localDir) |
| 90 | + |
| 91 | + |
| 92 | +def test_putDirectory(se, tempFile, tmp_path): |
| 93 | + print("\n\n#########################################################################\n\n\t\t\tSE.PutDirectory\n") |
| 94 | + targetDir = os.path.join(LFN_PATH, "myPutDir") |
| 95 | + localDir = os.path.join(tmp_path, "myPutDir") |
| 96 | + os.mkdir(localDir) |
| 97 | + with open(os.path.join(localDir, os.path.basename(tempFile)), "w") as newFile: |
| 98 | + newFile.write("foo bar" + (str(time.time()))) |
| 99 | + getRes = se.putDirectory({targetDir: localDir}) |
| 100 | + # Check that the get was successful |
| 101 | + pprint(getRes) |
| 102 | + assertResult(getRes, lfn=targetDir) |
| 103 | + assert os.path.isdir(localDir) |
| 104 | + assert se.listDirectory(targetDir) |
| 105 | + |
| 106 | + |
| 107 | +def test_getOccupancy(se): |
| 108 | + print("\n\n#########################################################################\n\n\t\t\tSE.PutDirectory\n") |
| 109 | + res = se.getOccupancy() |
| 110 | + assert res["OK"], pformat(res) |
0 commit comments