1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2023-2024 CEA/DEN, EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
29 class TestProxy(unittest.TestCase):
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)
36 hostname = "localhost"
37 cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test")
39 with tempfile.TemporaryDirectory() as tmpdirname:
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)
49 script_st = """import os
51 _,a = KernelBasis.GetBigObjOnDiskProtocolAndDirectory()
53 c = KernelBasis.GetBigObjOnDiskThreshold()
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 )
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)
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().getFileName() ) )# very important part of test
78 self.assertTrue( ret3.get() == list(range(100)) )
79 fn = ret3.getFileName().getFileName()
80 self.assertTrue( os.path.exists( fn ) )
81 ret3.unlinkOnDestructor()
85 self.assertTrue( not os.path.exists( fn ) ) # at destruction of ret3 the corresponding pckl file must be destructed
88 def testExecCodeAtInit(self):
90 [EDF28648] : allow initialisation script at startup
93 with tempfile.NamedTemporaryFile() as tmpFileName:
95 salome.cm.SetCodeOnContainerStartUp("""
96 with open("{}","w") as f:
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
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
107 if __name__ == '__main__':