Salome HOME
typo-fix by Kunda
[modules/kernel.git] / src / LifeCycleCORBA / TestContainerManager.cxx
1 // Copyright (C) 2007-2016  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_ResourcesManager.hxx"
34 #include "SALOME_ContainerManager.hxx"
35 #include "SALOME_LifeCycleCORBA.hxx"
36 #include "SALOME_ResourcesManager.hxx"
37 #include "NamingService_WaitForServerReadiness.hxx"
38 #include "OpUtil.hxx"
39 #include "Utils_ORB_INIT.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "Utils_SALOME_Exception.hxx"
42 #include "Utils_CommException.hxx"
43 #include "Basics_DirUtils.hxx"
44
45 int main (int argc, char * argv[])
46 {
47   std::map<std::string, int> cycle;
48   std::map<std::string, int> first;
49   Engines::Container_ptr cont;
50   Engines::EngineComponent_ptr compo;
51   bool error = false;
52   bool bestImplemented;
53   int status;
54
55   // Initializing omniORB
56   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
57   CORBA::ORB_ptr orb = init( argc , argv ) ;
58
59   SALOME_NamingService *_NS=new SALOME_NamingService(orb);
60
61   CORBA::Object_var obj = _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
62   ASSERT( !CORBA::is_nil(obj));
63   Engines::ContainerManager_var _ContManager=Engines::ContainerManager::_narrow(obj);
64   obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
65   ASSERT( !CORBA::is_nil(obj));
66   Engines::ResourcesManager_var _ResManager=Engines::ResourcesManager::_narrow(obj);
67
68   Engines::ContainerParameters p;
69   p.resource_params.componentList.length(2);
70   p.resource_params.componentList[0] = "MED";
71   p.resource_params.componentList[1] = "GEOM";
72
73   p.resource_params.hostname = "";
74   p.resource_params.OS = "LINUX";
75   p.resource_params.mem_mb = 1000;
76   p.resource_params.cpu_clock = 1000;
77   p.resource_params.nb_proc_per_node = 1;
78   p.resource_params.nb_node = 1;
79   p.isMPI = false;
80
81   char st[10];
82   for(int i=0;i<10;i++){
83     sprintf(st,"cycl_%d",i);
84     p.container_name = CORBA::string_dup(st);
85     p.resource_params.policy="cycl";
86     cont = _ContManager->GiveContainer(p);
87     if(CORBA::is_nil(cont)) error = true;
88   }
89
90   for(int i=0;i<10;i++){
91     sprintf(st,"first_%d",i);
92     p.container_name = CORBA::string_dup(st);
93     p.resource_params.policy="first";
94     cont = _ContManager->GiveContainer(p);
95     if(CORBA::is_nil(cont)) error = true;
96   }
97
98   p.container_name = CORBA::string_dup("best");
99   p.resource_params.policy="best";
100   cont = _ContManager->GiveContainer(p);
101   if(CORBA::is_nil(cont)) bestImplemented = false;
102   else bestImplemented = true;
103
104   SALOME_LifeCycleCORBA LCC(_NS);
105   p.container_name = CORBA::string_dup("FactoryServer");
106   compo = LCC.FindOrLoad_Component(p,"SMESH");
107   if(CORBA::is_nil(compo)) error = true;
108   compo = LCC.FindOrLoad_Component(p,"SMESH");
109   if(CORBA::is_nil(compo)) error = true;
110
111   _NS->Change_Directory("/Containers");
112
113   std::vector<std::string> vec = _NS->list_directory_recurs();
114   std::list<std::string> lstCont;
115   for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++){
116     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
117     Engines::Container_var cont=Engines::Container::_narrow(obj);
118     if(!CORBA::is_nil(cont)){
119       cycle[cont->getHostName()]=0;
120       first[cont->getHostName()]=0;
121       lstCont.push_back((*iter));
122     }
123   }
124   for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
125     CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
126     Engines::Container_var cont=Engines::Container::_narrow(obj);
127     if(!CORBA::is_nil(cont)){
128       std::string basename = Kernel_Utils::GetBaseName(cont->name());
129       if(basename.compare(0,4,"cycl")==0)
130         cycle[cont->getHostName()]++;
131       if(basename.compare(0,5,"first")==0)
132         first[cont->getHostName()]++;
133     }
134   }
135   _ContManager->ShutdownContainers();
136
137   int cmin=10;
138   int cmax=0;
139   int fmin=10;
140   int fmax=0;
141   int nbpmax;
142   for(std::map<std::string,int>::iterator iter=cycle.begin();iter!=cycle.end();iter++){
143     if(strcmp((*iter).first.c_str(),"localhost")!=0){
144       Engines::ResourceDefinition *p = _ResManager->GetResourceDefinition((*iter).first.c_str());
145       int nbproc = p->nb_node * p->nb_proc_per_node;
146       if(cycle[(*iter).first]/nbproc<cmin) cmin=cycle[(*iter).first]/nbproc;
147       if(cycle[(*iter).first]/nbproc>cmax) cmax=cycle[(*iter).first]/nbproc;
148       if(first[(*iter).first]/nbproc<fmin) fmin=first[(*iter).first]/nbproc;
149       if(first[(*iter).first]/nbproc>fmax){
150         fmax=first[(*iter).first]/nbproc;
151         nbpmax = nbproc;
152       }
153     }
154   }
155   std::string msg;
156   if( ((cmax-cmin) <= 1) && (fmax == 10/nbpmax) && !error ){
157     if(bestImplemented)
158       msg = "TEST OK";
159     else
160       msg = "TEST OK but FindBest not implemented!";
161     status=0;
162   }
163   else{
164     msg ="TEST KO";
165     status=1;
166   }
167   std::cout << msg << std::endl;
168
169   return status;
170 }