|
| 1 | +""".""" |
| 2 | + |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | + |
| 6 | +import pytest |
| 7 | +from DIRACCommon.Core.Utilities.ReturnValues import S_OK |
| 8 | +from pytest_mock import MockerFixture |
| 9 | + |
| 10 | +from dirac_cwl_proto.commands import UploadLogFile |
| 11 | + |
| 12 | + |
| 13 | +class TestUploadLogFile: |
| 14 | + """Collection of tests for the UploadLogFile command.""" |
| 15 | + |
| 16 | + FILENAMES = ["file.txt", "file.log", "file.err", "file.out", "file.extra"] |
| 17 | + JOB_ID = "8042" |
| 18 | + PRODUCTION_ID = "95376" |
| 19 | + NAMESPACE = "MC" |
| 20 | + CONFIG_VERSION = "2016" |
| 21 | + |
| 22 | + @pytest.fixture |
| 23 | + def basedir(self): |
| 24 | + """Fixture to initialize the working directory.""" |
| 25 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 26 | + for file in self.FILENAMES: |
| 27 | + with open(os.path.join(tmpdir, file), "x") as f: |
| 28 | + f.write("EMPTY") |
| 29 | + |
| 30 | + yield tmpdir |
| 31 | + |
| 32 | + def test_upload_ok(self, basedir, mocker: MockerFixture): |
| 33 | + """Test a correct upload.""" |
| 34 | + base_lfn = f"/lhcb/{self.NAMESPACE}/{self.CONFIG_VERSION}/LOG/{self.PRODUCTION_ID.zfill(8)}/0000/" |
| 35 | + zip_name = self.JOB_ID.zfill(8) + ".zip" |
| 36 | + |
| 37 | + expected_lfn = os.path.join(base_lfn, zip_name) |
| 38 | + expected_zip = os.path.join(basedir, zip_name) |
| 39 | + |
| 40 | + mock_put = mocker.patch("dirac_cwl_proto.data_management_mocks.data_manager.MockDataManager.put") |
| 41 | + mock_putAndRegister = mocker.patch( |
| 42 | + "dirac_cwl_proto.data_management_mocks.data_manager.MockDataManager.putAndRegister", |
| 43 | + ) |
| 44 | + |
| 45 | + mock_put.return_value = S_OK({"Successful": {expected_lfn: "OKAY"}, "Failed": {}}) |
| 46 | + |
| 47 | + result = UploadLogFile().execute( |
| 48 | + basedir, |
| 49 | + job_id=self.JOB_ID, |
| 50 | + production_id=self.PRODUCTION_ID, |
| 51 | + namespace=self.NAMESPACE, |
| 52 | + config_version=self.CONFIG_VERSION, |
| 53 | + ) |
| 54 | + |
| 55 | + mock_put.assert_called_once_with(expected_lfn, expected_zip, "LogSE") |
| 56 | + mock_putAndRegister.assert_not_called() |
| 57 | + assert result["OK"] |
| 58 | + |
| 59 | + def test_upload_ok_to_failover(self, basedir, mocker: MockerFixture): |
| 60 | + """Test a failure to upload to the LogSE but a correct one to the Failover.""" |
| 61 | + base_lfn = f"/lhcb/{self.NAMESPACE}/{self.CONFIG_VERSION}/LOG/{self.PRODUCTION_ID.zfill(8)}/0000/" |
| 62 | + zip_name = self.JOB_ID.zfill(8) + ".zip" |
| 63 | + |
| 64 | + expected_lfn = os.path.join(base_lfn, zip_name) |
| 65 | + expected_zip = os.path.join(basedir, zip_name) |
| 66 | + |
| 67 | + mock_put = mocker.patch("dirac_cwl_proto.data_management_mocks.data_manager.MockDataManager.put") |
| 68 | + mock_putAndRegister = mocker.patch( |
| 69 | + "dirac_cwl_proto.data_management_mocks.data_manager.MockDataManager.putAndRegister", |
| 70 | + ) |
| 71 | + |
| 72 | + mock_put.return_value = S_OK({"Successful": {}, "Failed": {expected_lfn: "Borked"}}) |
| 73 | + mock_putAndRegister.return_value = S_OK({"Successful": {expected_lfn: "OKAY"}, "Failed": {}}) |
| 74 | + |
| 75 | + result = UploadLogFile().execute( |
| 76 | + basedir, |
| 77 | + job_id=self.JOB_ID, |
| 78 | + production_id=self.PRODUCTION_ID, |
| 79 | + namespace=self.NAMESPACE, |
| 80 | + config_version=self.CONFIG_VERSION, |
| 81 | + ) |
| 82 | + |
| 83 | + mock_put.assert_called_once_with(expected_lfn, expected_zip, "LogSE") |
| 84 | + mock_putAndRegister.assert_called_once_with(expected_lfn, expected_zip, "Tier1-Failover") |
| 85 | + assert result["OK"] |
| 86 | + |
| 87 | + def test_upload_fail(self, basedir, mocker: MockerFixture): |
| 88 | + """Test both a failure to LogSE and the FailoverSE.""" |
| 89 | + base_lfn = f"/lhcb/{self.NAMESPACE}/{self.CONFIG_VERSION}/LOG/{self.PRODUCTION_ID.zfill(8)}/0000/" |
| 90 | + zip_name = self.JOB_ID.zfill(8) + ".zip" |
| 91 | + |
| 92 | + expected_lfn = os.path.join(base_lfn, zip_name) |
| 93 | + |
| 94 | + mock_put = mocker.patch("dirac_cwl_proto.data_management_mocks.data_manager.MockDataManager.put") |
| 95 | + mock_putAndRegister = mocker.patch( |
| 96 | + "dirac_cwl_proto.data_management_mocks.data_manager.MockDataManager.putAndRegister", |
| 97 | + ) |
| 98 | + |
| 99 | + mock_put.return_value = S_OK({"Successful": {}, "Failed": {expected_lfn: "Borked"}}) |
| 100 | + mock_putAndRegister.return_value = S_OK({"Successful": {}, "Failed": {expected_lfn: "Borked"}}) |
| 101 | + |
| 102 | + result = UploadLogFile().execute( |
| 103 | + basedir, |
| 104 | + job_id=self.JOB_ID, |
| 105 | + production_id=self.PRODUCTION_ID, |
| 106 | + namespace=self.NAMESPACE, |
| 107 | + config_version=self.CONFIG_VERSION, |
| 108 | + ) |
| 109 | + |
| 110 | + assert not result["OK"] |
| 111 | + |
| 112 | + # TO TEST: Failed to zip files - No outputs generated by the job |
0 commit comments