Salome HOME
175ff62eb8d308e32e6ef4d3e95e9f079f7ffb7b
[modules/kernel.git] / src / LifeCycleCORBA / TestContainerManager.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SALOME TestContainer : test of container creation and its life cycle
21 //  File   : TestContainer.cxx
22 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
23 //  Module : SALOME
24 //  $Header$
25 //
26 #include "utilities.h"
27 #include <iostream>
28 #ifndef WIN32
29 #include <unistd.h>
30 #endif
31 #include <SALOMEconfig.h>
32 #include "SALOME_NamingService.hxx"
33 #include "SALOME_ContainerManager.hxx"
34 #include "SALOME_LifeCycleCORBA.hxx"
35 #include "NamingService_WaitForServerReadiness.hxx"
36 #include "OpUtil.hxx"
37 #include "Utils_ORB_INIT.hxx"
38 #include "Utils_SINGLETON.hxx"
39 #include "Utils_SALOME_Exception.hxx"
40 #include "Utils_CommException.hxx"
41 #include "Basics_DirUtils.hxx"
42
43 int main (int argc, char * argv[])
44 {
45   std::map<std::string, int> cycle;
46   std::map<std::string, int> first;
47   Engines::Container_ptr cont;
48   Engines::EngineComponent_ptr compo;
49   bool error = false;
50   bool bestImplemented;
51   int status;
52
53   // Initializing omniORB
54   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
55   CORBA::ORB_ptr orb = init( argc , argv ) ;
56
57   SALOME_NamingService *_NS=new SALOME_NamingService(orb);
58
59   CORBA::Object_var obj = _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
60   ASSERT( !CORBA::is_nil(obj));
61   Engines::ContainerManager_var _ContManager=Engines::ContainerManager::_narrow(obj);
62   obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
63   ASSERT( !CORBA::is_nil(obj));
64   Engines::ResourcesManager_var _ResManager=Engines::ResourcesManager::_narrow(obj);
65
66   Engines::ContainerParameters p;
67   p.resource_params.componentList.length(2);
68   p.resource_params.componentList[0] = "MED";
69   p.resource_params.componentList[1] = "GEOM";
70
71   p.resource_params.hostname = "";
72   p.resource_params.OS = "LINUX";
73   p.resource_params.mem_mb = 1000;
74   p.resource_params.cpu_clock = 1000;
75   p.resource_params.nb_proc_per_node = 1;
76   p.resource_params.nb_node = 1;
77   p.isMPI = false;
78
79   char st[10];
80   for(int i=0;i<10;i++){
81     sprintf(st,"cycl_%d",i);
82     p.container_name = CORBA::string_dup(st);
83     p.resource_params.policy="cycl";
84     cont = _ContManager->GiveContainer(p);
85     if(CORBA::is_nil(cont)) error = true;
86   }
87
88   for(int i=0;i<10;i++){
89     sprintf(st,"first_%d",i);
90     p.container_name = CORBA::string_dup(st);
91     p.resource_params.policy="first";
92     cont = _ContManager->GiveContainer(p);
93     if(CORBA::is_nil(cont)) error = true;
94   }
95
96   p.container_name = CORBA::string_dup("best");
97   p.resource_params.policy="best";
98   cont = _ContManager->GiveContainer(p);
99   if(CORBA::is_nil(cont)) bestImplemented = false;
100   else bestImplemented = true;
101
102   SALOME_LifeCycleCORBA LCC(_NS);
103   p.container_name = CORBA::string_dup("FactoryServer");
104   compo = LCC.FindOrLoad_Component(p,"SMESH");
105   if(CORBA::is_nil(compo)) error = true;
106   compo = LCC.FindOrLoad_Component(p,"SMESH");
107   if(CORBA::is_nil(compo)) error = true;
108
109   _NS->Change_Directory("/Containers");
110
111   std::vector<std::string> vec = _NS->list_directory_recurs();
112   std::list<std::string> lstCont;
113   for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++){
114     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
115     Engines::Container_var cont=Engines::Container::_narrow(obj);
116     if(!CORBA::is_nil(cont)){
117       cycle[cont->getHostName()]=0;
118       first[cont->getHostName()]=0;
119       lstCont.push_back((*iter));
120     }
121   }
122   for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
123     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
124     Engines::Container_var cont=Engines::Container::_narrow(obj);
125     if(!CORBA::is_nil(cont)){
126       std::string basename = Kernel_Utils::GetBaseName(cont->name());
127       if(basename.compare(0,4,"cycl")==0)
128         cycle[cont->getHostName()]++;
129       if(basename.compare(0,5,"first")==0)
130         first[cont->getHostName()]++;
131     }
132   }
133   _ContManager->ShutdownContainers();
134
135   int cmin=10;
136   int cmax=0;
137   int fmin=10;
138   int fmax=0;
139   int nbpmax;
140   for(std::map<std::string,int>::iterator iter=cycle.begin();iter!=cycle.end();iter++){
141     if(strcmp((*iter).first.c_str(),"localhost")!=0){
142       Engines::ResourceDefinition *p = _ResManager->GetResourceDefinition((*iter).first.c_str());
143       int nbproc = p->nb_node * p->nb_proc_per_node;
144       if(cycle[(*iter).first]/nbproc<cmin) cmin=cycle[(*iter).first]/nbproc;
145       if(cycle[(*iter).first]/nbproc>cmax) cmax=cycle[(*iter).first]/nbproc;
146       if(first[(*iter).first]/nbproc<fmin) fmin=first[(*iter).first]/nbproc;
147       if(first[(*iter).first]/nbproc>fmax){
148         fmax=first[(*iter).first]/nbproc;
149         nbpmax = nbproc;
150       }
151     }
152   }
153   std::string msg;
154   if( ((cmax-cmin) <= 1) && (fmax == 10/nbpmax) && !error ){
155     if(bestImplemented)
156       msg = "TEST OK";
157     else
158       msg = "TEST OK but FindBest not implemented!";
159     status=0;
160   }
161   else{
162     msg ="TEST KO";
163     status=1;
164   }
165   std::cout << msg << std::endl;
166
167   return status;
168 }