Salome HOME
ab07a4089f91d0d48423b815a6ffd11928405694
[modules/yacs.git] / src / yacsloader_swig / Test / testWorkloadManager.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2020  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import sys
22 import pilot
23 import SALOMERuntime
24 import loader
25 import unittest
26 import tempfile
27 import os
28 import salome
29
30 class TestEdit(unittest.TestCase):
31
32     def setUp(self):
33         SALOMERuntime.RuntimeSALOME_setRuntime()
34         self.r = pilot.getRuntime()
35         self.l = loader.YACSLoader()
36         self.e = pilot.ExecutorSwig()
37         # We need a catalog which contains only one resource named "localhost"
38         # with 16 cores. The modifications made here are not saved to the
39         # catalog file.
40         salome.salome_init()
41         resourceManager = salome.lcc.getResourcesManager()
42         resource_definition = resourceManager.GetResourceDefinition("localhost")
43         resource_definition.nb_node = 16
44         resourceManager.AddResource(resource_definition, False, "")
45         resource_required = salome.ResourceParameters()
46         resource_required.can_run_containers = True
47         res_list = resourceManager.GetFittingResources(resource_required)
48         for r in res_list:
49           if r != "localhost":
50             resourceManager.RemoveResource(r, False, "")
51         #resource_definition = resourceManager.GetResourceDefinition("localhost")
52         #self.assertEqual(resource_definition.nb_node, 16)
53
54     def tearDown(self):
55         cm = salome.lcc.getContainerManager()
56         cm.ShutdownContainers()
57
58     def test1(self):
59         """ Two parallel foreach-s with different containers
60         """
61         proc = self.l.load("samples/wlm_2foreach.xml")
62         self.e.RunW(proc,0)
63         self.assertEqual(proc.getState(),pilot.DONE)
64         res_port = proc.getChildByName("End").getOutputPort("r")
65         # theoretical time should be 15s
66         execution_time = res_port.getPyObj()
67         # lower time means some resources are overloaded
68         self.assertTrue(execution_time > 13)
69         # The containers may need some time to be launched.
70         # We need some delay to add to the 15s.
71         self.assertTrue(execution_time < 20)
72
73     def test2(self):
74         """ Two parallel foreach-s with different containers and python nodes
75             using cache.
76         """
77         proc = self.l.load("samples/wlm_2foreach_with_cache.xml")
78         self.e.RunW(proc,0)
79         self.assertEqual(proc.getState(),pilot.DONE)
80         ok = proc.getChildByName("End").getOutputPort("ok")
81         self.assertTrue(ok)
82         total_time = proc.getChildByName("End").getOutputPort("total_time")
83         # theoretical time should be 16s
84         execution_time = total_time.getPyObj()
85         # lower time means some resources are overloaded
86         self.assertTrue(execution_time > 14)
87         # The containers may need some time to be launched.
88         # We need some delay to add to the 16s.
89         self.assertTrue(execution_time < 20)
90
91     def test3(self):
92         """ Launch 8 independent nodes in parallel.
93         """
94         proc = self.l.load("samples/wlm_8nodes.xml")
95         self.e.RunW(proc,0)
96         self.assertEqual(proc.getState(),pilot.DONE)
97         ok = proc.getChildByName("End").getOutputPort("ok")
98         if not ok :
99           err_message = proc.getChildByName("End").getOutputPort("err_message")
100           self.fail(err_message)
101
102 if __name__ == '__main__':
103   dir_test = tempfile.mkdtemp(suffix=".yacstest")
104   file_test = os.path.join(dir_test,"UnitTestsResult")
105   with open(file_test, 'a') as f:
106       f.write("  --- TEST src/yacsloader: testWorkloadManager.py\n")
107       suite = unittest.makeSuite(TestEdit)
108       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
109   sys.exit(not result.wasSuccessful())