1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
21 #include "LifeCycleCORBATest.hxx"
22 #include "SALOME_LifeCycleCORBA.hxx"
23 #include "SALOME_FileTransferCORBA.hxx"
24 #include "Utils_ORB_INIT.hxx"
25 #include "Utils_SINGLETON.hxx"
35 // --- uncomment to have some traces on standard error
36 // (useful only when adding new tests...)
40 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
41 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
47 #define TRACEFILE "/tmp/traceUnitTest.log"
49 // ============================================================================
52 * - delete preexisting trace classes if any
54 * Get or initialize the orb
55 * Create a SALOME_NamingService instance
57 // ============================================================================
60 LifeCycleCORBATest::setUp()
62 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
64 bp1->deleteInstance(bp1);
67 char *theFileName = TRACEFILE;
73 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
76 // traceFile.open(theFileName, ios::out | ios::trunc);
77 traceFile.open(theFileName, ios::out | ios::app);
78 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
81 bp1 = LocalTraceBufferPool::instance();
84 // --- Get or initialize the orb
88 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
89 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
90 _orb = init(_argc , _argv ) ;
92 // --- Create a SALOME_NamingService instance
97 // ============================================================================
99 * - delete trace classes
101 // ============================================================================
104 LifeCycleCORBATest::tearDown()
107 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
109 bp1->deleteInstance(bp1);
112 // ============================================================================
114 * Check FindOrLoad_Component.
115 * - get a local container (no hostname given),
116 * load an engine, check that the CORBA object is not null.
119 // ============================================================================
122 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
124 SALOME_LifeCycleCORBA _LCC(&_NS);
126 // --- get a local container,
127 // load an engine, check that the CORBA object is not null
129 string containerName = "myContainer";
130 Engines::Component_var mycompo =
131 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
132 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
136 Engines::TestComponent_var m1;
137 m1 = Engines::TestComponent::_narrow(mycompo);
138 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
141 // ============================================================================
143 * Check FindOrLoad_Component.
144 * - Call 2 times FindOrLoad_Component with the same parameters
145 * - check if we get the same engine
147 // ============================================================================
150 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
152 SALOME_LifeCycleCORBA _LCC(&_NS);
154 // --- get a local container,
155 // load an engine, check that the CORBA object is not null
157 string containerName = "myContainer";
159 Engines::Component_var mycompo1 =
160 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
161 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
163 Engines::Component_var mycompo2 =
164 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
165 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
169 Engines::TestComponent_var m1;
170 m1 = Engines::TestComponent::_narrow(mycompo1);
171 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
173 Engines::TestComponent_var m2;
174 m2 = Engines::TestComponent::_narrow(mycompo2);
175 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
177 // --- check equality of instance names
179 string name1 = m1->instanceName();
180 string name2 = m2->instanceName();
181 CPPUNIT_ASSERT_EQUAL(name1, name2);
184 // ============================================================================
186 * Check FindOrLoad_Component with Python Component on C++ Container
187 * load an engine, check that the CORBA object is not null.
190 // ============================================================================
193 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
195 SALOME_LifeCycleCORBA _LCC(&_NS);
197 // --- get a local container,
198 // load an engine, check that the CORBA object is not null
200 string containerName = "myContainer";
202 Engines::Component_var mycompo1 =
203 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
204 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
208 Engines::TestComponent_var m1;
209 m1 = Engines::TestComponent::_narrow(mycompo1);
210 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
213 // ============================================================================
215 * Check FindOrLoad_Component with Python Component on C++ Container
216 * - Call 2 times FindOrLoad_Component with the same parameters
217 * - check if we get the same engine
219 // ============================================================================
222 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
224 SALOME_LifeCycleCORBA _LCC(&_NS);
226 // --- get a local container (with a name based on local hostname),
227 // load an engine, check that the CORBA object is not null
229 string containerName = "myContainer";
231 Engines::Component_var mycompo1 =
232 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
233 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
235 Engines::Component_var mycompo2 =
236 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
237 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
241 Engines::TestComponent_var m1;
242 m1 = Engines::TestComponent::_narrow(mycompo1);
243 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
245 Engines::TestComponent_var m2;
246 m2 = Engines::TestComponent::_narrow(mycompo2);
247 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
249 // --- check equality of instance names
251 string name1 = m1->instanceName();
252 string name2 = m2->instanceName();
253 CPPUNIT_ASSERT_EQUAL(name1, name2);
256 // ============================================================================
258 * Check FindOrLoad_Component with a component name not in catalog.
259 * See list of catalog given to module catalog server.
260 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
261 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
263 // ============================================================================
266 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
268 SALOME_LifeCycleCORBA _LCC(&_NS);
270 // --- get a local container (with a name based on local hostname),
271 // load an engine, check that the CORBA object is not null
273 string containerName = "myContainer";
275 Engines::Component_var mycompo1 =
276 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
277 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
280 // ============================================================================
282 * Check FindOrLoad_Component with hostname given
283 * - get a local container : getHostName()/componentName,
284 * load an engine, check that the CORBA object is not null.
287 // ============================================================================
290 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
292 SALOME_LifeCycleCORBA _LCC(&_NS);
294 // --- get a local container (with a name based on local hostname),
295 // load an engine, check that the CORBA object is not null
297 string containerName = GetHostname();
298 containerName += "/theContainer";
299 DEVTRACE("containerName = " << containerName);
300 Engines::Component_var mycompo =
301 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
302 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
306 Engines::TestComponent_var m1;
307 m1 = Engines::TestComponent::_narrow(mycompo);
308 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
311 // ============================================================================
313 * Check FindOrLoad_Component with and without local hostname given.
314 * We must get the same container, the same instance of component
316 // ============================================================================
319 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
321 SALOME_LifeCycleCORBA _LCC(&_NS);
323 // --- get a local container (with a name based on local hostname),
324 // load an engine, check that the CORBA object is not null
326 string containerName = "aContainer";
328 Engines::Component_var mycompo1 =
329 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
330 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
332 containerName = GetHostname();
333 containerName += "/aContainer";
334 DEVTRACE("containerName = " << containerName);
335 Engines::Component_var mycompo2 =
336 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
337 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
341 Engines::TestComponent_var m1;
342 m1 = Engines::TestComponent::_narrow(mycompo1);
343 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
345 Engines::TestComponent_var m2;
346 m2 = Engines::TestComponent::_narrow(mycompo2);
347 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
349 // --- check equality of instance names
351 string name1 = m1->instanceName();
352 string name2 = m2->instanceName();
353 CPPUNIT_ASSERT_EQUAL(name1, name2);
355 // --- check containers are the same servant (same container name+hostname)
357 Engines::Container_var c1 = m1->GetContainerRef();
358 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
359 Engines::Container_var c2 = m2->GetContainerRef();
360 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
361 string cname1 = c1->name();
362 string cname2 = c2->name();
363 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
364 string hostname1 = c1->getHostName();
365 string hostname2 = c2->getHostName();
366 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
367 CORBA::Long pidc1 = c1->getPID();
368 CORBA::Long pidc2 = c2->getPID();
369 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
372 // ============================================================================
374 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
375 * We must catch a Salome Exception with "unknown host" message
377 // ============================================================================
380 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
382 SALOME_LifeCycleCORBA _LCC(&_NS);
384 // --- try to get a distant container on an unknown machine (not existing)
385 // check that the CORBA object is null
387 string containerName = "aFarAwayComputer";
388 containerName += "/theContainer";
389 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
390 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
393 Engines::Component_var mycompo =
394 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
396 catch(const SALOME::SALOME_Exception &ex)
398 CPPUNIT_ASSERT(true);
399 // string expectedMessage = "BAD PARAM";
402 // string actualMessage = os.str();
403 // DEVTRACE("actual Exception Message = " << actualMessage);
404 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != string::npos);
408 // ============================================================================
410 * Check FindOrLoad_Component, empty params must give an instance
412 // ============================================================================
415 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
417 SALOME_LifeCycleCORBA _LCC(&_NS);
419 Engines::MachineParameters params;
421 Engines::Component_var mycompo =
422 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
423 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
427 Engines::TestComponent_var m1;
428 m1 = Engines::TestComponent::_narrow(mycompo);
431 // ============================================================================
433 * Check FindOrLoad_Component params = local container
435 // ============================================================================
438 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
440 SALOME_LifeCycleCORBA _LCC(&_NS);
442 Engines::MachineParameters params;
444 string hostname=GetHostname();
445 params.hostname=hostname.c_str();
446 Engines::Component_var mycompo =
447 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
448 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
452 Engines::TestComponent_var m1;
453 m1 = Engines::TestComponent::_narrow(mycompo);
455 // --- check that container is on local computer
457 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
458 Engines::Container_var c1 = m1->GetContainerRef();
459 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
460 string hostname1 = c1->getHostName();
461 CPPUNIT_ASSERT_EQUAL(hostname1, GetHostname());
465 // ============================================================================
467 * Check FindOrLoad_Component params = containerName
469 // ============================================================================
472 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
474 SALOME_LifeCycleCORBA _LCC(&_NS);
476 Engines::MachineParameters params;
478 string containerName = "myContainer";
479 params.container_name = containerName.c_str();
480 Engines::Component_var mycompo =
481 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
482 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
486 Engines::TestComponent_var m1;
487 m1 = Engines::TestComponent::_narrow(mycompo);
489 // --- check that container has good name
491 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
492 Engines::Container_var c1 = m1->GetContainerRef();
493 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
494 string hostname1 = c1->getHostName();
495 CPPUNIT_ASSERT_EQUAL(hostname1, GetHostname());
496 string cname1 = c1->name();
497 CPPUNIT_ASSERT(cname1.find(containerName) != string::npos);
500 // ============================================================================
502 * Check FindOrLoad_Component on remote computer
504 // ============================================================================
507 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
509 SALOME_LifeCycleCORBA _LCC(&_NS);
511 string remoteHost = GetRemoteHost();
513 string containerName = remoteHost;
514 containerName += "/aContainer";
515 DEVTRACE("containerName = " << containerName);
516 Engines::Component_var mycompo1 =
517 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
518 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
519 "computers from ResourcesCatalog",
520 !CORBA::is_nil(mycompo1));
524 Engines::TestComponent_var m1;
525 m1 = Engines::TestComponent::_narrow(mycompo1);
527 // --- check that container is on good computer
529 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
530 Engines::Container_var c1 = m1->GetContainerRef();
531 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
532 string hostname1 = c1->getHostName();
533 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
536 // ============================================================================
538 * Check FindOrLoad_Component with params on remote computer
539 * params empty except hostname
541 // ============================================================================
544 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
546 SALOME_LifeCycleCORBA _LCC(&_NS);
548 string remoteHost = GetRemoteHost();
550 Engines::MachineParameters params;
552 params.hostname = remoteHost.c_str();
554 Engines::Component_var mycompo1 =
555 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
556 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
557 "computers from ResourcesCatalog",
558 !CORBA::is_nil(mycompo1));
562 Engines::TestComponent_var m1;
563 m1 = Engines::TestComponent::_narrow(mycompo1);
565 // --- check that container is on good computer
567 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
568 Engines::Container_var c1 = m1->GetContainerRef();
569 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
570 string hostname1 = c1->getHostName();
571 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
574 // ============================================================================
576 * Check FindOrLoad_Component with params on remote computer
577 * params empty except hostname and container_name
579 // ============================================================================
582 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
584 SALOME_LifeCycleCORBA _LCC(&_NS);
586 string remoteHost = GetRemoteHost();
588 Engines::MachineParameters params;
590 params.hostname = remoteHost.c_str();
591 params.container_name = "anotherContainer";
593 Engines::Component_var mycompo1 =
594 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
595 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
596 "computers from ResourcesCatalog",
597 !CORBA::is_nil(mycompo1));
601 Engines::TestComponent_var m1;
602 m1 = Engines::TestComponent::_narrow(mycompo1);
604 // --- check that container is on good computer
606 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
607 Engines::Container_var c1 = m1->GetContainerRef();
608 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
609 string hostname1 = c1->getHostName();
610 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
613 // ============================================================================
615 * Check SALOME_FileTransferCORBA on local machine
617 // ============================================================================
619 void LifeCycleCORBATest::testgetLocalFile_localComputer()
621 SALOME_LifeCycleCORBA _LCC(&_NS);
622 string origFileName = getenv("KERNEL_ROOT_DIR");
623 origFileName += "/lib/salome/libSalomeLifeCycleCORBA.so.0.0.0";
624 SALOME_FileTransferCORBA transfer( GetHostname(),
626 string local = transfer.getLocalFile();
627 CPPUNIT_ASSERT(!local.empty());
628 CPPUNIT_ASSERT_EQUAL(local, origFileName);
631 // ============================================================================
633 * Check SALOME_FileTransferCORBA on remote machine
635 // ============================================================================
637 void LifeCycleCORBATest::testgetLocalFile_remoteComputer()
639 SALOME_LifeCycleCORBA _LCC(&_NS);
640 string origFileName = getenv("KERNEL_ROOT_DIR");
641 origFileName += "/lib/salome/libSalomeContainer.so.0.0.0";
642 SALOME_FileTransferCORBA transfer( GetRemoteHost(),
644 string local = transfer.getLocalFile();
645 CPPUNIT_ASSERT(!local.empty());
646 string local2 = transfer.getLocalFile();
647 CPPUNIT_ASSERT(!local2.empty());
648 CPPUNIT_ASSERT_EQUAL(local, local2);
651 // ============================================================================
653 * Check FindOrLoad_Component params = containerName
655 // ============================================================================
658 // LifeCycleCORBATest::testFindOrLoad_Component_()
665 // ============================================================================
667 * Get a remote HostName in the Resource Catalog
669 // ============================================================================
671 string LifeCycleCORBATest::GetRemoteHost()
673 SALOME_LifeCycleCORBA _LCC(&_NS);
675 CORBA::Object_var obj = _NS.Resolve("/ContainerManager");
676 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
677 Engines::ContainerManager_var containerManager =
678 Engines::ContainerManager::_narrow(obj);
679 CPPUNIT_ASSERT(!CORBA::is_nil(containerManager));
681 Engines::MachineParameters params;
682 _LCC.preSet(params); // empty params to get all the machines
684 Engines::MachineList_var hostList =
685 containerManager->GetFittingResources(params,"SalomeTestComponent");
686 CPPUNIT_ASSERT(hostList->length() > 1);
688 string localHost = GetHostname();
690 for (unsigned int i=0; i < hostList->length(); i++)
692 const char* aMachine = hostList[i];
693 string machine(aMachine);
694 if (machine != localHost)
696 remoteHost = machine;
700 CPPUNIT_ASSERT(remoteHost != "");