Salome HOME
ac0d47c8e2fd8146a8957b4266251e67f10d3373
[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 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.SetOverrideEnvForContainersSimple(env = [("jj",val_for_jj),("SALOME_FILE_BIG_OBJ_DIR",val_for_big_obj),("SALOME_BIG_OBJ_ON_DISK_THRES",val_for_thres)])
45             cont = salome.cm.GiveContainer(cp)
46             ## Time to test it
47             script_st = """import os
48 a = os.environ["SALOME_FILE_BIG_OBJ_DIR"]
49 b = os.environ["jj"]
50 c = os.environ["SALOME_BIG_OBJ_ON_DISK_THRES"]
51 j = a,b,c"""
52             pyscript = cont.createPyScriptNode("testScript",script_st)
53             a,b,c = pickle.loads(pyscript.execute(["j"],pickle.dumps(([],{}))))[0]
54             self.assertTrue( a == val_for_big_obj )
55             self.assertTrue( b == val_for_jj )
56             self.assertTrue( c == val_for_thres )
57             # check environment using POSIX API in the container process
58             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)]:
59                 assert( {elt.key:elt.value.value() for elt in cont.get_os_environment()}[k] == v )
60             #
61             import SALOME_PyNode
62             poa = salome.orb.resolve_initial_references("RootPOA")
63             obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( ([],{}) ))
64             id_o = poa.activate_object(obj)
65             refPtr = poa.id_to_reference(id_o)
66             script_st2 = """ob = list( range(100) )""" # size of pickled ob must be greater than val_for_thres
67             pyscript2 = cont.createPyScriptNode("testScript2",script_st2)
68             pyscript2.executeFirst(refPtr)
69             ret2 = pyscript2.executeSecond(["ob"])
70             self.assertTrue( len(ret2) == 1)
71             ret2 = ret2[0]
72             ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
73             self.assertTrue( isinstance( ret3, SALOME_PyNode.BigObjectOnDiskList ) )
74             self.assertTrue( val_for_big_obj == os.path.dirname( ret3.getFileName() ) )# very important part of test
75             self.assertTrue( ret3.get() == list(range(100)) )
76             fn = ret3.getFileName()
77             self.assertTrue( os.path.exists( fn ) )
78             ret3.unlinkOnDestructor()
79             del ret3
80             import gc
81             gc.collect(0)
82             self.assertTrue( not os.path.exists( fn ) ) # at destruction of ret3 the corresponding pckl file must be destructed
83             cont.Shutdown()
84
85     def testExecCodeAtInit(self):
86         """
87         [EDF28648] : allow initialisation script at startup
88         """
89         import os
90         with tempfile.NamedTemporaryFile() as tmpFileName:
91             tmpFileName.close()
92             salome.cm.SetCodeOnContainerStartUp("""
93 with open("{}","w") as f:
94   f.write("coucou")
95 """.format(tmpFileName.name) )# injection of python code expected to be executed at startup
96             cp = pylauncher.GetRequestForGiveContainer("localhost","gg")
97             cont = salome.cm.GiveContainer(cp) # code is expected to be executed in process abroad -> file is supposed to contain coucou
98             cont.Shutdown()
99             salome.cm.SetCodeOnContainerStartUp("") # no more startup code for other tests
100             with open(tmpFileName.name,"r") as f:
101                 self.assertTrue(f.read()=="coucou")
102             os.unlink( tmpFileName.name ) # context manager do not clean file
103
104 if __name__ == '__main__':
105     salome.standalone()
106     salome.salome_init()
107     unittest.main()