Salome HOME
d3e7b50dbee2a7d9cdb0e9c076094c860feecb30
[modules/kernel.git] / src / Container / Test / testProxy.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2023  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 unittest
22 import os
23 import salome
24 import Engines
25 import pickle
26 import tempfile
27
28 class TestProxy(unittest.TestCase):
29     def testProxy(self):
30         """
31         [EDF27816] : This test checks two things :
32         - Capability of ContainerManager to launch a Container with overriden environement
33         - Management of proxy creation to manage big obj exchange between process (here between Container and the current process)
34         """
35         hostname = "localhost"
36         rp=Engines.ResourceParameters(name=hostname,
37                                         hostname=hostname,
38                                         can_launch_batch_jobs=False,
39                                         can_run_containers=True,
40                                         OS="Linux",
41                                         componentList=[],
42                                         nb_proc=1,
43                                         mem_mb=1000,
44                                         cpu_clock=1000,
45                                         nb_node=1,
46                                         nb_proc_per_node=1,
47                                         policy="first",
48                                         resList=[])
49
50         cp=Engines.ContainerParameters(container_name="container_test",
51                                         mode="start",
52                                         workingdir=os.getcwd(),
53                                         nb_proc=1,
54                                         isMPI=False,
55                                         parallelLib="",
56                                         resource_params=rp)
57
58         with tempfile.TemporaryDirectory() as tmpdirname:
59             val_for_jj = "3333"
60             val_for_big_obj = str( tmpdirname )
61             val_for_thres = "100" # force proxy file
62             # Override environement for all containers launched
63             salome.cm.SetOverrideEnvForContainersSimple(env = [("jj",val_for_jj),("SALOME_FILE_BIG_OBJ_DIR",val_for_big_obj),("SALOME_BIG_OBJ_ON_DISK_THRES",val_for_thres)])
64             cont = salome.cm.GiveContainer(cp)
65             ## Time to test it
66             script_st = """import os
67 a = os.environ["SALOME_FILE_BIG_OBJ_DIR"]
68 b = os.environ["jj"]
69 c = os.environ["SALOME_BIG_OBJ_ON_DISK_THRES"]
70 j = a,b,c"""
71             pyscript = cont.createPyScriptNode("testScript",script_st)
72             a,b,c = pickle.loads(pyscript.execute(["j"],pickle.dumps(([],{}))))[0]
73             assert( a == val_for_big_obj )
74             assert( b == val_for_jj )
75             assert( c == val_for_thres )
76             # check environment using POSIX API in the container process
77             for k,v in [("SALOME_FILE_BIG_OBJ_DIR",val_for_big_obj),("SALOME_BIG_OBJ_ON_DISK_THRES",val_for_thres),("jj",val_for_jj)]:
78                 assert( {elt.key:elt.value.value() for elt in cont.get_os_environment()}[k] == v )
79             #
80             import SALOME_PyNode
81             poa = salome.orb.resolve_initial_references("RootPOA")
82             obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( ([],{}) ))
83             id_o = poa.activate_object(obj)
84             refPtr = poa.id_to_reference(id_o)
85             script_st2 = """ob = list( range(100) )""" # size of pickled ob must be greater than val_for_thres
86             pyscript2 = cont.createPyScriptNode("testScript2",script_st2)
87             pyscript2.executeFirst(refPtr)
88             ret2 = pyscript2.executeSecond(["ob"])
89             assert( len(ret2) == 1)
90             ret2 = ret2[0]
91             ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
92             assert( isinstance( ret3, SALOME_PyNode.BigObjectOnDiskList ) )
93             assert( val_for_big_obj == os.path.dirname( ret3.getFileName() ) )# very important part of test
94             assert( ret3.get() == list(range(100)) )
95             fn = ret3.getFileName()
96             assert( os.path.exists( fn ) )
97             ret3.unlinkOnDestructor()
98             del ret3
99             import gc
100             gc.collect(0)
101             assert( not os.path.exists( fn ) ) # at destruction of ret3 the corresponding pckl file must be destructed
102             cont.Shutdown()
103
104 if __name__ == '__main__':
105     salome.standalone()
106     salome.salome_init()
107     unittest.main()