1 // Copyright (C) 2007-2008 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
22 #include "LifeCycleCORBATest.hxx"
23 #include "SALOME_LifeCycleCORBA.hxx"
24 #include "SALOME_FileTransferCORBA.hxx"
25 #include "Utils_ORB_INIT.hxx"
26 #include "Utils_SINGLETON.hxx"
27 #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 TRACEFILE "/tmp/traceUnitTest.log"
50 // ============================================================================
53 * - delete preexisting trace classes if any
55 * Get or initialize the orb
56 * Create a SALOME_NamingService instance
58 // ============================================================================
61 LifeCycleCORBATest::setUp()
63 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
65 bp1->deleteInstance(bp1);
68 const char *theFileName = TRACEFILE;
74 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
77 // traceFile.open(theFileName, ios::out | ios::trunc);
78 traceFile.open(theFileName, ios::out | ios::app);
79 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
82 bp1 = LocalTraceBufferPool::instance();
85 // --- Get or initialize the orb
88 char* _argv[] = {(char*)""};
89 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
90 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
91 _orb = init(_argc , _argv ) ;
93 // --- Create a SALOME_NamingService instance
98 // ============================================================================
100 * - delete trace classes
102 // ============================================================================
105 LifeCycleCORBATest::tearDown()
108 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
110 bp1->deleteInstance(bp1);
113 // ============================================================================
115 * Check FindOrLoad_Component.
116 * - get a local container (no hostname given),
117 * load an engine, check that the CORBA object is not null.
120 // ============================================================================
123 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
125 SALOME_LifeCycleCORBA _LCC(&_NS);
127 // --- get a local container,
128 // load an engine, check that the CORBA object is not null
130 string containerName = "myContainer";
131 Engines::Component_var mycompo =
132 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
133 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
137 Engines::TestComponent_var m1;
138 m1 = Engines::TestComponent::_narrow(mycompo);
139 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
142 // ============================================================================
144 * Check FindOrLoad_Component.
145 * - Call 2 times FindOrLoad_Component with the same parameters
146 * - check if we get the same engine
148 // ============================================================================
151 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
153 SALOME_LifeCycleCORBA _LCC(&_NS);
155 // --- get a local container,
156 // load an engine, check that the CORBA object is not null
158 string containerName = "myContainer";
160 Engines::Component_var mycompo1 =
161 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
162 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
164 Engines::Component_var mycompo2 =
165 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
166 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
170 Engines::TestComponent_var m1;
171 m1 = Engines::TestComponent::_narrow(mycompo1);
172 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
174 Engines::TestComponent_var m2;
175 m2 = Engines::TestComponent::_narrow(mycompo2);
176 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
178 // --- check equality of instance names
180 string name1 = m1->instanceName();
181 string name2 = m2->instanceName();
182 CPPUNIT_ASSERT_EQUAL(name1, name2);
185 // ============================================================================
187 * Check FindOrLoad_Component with Python Component on C++ Container
188 * load an engine, check that the CORBA object is not null.
191 // ============================================================================
194 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
196 SALOME_LifeCycleCORBA _LCC(&_NS);
198 // --- get a local container,
199 // load an engine, check that the CORBA object is not null
201 string containerName = "myContainer";
203 Engines::Component_var mycompo1 =
204 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
205 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
209 Engines::TestComponent_var m1;
210 m1 = Engines::TestComponent::_narrow(mycompo1);
211 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
214 // ============================================================================
216 * Check FindOrLoad_Component with Python Component on C++ Container
217 * - Call 2 times FindOrLoad_Component with the same parameters
218 * - check if we get the same engine
220 // ============================================================================
223 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
225 SALOME_LifeCycleCORBA _LCC(&_NS);
227 // --- get a local container (with a name based on local hostname),
228 // load an engine, check that the CORBA object is not null
230 string containerName = "myContainer";
232 Engines::Component_var mycompo1 =
233 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
234 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
236 Engines::Component_var mycompo2 =
237 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
238 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
242 Engines::TestComponent_var m1;
243 m1 = Engines::TestComponent::_narrow(mycompo1);
244 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
246 Engines::TestComponent_var m2;
247 m2 = Engines::TestComponent::_narrow(mycompo2);
248 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
250 // --- check equality of instance names
252 string name1 = m1->instanceName();
253 string name2 = m2->instanceName();
254 CPPUNIT_ASSERT_EQUAL(name1, name2);
257 // ============================================================================
259 * Check FindOrLoad_Component with a component name not in catalog.
260 * See list of catalog given to module catalog server.
261 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
262 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
264 // ============================================================================
267 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
269 SALOME_LifeCycleCORBA _LCC(&_NS);
271 // --- get a local container (with a name based on local hostname),
272 // load an engine, check that the CORBA object is not null
274 string containerName = "myContainer";
276 Engines::Component_var mycompo1 =
277 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
278 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
281 // ============================================================================
283 * Check FindOrLoad_Component with hostname given
284 * - get a local container : getHostName()/componentName,
285 * load an engine, check that the CORBA object is not null.
288 // ============================================================================
291 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
293 SALOME_LifeCycleCORBA _LCC(&_NS);
295 // --- get a local container (with a name based on local hostname),
296 // load an engine, check that the CORBA object is not null
298 string containerName = Kernel_Utils::GetHostname();
299 containerName += "/theContainer";
300 DEVTRACE("containerName = " << containerName);
301 Engines::Component_var mycompo =
302 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
303 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
307 Engines::TestComponent_var m1;
308 m1 = Engines::TestComponent::_narrow(mycompo);
309 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
312 // ============================================================================
314 * Check FindOrLoad_Component with and without local hostname given.
315 * We must get the same container, the same instance of component
317 // ============================================================================
320 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
322 SALOME_LifeCycleCORBA _LCC(&_NS);
324 // --- get a local container (with a name based on local hostname),
325 // load an engine, check that the CORBA object is not null
327 string containerName = "aContainer";
329 Engines::Component_var mycompo1 =
330 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
331 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
333 containerName = Kernel_Utils::GetHostname();
334 containerName += "/aContainer";
335 DEVTRACE("containerName = " << containerName);
336 Engines::Component_var mycompo2 =
337 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
338 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
342 Engines::TestComponent_var m1;
343 m1 = Engines::TestComponent::_narrow(mycompo1);
344 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
346 Engines::TestComponent_var m2;
347 m2 = Engines::TestComponent::_narrow(mycompo2);
348 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
350 // --- check equality of instance names
352 string name1 = m1->instanceName();
353 string name2 = m2->instanceName();
354 CPPUNIT_ASSERT_EQUAL(name1, name2);
356 // --- check containers are the same servant (same container name+hostname)
358 Engines::Container_var c1 = m1->GetContainerRef();
359 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
360 Engines::Container_var c2 = m2->GetContainerRef();
361 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
362 string cname1 = c1->name();
363 string cname2 = c2->name();
364 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
365 string hostname1 = c1->getHostName();
366 string hostname2 = c2->getHostName();
367 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
368 CORBA::Long pidc1 = c1->getPID();
369 CORBA::Long pidc2 = c2->getPID();
370 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
373 // ============================================================================
375 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
376 * We must catch a Salome Exception with "unknown host" message
378 // ============================================================================
381 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
383 SALOME_LifeCycleCORBA _LCC(&_NS);
385 // --- try to get a distant container on an unknown machine (not existing)
386 // check that the CORBA object is null
388 string containerName = "aFarAwayComputer";
389 containerName += "/theContainer";
390 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
391 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
394 Engines::Component_var mycompo =
395 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
397 catch(const SALOME::SALOME_Exception &ex)
399 CPPUNIT_ASSERT(true);
400 // string expectedMessage = "BAD PARAM";
403 // string actualMessage = os.str();
404 // DEVTRACE("actual Exception Message = " << actualMessage);
405 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != string::npos);
409 // ============================================================================
411 * Check FindOrLoad_Component, empty params must give an instance
413 // ============================================================================
416 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
418 SALOME_LifeCycleCORBA _LCC(&_NS);
420 Engines::MachineParameters params;
422 Engines::Component_var mycompo =
423 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
424 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
428 Engines::TestComponent_var m1;
429 m1 = Engines::TestComponent::_narrow(mycompo);
432 // ============================================================================
434 * Check FindOrLoad_Component params = local container
436 // ============================================================================
439 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
441 SALOME_LifeCycleCORBA _LCC(&_NS);
443 Engines::MachineParameters params;
445 string hostname=Kernel_Utils::GetHostname();
446 params.hostname=hostname.c_str();
447 Engines::Component_var mycompo =
448 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
449 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
453 Engines::TestComponent_var m1;
454 m1 = Engines::TestComponent::_narrow(mycompo);
456 // --- check that container is on local computer
458 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
459 Engines::Container_var c1 = m1->GetContainerRef();
460 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
461 string hostname1 = c1->getHostName();
462 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
466 // ============================================================================
468 * Check FindOrLoad_Component params = containerName
470 // ============================================================================
473 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
475 SALOME_LifeCycleCORBA _LCC(&_NS);
477 Engines::MachineParameters params;
479 string containerName = "myContainer";
480 params.container_name = containerName.c_str();
481 Engines::Component_var mycompo =
482 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
483 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
487 Engines::TestComponent_var m1;
488 m1 = Engines::TestComponent::_narrow(mycompo);
490 // --- check that container has good name
492 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
493 Engines::Container_var c1 = m1->GetContainerRef();
494 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
495 string hostname1 = c1->getHostName();
496 CPPUNIT_ASSERT_EQUAL(hostname1, Kernel_Utils::GetHostname());
497 string cname1 = c1->name();
498 CPPUNIT_ASSERT(cname1.find(containerName) != string::npos);
501 // ============================================================================
503 * Check FindOrLoad_Component on remote computer
505 // ============================================================================
508 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
510 SALOME_LifeCycleCORBA _LCC(&_NS);
512 string remoteHost = GetRemoteHost();
514 string containerName = remoteHost;
515 containerName += "/aContainer";
516 DEVTRACE("containerName = " << containerName);
517 Engines::Component_var mycompo1 =
518 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
519 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
520 "computers from ResourcesCatalog",
521 !CORBA::is_nil(mycompo1));
525 Engines::TestComponent_var m1;
526 m1 = Engines::TestComponent::_narrow(mycompo1);
528 // --- check that container is on good computer
530 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
531 Engines::Container_var c1 = m1->GetContainerRef();
532 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
533 string hostname1 = c1->getHostName();
534 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
537 // ============================================================================
539 * Check FindOrLoad_Component with params on remote computer
540 * params empty except hostname
542 // ============================================================================
545 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
547 SALOME_LifeCycleCORBA _LCC(&_NS);
549 string remoteHost = GetRemoteHost();
551 Engines::MachineParameters params;
553 params.hostname = remoteHost.c_str();
555 Engines::Component_var mycompo1 =
556 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
557 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
558 "computers from ResourcesCatalog",
559 !CORBA::is_nil(mycompo1));
563 Engines::TestComponent_var m1;
564 m1 = Engines::TestComponent::_narrow(mycompo1);
566 // --- check that container is on good computer
568 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
569 Engines::Container_var c1 = m1->GetContainerRef();
570 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
571 string hostname1 = c1->getHostName();
572 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
575 // ============================================================================
577 * Check FindOrLoad_Component with params on remote computer
578 * params empty except hostname and container_name
580 // ============================================================================
583 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
585 SALOME_LifeCycleCORBA _LCC(&_NS);
587 string remoteHost = GetRemoteHost();
589 Engines::MachineParameters params;
591 params.hostname = remoteHost.c_str();
592 params.container_name = "anotherContainer";
594 Engines::Component_var mycompo1 =
595 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
596 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
597 "computers from ResourcesCatalog",
598 !CORBA::is_nil(mycompo1));
602 Engines::TestComponent_var m1;
603 m1 = Engines::TestComponent::_narrow(mycompo1);
605 // --- check that container is on good computer
607 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
608 Engines::Container_var c1 = m1->GetContainerRef();
609 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
610 string hostname1 = c1->getHostName();
611 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
614 // ============================================================================
616 * Check SALOME_FileTransferCORBA on local machine
618 // ============================================================================
620 void LifeCycleCORBATest::testgetLocalFile_localComputer()
622 SALOME_LifeCycleCORBA _LCC(&_NS);
623 string origFileName = getenv("KERNEL_ROOT_DIR");
624 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.so.0.0.0";
625 SALOME_FileTransferCORBA transfer( Kernel_Utils::GetHostname(),
627 string local = transfer.getLocalFile();
628 CPPUNIT_ASSERT(!local.empty());
629 CPPUNIT_ASSERT_EQUAL(local, origFileName);
632 // ============================================================================
634 * Check SALOME_FileTransferCORBA on remote machine
636 // ============================================================================
638 void LifeCycleCORBATest::testgetLocalFile_remoteComputer()
640 SALOME_LifeCycleCORBA _LCC(&_NS);
641 string origFileName = getenv("KERNEL_ROOT_DIR");
642 origFileName += "/lib/salome/libSalomeContainer.so.0.0.0";
643 SALOME_FileTransferCORBA transfer( GetRemoteHost(),
645 string local = transfer.getLocalFile();
646 CPPUNIT_ASSERT(!local.empty());
647 string local2 = transfer.getLocalFile();
648 CPPUNIT_ASSERT(!local2.empty());
649 CPPUNIT_ASSERT_EQUAL(local, local2);
652 // ============================================================================
654 * Check FindOrLoad_Component params = containerName
656 // ============================================================================
659 // LifeCycleCORBATest::testFindOrLoad_Component_()
666 // ============================================================================
668 * Get a remote HostName in the Resource Catalog
670 // ============================================================================
672 string LifeCycleCORBATest::GetRemoteHost()
674 SALOME_LifeCycleCORBA _LCC(&_NS);
676 CORBA::Object_var obj = _NS.Resolve("/ResourcesManager");
677 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
678 Engines::ResourcesManager_var resourcesManager =
679 Engines::ResourcesManager::_narrow(obj);
680 CPPUNIT_ASSERT(!CORBA::is_nil(resourcesManager));
682 Engines::MachineParameters params;
683 _LCC.preSet(params); // empty params to get all the machines
685 Engines::CompoList clist;
687 clist[0] = "SalomeTestComponent";
688 Engines::MachineList_var hostList =
689 resourcesManager->GetFittingResources(params,clist);
690 CPPUNIT_ASSERT(hostList->length() > 1);
692 string localHost = Kernel_Utils::GetHostname();
694 for (unsigned int i=0; i < hostList->length(); i++)
696 const char* aMachine = hostList[i];
697 string machine(aMachine);
698 if (machine != localHost)
700 remoteHost = machine;
704 CPPUNIT_ASSERT(remoteHost != "");