1 // Copyright (C) 2007-2016 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 "Basics_DirUtils.hxx"
30 #include "utilities.h"
38 // --- uncomment to have some traces on standard error
39 // (useful only when adding new tests...)
43 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
44 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
50 #define setenv Kernel_Utils::setenv
54 LifeCycleCORBATest::_getTraceFileName()
56 std::string dir = Kernel_Utils::GetTmpDir();
57 return dir + "traceUnitTest-LifeCycleCORBATest.log";
60 // ============================================================================
63 * - delete preexisting trace classes if any
65 * Get or initialize the orb
66 * Create a SALOME_NamingService instance
68 // ============================================================================
71 LifeCycleCORBATest::setUp()
73 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
75 bp1->deleteInstance(bp1);
78 std::string theFileName = _getTraceFileName();
80 std::string s = "file:";
84 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
86 std::ofstream traceFile;
87 // traceFile.open(theFileName, std::ios::out | std::ios::trunc);
88 traceFile.open(theFileName.c_str(), std::ios::out | std::ios::app);
89 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
92 bp1 = LocalTraceBufferPool::instance();
95 // --- Get or initialize the orb
98 char* _argv[] = {(char*)""};
99 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
100 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
101 _orb = init(_argc , _argv ) ;
103 // --- Create a SALOME_NamingService instance
108 // ============================================================================
110 * - delete trace classes
112 // ============================================================================
115 LifeCycleCORBATest::tearDown()
118 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
120 bp1->deleteInstance(bp1);
123 // ============================================================================
125 * Check FindOrLoad_Component.
126 * - get a local container (no hostname given),
127 * load an engine, check that the CORBA object is not null.
130 // ============================================================================
133 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
135 SALOME_LifeCycleCORBA _LCC(&_NS);
137 // --- get a local container,
138 // load an engine, check that the CORBA object is not null
140 std::string containerName = "myContainer";
141 Engines::EngineComponent_var mycompo =
142 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
143 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
147 Engines::TestComponent_var m1;
148 m1 = Engines::TestComponent::_narrow(mycompo);
149 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
152 // ============================================================================
154 * Check FindOrLoad_Component.
155 * - Call 2 times FindOrLoad_Component with the same parameters
156 * - check if we get the same engine
158 // ============================================================================
161 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
163 SALOME_LifeCycleCORBA _LCC(&_NS);
165 // --- get a local container,
166 // load an engine, check that the CORBA object is not null
168 std::string containerName = "myContainer";
170 Engines::EngineComponent_var mycompo1 =
171 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
172 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
174 Engines::EngineComponent_var mycompo2 =
175 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
176 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
180 Engines::TestComponent_var m1;
181 m1 = Engines::TestComponent::_narrow(mycompo1);
182 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
184 Engines::TestComponent_var m2;
185 m2 = Engines::TestComponent::_narrow(mycompo2);
186 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
188 // --- check equality of instance names
190 std::string name1 = m1->instanceName();
191 std::string name2 = m2->instanceName();
192 CPPUNIT_ASSERT_EQUAL(name1, name2);
195 // ============================================================================
197 * Check FindOrLoad_Component with Python Component on C++ Container
198 * load an engine, check that the CORBA object is not null.
201 // ============================================================================
204 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
206 SALOME_LifeCycleCORBA _LCC(&_NS);
208 // --- get a local container,
209 // load an engine, check that the CORBA object is not null
211 std::string containerName = "myContainer";
213 Engines::EngineComponent_var mycompo1 =
214 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
215 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
219 Engines::TestComponent_var m1;
220 m1 = Engines::TestComponent::_narrow(mycompo1);
221 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
224 // ============================================================================
226 * Check FindOrLoad_Component with Python Component on C++ Container
227 * - Call 2 times FindOrLoad_Component with the same parameters
228 * - check if we get the same engine
230 // ============================================================================
233 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
235 SALOME_LifeCycleCORBA _LCC(&_NS);
237 // --- get a local container (with a name based on local hostname),
238 // load an engine, check that the CORBA object is not null
240 std::string containerName = "myContainer";
242 Engines::EngineComponent_var mycompo1 =
243 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
244 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
246 Engines::EngineComponent_var mycompo2 =
247 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
248 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
252 Engines::TestComponent_var m1;
253 m1 = Engines::TestComponent::_narrow(mycompo1);
254 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
256 Engines::TestComponent_var m2;
257 m2 = Engines::TestComponent::_narrow(mycompo2);
258 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
260 // --- check equality of instance names
262 std::string name1 = m1->instanceName();
263 std::string name2 = m2->instanceName();
264 CPPUNIT_ASSERT_EQUAL(name1, name2);
267 // ============================================================================
269 * Check FindOrLoad_Component with a component name not in catalog.
270 * See list of catalog given to module catalog server.
271 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
272 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
274 // ============================================================================
277 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
279 SALOME_LifeCycleCORBA _LCC(&_NS);
281 // --- get a local container (with a name based on local hostname),
282 // load an engine, check that the CORBA object is not null
284 std::string containerName = "myContainer";
286 Engines::EngineComponent_var mycompo1 =
287 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
288 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
291 // ============================================================================
293 * Check FindOrLoad_Component with hostname given
294 * - get a local container : getHostName()/componentName,
295 * load an engine, check that the CORBA object is not null.
298 // ============================================================================
301 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
303 SALOME_LifeCycleCORBA _LCC(&_NS);
305 // --- get a local container (with a name based on local hostname),
306 // load an engine, check that the CORBA object is not null
308 std::string containerName = Kernel_Utils::GetHostname();
309 containerName += "/theContainer";
310 DEVTRACE("containerName = " << containerName);
311 Engines::EngineComponent_var mycompo =
312 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
313 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
317 Engines::TestComponent_var m1;
318 m1 = Engines::TestComponent::_narrow(mycompo);
319 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
322 // ============================================================================
324 * Check FindOrLoad_Component with and without local hostname given.
325 * We must get the same container, the same instance of component
327 // ============================================================================
330 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
332 SALOME_LifeCycleCORBA _LCC(&_NS);
334 // --- get a local container (with a name based on local hostname),
335 // load an engine, check that the CORBA object is not null
337 std::string containerName = "aContainer";
339 Engines::EngineComponent_var mycompo1 =
340 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
341 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
343 containerName = Kernel_Utils::GetHostname();
344 containerName += "/aContainer";
345 DEVTRACE("containerName = " << containerName);
346 Engines::EngineComponent_var mycompo2 =
347 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
348 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
352 Engines::TestComponent_var m1;
353 m1 = Engines::TestComponent::_narrow(mycompo1);
354 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
356 Engines::TestComponent_var m2;
357 m2 = Engines::TestComponent::_narrow(mycompo2);
358 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
360 // --- check equality of instance names
362 std::string name1 = m1->instanceName();
363 std::string name2 = m2->instanceName();
364 CPPUNIT_ASSERT_EQUAL(name1, name2);
366 // --- check containers are the same servant (same container name+hostname)
368 Engines::Container_var c1 = m1->GetContainerRef();
369 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
370 Engines::Container_var c2 = m2->GetContainerRef();
371 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
372 std::string cname1 = c1->name();
373 std::string cname2 = c2->name();
374 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
375 std::string hostname1 = c1->getHostName();
376 std::string hostname2 = c2->getHostName();
377 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
378 CORBA::Long pidc1 = c1->getPID();
379 CORBA::Long pidc2 = c2->getPID();
380 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
383 // ============================================================================
385 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
386 * We must catch a Salome Exception with "unknown host" message
388 // ============================================================================
391 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
393 SALOME_LifeCycleCORBA _LCC(&_NS);
395 // --- try to get a distant container on an unknown machine (not existing)
396 // check that the CORBA object is null
398 std::string containerName = "aFarAwayComputer";
399 containerName += "/theContainer";
400 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
401 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
404 Engines::EngineComponent_var mycompo =
405 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
407 catch(const SALOME::SALOME_Exception &ex)
409 CPPUNIT_ASSERT(true);
410 // std::string expectedMessage = "BAD PARAM";
413 // std::string actualMessage = os.str();
414 // DEVTRACE("actual Exception Message = " << actualMessage);
415 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != std::string::npos);
419 // ============================================================================
421 * Check FindOrLoad_Component, empty params must give an instance
423 // ============================================================================
426 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
428 SALOME_LifeCycleCORBA _LCC(&_NS);
430 Engines::ContainerParameters params;
432 Engines::EngineComponent_var mycompo =
433 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
434 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
438 Engines::TestComponent_var m1;
439 m1 = Engines::TestComponent::_narrow(mycompo);
442 // ============================================================================
444 * Check FindOrLoad_Component params = local container
446 // ============================================================================
449 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
451 SALOME_LifeCycleCORBA _LCC(&_NS);
453 Engines::ContainerParameters params;
455 std::string hostname=Kernel_Utils::GetHostname();
456 params.resource_params.hostname=hostname.c_str();
457 Engines::EngineComponent_var mycompo =
458 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
459 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
463 Engines::TestComponent_var m1;
464 m1 = Engines::TestComponent::_narrow(mycompo);
466 // --- check that container is on local computer
468 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
469 Engines::Container_var c1 = m1->GetContainerRef();
470 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
471 std::string hostname1 = c1->getHostName();
472 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
476 // ============================================================================
478 * Check FindOrLoad_Component params = containerName
480 // ============================================================================
483 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
485 SALOME_LifeCycleCORBA _LCC(&_NS);
487 Engines::ContainerParameters params;
489 std::string containerName = "myContainer";
490 params.container_name = containerName.c_str();
491 Engines::EngineComponent_var mycompo =
492 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
493 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
497 Engines::TestComponent_var m1;
498 m1 = Engines::TestComponent::_narrow(mycompo);
500 // --- check that container has good name
502 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
503 Engines::Container_var c1 = m1->GetContainerRef();
504 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
505 std::string hostname1 = c1->getHostName();
506 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
507 std::string cname1 = c1->name();
508 CPPUNIT_ASSERT(cname1.find(containerName) != std::string::npos);
511 // ============================================================================
513 * Check FindOrLoad_Component on remote computer
515 // ============================================================================
518 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
520 SALOME_LifeCycleCORBA _LCC(&_NS);
522 std::string remoteHost = GetRemoteHost();
524 std::string containerName = remoteHost;
525 containerName += "/aContainer";
526 DEVTRACE("containerName = " << containerName);
527 Engines::EngineComponent_var mycompo1 =
528 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
529 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
530 "computers from ResourcesCatalog",
531 !CORBA::is_nil(mycompo1));
535 Engines::TestComponent_var m1;
536 m1 = Engines::TestComponent::_narrow(mycompo1);
538 // --- check that container is on good computer
540 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
541 Engines::Container_var c1 = m1->GetContainerRef();
542 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
543 std::string hostname1 = c1->getHostName();
544 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
547 // ============================================================================
549 * Check FindOrLoad_Component with params on remote computer
550 * params empty except hostname
552 // ============================================================================
555 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
557 SALOME_LifeCycleCORBA _LCC(&_NS);
559 std::string remoteHost = GetRemoteHost();
561 Engines::ContainerParameters params;
563 params.resource_params.hostname = remoteHost.c_str();
565 Engines::EngineComponent_var mycompo1 =
566 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
567 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
568 "computers from ResourcesCatalog",
569 !CORBA::is_nil(mycompo1));
573 Engines::TestComponent_var m1;
574 m1 = Engines::TestComponent::_narrow(mycompo1);
576 // --- check that container is on good computer
578 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
579 Engines::Container_var c1 = m1->GetContainerRef();
580 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
581 std::string hostname1 = c1->getHostName();
582 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
585 // ============================================================================
587 * Check FindOrLoad_Component with params on remote computer
588 * params empty except hostname and container_name
590 // ============================================================================
593 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
595 SALOME_LifeCycleCORBA _LCC(&_NS);
597 std::string remoteHost = GetRemoteHost();
599 Engines::ContainerParameters params;
601 params.resource_params.hostname = remoteHost.c_str();
602 params.container_name = "anotherContainer";
604 Engines::EngineComponent_var mycompo1 =
605 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
606 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
607 "computers from ResourcesCatalog",
608 !CORBA::is_nil(mycompo1));
612 Engines::TestComponent_var m1;
613 m1 = Engines::TestComponent::_narrow(mycompo1);
615 // --- check that container is on good computer
617 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
618 Engines::Container_var c1 = m1->GetContainerRef();
619 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
620 std::string hostname1 = c1->getHostName();
621 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
624 // ============================================================================
626 * Check SALOME_FileTransferCORBA on local machine
628 // ============================================================================
630 void LifeCycleCORBATest::testgetLocalFile_localComputer()
632 SALOME_LifeCycleCORBA _LCC(&_NS);
633 std::string origFileName = getenv("KERNEL_ROOT_DIR");
634 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.so.0.0.0";
635 SALOME_FileTransferCORBA transfer( Kernel_Utils::GetHostname(),
637 std::string local = transfer.getLocalFile();
638 CPPUNIT_ASSERT(!local.empty());
639 CPPUNIT_ASSERT_EQUAL(local, origFileName);
642 // ============================================================================
644 * Check SALOME_FileTransferCORBA on remote machine
646 // ============================================================================
648 void LifeCycleCORBATest::testgetLocalFile_remoteComputer()
650 SALOME_LifeCycleCORBA _LCC(&_NS);
651 std::string origFileName = getenv("KERNEL_ROOT_DIR");
652 origFileName += "/lib/salome/libSalomeContainer.so.0.0.0";
653 SALOME_FileTransferCORBA transfer( GetRemoteHost(),
655 std::string local = transfer.getLocalFile();
656 CPPUNIT_ASSERT(!local.empty());
657 std::string local2 = transfer.getLocalFile();
658 CPPUNIT_ASSERT(!local2.empty());
659 CPPUNIT_ASSERT_EQUAL(local, local2);
662 // ============================================================================
664 * Check FindOrLoad_Component params = containerName
666 // ============================================================================
669 // LifeCycleCORBATest::testFindOrLoad_Component_()
676 // ============================================================================
678 * Get a remote HostName in the Resource Catalog
680 // ============================================================================
682 std::string LifeCycleCORBATest::GetRemoteHost()
684 SALOME_LifeCycleCORBA _LCC(&_NS);
686 CORBA::Object_var obj = _NS.Resolve("/ResourcesManager");
687 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
688 Engines::ResourcesManager_var resourcesManager =
689 Engines::ResourcesManager::_narrow(obj);
690 CPPUNIT_ASSERT(!CORBA::is_nil(resourcesManager));
692 Engines::ContainerParameters params;
693 _LCC.preSet(params); // empty params to get all the machines
694 params.resource_params.componentList.length(1);
695 params.resource_params.componentList[0]="SalomeTestComponent";
696 params.resource_params.can_run_containers = true;
698 Engines::ResourceList_var hostList = resourcesManager->GetFittingResources(params.resource_params);
699 CPPUNIT_ASSERT(hostList->length() > 1);
701 std::string localHost = Kernel_Utils::GetHostname();
702 std::string remoteHost;
703 for (unsigned int i=0; i < hostList->length(); i++)
705 const char* aMachine = hostList[i];
706 Engines::ResourceDefinition_var resource_definition = resourcesManager->GetResourceDefinition(aMachine);
707 std::string machine(resource_definition->hostname.in());
708 if (machine != localHost)
710 remoteHost = machine;
714 CPPUNIT_ASSERT(remoteHost != "");