1 // Copyright (C) 2007-2022 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"
27 #include "Basics_Utils.hxx"
28 #include "Basics_DirUtils.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
53 LifeCycleCORBATest::_getTraceFileName()
55 std::string dir = Kernel_Utils::GetTmpDir();
56 return dir + "traceUnitTest-LifeCycleCORBATest.log";
59 // ============================================================================
62 * - delete preexisting trace classes if any
64 * Get or initialize the orb
65 * Create a SALOME_NamingService instance
67 // ============================================================================
70 LifeCycleCORBATest::setUp()
72 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
74 bp1->deleteInstance(bp1);
77 std::string theFileName = _getTraceFileName();
79 std::string s = "file:";
83 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
85 std::ofstream traceFile;
86 // traceFile.open(theFileName, std::ios::out | std::ios::trunc);
87 traceFile.open(theFileName.c_str(), std::ios::out | std::ios::app);
88 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
91 bp1 = LocalTraceBufferPool::instance();
94 // --- Get or initialize the orb
96 _orb = KERNEL::GetRefToORB();
98 // --- Create a SALOME_NamingService instance
103 // ============================================================================
105 * - delete trace classes
107 // ============================================================================
110 LifeCycleCORBATest::tearDown()
113 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
115 bp1->deleteInstance(bp1);
118 // ============================================================================
120 * Check FindOrLoad_Component.
121 * - get a local container (no hostname given),
122 * load an engine, check that the CORBA object is not null.
125 // ============================================================================
128 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
130 SALOME_LifeCycleCORBA _LCC(&_NS);
132 // --- get a local container,
133 // load an engine, check that the CORBA object is not null
135 std::string containerName = "myContainer";
136 Engines::EngineComponent_var mycompo =
137 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
138 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
142 Engines::TestComponent_var m1;
143 m1 = Engines::TestComponent::_narrow(mycompo);
144 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
147 // ============================================================================
149 * Check FindOrLoad_Component.
150 * - Call 2 times FindOrLoad_Component with the same parameters
151 * - check if we get the same engine
153 // ============================================================================
156 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
158 SALOME_LifeCycleCORBA _LCC(&_NS);
160 // --- get a local container,
161 // load an engine, check that the CORBA object is not null
163 std::string containerName = "myContainer";
165 Engines::EngineComponent_var mycompo1 =
166 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
167 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
169 Engines::EngineComponent_var mycompo2 =
170 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
171 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
175 Engines::TestComponent_var m1;
176 m1 = Engines::TestComponent::_narrow(mycompo1);
177 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
179 Engines::TestComponent_var m2;
180 m2 = Engines::TestComponent::_narrow(mycompo2);
181 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
183 // --- check equality of instance names
185 std::string name1 = m1->instanceName();
186 std::string name2 = m2->instanceName();
187 CPPUNIT_ASSERT_EQUAL(name1, name2);
190 // ============================================================================
192 * Check FindOrLoad_Component with Python Component on C++ Container
193 * load an engine, check that the CORBA object is not null.
196 // ============================================================================
199 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
201 SALOME_LifeCycleCORBA _LCC(&_NS);
203 // --- get a local container,
204 // load an engine, check that the CORBA object is not null
206 std::string containerName = "myContainer";
208 Engines::EngineComponent_var mycompo1 =
209 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
210 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
214 Engines::TestComponent_var m1;
215 m1 = Engines::TestComponent::_narrow(mycompo1);
216 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
219 // ============================================================================
221 * Check FindOrLoad_Component with Python Component on C++ Container
222 * - Call 2 times FindOrLoad_Component with the same parameters
223 * - check if we get the same engine
225 // ============================================================================
228 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
230 SALOME_LifeCycleCORBA _LCC(&_NS);
232 // --- get a local container (with a name based on local hostname),
233 // load an engine, check that the CORBA object is not null
235 std::string containerName = "myContainer";
237 Engines::EngineComponent_var mycompo1 =
238 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
239 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
241 Engines::EngineComponent_var mycompo2 =
242 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
243 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
247 Engines::TestComponent_var m1;
248 m1 = Engines::TestComponent::_narrow(mycompo1);
249 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
251 Engines::TestComponent_var m2;
252 m2 = Engines::TestComponent::_narrow(mycompo2);
253 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
255 // --- check equality of instance names
257 std::string name1 = m1->instanceName();
258 std::string name2 = m2->instanceName();
259 CPPUNIT_ASSERT_EQUAL(name1, name2);
262 // ============================================================================
264 * Check FindOrLoad_Component with a component name not in catalog.
265 * See list of catalog given to module catalog server.
266 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
267 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
269 // ============================================================================
272 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
274 SALOME_LifeCycleCORBA _LCC(&_NS);
276 // --- get a local container (with a name based on local hostname),
277 // load an engine, check that the CORBA object is not null
279 std::string containerName = "myContainer";
281 Engines::EngineComponent_var mycompo1 =
282 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
283 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
286 // ============================================================================
288 * Check FindOrLoad_Component with hostname given
289 * - get a local container : getHostName()/componentName,
290 * load an engine, check that the CORBA object is not null.
293 // ============================================================================
296 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
298 SALOME_LifeCycleCORBA _LCC(&_NS);
300 // --- get a local container (with a name based on local hostname),
301 // load an engine, check that the CORBA object is not null
303 std::string containerName = Kernel_Utils::GetHostname();
304 containerName += "/theContainer";
305 DEVTRACE("containerName = " << containerName);
306 Engines::EngineComponent_var mycompo =
307 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
308 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
312 Engines::TestComponent_var m1;
313 m1 = Engines::TestComponent::_narrow(mycompo);
314 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
317 // ============================================================================
319 * Check FindOrLoad_Component with and without local hostname given.
320 * We must get the same container, the same instance of component
322 // ============================================================================
325 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
327 SALOME_LifeCycleCORBA _LCC(&_NS);
329 // --- get a local container (with a name based on local hostname),
330 // load an engine, check that the CORBA object is not null
332 std::string containerName = "aContainer";
334 Engines::EngineComponent_var mycompo1 =
335 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
336 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
338 containerName = Kernel_Utils::GetHostname();
339 containerName += "/aContainer";
340 DEVTRACE("containerName = " << containerName);
341 Engines::EngineComponent_var mycompo2 =
342 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
343 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
347 Engines::TestComponent_var m1;
348 m1 = Engines::TestComponent::_narrow(mycompo1);
349 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
351 Engines::TestComponent_var m2;
352 m2 = Engines::TestComponent::_narrow(mycompo2);
353 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
355 // --- check equality of instance names
357 std::string name1 = m1->instanceName();
358 std::string name2 = m2->instanceName();
359 CPPUNIT_ASSERT_EQUAL(name1, name2);
361 // --- check containers are the same servant (same container name+hostname)
363 Engines::Container_var c1 = m1->GetContainerRef();
364 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
365 Engines::Container_var c2 = m2->GetContainerRef();
366 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
367 std::string cname1 = c1->name();
368 std::string cname2 = c2->name();
369 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
370 std::string hostname1 = c1->getHostName();
371 std::string hostname2 = c2->getHostName();
372 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
373 CORBA::Long pidc1 = c1->getPID();
374 CORBA::Long pidc2 = c2->getPID();
375 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
378 // ============================================================================
380 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
381 * We must catch a Salome Exception with "unknown host" message
383 // ============================================================================
386 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
388 SALOME_LifeCycleCORBA _LCC(&_NS);
390 // --- try to get a distant container on an unknown machine (not existing)
391 // check that the CORBA object is null
393 std::string containerName = "aFarAwayComputer";
394 containerName += "/theContainer";
395 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
396 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
399 Engines::EngineComponent_var mycompo =
400 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
402 catch(const SALOME::SALOME_Exception &ex)
404 CPPUNIT_ASSERT(true);
405 // std::string expectedMessage = "BAD PARAM";
408 // std::string actualMessage = os.str();
409 // DEVTRACE("actual Exception Message = " << actualMessage);
410 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
414 // ============================================================================
416 * Check FindOrLoad_Component, empty params must give an instance
418 // ============================================================================
421 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
423 SALOME_LifeCycleCORBA _LCC(&_NS);
425 Engines::ContainerParameters params;
427 Engines::EngineComponent_var mycompo =
428 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
429 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
433 Engines::TestComponent_var m1;
434 m1 = Engines::TestComponent::_narrow(mycompo);
437 // ============================================================================
439 * Check FindOrLoad_Component params = local container
441 // ============================================================================
444 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
446 SALOME_LifeCycleCORBA _LCC(&_NS);
448 Engines::ContainerParameters params;
450 std::string hostname=Kernel_Utils::GetHostname();
451 params.resource_params.hostname=hostname.c_str();
452 Engines::EngineComponent_var mycompo =
453 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
454 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
458 Engines::TestComponent_var m1;
459 m1 = Engines::TestComponent::_narrow(mycompo);
461 // --- check that container is on local computer
463 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
464 Engines::Container_var c1 = m1->GetContainerRef();
465 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
466 std::string hostname1 = c1->getHostName();
467 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
471 // ============================================================================
473 * Check FindOrLoad_Component params = containerName
475 // ============================================================================
478 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
480 SALOME_LifeCycleCORBA _LCC(&_NS);
482 Engines::ContainerParameters params;
484 std::string containerName = "myContainer";
485 params.container_name = containerName.c_str();
486 Engines::EngineComponent_var mycompo =
487 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
488 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
492 Engines::TestComponent_var m1;
493 m1 = Engines::TestComponent::_narrow(mycompo);
495 // --- check that container has good name
497 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
498 Engines::Container_var c1 = m1->GetContainerRef();
499 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
500 std::string hostname1 = c1->getHostName();
501 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
502 std::string cname1 = c1->name();
503 CPPUNIT_ASSERT(cname1.find(containerName) != std::string::npos);
506 // ============================================================================
508 * Check FindOrLoad_Component on remote computer
510 // ============================================================================
513 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
515 SALOME_LifeCycleCORBA _LCC(&_NS);
517 std::string remoteHost = GetRemoteHost();
519 std::string containerName = remoteHost;
520 containerName += "/aContainer";
521 DEVTRACE("containerName = " << containerName);
522 Engines::EngineComponent_var mycompo1 =
523 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
524 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
525 "computers from ResourcesCatalog",
526 !CORBA::is_nil(mycompo1));
530 Engines::TestComponent_var m1;
531 m1 = Engines::TestComponent::_narrow(mycompo1);
533 // --- check that container is on good computer
535 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
536 Engines::Container_var c1 = m1->GetContainerRef();
537 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
538 std::string hostname1 = c1->getHostName();
539 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
542 // ============================================================================
544 * Check FindOrLoad_Component with params on remote computer
545 * params empty except hostname
547 // ============================================================================
550 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
552 SALOME_LifeCycleCORBA _LCC(&_NS);
554 std::string remoteHost = GetRemoteHost();
556 Engines::ContainerParameters params;
558 params.resource_params.hostname = remoteHost.c_str();
560 Engines::EngineComponent_var mycompo1 =
561 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
562 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
563 "computers from ResourcesCatalog",
564 !CORBA::is_nil(mycompo1));
568 Engines::TestComponent_var m1;
569 m1 = Engines::TestComponent::_narrow(mycompo1);
571 // --- check that container is on good computer
573 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
574 Engines::Container_var c1 = m1->GetContainerRef();
575 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
576 std::string hostname1 = c1->getHostName();
577 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
580 // ============================================================================
582 * Check FindOrLoad_Component with params on remote computer
583 * params empty except hostname and container_name
585 // ============================================================================
588 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
590 SALOME_LifeCycleCORBA _LCC(&_NS);
592 std::string remoteHost = GetRemoteHost();
594 Engines::ContainerParameters params;
596 params.resource_params.hostname = remoteHost.c_str();
597 params.container_name = "anotherContainer";
599 Engines::EngineComponent_var mycompo1 =
600 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
601 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
602 "computers from ResourcesCatalog",
603 !CORBA::is_nil(mycompo1));
607 Engines::TestComponent_var m1;
608 m1 = Engines::TestComponent::_narrow(mycompo1);
610 // --- check that container is on good computer
612 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
613 Engines::Container_var c1 = m1->GetContainerRef();
614 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
615 std::string hostname1 = c1->getHostName();
616 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
619 // ============================================================================
621 * Check SALOME_FileTransferCORBA on local machine
623 // ============================================================================
625 void LifeCycleCORBATest::testgetLocalFile_localComputer()
627 SALOME_LifeCycleCORBA _LCC(&_NS);
628 std::string origFileName = getenv("KERNEL_ROOT_DIR");
630 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.dylib";
632 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.so.0.0.0";
634 SALOME_FileTransferCORBA transfer( Kernel_Utils::GetHostname(),
636 std::string local = transfer.getLocalFile();
637 CPPUNIT_ASSERT(!local.empty());
638 CPPUNIT_ASSERT_EQUAL(local, origFileName);
641 // ============================================================================
643 * Check SALOME_FileTransferCORBA on remote machine
645 // ============================================================================
647 void LifeCycleCORBATest::testgetLocalFile_remoteComputer()
649 SALOME_LifeCycleCORBA _LCC(&_NS);
650 std::string origFileName = getenv("KERNEL_ROOT_DIR");
652 origFileName += "/lib/salome/libSalomeContainer.dylib";
654 origFileName += "/lib/salome/libSalomeContainer.so.0.0.0";
656 SALOME_FileTransferCORBA transfer( GetRemoteHost(),
658 std::string local = transfer.getLocalFile();
659 CPPUNIT_ASSERT(!local.empty());
660 std::string local2 = transfer.getLocalFile();
661 CPPUNIT_ASSERT(!local2.empty());
662 CPPUNIT_ASSERT_EQUAL(local, local2);
665 // ============================================================================
667 * Check FindOrLoad_Component params = containerName
669 // ============================================================================
672 // LifeCycleCORBATest::testFindOrLoad_Component_()
679 // ============================================================================
681 * Get a remote HostName in the Resource Catalog
683 // ============================================================================
685 std::string LifeCycleCORBATest::GetRemoteHost()
687 SALOME_LifeCycleCORBA _LCC(&_NS);
689 CORBA::Object_var obj = _NS.Resolve("/ResourcesManager");
690 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
691 Engines::ResourcesManager_var resourcesManager =
692 Engines::ResourcesManager::_narrow(obj);
693 CPPUNIT_ASSERT(!CORBA::is_nil(resourcesManager));
695 Engines::ContainerParameters params;
696 _LCC.preSet(params); // empty params to get all the machines
697 params.resource_params.componentList.length(1);
698 params.resource_params.componentList[0]="SalomeTestComponent";
699 params.resource_params.can_run_containers = true;
701 Engines::ResourceList_var hostList = resourcesManager->GetFittingResources(params.resource_params);
702 CPPUNIT_ASSERT(hostList->length() > 1);
704 std::string localHost = Kernel_Utils::GetHostname();
705 std::string remoteHost;
706 for (unsigned int i=0; i < hostList->length(); i++)
708 const char* aMachine = hostList[i];
709 Engines::ResourceDefinition_var resource_definition = resourcesManager->GetResourceDefinition(aMachine);
710 std::string machine(resource_definition->hostname.in());
711 if (machine != localHost)
713 remoteHost = machine;
717 CPPUNIT_ASSERT(remoteHost != "");