diff --git a/tests/Integration/ResourceStatusSystem/Test_FullChain.py b/tests/Integration/ResourceStatusSystem/Test_FullChain.py index a86f0110f32..0198d169acd 100644 --- a/tests/Integration/ResourceStatusSystem/Test_FullChain.py +++ b/tests/Integration/ResourceStatusSystem/Test_FullChain.py @@ -52,11 +52,10 @@ } """ -# pylint: disable=invalid-name,wrong-import-position +# pylint: disable=missing-docstring,wrong-import-position -import unittest -import sys +import pytest import DIRAC DIRAC.initialize() # Initialize configuration @@ -64,99 +63,83 @@ from DIRAC import gLogger from DIRAC.ResourceStatusSystem.PolicySystem.PDP import PDP - -class PDPTestCase(unittest.TestCase): - """PDPTestCase""" - - def setUp(self): - """test case set up""" - - gLogger.setLevel("DEBUG") - - def tearDown(self): - """clean up""" - pass - - -class PDPDecision_Success(PDPTestCase): - def test_site(self): - - pdp = PDP() - - # empty - pdp.setup(None) - res = pdp.takeDecision() - self.assertTrue(res["OK"]) - - # site - decisionParams = { - "element": "Site", - "name": "Site1", - "elementType": None, - "statusType": "ReadAccess", - "status": "Active", - "reason": None, - "tokenOwner": None, - } - pdp.setup(decisionParams) - res = pdp.takeDecision() - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"]["policyCombinedResult"]["Status"], "Banned") - - # site2 - decisionParams = { - "element": "Site", - "name": "Site2", - "elementType": "CE", - "statusType": "ReadAccess", - "status": "Active", - "domain": "test", - "reason": None, - "tokenOwner": None, - } - pdp.setup(decisionParams) - res = pdp.takeDecision() - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"]["policyCombinedResult"]["Status"], "Banned") - - # mySE - decisionParams = { - "element": "Resource", - "name": "mySE", - "elementType": "StorageElement", - "statusType": "ReadAccess", - "status": "Active", - "reason": None, - "tokenOwner": None, - } - pdp.setup(decisionParams) - res = pdp.takeDecision() - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"]["policyCombinedResult"]["Status"], "Active") - - # SE1 - decisionParams = { - "element": "Resource", - "name": "SE1", - "elementType": "StorageElement", - "statusType": "ReadAccess", - "status": "Active", - "reason": None, - "tokenOwner": None, - } - pdp.setup(decisionParams) - res = pdp.takeDecision() - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"]["policyCombinedResult"]["Status"], "Banned") - - -################################################################################ - - -if __name__ == "__main__": - suite = unittest.defaultTestLoader.loadTestsFromTestCase(PDPTestCase) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(PDPDecision_Success)) - testResult = unittest.TextTestRunner(verbosity=2).run(suite) - sys.exit(not testResult.wasSuccessful()) - -# EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF +gLogger.setLevel("DEBUG") +pdp = PDP() + + +def test_takeDecision_noDecisionParams(): + # Arrange + pdp.setup() + + # Act + result = pdp.takeDecision() + + # Assert + assert result["OK"] is True, result["Message"] + + +@pytest.mark.parametrize( + "decisionParams, status", + [ + ( + { + "element": "Site", + "name": "Site1", + "elementType": None, + "statusType": "ReadAccess", + "status": "Active", + "reason": None, + "tokenOwner": None, + }, + "Banned", + ), + ( + { + "element": "Site", + "name": "Site2", + "elementType": "CE", + "statusType": "ReadAccess", + "status": "Active", + "domain": "test", + "reason": None, + "tokenOwner": None, + }, + "Banned", + ), + ( + { + "element": "Resource", + "name": "mySE", + "elementType": "StorageElement", + "statusType": "ReadAccess", + "status": "Active", + "reason": None, + "tokenOwner": None, + }, + "Active", + ), + ( + { + "element": "Resource", + "name": "SE1", + "elementType": "StorageElement", + "statusType": "ReadAccess", + "status": "Active", + "reason": None, + "tokenOwner": None, + }, + "Banned", + ), + ], +) +def test_takeDecision_decisionParams(decisionParams, status): + + # Arrange + pdp.setup(decisionParams) + + # Act + res = pdp.takeDecision() + + # Assert + assert res["OK"] is True, res["Message"] + assert res["Value"]["policyCombinedResult"]["Status"] == status diff --git a/tests/Integration/ResourceStatusSystem/Test_Publisher.py b/tests/Integration/ResourceStatusSystem/Test_Publisher.py index d7d530c8c07..66e33e69dd4 100644 --- a/tests/Integration/ResourceStatusSystem/Test_Publisher.py +++ b/tests/Integration/ResourceStatusSystem/Test_Publisher.py @@ -2,7 +2,7 @@ It supposes that the RSS DBs are present, and that the service is running """ -# pylint: disable=invalid-name,wrong-import-position +# pylint: disable=wrong-import-position import DIRAC diff --git a/tests/Integration/ResourceStatusSystem/Test_ResourceManagement.py b/tests/Integration/ResourceStatusSystem/Test_ResourceManagement.py index 095b9580215..761a57e049c 100644 --- a/tests/Integration/ResourceStatusSystem/Test_ResourceManagement.py +++ b/tests/Integration/ResourceStatusSystem/Test_ResourceManagement.py @@ -4,11 +4,11 @@ The DB is supposed to be empty when the test starts """ -# pylint: disable=invalid-name,wrong-import-position +# pylint: disable=wrong-import-position, missing-docstring -import sys import datetime -import unittest + +import pytest import DIRAC @@ -23,321 +23,309 @@ lastCheckTime = datetime.datetime.now() -class TestClientResourceManagementTestCase(unittest.TestCase): - def setUp(self): - self.rmClient = ResourceManagementClient() - - def tearDown(self): - pass - - -class ResourceManagementClientChain(TestClientResourceManagementTestCase): - def test_AccountingCache(self): - """ - DowntimeCache table - """ - - res = self.rmClient.deleteAccountingCache("TestName12345") # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyAccountingCache - res = self.rmClient.addOrModifyAccountingCache( - "TestName12345", "plotType", "plotName", "result", datetime.datetime.now(), datetime.datetime.now() - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectAccountingCache("TestName12345") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName12345' - self.assertEqual(res["Value"][0][0], "TestName12345") - - res = self.rmClient.addOrModifyAccountingCache( - "TestName12345", "plotType", "plotName", "changedresult", dateEffective, lastCheckTime - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectAccountingCache("TestName12345") - # check if the result has changed - self.assertEqual(res["Value"][0][4], "changedresult") - - # TEST deleteAccountingCache - # ............................................................................... - res = self.rmClient.deleteAccountingCache("TestName12345") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectAccountingCache("TestName12345") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_DowntimeCache(self): - """ - DowntimeCache table - """ - - res = self.rmClient.deleteDowntimeCache("TestName12345") # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyDowntimeCache - res = self.rmClient.addOrModifyDowntimeCache( - "TestName12345", - "element", - "name", - datetime.datetime.now(), - datetime.datetime.now(), - "severity", - "description", - "link", - datetime.datetime.now(), - datetime.datetime.now(), - "gOCDBServiceType", - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectDowntimeCache("TestName12345") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName12345' - self.assertEqual(res["Value"][0][0], "TestName12345") - - res = self.rmClient.addOrModifyDowntimeCache("TestName12345", "element", "name", severity="changedSeverity") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectDowntimeCache("TestName12345") - # check if the result has changed - self.assertEqual(res["Value"][0][4], "changedSeverity") - - # TEST deleteDowntimeCache - # ............................................................................... - res = self.rmClient.deleteDowntimeCache("TestName12345") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectDowntimeCache("TestName12345") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_GGUSTicketsCache(self): - """ - GGUSTicketsCache table - """ - - res = self.rmClient.deleteGGUSTicketsCache("TestName12345") # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyGGUSTicketsCache - res = self.rmClient.addOrModifyGGUSTicketsCache("TestName12345", "link", 0, "tickets", datetime.datetime.now()) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectGGUSTicketsCache("TestName12345") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName12345' - self.assertEqual(res["Value"][0][0], "TestName12345") - - res = self.rmClient.addOrModifyGGUSTicketsCache("TestName12345", "newLink") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectGGUSTicketsCache("TestName12345") - # check if the result has changed - self.assertEqual(res["Value"][0][3], "newLink") - - # TEST deleteGGUSTicketsCache - # ............................................................................... - res = self.rmClient.deleteGGUSTicketsCache("TestName12345") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectGGUSTicketsCache("TestName12345") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_JobCache(self): - """ - JobCache table - """ - - res = self.rmClient.deleteJobCache("TestName12345") # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyJobCache - res = self.rmClient.addOrModifyJobCache("TestName12345", "maskstatus", 50.89, "status", datetime.datetime.now()) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectJobCache("TestName12345") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName12345' - self.assertEqual(res["Value"][0][0], "TestName12345") - - res = self.rmClient.addOrModifyJobCache("TestName12345", status="newStatus") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectJobCache("TestName12345") - # check if the result has changed - self.assertEqual(res["Value"][0][1], "newStatus") - - # TEST deleteJobCache - # ............................................................................... - res = self.rmClient.deleteJobCache("TestName12345") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectJobCache("TestName12345") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_PilotCache(self): - """ - PilotCache table - """ - - res = self.rmClient.deletePilotCache("TestName12345") # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyPilotCache - res = self.rmClient.addOrModifyPilotCache("TestName12345", "CE", 0.0, 25.5, "status", datetime.datetime.now()) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectPilotCache("TestName12345") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName12345' - self.assertEqual(res["Value"][0][0], "TestName12345") - self.assertEqual(res["Value"][0][6], "all") # default value for vo, as the last element - - res = self.rmClient.addOrModifyPilotCache("TestName12345", status="newStatus") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectPilotCache("TestName12345") - # check if the result has changed. - self.assertEqual(res["Value"][0][2], "newStatus") - - # TEST deletePilotCache - # ............................................................................... - res = self.rmClient.deletePilotCache("TestName12345") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectPilotCache("TestName12345") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_PolicyResult(self): - """ - PolicyResult table - """ - - res = self.rmClient.deletePolicyResult( - "element", "TestName12345", "policyName", "statusType" - ) # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyPolicyResult - res = self.rmClient.addOrModifyPolicyResult( - "element", - "TestName12345", - "policyName", - "statusType", - "status", - "reason", - datetime.datetime.now(), - datetime.datetime.now(), - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectPolicyResult("element", "TestName12345", "policyName", "statusType") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName12345' - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][8], "all") # default value for vo, as the last element - - res = self.rmClient.addOrModifyPolicyResult( - "element", "TestName12345", "policyName", "statusType", status="newStatus" - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectPolicyResult("element", "TestName12345", "policyName", "statusType") - # check if the result has changed. - self.assertEqual(res["Value"][0][4], "newStatus") - - # TEST deletePolicyResult - # ............................................................................... - res = self.rmClient.deletePolicyResult("element", "TestName12345", "policyName", "statusType") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectPolicyResult("element", "TestName12345", "policyName", "statusType") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_SpaceTokenOccupancy(self): - """ - SpaceTokenOccupancy table - """ - - res = self.rmClient.deleteSpaceTokenOccupancyCache("endpoint", "token") # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifySpaceTokenOccupancy - res = self.rmClient.addOrModifySpaceTokenOccupancyCache( - "endpoint", "token", 500.0, 1000.0, 200.0, datetime.datetime.now() - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectSpaceTokenOccupancyCache("endpoint", "token") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'token' - self.assertEqual(res["Value"][0][1], "token") - - res = self.rmClient.addOrModifySpaceTokenOccupancyCache("endpoint", "token", free=100.0) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectSpaceTokenOccupancyCache("endpoint", "token") - # check if the result has changed - self.assertEqual(res["Value"][0][3], 100.0) - - # TEST deleteSpaceTokenOccupancy - # ............................................................................... - res = self.rmClient.deleteSpaceTokenOccupancyCache("endpoint", "token") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectSpaceTokenOccupancyCache("endpoint", "token") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - def test_Transfer(self): - """ - TransferOccupancy table - """ - - res = self.rmClient.deleteTransferCache( - "sourcename", "destinationname" - ) # just making sure it's not there (yet) - self.assertTrue(res["OK"]) - - # TEST addOrModifyTransferOccupancy - res = self.rmClient.addOrModifyTransferCache( - "sourcename", "destinationname", "metric", 1000.0, datetime.datetime.now() - ) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectTransferCache("sourcename", "destinationname") - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'destinationname' - self.assertEqual(res["Value"][0][2], "metric") - - res = self.rmClient.addOrModifyTransferCache("sourcename", "destinationname", value=200.0) - self.assertTrue(res["OK"]) - - res = self.rmClient.selectTransferCache("sourcename", "destinationname") - # check if the result has changed - self.assertEqual(res["Value"][0][3], 200.0) - - # TEST deleteTransferOccupancy - # ............................................................................... - res = self.rmClient.deleteTransferCache("sourcename", "destinationname") - self.assertTrue(res["OK"]) - - res = self.rmClient.selectTransferCache("sourcename", "destinationname") - self.assertTrue(res["OK"]) - self.assertFalse(res["Value"]) - - -if __name__ == "__main__": - suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestClientResourceManagementTestCase) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ResourceManagementClientChain)) - testResult = unittest.TextTestRunner(verbosity=2).run(suite) - sys.exit(not testResult.wasSuccessful()) - -# EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF +@pytest.fixture(name="rmClient") +def fixtureResourceManagementClient(): + yield ResourceManagementClient() + + +def test_AccountingCache(rmClient): + """ + DowntimeCache table + """ + + res = rmClient.deleteAccountingCache("TestName12345") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyAccountingCache + res = rmClient.addOrModifyAccountingCache( + "TestName12345", "plotType", "plotName", "result", datetime.datetime.now(), datetime.datetime.now() + ) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectAccountingCache("TestName12345") + assert res["OK"] is True, res["Message"] + # check if the name that we got is equal to the previously added 'TestName12345' + assert res["Value"][0][0] == "TestName12345" + + res = rmClient.addOrModifyAccountingCache( + "TestName12345", "plotType", "plotName", "changedresult", dateEffective, lastCheckTime + ) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectAccountingCache("TestName12345") + # check if the result has changed + rmClient.assertEqual(res["Value"][0][4], "changedresult") + + # TEST deleteAccountingCache + # ............................................................................... + res = rmClient.deleteAccountingCache("TestName12345") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectAccountingCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_DowntimeCache(rmClient): + """ + DowntimeCache table + """ + + res = rmClient.deleteDowntimeCache("TestName12345") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyDowntimeCache + res = rmClient.addOrModifyDowntimeCache( + "TestName12345", + "element", + "name", + datetime.datetime.now(), + datetime.datetime.now(), + "severity", + "description", + "link", + datetime.datetime.now(), + datetime.datetime.now(), + "gOCDBServiceType", + ) + assert res["OK"] is True, res["Message"] + + # check if the name that we got is equal to the previously added 'TestName12345' + res = rmClient.selectDowntimeCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName12345" + + res = rmClient.addOrModifyDowntimeCache("TestName12345", "element", "name", severity="changedSeverity") + assert res["OK"] is True, res["Message"] + + # check if the result has changed + res = rmClient.selectDowntimeCache("TestName12345") + assert res["Value"][0][4] == "changedSeverity" + + # TEST deleteDowntimeCache + # ............................................................................... + res = rmClient.deleteDowntimeCache("TestName12345") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectDowntimeCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_GGUSTicketsCache(rmClient): + """ + GGUSTicketsCache table + """ + + res = rmClient.deleteGGUSTicketsCache("TestName12345") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyGGUSTicketsCache + res = rmClient.addOrModifyGGUSTicketsCache("TestName12345", "link", 0, "tickets", datetime.datetime.now()) + assert res["OK"] is True, res["Message"] + + # check if the name that we got is equal to the previously added 'TestName12345' + res = rmClient.selectGGUSTicketsCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName12345" + + res = rmClient.addOrModifyGGUSTicketsCache("TestName12345", "newLink") + assert res["OK"] is True, res["Message"] + + # check if the result has changed + res = rmClient.selectGGUSTicketsCache("TestName12345") + assert res["Value"][0][3] == "newLink" + + # TEST deleteGGUSTicketsCache + # ............................................................................... + res = rmClient.deleteGGUSTicketsCache("TestName12345") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectGGUSTicketsCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_JobCache(rmClient): + """ + JobCache table + """ + + res = rmClient.deleteJobCache("TestName12345") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyJobCache + res = rmClient.addOrModifyJobCache("TestName12345", "maskstatus", 50.89, "status", datetime.datetime.now()) + assert res["OK"] is True, res["Message"] + + # check if the name that we got is equal to the previously added 'TestName12345' + res = rmClient.selectJobCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName12345" + + res = rmClient.addOrModifyJobCache("TestName12345", status="newStatus") + assert res["OK"] is True, res["Message"] + + # check if the result has changed + res = rmClient.selectJobCache("TestName12345") + assert res["Value"][0][1] == "newStatus" + + # TEST deleteJobCache + # ............................................................................... + res = rmClient.deleteJobCache("TestName12345") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectJobCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_PilotCache(rmClient): + """ + PilotCache table + """ + + res = rmClient.deletePilotCache("TestName12345") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyPilotCache + res = rmClient.addOrModifyPilotCache("TestName12345", "CE", 0.0, 25.5, "status", datetime.datetime.now()) + assert res["OK"] is True, res["Message"] + + # check if the name that we got is equal to the previously added 'TestName12345' + res = rmClient.selectPilotCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName12345" + assert res["Value"][0][6] == "all" # default value for vo, as the last element + + res = rmClient.addOrModifyPilotCache("TestName12345", status="newStatus") + assert res["OK"] is True, res["Message"] + + # check if the result has changed. + res = rmClient.selectPilotCache("TestName12345") + assert res["Value"][0][2] == "newStatus" + + # TEST deletePilotCache + # ............................................................................... + res = rmClient.deletePilotCache("TestName12345") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectPilotCache("TestName12345") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_PolicyResult(rmClient): + """ + PolicyResult table + """ + + res = rmClient.deletePolicyResult( + "element", "TestName12345", "policyName", "statusType" + ) # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyPolicyResult + res = rmClient.addOrModifyPolicyResult( + "element", + "TestName12345", + "policyName", + "statusType", + "status", + "reason", + datetime.datetime.now(), + datetime.datetime.now(), + ) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectPolicyResult("element", "TestName12345", "policyName", "statusType") + assert res["OK"] is True, res["Message"] + # check if the name that we got is equal to the previously added 'TestName12345' + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][8] == "all" # default value for vo, as the last element + + res = rmClient.addOrModifyPolicyResult("element", "TestName12345", "policyName", "statusType", status="newStatus") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectPolicyResult("element", "TestName12345", "policyName", "statusType") + # check if the result has changed. + assert res["Value"][0][4] == "newStatus" + + # TEST deletePolicyResult + # ............................................................................... + res = rmClient.deletePolicyResult("element", "TestName12345", "policyName", "statusType") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectPolicyResult("element", "TestName12345", "policyName", "statusType") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_SpaceTokenOccupancy(rmClient): + """ + SpaceTokenOccupancy table + """ + + res = rmClient.deleteSpaceTokenOccupancyCache("endpoint", "token") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifySpaceTokenOccupancy + res = rmClient.addOrModifySpaceTokenOccupancyCache( + "endpoint", "token", 500.0, 1000.0, 200.0, datetime.datetime.now() + ) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectSpaceTokenOccupancyCache("endpoint", "token") + assert res["OK"] is True, res["Message"] + # check if the name that we got is equal to the previously added 'token' + assert res["Value"][0][1] == "token" + + res = rmClient.addOrModifySpaceTokenOccupancyCache("endpoint", "token", free=100.0) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectSpaceTokenOccupancyCache("endpoint", "token") + # check if the result has changed + assert res["Value"][0][3] == 100.0 + + # TEST deleteSpaceTokenOccupancy + # ............................................................................... + res = rmClient.deleteSpaceTokenOccupancyCache("endpoint", "token") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectSpaceTokenOccupancyCache("endpoint", "token") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] + + +def test_Transfer(rmClient): + """ + TransferOccupancy table + """ + + res = rmClient.deleteTransferCache("sourcename", "destinationname") # just making sure it's not there (yet) + assert res["OK"] is True, res["Message"] + + # TEST addOrModifyTransferOccupancy + res = rmClient.addOrModifyTransferCache("sourcename", "destinationname", "metric", 1000.0, datetime.datetime.now()) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectTransferCache("sourcename", "destinationname") + assert res["OK"] is True, res["Message"] + # check if the name that we got is equal to the previously added 'destinationname' + assert res["Value"][0][2] == "metric" + + res = rmClient.addOrModifyTransferCache("sourcename", "destinationname", value=200.0) + assert res["OK"] is True, res["Message"] + + res = rmClient.selectTransferCache("sourcename", "destinationname") + # check if the result has changed + assert res["Value"][0][3] == 200.0 + + # TEST deleteTransferOccupancy + # ............................................................................... + res = rmClient.deleteTransferCache("sourcename", "destinationname") + assert res["OK"] is True, res["Message"] + + res = rmClient.selectTransferCache("sourcename", "destinationname") + assert res["OK"] is True, res["Message"] + assert not res["Value"], res["Value"] diff --git a/tests/Integration/ResourceStatusSystem/Test_ResourceStatus.py b/tests/Integration/ResourceStatusSystem/Test_ResourceStatus.py index db5ae818b50..83bb366f51b 100644 --- a/tests/Integration/ResourceStatusSystem/Test_ResourceStatus.py +++ b/tests/Integration/ResourceStatusSystem/Test_ResourceStatus.py @@ -3,11 +3,11 @@ It supposes that the DB is present, and that the service is running """ -# pylint: disable=invalid-name,wrong-import-position -import sys +# pylint: disable=wrong-import-position, missing-docstring import time import datetime -import unittest + +import pytest import DIRAC @@ -18,433 +18,409 @@ gLogger.setLevel("DEBUG") -rssClient = ResourceStatusClient() Datetime = datetime.datetime.utcnow() - datetime.timedelta(hours=1) -class TestClientResourceStatusTestCase(unittest.TestCase): - def setUp(self): - self.rsClient = ResourceStatusClient() - - def tearDown(self): - pass - - -class ResourceStatusClientChain(TestClientResourceStatusTestCase): - def test_addAndRemove(self): - - # clean up - rssClient.deleteStatusElement("Site", "Status", "TestSite1234") - rssClient.deleteStatusElement("Site", "History", "TestSite1234") - rssClient.deleteStatusElement("Site", "Log", "TestSite1234") - rssClient.deleteStatusElement("Resource", "Status", "TestName1234") - rssClient.deleteStatusElement("Resource", "History", "TestName1234") - rssClient.deleteStatusElement("Resource", "Log", "TestName1234") - rssClient.deleteStatusElement("Resource", "Status", "TestName123456789") - rssClient.deleteStatusElement("Resource", "History", "TestName123456789") - rssClient.deleteStatusElement("Resource", "Log", "TestName123456789") - - # TEST insertStatusElement - # ............................................................................... - - # add an element - res = rssClient.insertStatusElement( - "Resource", - "Status", - "TestName1234", - "statusType", - "Active", - "elementType", - "reason", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - # check if the insert query was executed properly - self.assertTrue(res["OK"]) - - # select the previously entered element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestName1234") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Active") - self.assertEqual(res["Value"][0][9], "all") # vo value at the end of the list - - # try to select the previously entered element from the Log table (it should NOT be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"], []) - - # try to select the previously entered element from the Log table, - # with a reduced list of columns - # (it should NOT be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234", meta={"columns": ["name"]}) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"], []) - - # TEST insertStatusElement (now site) - # ............................................................................... - - print("add an element (status: Active)") - res = rssClient.insertStatusElement( - "Site", - "Status", - "TestSite1234", - "statusType", - "Active", - "elementType", - "reason", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - # check if the insert query was executed properly - self.assertTrue(res["OK"]) - - # select the previously entered element - res = rssClient.selectStatusElement("Site", "Status", "TestSite1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestSite1234") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Active") - print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) - - # try to select the previously entered element from the Log table (it should NOT be there) - res = rssClient.selectStatusElement("Site", "Log", "TestSite1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"], []) - - # try to select the previously entered element from the Log table, - # with a reduced list of columns - # (it should NOT be there) - res = rssClient.selectStatusElement("Site", "Log", "TestName1234", meta={"columns": ["name"]}) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"], []) - - # TEST addOrModifyStatusElement (this time for modifying) - # ............................................................................... - - print("modify the previously entered element (making it Banned)") - res = rssClient.addOrModifyStatusElement( - "Resource", "Status", "TestName1234", "statusType", "Banned", "elementType", "reason" - ) - # check if the addOrModify query was executed properly - self.assertTrue(res["OK"]) - - # select the previously modified element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestName1234") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Banned") - print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) - - # try to select the previously entered element from the Log table (now it should be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][1], "TestName1234") - self.assertEqual(res["Value"][0][2], "statusType") - self.assertEqual(res["Value"][0][3], "Banned") - - # try to select the previously entered element from the Log table - # with a reduced list of columns - # (now it should be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234", meta={"columns": ["name"]}) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestName1234") - - # try to select the previously entered element from the Log table - # with a reduced list of columns - # (now it should be there) - res = rssClient.selectStatusElement( - "Resource", "Log", "TestName1234", meta={"columns": ["statustype", "status"]} - ) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "statusType") - self.assertEqual(res["Value"][0][1], "Banned") - - # TEST modifyStatusElement - # ............................................................................... - - print("modify again the previously entered element, putting it back to active") - res = rssClient.modifyStatusElement( - "Resource", "Status", "TestName1234", "statusType", "Active", "elementType", "reason" - ) - # check if the modify query was executed properly - self.assertTrue(res["OK"]) - - # select the previously modified element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestName1234") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Active") - print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) - - # try to select the previously entered element from the Log table (now it should be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][1], "TestName1234") - self.assertEqual(res["Value"][0][2], "statusType") - self.assertEqual(res["Value"][0][3], "Banned") - self.assertEqual(res["Value"][1][3], "Active") # this is the last one - - print("modifing once more the previously entered element") - res = rssClient.modifyStatusElement( - "Resource", "Status", "TestName1234", "statusType", "Probing", "elementType", "reason" - ) - # check if the modify query was executed properly - self.assertTrue(res["OK"]) - - # select the previously modified element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestName1234") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Probing") - print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) - - # try to select the previously entered element from the Log table (now it should be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][1], "TestName1234") - self.assertEqual(res["Value"][0][2], "statusType") - self.assertEqual(res["Value"][0][3], "Banned") - self.assertEqual(res["Value"][1][3], "Active") - self.assertEqual(res["Value"][2][3], "Probing") # this is the last one - - # try to select the previously entered element from the Log table (now it should be there) - # with a reduced list of columns - res = rssClient.selectStatusElement( - "Resource", "Log", "TestName1234", meta={"columns": ["status", "StatusType"]} - ) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "Banned") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][1][0], "Active") - self.assertEqual(res["Value"][2][0], "Probing") # this is the last one - - time.sleep(3) # just for seeing a difference between lastCheckTime and DateEffective - print("modifing once more the previously entered element, but this time we only modify the reason") - res = rssClient.modifyStatusElement( - "Resource", "Status", "TestName1234", "statusType", "Probing", "elementType", "a new reason" - ) - # check if the modify query was executed properly - self.assertTrue(res["OK"]) - - # select the previously modified element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "TestName1234") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Probing") - self.assertEqual(res["Value"][0][3], "a new reason") - print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) - self.assertNotEqual(res["Value"][0][7], res["Value"][0][4]) - - # try to select the previously entered element from the Log table (now it should be there) - res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][1], "TestName1234") - self.assertEqual(res["Value"][0][2], "statusType") - self.assertEqual(res["Value"][0][3], "Banned") - self.assertEqual(res["Value"][1][3], "Active") - self.assertEqual(res["Value"][2][3], "Probing") - self.assertEqual(res["Value"][3][3], "Probing") # this is the last one - - # try to select the previously entered element from the Log table (now it should be there) - # Using also Meta - res = rssClient.selectStatusElement( - "Resource", - "Log", - "TestName1234", - meta={"columns": ["Status", "StatusType"], "newer": ["DateEffective", Datetime]}, - ) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "Banned") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][1][0], "Active") - self.assertEqual(res["Value"][1][1], "statusType") - self.assertEqual(res["Value"][2][0], "Probing") - self.assertEqual(res["Value"][2][1], "statusType") - self.assertEqual(res["Value"][3][0], "Probing") # this is the last one - self.assertEqual(res["Value"][3][1], "statusType") - - # try to select the previously entered element from the Log table (now it should be there) - # Using also Meta - res = rssClient.selectStatusElement( - "Resource", - "Log", - "TestName1234", - meta={"columns": ["Status", "StatusType"], "older": ["DateEffective", Datetime]}, - ) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"], []) - - # try to select the previously entered element from the Log table (now it should be there) - # Using Meta with order - res = rssClient.selectStatusElement( - "Resource", - "Log", - "TestName1234", - meta={ - "columns": ["Status", "StatusType"], - "newer": ["DateEffective", Datetime], - "order": ["status", "DESC"], - }, - ) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "Probing") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][1][0], "Probing") - self.assertEqual(res["Value"][1][1], "statusType") - self.assertEqual(res["Value"][2][0], "Banned") - self.assertEqual(res["Value"][2][1], "statusType") - self.assertEqual(res["Value"][3][0], "Active") # this is the last one (in this order) - self.assertEqual(res["Value"][3][1], "statusType") - - # try to select the previously entered element from the Log table (now it should be there) - # Using Meta with limit - res = rssClient.selectStatusElement( - "Resource", - "Log", - "TestName1234", - meta={ - "columns": ["Status", "StatusType"], - "newer": ["DateEffective", Datetime], - "order": "status", - "limit": 1, - }, - ) - # check if the select query was executed properly - self.assertTrue(res["OK"]) - self.assertEqual(res["Value"][0][0], "Active") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(len(res["Value"]), 1) - - # TEST deleteStatusElement - # ............................................................................... - - # delete the element - res = rssClient.deleteStatusElement("Resource", "Status", "TestName1234") - # check if the delete query was executed properly - self.assertTrue(res["OK"]) - - res = rssClient.deleteStatusElement("Site", "Status", "TestSite1234") - # check if the delete query was executed properly - self.assertTrue(res["OK"]) - - res = rssClient.deleteStatusElement("Site", "Log", "TestSite1234") - # check if the delete query was executed properly - self.assertTrue(res["OK"]) - - # try to select the previously deleted element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - # check if the returned value is empty - self.assertFalse(res["Value"]) - - # TEST addIfNotThereStatusElement - # ............................................................................... - - # add the element - res = rssClient.addIfNotThereStatusElement( - "Resource", - "Status", - "TestName123456789", - "statusType", - "Active", - "elementType", - "reason", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - # check if the addIfNotThereStatus query was executed properly - self.assertTrue(res["OK"]) - - res = rssClient.selectStatusElement("Resource", "Status", "TestName123456789") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName123456789' - self.assertEqual(res["Value"][0][0], "TestName123456789") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Active") - - # try to re-add the same element but with different value - res = rssClient.addIfNotThereStatusElement( - "Resource", - "Status", - "TestName123456789", - "statusType", - "Banned", - "elementType", - "another reason", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - # check if the addIfNotThereStatus query was executed properly - self.assertTrue(res["OK"]) - res = rssClient.selectStatusElement("Resource", "Status", "TestName123456789") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - # check if the name that we got is equal to the previously added 'TestName123456789' - self.assertEqual(res["Value"][0][0], "TestName123456789") - self.assertEqual(res["Value"][0][1], "statusType") - self.assertEqual(res["Value"][0][2], "Active") # NOT Banned - - # delete it - res = rssClient.deleteStatusElement("Resource", "Status", "TestName123456789") - # check if the delete query was executed properly - self.assertTrue(res["OK"]) - - # ............................................................................... - # The below values should be empty since they were modified - - # try to select the previously modified element - res = rssClient.selectStatusElement("Resource", "Status", "TestName123456789") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - # check if the returned value is empty - self.assertFalse(res["Value"]) - - # try to select the previously modified element - res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") - # check if the select query was executed properly - self.assertTrue(res["OK"]) - # check if the returned value is empty - self.assertFalse(res["Value"]) - - -if __name__ == "__main__": - suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestClientResourceStatusTestCase) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ResourceStatusClientChain)) - testResult = unittest.TextTestRunner(verbosity=2).run(suite) - sys.exit(not testResult.wasSuccessful()) +@pytest.fixture(name="rssClient") +def fixtureResourceStatusClient(): + yield ResourceStatusClient() + + +def test_addAndRemove(rssClient: ResourceStatusClient): + + # clean up + rssClient.deleteStatusElement("Site", "Status", "TestSite1234") + rssClient.deleteStatusElement("Site", "History", "TestSite1234") + rssClient.deleteStatusElement("Site", "Log", "TestSite1234") + rssClient.deleteStatusElement("Resource", "Status", "TestName1234") + rssClient.deleteStatusElement("Resource", "History", "TestName1234") + rssClient.deleteStatusElement("Resource", "Log", "TestName1234") + + # TEST insertStatusElement + # ............................................................................... + + # add an element + res = rssClient.insertStatusElement( + "Resource", + "Status", + "TestName1234", + "statusType", + "Active", + "elementType", + "reason", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + # check if the insert query was executed properly + assert res["OK"] is True, res["Message"] + + # select the previously entered element + res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName1234" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Active" + assert res["Value"][0][9] == "all" # vo value at the end of the list + + # try to select the previously entered element from the Log table (it should NOT be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"] == [] + + # try to select the previously entered element from the Log table, + # with a reduced list of columns + # (it should NOT be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234", meta={"columns": ["name"]}) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"] == [] + + # TEST insertStatusElement (now site) + # ............................................................................... + + print("add an element (status: Active)") + res = rssClient.insertStatusElement( + "Site", + "Status", + "TestSite1234", + "statusType", + "Active", + "elementType", + "reason", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + # check if the insert query was executed properly + assert res["OK"] is True, res["Message"] + + # select the previously entered element + res = rssClient.selectStatusElement("Site", "Status", "TestSite1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestSite1234" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Active" + print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) + + # try to select the previously entered element from the Log table (it should NOT be there) + res = rssClient.selectStatusElement("Site", "Log", "TestSite1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"] == [] + + # try to select the previously entered element from the Log table, + # with a reduced list of columns + # (it should NOT be there) + res = rssClient.selectStatusElement("Site", "Log", "TestName1234", meta={"columns": ["name"]}) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"] == [] + + # TEST addOrModifyStatusElement (this time for modifying) + # ............................................................................... + + print("modify the previously entered element (making it Banned)") + res = rssClient.addOrModifyStatusElement( + "Resource", "Status", "TestName1234", "statusType", "Banned", "elementType", "reason" + ) + # check if the addOrModify query was executed properly + assert res["OK"] is True, res["Message"] + + # select the previously modified element + res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName1234" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Banned" + print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) + + # try to select the previously entered element from the Log table (now it should be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][1] == "TestName1234" + assert res["Value"][0][2] == "statusType" + assert res["Value"][0][3] == "Banned" + + # try to select the previously entered element from the Log table + # with a reduced list of columns + # (now it should be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234", meta={"columns": ["name"]}) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName1234" + + # try to select the previously entered element from the Log table + # with a reduced list of columns + # (now it should be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234", meta={"columns": ["statustype", "status"]}) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "statusType" + assert res["Value"][0][1] == "Banned" + + # TEST modifyStatusElement + # ............................................................................... + + print("modify again the previously entered element, putting it back to active") + res = rssClient.modifyStatusElement( + "Resource", "Status", "TestName1234", "statusType", "Active", "elementType", "reason" + ) + # check if the modify query was executed properly + assert res["OK"] is True, res["Message"] + + # select the previously modified element + res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName1234" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Active" + print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) + + # try to select the previously entered element from the Log table (now it should be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][1] == "TestName1234" + assert res["Value"][0][2] == "statusType" + assert res["Value"][0][3] == "Banned" + assert res["Value"][1][3] == "Active" # this is the last one + + print("modifing once more the previously entered element") + res = rssClient.modifyStatusElement( + "Resource", "Status", "TestName1234", "statusType", "Probing", "elementType", "reason" + ) + # check if the modify query was executed properly + assert res["OK"] is True, res["Message"] + + # select the previously modified element + res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName1234" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Probing" + print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) + + # try to select the previously entered element from the Log table (now it should be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][1] == "TestName1234" + assert res["Value"][0][2] == "statusType" + assert res["Value"][0][3] == "Banned" + assert res["Value"][1][3] == "Active" + assert res["Value"][2][3] == "Probing" # this is the last one + + # try to select the previously entered element from the Log table (now it should be there) + # with a reduced list of columns + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234", meta={"columns": ["status", "StatusType"]}) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "Banned" + assert res["Value"][0][1] == "statusType" + assert res["Value"][1][0] == "Active" + assert res["Value"][2][0] == "Probing" # this is the last one + + time.sleep(3) # just for seeing a difference between lastCheckTime and DateEffective + print("modifing once more the previously entered element, but this time we only modify the reason") + res = rssClient.modifyStatusElement( + "Resource", "Status", "TestName1234", "statusType", "Probing", "elementType", "a new reason" + ) + # check if the modify query was executed properly + assert res["OK"] is True, res["Message"] + + # select the previously modified element + res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "TestName1234" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Probing" + assert res["Value"][0][3] == "a new reason" + print("inserted lastCheckTime and DateEffective: %s, %s" % (res["Value"][0][7], res["Value"][0][4])) + assert res["Value"][0][7] != res["Value"][0][4] + + # try to select the previously entered element from the Log table (now it should be there) + res = rssClient.selectStatusElement("Resource", "Log", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][1] == "TestName1234" + assert res["Value"][0][2] == "statusType" + assert res["Value"][0][3] == "Banned" + assert res["Value"][1][3] == "Active" + assert res["Value"][2][3] == "Probing" + assert res["Value"][3][3] == "Probing" # this is the last one + + # try to select the previously entered element from the Log table (now it should be there) + # Using also Meta + res = rssClient.selectStatusElement( + "Resource", + "Log", + "TestName1234", + meta={"columns": ["Status", "StatusType"], "newer": ["DateEffective", Datetime]}, + ) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "Banned" + assert res["Value"][0][1] == "statusType" + assert res["Value"][1][0] == "Active" + assert res["Value"][1][1] == "statusType" + assert res["Value"][2][0] == "Probing" + assert res["Value"][2][1] == "statusType" + assert res["Value"][3][0] == "Probing" # this is the last one + assert res["Value"][3][1] == "statusType" + + # try to select the previously entered element from the Log table (now it should be there) + # Using also Meta + res = rssClient.selectStatusElement( + "Resource", + "Log", + "TestName1234", + meta={"columns": ["Status", "StatusType"], "older": ["DateEffective", Datetime]}, + ) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"] == [] + + # try to select the previously entered element from the Log table (now it should be there) + # Using Meta with order + res = rssClient.selectStatusElement( + "Resource", + "Log", + "TestName1234", + meta={ + "columns": ["Status", "StatusType"], + "newer": ["DateEffective", Datetime], + "order": ["status", "DESC"], + }, + ) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "Probing" + assert res["Value"][0][1] == "statusType" + assert res["Value"][1][0] == "Probing" + assert res["Value"][1][1] == "statusType" + assert res["Value"][2][0] == "Banned" + assert res["Value"][2][1] == "statusType" + assert res["Value"][3][0] == "Active" # this is the last one (in this order) + assert res["Value"][3][1] == "statusType" + + # try to select the previously entered element from the Log table (now it should be there) + # Using Meta with limit + res = rssClient.selectStatusElement( + "Resource", + "Log", + "TestName1234", + meta={ + "columns": ["Status", "StatusType"], + "newer": ["DateEffective", Datetime], + "order": "status", + "limit": 1, + }, + ) + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + assert res["Value"][0][0] == "Active" + assert res["Value"][0][1] == "statusType" + assert len(res["Value"]) == 1 + + # TEST deleteStatusElement + # ............................................................................... + + # delete the element + res = rssClient.deleteStatusElement("Resource", "Status", "TestName1234") + # check if the delete query was executed properly + assert res["OK"] is True, res["Message"] + + res = rssClient.deleteStatusElement("Site", "Status", "TestSite1234") + # check if the delete query was executed properly + assert res["OK"] is True, res["Message"] + + res = rssClient.deleteStatusElement("Site", "Log", "TestSite1234") + # check if the delete query was executed properly + assert res["OK"] is True, res["Message"] + + # try to select the previously deleted element + res = rssClient.selectStatusElement("Resource", "Status", "TestName1234") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + # check if the returned value is empty + assert not res["Value"] + + +def test_addIfNotThereStatusElement(rssClient: ResourceStatusClient): + + # Clean up + rssClient.deleteStatusElement("Resource", "Status", "TestName123456789") + rssClient.deleteStatusElement("Resource", "History", "TestName123456789") + rssClient.deleteStatusElement("Resource", "Log", "TestName123456789") + + # add the element + res = rssClient.addIfNotThereStatusElement( + "Resource", + "Status", + "TestName123456789", + "statusType", + "Active", + "elementType", + "reason", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + # check if the addIfNotThereStatus query was executed properly + assert res["OK"] is True, res["Message"] + + res = rssClient.selectStatusElement("Resource", "Status", "TestName123456789") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + # check if the name that we got is equal to the previously added 'TestName123456789' + assert res["Value"][0][0] == "TestName123456789" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Active" + + # try to re-add the same element but with different value + res = rssClient.addIfNotThereStatusElement( + "Resource", + "Status", + "TestName123456789", + "statusType", + "Banned", + "elementType", + "another reason", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + # check if the addIfNotThereStatus query was executed properly + assert res["OK"] is True, res["Message"] + res = rssClient.selectStatusElement("Resource", "Status", "TestName123456789") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + # check if the name that we got is equal to the previously added 'TestName123456789' + assert res["Value"][0][0] == "TestName123456789" + assert res["Value"][0][1] == "statusType" + assert res["Value"][0][2] == "Active" # NOT Banned + + # delete it + res = rssClient.deleteStatusElement("Resource", "Status", "TestName123456789") + # check if the delete query was executed properly + assert res["OK"] is True, res["Message"] + + # try to select the previously deleted element + res = rssClient.selectStatusElement("Resource", "Status", "TestName123456789") + # check if the select query was executed properly + assert res["OK"] is True, res["Message"] + # check if the returned value is empty + assert not res["Value"] diff --git a/tests/Integration/ResourceStatusSystem/Test_SiteStatus.py b/tests/Integration/ResourceStatusSystem/Test_SiteStatus.py index eea14806d64..67145f95d09 100644 --- a/tests/Integration/ResourceStatusSystem/Test_SiteStatus.py +++ b/tests/Integration/ResourceStatusSystem/Test_SiteStatus.py @@ -2,11 +2,11 @@ SiteStatus -> ResourceStatusClient -> ResourceStatusDB It supposes that the DB is present, and that the service is running """ -# pylint: disable=invalid-name,wrong-import-position +# pylint: disable=wrong-import-position, missing-docstring from datetime import datetime -import unittest -import sys + +import pytest import DIRAC @@ -21,162 +21,148 @@ testSite = "test1234.test.test" -class TestClientSiteStatusTestCase(unittest.TestCase): - def setUp(self): - self.rsClient = ResourceStatusClient() - self.stClient = SiteStatus() - self.stClient.rssFlag = True - - def tearDown(self): - pass - - -class ClientChain(TestClientSiteStatusTestCase): - def test_addAndRemove(self): - - # make sure that the test sites are not presented in the db - self.rsClient.deleteStatusElement("Site", "Status", testSite) - self.rsClient.deleteStatusElement("Site", "Status", "testActive1.test.test") - self.rsClient.deleteStatusElement("Site", "Status", "testActive.test.test") - self.rsClient.deleteStatusElement("Site", "Status", "testBanned.test.test") - - # add test site - res = self.rsClient.insertStatusElement( - "Site", - "Status", - testSite, - "all", - "Active", - "Site", - "Synchronized", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - self.assertTrue(res["OK"]) - self.stClient.rssCache.refreshCache() - - # TEST getSites - # ............................................................................... - - result = self.stClient.getSites() - self.assertTrue(result["OK"]) - - self.assertTrue(testSite in result["Value"]) - - # TEST getSiteStatuses - # ............................................................................... - - result = self.stClient.getSiteStatuses([testSite]) - self.assertTrue(result["OK"]) - - self.assertEqual(result["Value"][testSite], "Active") - - # TEST getUsableSites - # ............................................................................... - - result = self.stClient.getUsableSites([testSite]) - self.assertTrue(result["OK"]) - - self.assertEqual(result["Value"][0], testSite) - - # finally delete the test site - res = self.rsClient.deleteStatusElement("Site", "Status", testSite) - self.assertTrue(res["OK"]) - - # ............................................................................... - # adding some more test sites and more complex tests - # ............................................................................... - - res = self.rsClient.insertStatusElement( - "Site", - "Status", - "testActive.test.test", - "all", - "Active", - "Site", - "Synchronized", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - self.assertTrue(res["OK"]) - - res = self.rsClient.insertStatusElement( - "Site", - "Status", - "testActive1.test.test", - "all", - "Active", - "Site", - "Synchronized", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - self.assertTrue(res["OK"]) - - res = self.rsClient.insertStatusElement( - "Site", - "Status", - "testBanned.test.test", - "all", - "Banned", - "Site", - "Synchronized", - Datetime, - Datetime, - "tokenOwner", - Datetime, - ) - self.assertTrue(res["OK"]) - self.stClient.rssCache.refreshCache() - - # TEST getSites - # ............................................................................... - - result = self.stClient.getSites() - self.assertTrue(result["OK"]) - - self.assertTrue("testActive1.test.test" in result["Value"]) - self.assertTrue("testActive.test.test" in result["Value"]) - self.assertFalse("testBanned.test.test" in result["Value"]) - - # TEST getSites - # ............................................................................... - - result = self.stClient.getSites("All") - self.assertTrue(result["OK"]) - - self.assertTrue("testActive1.test.test" in result["Value"]) - self.assertTrue("testActive.test.test" in result["Value"]) - self.assertTrue("testBanned.test.test" in result["Value"]) - - # TEST getUsableSites - # ............................................................................... - - result = self.stClient.getUsableSites() - self.assertTrue(result["OK"]) - - self.assertTrue("testActive1.test.test" in result["Value"]) - self.assertTrue("testActive.test.test" in result["Value"]) - - # setting a status - result = self.stClient.setSiteStatus("testBanned.test.test", "Probing") - self.assertTrue(result["OK"]) - self.stClient.rssCache.refreshCache() - - result = self.stClient.getSites("Probing") - self.assertTrue(result["OK"]) - self.assertTrue("testBanned.test.test" in result["Value"]) - self.assertFalse("testActive.test.test" in result["Value"]) - - -if __name__ == "__main__": - suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestClientSiteStatusTestCase) - suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ClientChain)) - testResult = unittest.TextTestRunner(verbosity=2).run(suite) - sys.exit(not testResult.wasSuccessful()) +@pytest.fixture(name="stClient") +def fixtureSiteStatus(): + siteStatus = SiteStatus() + siteStatus.rssFlag = True + yield siteStatus + + +def test_addAndRemove_simpleCase(stClient): + + # make sure that the test sites are not presented in the db + rsClient = ResourceStatusClient() + rsClient.deleteStatusElement("Site", "Status", testSite) + rsClient.deleteStatusElement("Site", "Status", "testActive1.test.test") + rsClient.deleteStatusElement("Site", "Status", "testActive.test.test") + rsClient.deleteStatusElement("Site", "Status", "testBanned.test.test") + + # add test site + result = rsClient.insertStatusElement( + "Site", + "Status", + testSite, + "all", + "Active", + "Site", + "Synchronized", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + assert result["OK"] is True, result["Message"] + stClient.rssCache.refreshCache() + + # TEST getSites + # ............................................................................... + + result = stClient.getSites() + assert result["OK"] is True, result["Message"] + assert testSite in result["Value"] + + # TEST getSiteStatuses + # ............................................................................... + + result = stClient.getSiteStatuses([testSite]) + assert result["OK"] is True, result["Message"] + assert result["Value"][testSite] == "Active" + + # TEST getUsableSites + # ............................................................................... + + result = stClient.getUsableSites([testSite]) + assert result["OK"] is True, result["Message"] + assert testSite in result["Value"] + + # finally delete the test site + result = rsClient.deleteStatusElement("Site", "Status", testSite) + assert result["OK"] is True, result["Message"] + + +def test_addAndRemove_complicatedTest(stClient): + rsClient = ResourceStatusClient() + result = rsClient.insertStatusElement( + "Site", + "Status", + "testActive.test.test", + "all", + "Active", + "Site", + "Synchronized", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + assert result["OK"] is True, result["Message"] + + result = rsClient.insertStatusElement( + "Site", + "Status", + "testActive1.test.test", + "all", + "Active", + "Site", + "Synchronized", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + assert result["OK"] is True, result["Message"] + + result = rsClient.insertStatusElement( + "Site", + "Status", + "testBanned.test.test", + "all", + "Banned", + "Site", + "Synchronized", + Datetime, + Datetime, + "tokenOwner", + Datetime, + ) + assert result["OK"] is True, result["Message"] + stClient.rssCache.refreshCache() + + # TEST getSites + # ............................................................................... + + result = stClient.getSites() + assert result["OK"] is True, result["Message"] + + assert "testActive1.test.test" in result["Value"] + assert "testActive.test.test" in result["Value"] + assert "testBanned.test.test" not in result["Value"] + + # TEST getSites + # ............................................................................... + + result = stClient.getSites("All") + assert result["OK"] is True, result["Message"] + + assert "testActive1.test.test" in result["Value"] + assert "testActive.test.test" in result["Value"] + assert "testBanned.test.test" in result["Value"] + + # TEST getUsableSites + # ............................................................................... + + result = stClient.getUsableSites() + assert result["OK"] is True, result["Message"] + + assert "testActive1.test.test" in result["Value"] + assert "testActive.test.test" in result["Value"] + + # setting a status + result = stClient.setSiteStatus("testBanned.test.test", "Probing") + assert result["OK"] is True, result["Message"] + stClient.rssCache.refreshCache() + + result = stClient.getSites("Probing") + assert result["OK"] is True, result["Message"] + assert "testBanned.test.test" in result["Value"] + assert "testActive.test.test" not in result["Value"] diff --git a/tests/Integration/WorkloadManagementSystem/Test_JobLoggingDB.py b/tests/Integration/WorkloadManagementSystem/Test_JobLoggingDB.py index e3ad16c4328..39578d7efc0 100755 --- a/tests/Integration/WorkloadManagementSystem/Test_JobLoggingDB.py +++ b/tests/Integration/WorkloadManagementSystem/Test_JobLoggingDB.py @@ -1,10 +1,10 @@ """ This test only need the JobLoggingDB to be present """ -# pylint: disable=invalid-name,wrong-import-position +# pylint: disable=wrong-import-position, missing-docstring -import unittest import datetime -import sys + +import pytest import DIRAC @@ -13,48 +13,38 @@ from DIRAC.WorkloadManagementSystem.DB.JobLoggingDB import JobLoggingDB -class JobLoggingDBTestCase(unittest.TestCase): - """Base class for the JobLoggingDB test cases""" - - def setUp(self): - self.jlogDB = JobLoggingDB() +@pytest.fixture(name="jobLoggingDB") +def fixtureJobLoggingDB(): + yield JobLoggingDB() - def tearDown(self): - pass +def test_JobStatus(jobLoggingDB: JobLoggingDB): -class JobLoggingCase(JobLoggingDBTestCase): - """TestJobDB represents a test suite for the JobDB database front-end""" + result = jobLoggingDB.addLoggingRecord( + 1, + status="testing", + minorStatus="date=datetime.datetime.utcnow()", + date=datetime.datetime.utcnow(), + source="Unittest", + ) + assert result["OK"] is True, result["Message"] - def test_JobStatus(self): + date = "2006-04-25 14:20:17" + result = jobLoggingDB.addLoggingRecord( + 1, status="testing", minorStatus="2006-04-25 14:20:17", date=date, source="Unittest" + ) + assert result["OK"] is True, result["Message"] - result = self.jlogDB.addLoggingRecord( - 1, - status="testing", - minorStatus="date=datetime.datetime.utcnow()", - date=datetime.datetime.utcnow(), - source="Unittest", - ) - self.assertTrue(result["OK"], result.get("Message")) - date = "2006-04-25 14:20:17" - result = self.jlogDB.addLoggingRecord( - 1, status="testing", minorStatus="2006-04-25 14:20:17", date=date, source="Unittest" - ) - self.assertTrue(result["OK"], result.get("Message")) - result = self.jlogDB.addLoggingRecord(1, status="testing", minorStatus="No date 1", source="Unittest") - self.assertTrue(result["OK"], result.get("Message")) - result = self.jlogDB.addLoggingRecord(1, status="testing", minorStatus="No date 2", source="Unittest") - self.assertTrue(result["OK"], result.get("Message")) - result = self.jlogDB.getJobLoggingInfo(1) - self.assertTrue(result["OK"], result.get("Message")) + result = jobLoggingDB.addLoggingRecord(1, status="testing", minorStatus="No date 1", source="Unittest") + assert result["OK"] is True, result["Message"] - result = self.jlogDB.getWMSTimeStamps(1) - self.assertTrue(result["OK"], result.get("Message")) + result = jobLoggingDB.addLoggingRecord(1, status="testing", minorStatus="No date 2", source="Unittest") + assert result["OK"] is True, result["Message"] - self.jlogDB.deleteJob(1) + result = jobLoggingDB.getJobLoggingInfo(1) + assert result["OK"] is True, result["Message"] + result = jobLoggingDB.getWMSTimeStamps(1) + assert result["OK"] is True, result["Message"] -if __name__ == "__main__": - suite = unittest.defaultTestLoader.loadTestsFromTestCase(JobLoggingCase) - testResult = unittest.TextTestRunner(verbosity=2).run(suite) - sys.exit(not testResult.wasSuccessful()) + jobLoggingDB.deleteJob(1)