1 // Copyright (C) 2007-2013 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.
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"
36 // --- uncomment to have some traces on standard error
37 // (useful only when adding new tests...)
41 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
42 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
48 #define setenv Kernel_Utils::setenv
51 #define TRACEFILE "/tmp/traceUnitTest.log"
53 // ============================================================================
56 * - delete preexisting trace classes if any
58 * Get or initialize the orb
59 * Create a SALOME_NamingService instance
61 // ============================================================================
64 LifeCycleCORBATest::setUp()
66 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
68 bp1->deleteInstance(bp1);
71 const char *theFileName = TRACEFILE;
73 std::string s = "file:";
77 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
79 std::ofstream traceFile;
80 // traceFile.open(theFileName, std::ios::out | std::ios::trunc);
81 traceFile.open(theFileName, std::ios::out | std::ios::app);
82 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
85 bp1 = LocalTraceBufferPool::instance();
88 // --- Get or initialize the orb
91 char* _argv[] = {(char*)""};
92 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
93 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
94 _orb = init(_argc , _argv ) ;
96 // --- Create a SALOME_NamingService instance
101 // ============================================================================
103 * - delete trace classes
105 // ============================================================================
108 LifeCycleCORBATest::tearDown()
111 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
113 bp1->deleteInstance(bp1);
116 // ============================================================================
118 * Check FindOrLoad_Component.
119 * - get a local container (no hostname given),
120 * load an engine, check that the CORBA object is not null.
123 // ============================================================================
126 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
128 SALOME_LifeCycleCORBA _LCC(&_NS);
130 // --- get a local container,
131 // load an engine, check that the CORBA object is not null
133 std::string containerName = "myContainer";
134 Engines::EngineComponent_var mycompo =
135 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
136 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
140 Engines::TestComponent_var m1;
141 m1 = Engines::TestComponent::_narrow(mycompo);
142 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
145 // ============================================================================
147 * Check FindOrLoad_Component.
148 * - Call 2 times FindOrLoad_Component with the same parameters
149 * - check if we get the same engine
151 // ============================================================================
154 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
156 SALOME_LifeCycleCORBA _LCC(&_NS);
158 // --- get a local container,
159 // load an engine, check that the CORBA object is not null
161 std::string containerName = "myContainer";
163 Engines::EngineComponent_var mycompo1 =
164 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
165 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
167 Engines::EngineComponent_var mycompo2 =
168 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
169 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
173 Engines::TestComponent_var m1;
174 m1 = Engines::TestComponent::_narrow(mycompo1);
175 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
177 Engines::TestComponent_var m2;
178 m2 = Engines::TestComponent::_narrow(mycompo2);
179 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
181 // --- check equality of instance names
183 std::string name1 = m1->instanceName();
184 std::string name2 = m2->instanceName();
185 CPPUNIT_ASSERT_EQUAL(name1, name2);
188 // ============================================================================
190 * Check FindOrLoad_Component with Python Component on C++ Container
191 * load an engine, check that the CORBA object is not null.
194 // ============================================================================
197 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
199 SALOME_LifeCycleCORBA _LCC(&_NS);
201 // --- get a local container,
202 // load an engine, check that the CORBA object is not null
204 std::string containerName = "myContainer";
206 Engines::EngineComponent_var mycompo1 =
207 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
208 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
212 Engines::TestComponent_var m1;
213 m1 = Engines::TestComponent::_narrow(mycompo1);
214 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
217 // ============================================================================
219 * Check FindOrLoad_Component with Python Component on C++ Container
220 * - Call 2 times FindOrLoad_Component with the same parameters
221 * - check if we get the same engine
223 // ============================================================================
226 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
228 SALOME_LifeCycleCORBA _LCC(&_NS);
230 // --- get a local container (with a name based on local hostname),
231 // load an engine, check that the CORBA object is not null
233 std::string containerName = "myContainer";
235 Engines::EngineComponent_var mycompo1 =
236 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
237 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
239 Engines::EngineComponent_var mycompo2 =
240 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
241 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
245 Engines::TestComponent_var m1;
246 m1 = Engines::TestComponent::_narrow(mycompo1);
247 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
249 Engines::TestComponent_var m2;
250 m2 = Engines::TestComponent::_narrow(mycompo2);
251 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
253 // --- check equality of instance names
255 std::string name1 = m1->instanceName();
256 std::string name2 = m2->instanceName();
257 CPPUNIT_ASSERT_EQUAL(name1, name2);
260 // ============================================================================
262 * Check FindOrLoad_Component with a component name not in catalog.
263 * See list of catalog given to module catalog server.
264 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
265 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
267 // ============================================================================
270 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
272 SALOME_LifeCycleCORBA _LCC(&_NS);
274 // --- get a local container (with a name based on local hostname),
275 // load an engine, check that the CORBA object is not null
277 std::string containerName = "myContainer";
279 Engines::EngineComponent_var mycompo1 =
280 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
281 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
284 // ============================================================================
286 * Check FindOrLoad_Component with hostname given
287 * - get a local container : getHostName()/componentName,
288 * load an engine, check that the CORBA object is not null.
291 // ============================================================================
294 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
296 SALOME_LifeCycleCORBA _LCC(&_NS);
298 // --- get a local container (with a name based on local hostname),
299 // load an engine, check that the CORBA object is not null
301 std::string containerName = Kernel_Utils::GetHostname();
302 containerName += "/theContainer";
303 DEVTRACE("containerName = " << containerName);
304 Engines::EngineComponent_var mycompo =
305 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
306 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
310 Engines::TestComponent_var m1;
311 m1 = Engines::TestComponent::_narrow(mycompo);
312 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
315 // ============================================================================
317 * Check FindOrLoad_Component with and without local hostname given.
318 * We must get the same container, the same instance of component
320 // ============================================================================
323 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
325 SALOME_LifeCycleCORBA _LCC(&_NS);
327 // --- get a local container (with a name based on local hostname),
328 // load an engine, check that the CORBA object is not null
330 std::string containerName = "aContainer";
332 Engines::EngineComponent_var mycompo1 =
333 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
334 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
336 containerName = Kernel_Utils::GetHostname();
337 containerName += "/aContainer";
338 DEVTRACE("containerName = " << containerName);
339 Engines::EngineComponent_var mycompo2 =
340 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
341 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
345 Engines::TestComponent_var m1;
346 m1 = Engines::TestComponent::_narrow(mycompo1);
347 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
349 Engines::TestComponent_var m2;
350 m2 = Engines::TestComponent::_narrow(mycompo2);
351 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
353 // --- check equality of instance names
355 std::string name1 = m1->instanceName();
356 std::string name2 = m2->instanceName();
357 CPPUNIT_ASSERT_EQUAL(name1, name2);
359 // --- check containers are the same servant (same container name+hostname)
361 Engines::Container_var c1 = m1->GetContainerRef();
362 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
363 Engines::Container_var c2 = m2->GetContainerRef();
364 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
365 std::string cname1 = c1->name();
366 std::string cname2 = c2->name();
367 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
368 std::string hostname1 = c1->getHostName();
369 std::string hostname2 = c2->getHostName();
370 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
371 CORBA::Long pidc1 = c1->getPID();
372 CORBA::Long pidc2 = c2->getPID();
373 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
376 // ============================================================================
378 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
379 * We must catch a Salome Exception with "unknown host" message
381 // ============================================================================
384 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
386 SALOME_LifeCycleCORBA _LCC(&_NS);
388 // --- try to get a distant container on an unknown machine (not existing)
389 // check that the CORBA object is null
391 std::string containerName = "aFarAwayComputer";
392 containerName += "/theContainer";
393 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
394 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
397 Engines::EngineComponent_var mycompo =
398 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
400 catch(const SALOME::SALOME_Exception &ex)
402 CPPUNIT_ASSERT(true);
403 // std::string expectedMessage = "BAD PARAM";
406 // std::string actualMessage = os.str();
407 // DEVTRACE("actual Exception Message = " << actualMessage);
408 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
412 // ============================================================================
414 * Check FindOrLoad_Component, empty params must give an instance
416 // ============================================================================
419 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
421 SALOME_LifeCycleCORBA _LCC(&_NS);
423 Engines::ContainerParameters params;
425 Engines::EngineComponent_var mycompo =
426 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
427 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
431 Engines::TestComponent_var m1;
432 m1 = Engines::TestComponent::_narrow(mycompo);
435 // ============================================================================
437 * Check FindOrLoad_Component params = local container
439 // ============================================================================
442 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
444 SALOME_LifeCycleCORBA _LCC(&_NS);
446 Engines::ContainerParameters params;
448 std::string hostname=Kernel_Utils::GetHostname();
449 params.resource_params.hostname=hostname.c_str();
450 Engines::EngineComponent_var mycompo =
451 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
452 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
456 Engines::TestComponent_var m1;
457 m1 = Engines::TestComponent::_narrow(mycompo);
459 // --- check that container is on local computer
461 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
462 Engines::Container_var c1 = m1->GetContainerRef();
463 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
464 std::string hostname1 = c1->getHostName();
465 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
469 // ============================================================================
471 * Check FindOrLoad_Component params = containerName
473 // ============================================================================
476 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
478 SALOME_LifeCycleCORBA _LCC(&_NS);
480 Engines::ContainerParameters params;
482 std::string containerName = "myContainer";
483 params.container_name = containerName.c_str();
484 Engines::EngineComponent_var mycompo =
485 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
486 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
490 Engines::TestComponent_var m1;
491 m1 = Engines::TestComponent::_narrow(mycompo);
493 // --- check that container has good name
495 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
496 Engines::Container_var c1 = m1->GetContainerRef();
497 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
498 std::string hostname1 = c1->getHostName();
499 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
500 std::string cname1 = c1->name();
501 CPPUNIT_ASSERT(cname1.find(containerName) != std::string::npos);
504 // ============================================================================
506 * Check FindOrLoad_Component on remote computer
508 // ============================================================================
511 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
513 SALOME_LifeCycleCORBA _LCC(&_NS);
515 std::string remoteHost = GetRemoteHost();
517 std::string containerName = remoteHost;
518 containerName += "/aContainer";
519 DEVTRACE("containerName = " << containerName);
520 Engines::EngineComponent_var mycompo1 =
521 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
522 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
523 "computers from ResourcesCatalog",
524 !CORBA::is_nil(mycompo1));
528 Engines::TestComponent_var m1;
529 m1 = Engines::TestComponent::_narrow(mycompo1);
531 // --- check that container is on good computer
533 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
534 Engines::Container_var c1 = m1->GetContainerRef();
535 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
536 std::string hostname1 = c1->getHostName();
537 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
540 // ============================================================================
542 * Check FindOrLoad_Component with params on remote computer
543 * params empty except hostname
545 // ============================================================================
548 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
550 SALOME_LifeCycleCORBA _LCC(&_NS);
552 std::string remoteHost = GetRemoteHost();
554 Engines::ContainerParameters params;
556 params.resource_params.hostname = remoteHost.c_str();
558 Engines::EngineComponent_var mycompo1 =
559 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
560 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
561 "computers from ResourcesCatalog",
562 !CORBA::is_nil(mycompo1));
566 Engines::TestComponent_var m1;
567 m1 = Engines::TestComponent::_narrow(mycompo1);
569 // --- check that container is on good computer
571 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
572 Engines::Container_var c1 = m1->GetContainerRef();
573 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
574 std::string hostname1 = c1->getHostName();
575 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
578 // ============================================================================
580 * Check FindOrLoad_Component with params on remote computer
581 * params empty except hostname and container_name
583 // ============================================================================
586 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
588 SALOME_LifeCycleCORBA _LCC(&_NS);
590 std::string remoteHost = GetRemoteHost();
592 Engines::ContainerParameters params;
594 params.resource_params.hostname = remoteHost.c_str();
595 params.container_name = "anotherContainer";
597 Engines::EngineComponent_var mycompo1 =
598 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
599 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
600 "computers from ResourcesCatalog",
601 !CORBA::is_nil(mycompo1));
605 Engines::TestComponent_var m1;
606 m1 = Engines::TestComponent::_narrow(mycompo1);
608 // --- check that container is on good computer
610 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
611 Engines::Container_var c1 = m1->GetContainerRef();
612 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
613 std::string hostname1 = c1->getHostName();
614 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
617 // ============================================================================
619 * Check SALOME_FileTransferCORBA on local machine
621 // ============================================================================
623 void LifeCycleCORBATest::testgetLocalFile_localComputer()
625 SALOME_LifeCycleCORBA _LCC(&_NS);
626 std::string origFileName = getenv("KERNEL_ROOT_DIR");
627 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.so.0.0.0";
628 SALOME_FileTransferCORBA transfer( Kernel_Utils::GetHostname(),
630 std::string local = transfer.getLocalFile();
631 CPPUNIT_ASSERT(!local.empty());
632 CPPUNIT_ASSERT_EQUAL(local, origFileName);
635 // ============================================================================
637 * Check SALOME_FileTransferCORBA on remote machine
639 // ============================================================================
641 void LifeCycleCORBATest::testgetLocalFile_remoteComputer()
643 SALOME_LifeCycleCORBA _LCC(&_NS);
644 std::string origFileName = getenv("KERNEL_ROOT_DIR");
645 origFileName += "/lib/salome/libSalomeContainer.so.0.0.0";
646 SALOME_FileTransferCORBA transfer( GetRemoteHost(),
648 std::string local = transfer.getLocalFile();
649 CPPUNIT_ASSERT(!local.empty());
650 std::string local2 = transfer.getLocalFile();
651 CPPUNIT_ASSERT(!local2.empty());
652 CPPUNIT_ASSERT_EQUAL(local, local2);
655 // ============================================================================
657 * Check FindOrLoad_Component params = containerName
659 // ============================================================================
662 // LifeCycleCORBATest::testFindOrLoad_Component_()
669 // ============================================================================
671 * Get a remote HostName in the Resource Catalog
673 // ============================================================================
675 std::string LifeCycleCORBATest::GetRemoteHost()
677 SALOME_LifeCycleCORBA _LCC(&_NS);
679 CORBA::Object_var obj = _NS.Resolve("/ResourcesManager");
680 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
681 Engines::ResourcesManager_var resourcesManager =
682 Engines::ResourcesManager::_narrow(obj);
683 CPPUNIT_ASSERT(!CORBA::is_nil(resourcesManager));
685 Engines::ContainerParameters params;
686 _LCC.preSet(params); // empty params to get all the machines
687 params.resource_params.componentList.length(1);
688 params.resource_params.componentList[0]="SalomeTestComponent";
689 params.resource_params.can_run_containers = true;
691 Engines::ResourceList_var hostList = resourcesManager->GetFittingResources(params.resource_params);
692 CPPUNIT_ASSERT(hostList->length() > 1);
694 std::string localHost = Kernel_Utils::GetHostname();
695 std::string remoteHost;
696 for (unsigned int i=0; i < hostList->length(); i++)
698 const char* aMachine = hostList[i];
699 Engines::ResourceDefinition_var resource_definition = resourcesManager->GetResourceDefinition(aMachine);
700 std::string machine(resource_definition->hostname.in());
701 if (machine != localHost)
703 remoteHost = machine;
707 CPPUNIT_ASSERT(remoteHost != "");