1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "LifeCycleCORBATest.hxx"
24 #include "SALOME_LifeCycleCORBA.hxx"
25 #include "SALOME_FileTransferCORBA.hxx"
26 #include "Utils_ORB_INIT.hxx"
27 #include "Utils_SINGLETON.hxx"
28 #include "Basics_Utils.hxx"
29 #include "utilities.h"
37 // --- uncomment to have some traces on standard error
38 // (useful only when adding new tests...)
42 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
43 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
49 #define setenv Kernel_Utils::setenv
52 #define TRACEFILE "/tmp/traceUnitTest.log"
54 // ============================================================================
57 * - delete preexisting trace classes if any
59 * Get or initialize the orb
60 * Create a SALOME_NamingService instance
62 // ============================================================================
65 LifeCycleCORBATest::setUp()
67 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
69 bp1->deleteInstance(bp1);
72 const char *theFileName = TRACEFILE;
74 std::string s = "file:";
78 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
80 std::ofstream traceFile;
81 // traceFile.open(theFileName, std::ios::out | std::ios::trunc);
82 traceFile.open(theFileName, std::ios::out | std::ios::app);
83 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
86 bp1 = LocalTraceBufferPool::instance();
89 // --- Get or initialize the orb
92 char* _argv[] = {(char*)""};
93 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
94 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
95 _orb = init(_argc , _argv ) ;
97 // --- Create a SALOME_NamingService instance
102 // ============================================================================
104 * - delete trace classes
106 // ============================================================================
109 LifeCycleCORBATest::tearDown()
112 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
114 bp1->deleteInstance(bp1);
117 // ============================================================================
119 * Check FindOrLoad_Component.
120 * - get a local container (no hostname given),
121 * load an engine, check that the CORBA object is not null.
124 // ============================================================================
127 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
129 SALOME_LifeCycleCORBA _LCC(&_NS);
131 // --- get a local container,
132 // load an engine, check that the CORBA object is not null
134 std::string containerName = "myContainer";
135 Engines::EngineComponent_var mycompo =
136 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
137 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
141 Engines::TestComponent_var m1;
142 m1 = Engines::TestComponent::_narrow(mycompo);
143 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
146 // ============================================================================
148 * Check FindOrLoad_Component.
149 * - Call 2 times FindOrLoad_Component with the same parameters
150 * - check if we get the same engine
152 // ============================================================================
155 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
157 SALOME_LifeCycleCORBA _LCC(&_NS);
159 // --- get a local container,
160 // load an engine, check that the CORBA object is not null
162 std::string containerName = "myContainer";
164 Engines::EngineComponent_var mycompo1 =
165 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
166 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
168 Engines::EngineComponent_var mycompo2 =
169 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
170 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
174 Engines::TestComponent_var m1;
175 m1 = Engines::TestComponent::_narrow(mycompo1);
176 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
178 Engines::TestComponent_var m2;
179 m2 = Engines::TestComponent::_narrow(mycompo2);
180 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
182 // --- check equality of instance names
184 std::string name1 = m1->instanceName();
185 std::string name2 = m2->instanceName();
186 CPPUNIT_ASSERT_EQUAL(name1, name2);
189 // ============================================================================
191 * Check FindOrLoad_Component with Python Component on C++ Container
192 * load an engine, check that the CORBA object is not null.
195 // ============================================================================
198 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
200 SALOME_LifeCycleCORBA _LCC(&_NS);
202 // --- get a local container,
203 // load an engine, check that the CORBA object is not null
205 std::string containerName = "myContainer";
207 Engines::EngineComponent_var mycompo1 =
208 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
209 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
213 Engines::TestComponent_var m1;
214 m1 = Engines::TestComponent::_narrow(mycompo1);
215 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
218 // ============================================================================
220 * Check FindOrLoad_Component with Python Component on C++ Container
221 * - Call 2 times FindOrLoad_Component with the same parameters
222 * - check if we get the same engine
224 // ============================================================================
227 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
229 SALOME_LifeCycleCORBA _LCC(&_NS);
231 // --- get a local container (with a name based on local hostname),
232 // load an engine, check that the CORBA object is not null
234 std::string containerName = "myContainer";
236 Engines::EngineComponent_var mycompo1 =
237 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
238 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
240 Engines::EngineComponent_var mycompo2 =
241 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
242 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
246 Engines::TestComponent_var m1;
247 m1 = Engines::TestComponent::_narrow(mycompo1);
248 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
250 Engines::TestComponent_var m2;
251 m2 = Engines::TestComponent::_narrow(mycompo2);
252 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
254 // --- check equality of instance names
256 std::string name1 = m1->instanceName();
257 std::string name2 = m2->instanceName();
258 CPPUNIT_ASSERT_EQUAL(name1, name2);
261 // ============================================================================
263 * Check FindOrLoad_Component with a component name not in catalog.
264 * See list of catalog given to module catalog server.
265 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
266 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
268 // ============================================================================
271 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
273 SALOME_LifeCycleCORBA _LCC(&_NS);
275 // --- get a local container (with a name based on local hostname),
276 // load an engine, check that the CORBA object is not null
278 std::string containerName = "myContainer";
280 Engines::EngineComponent_var mycompo1 =
281 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
282 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
285 // ============================================================================
287 * Check FindOrLoad_Component with hostname given
288 * - get a local container : getHostName()/componentName,
289 * load an engine, check that the CORBA object is not null.
292 // ============================================================================
295 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
297 SALOME_LifeCycleCORBA _LCC(&_NS);
299 // --- get a local container (with a name based on local hostname),
300 // load an engine, check that the CORBA object is not null
302 std::string containerName = Kernel_Utils::GetHostname();
303 containerName += "/theContainer";
304 DEVTRACE("containerName = " << containerName);
305 Engines::EngineComponent_var mycompo =
306 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
307 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
311 Engines::TestComponent_var m1;
312 m1 = Engines::TestComponent::_narrow(mycompo);
313 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
316 // ============================================================================
318 * Check FindOrLoad_Component with and without local hostname given.
319 * We must get the same container, the same instance of component
321 // ============================================================================
324 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
326 SALOME_LifeCycleCORBA _LCC(&_NS);
328 // --- get a local container (with a name based on local hostname),
329 // load an engine, check that the CORBA object is not null
331 std::string containerName = "aContainer";
333 Engines::EngineComponent_var mycompo1 =
334 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
335 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
337 containerName = Kernel_Utils::GetHostname();
338 containerName += "/aContainer";
339 DEVTRACE("containerName = " << containerName);
340 Engines::EngineComponent_var mycompo2 =
341 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
342 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
346 Engines::TestComponent_var m1;
347 m1 = Engines::TestComponent::_narrow(mycompo1);
348 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
350 Engines::TestComponent_var m2;
351 m2 = Engines::TestComponent::_narrow(mycompo2);
352 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
354 // --- check equality of instance names
356 std::string name1 = m1->instanceName();
357 std::string name2 = m2->instanceName();
358 CPPUNIT_ASSERT_EQUAL(name1, name2);
360 // --- check containers are the same servant (same container name+hostname)
362 Engines::Container_var c1 = m1->GetContainerRef();
363 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
364 Engines::Container_var c2 = m2->GetContainerRef();
365 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
366 std::string cname1 = c1->name();
367 std::string cname2 = c2->name();
368 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
369 std::string hostname1 = c1->getHostName();
370 std::string hostname2 = c2->getHostName();
371 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
372 CORBA::Long pidc1 = c1->getPID();
373 CORBA::Long pidc2 = c2->getPID();
374 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
377 // ============================================================================
379 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
380 * We must catch a Salome Exception with "unknown host" message
382 // ============================================================================
385 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
387 SALOME_LifeCycleCORBA _LCC(&_NS);
389 // --- try to get a distant container on an unknown machine (not existing)
390 // check that the CORBA object is null
392 std::string containerName = "aFarAwayComputer";
393 containerName += "/theContainer";
394 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
395 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
398 Engines::EngineComponent_var mycompo =
399 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
401 catch(const SALOME::SALOME_Exception &ex)
403 CPPUNIT_ASSERT(true);
404 // std::string expectedMessage = "BAD PARAM";
407 // std::string actualMessage = os.str();
408 // DEVTRACE("actual Exception Message = " << actualMessage);
409 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
413 // ============================================================================
415 * Check FindOrLoad_Component, empty params must give an instance
417 // ============================================================================
420 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
422 SALOME_LifeCycleCORBA _LCC(&_NS);
424 Engines::ContainerParameters params;
426 Engines::EngineComponent_var mycompo =
427 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
428 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
432 Engines::TestComponent_var m1;
433 m1 = Engines::TestComponent::_narrow(mycompo);
436 // ============================================================================
438 * Check FindOrLoad_Component params = local container
440 // ============================================================================
443 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
445 SALOME_LifeCycleCORBA _LCC(&_NS);
447 Engines::ContainerParameters params;
449 std::string hostname=Kernel_Utils::GetHostname();
450 params.resource_params.hostname=hostname.c_str();
451 Engines::EngineComponent_var mycompo =
452 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
453 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
457 Engines::TestComponent_var m1;
458 m1 = Engines::TestComponent::_narrow(mycompo);
460 // --- check that container is on local computer
462 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
463 Engines::Container_var c1 = m1->GetContainerRef();
464 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
465 std::string hostname1 = c1->getHostName();
466 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
470 // ============================================================================
472 * Check FindOrLoad_Component params = containerName
474 // ============================================================================
477 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
479 SALOME_LifeCycleCORBA _LCC(&_NS);
481 Engines::ContainerParameters params;
483 std::string containerName = "myContainer";
484 params.container_name = containerName.c_str();
485 Engines::EngineComponent_var mycompo =
486 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
487 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
491 Engines::TestComponent_var m1;
492 m1 = Engines::TestComponent::_narrow(mycompo);
494 // --- check that container has good name
496 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
497 Engines::Container_var c1 = m1->GetContainerRef();
498 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
499 std::string hostname1 = c1->getHostName();
500 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
501 std::string cname1 = c1->name();
502 CPPUNIT_ASSERT(cname1.find(containerName) != std::string::npos);
505 // ============================================================================
507 * Check FindOrLoad_Component on remote computer
509 // ============================================================================
512 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
514 SALOME_LifeCycleCORBA _LCC(&_NS);
516 std::string remoteHost = GetRemoteHost();
518 std::string containerName = remoteHost;
519 containerName += "/aContainer";
520 DEVTRACE("containerName = " << containerName);
521 Engines::EngineComponent_var mycompo1 =
522 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
523 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
524 "computers from ResourcesCatalog",
525 !CORBA::is_nil(mycompo1));
529 Engines::TestComponent_var m1;
530 m1 = Engines::TestComponent::_narrow(mycompo1);
532 // --- check that container is on good computer
534 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
535 Engines::Container_var c1 = m1->GetContainerRef();
536 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
537 std::string hostname1 = c1->getHostName();
538 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
541 // ============================================================================
543 * Check FindOrLoad_Component with params on remote computer
544 * params empty except hostname
546 // ============================================================================
549 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
551 SALOME_LifeCycleCORBA _LCC(&_NS);
553 std::string remoteHost = GetRemoteHost();
555 Engines::ContainerParameters params;
557 params.resource_params.hostname = remoteHost.c_str();
559 Engines::EngineComponent_var mycompo1 =
560 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
561 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
562 "computers from ResourcesCatalog",
563 !CORBA::is_nil(mycompo1));
567 Engines::TestComponent_var m1;
568 m1 = Engines::TestComponent::_narrow(mycompo1);
570 // --- check that container is on good computer
572 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
573 Engines::Container_var c1 = m1->GetContainerRef();
574 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
575 std::string hostname1 = c1->getHostName();
576 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
579 // ============================================================================
581 * Check FindOrLoad_Component with params on remote computer
582 * params empty except hostname and container_name
584 // ============================================================================
587 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
589 SALOME_LifeCycleCORBA _LCC(&_NS);
591 std::string remoteHost = GetRemoteHost();
593 Engines::ContainerParameters params;
595 params.resource_params.hostname = remoteHost.c_str();
596 params.container_name = "anotherContainer";
598 Engines::EngineComponent_var mycompo1 =
599 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
600 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
601 "computers from ResourcesCatalog",
602 !CORBA::is_nil(mycompo1));
606 Engines::TestComponent_var m1;
607 m1 = Engines::TestComponent::_narrow(mycompo1);
609 // --- check that container is on good computer
611 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
612 Engines::Container_var c1 = m1->GetContainerRef();
613 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
614 std::string hostname1 = c1->getHostName();
615 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
618 // ============================================================================
620 * Check SALOME_FileTransferCORBA on local machine
622 // ============================================================================
624 void LifeCycleCORBATest::testgetLocalFile_localComputer()
626 SALOME_LifeCycleCORBA _LCC(&_NS);
627 std::string origFileName = getenv("KERNEL_ROOT_DIR");
628 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.so.0.0.0";
629 SALOME_FileTransferCORBA transfer( Kernel_Utils::GetHostname(),
631 std::string local = transfer.getLocalFile();
632 CPPUNIT_ASSERT(!local.empty());
633 CPPUNIT_ASSERT_EQUAL(local, origFileName);
636 // ============================================================================
638 * Check SALOME_FileTransferCORBA on remote machine
640 // ============================================================================
642 void LifeCycleCORBATest::testgetLocalFile_remoteComputer()
644 SALOME_LifeCycleCORBA _LCC(&_NS);
645 std::string origFileName = getenv("KERNEL_ROOT_DIR");
646 origFileName += "/lib/salome/libSalomeContainer.so.0.0.0";
647 SALOME_FileTransferCORBA transfer( GetRemoteHost(),
649 std::string local = transfer.getLocalFile();
650 CPPUNIT_ASSERT(!local.empty());
651 std::string local2 = transfer.getLocalFile();
652 CPPUNIT_ASSERT(!local2.empty());
653 CPPUNIT_ASSERT_EQUAL(local, local2);
656 // ============================================================================
658 * Check FindOrLoad_Component params = containerName
660 // ============================================================================
663 // LifeCycleCORBATest::testFindOrLoad_Component_()
670 // ============================================================================
672 * Get a remote HostName in the Resource Catalog
674 // ============================================================================
676 std::string LifeCycleCORBATest::GetRemoteHost()
678 SALOME_LifeCycleCORBA _LCC(&_NS);
680 CORBA::Object_var obj = _NS.Resolve("/ResourcesManager");
681 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
682 Engines::ResourcesManager_var resourcesManager =
683 Engines::ResourcesManager::_narrow(obj);
684 CPPUNIT_ASSERT(!CORBA::is_nil(resourcesManager));
686 Engines::ContainerParameters params;
687 _LCC.preSet(params); // empty params to get all the machines
688 params.resource_params.componentList.length(1);
689 params.resource_params.componentList[0]="SalomeTestComponent";
690 params.resource_params.can_run_containers = true;
692 Engines::ResourceList_var hostList = resourcesManager->GetFittingResources(params.resource_params);
693 CPPUNIT_ASSERT(hostList->length() > 1);
695 std::string localHost = Kernel_Utils::GetHostname();
696 std::string remoteHost;
697 for (unsigned int i=0; i < hostList->length(); i++)
699 const char* aMachine = hostList[i];
700 Engines::ResourceDefinition_var resource_definition = resourcesManager->GetResourceDefinition(aMachine);
701 std::string machine(resource_definition->hostname.in());
702 if (machine != localHost)
704 remoteHost = machine;
708 CPPUNIT_ASSERT(remoteHost != "");