Salome HOME
[EDF30356] : Extend management of maximum_time attribute format from pylauncher to...
[modules/kernel.git] / src / Container / Test / testProxy.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2023-2024  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 import pylauncher
28
29 class TestProxy(unittest.TestCase):
30     def testProxy(self):
31         """
32         [EDF27816] : This test checks two things :
33         - Capability of ContainerManager to launch a Container with overriden environement
34         - Management of proxy creation to manage big obj exchange between process (here between Container and the current process)
35         """
36         hostname = "localhost"
37         cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test")
38
39         with tempfile.TemporaryDirectory() as tmpdirname:
40             val_for_jj = "3333"
41             val_for_big_obj = str( tmpdirname )
42             val_for_thres = 100 # force proxy file
43             # Override environement for all containers launched
44             salome.cm.SetBigObjOnDiskDirectory(val_for_big_obj)
45             salome.cm.SetBigObjOnDiskThreshold(val_for_thres)
46             salome.cm.SetOverrideEnvForContainersSimple(env = [("jj",val_for_jj)])
47             cont = salome.cm.GiveContainer(cp)
48             ## Time to test it
49             script_st = """import os
50 import KernelBasis
51 a = KernelBasis.GetBigObjOnDiskDirectory()
52 b = os.environ["jj"]
53 c = KernelBasis.GetBigObjOnDiskThreshold()
54 j = a,b,c"""
55             pyscript = cont.createPyScriptNode("testScript",script_st)
56             a,b,c = pickle.loads(pyscript.execute(["j"],pickle.dumps(([],{}))))[0]
57             self.assertTrue( a == val_for_big_obj )
58             self.assertTrue( b == val_for_jj )
59             self.assertTrue( c == val_for_thres )
60             # check environment using POSIX API in the container process
61             for k,v in [("jj",val_for_jj)]:
62                 assert( {elt.key:elt.value.value() for elt in cont.get_os_environment()}[k] == v )
63             #
64             import SALOME_PyNode
65             poa = salome.orb.resolve_initial_references("RootPOA")
66             obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( ([],{}) ))
67             id_o = poa.activate_object(obj)
68             refPtr = poa.id_to_reference(id_o)
69             script_st2 = """ob = list( range(100) )""" # size of pickled ob must be greater than val_for_thres
70             pyscript2 = cont.createPyScriptNode("testScript2",script_st2)
71             pyscript2.executeFirst(refPtr)
72             ret2 = pyscript2.executeSecond(["ob"])
73             self.assertTrue( len(ret2) == 1)
74             ret2 = ret2[0]
75             ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
76             self.assertTrue( isinstance( ret3, SALOME_PyNode.BigObjectOnDiskList ) )
77             self.assertTrue( val_for_big_obj == os.path.dirname( ret3.getFileName() ) )# very important part of test
78             self.assertTrue( ret3.get() == list(range(100)) )
79             fn = ret3.getFileName()
80             self.assertTrue( os.path.exists( fn ) )
81             ret3.unlinkOnDestructor()
82             del ret3
83             import gc
84             gc.collect(0)
85             self.assertTrue( not os.path.exists( fn ) ) # at destruction of ret3 the corresponding pckl file must be destructed
86             cont.Shutdown()
87
88     def testExecCodeAtInit(self):
89         """
90         [EDF28648] : allow initialisation script at startup
91         """
92         import os
93         with tempfile.NamedTemporaryFile() as tmpFileName:
94             tmpFileName.close()
95             salome.cm.SetCodeOnContainerStartUp("""
96 with open("{}","w") as f:
97   f.write("coucou")
98 """.format(tmpFileName.name) )# injection of python code expected to be executed at startup
99             cp = pylauncher.GetRequestForGiveContainer("localhost","gg")
100             cont = salome.cm.GiveContainer(cp) # code is expected to be executed in process abroad -> file is supposed to contain coucou
101             cont.Shutdown()
102             salome.cm.SetCodeOnContainerStartUp("") # no more startup code for other tests
103             with open(tmpFileName.name,"r") as f:
104                 self.assertTrue(f.read()=="coucou")
105             os.unlink( tmpFileName.name ) # context manager do not clean file
106
107 if __name__ == '__main__':
108     salome.standalone()
109     salome.salome_init()
110     unittest.main()