hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
main.cpp
Go to the documentation of this file.
1 
14 #include <gtest/gtest.h>
15 #include <iostream>
16 #include <sstream>
17 
18 #include "Process.hpp"
19 #include "ProcExec.hpp"
20 #include "CommandExecuter.hpp"
21 
22 namespace HCE
23 {
24 namespace tests
25 {
26 //-----------------------------------------------------------------------------
27 std::string command = "sort";
28 HCE::Process::Args args = {"-r"};
29 std::string initialDirectory = "/tmp";
30 std::stringstream inMsg("def\nabc\n123\n");
31 HCE::Process::Env env = {{"key1", "value1"}};
32 
33 const std::string reverseInputStream = "123\nabc\ndef\n";
34 //-----------------------------------------------------------------------------
35 TEST(ProcessTest, first)
36 {
37  bool noErrors = true;
38  try
39  {
40  std::stringstream outMsg, errMsg;
42 
43  ASSERT_TRUE(ph.id());
44  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
45  ASSERT_EQ(outMsg.str(), inMsg.str());
46  ASSERT_TRUE(errMsg.str().empty());
47 
48  outMsg.str("");
49  HCE::Process::Args argsEmpty;
50  ph = HCE::Process::launch(command, argsEmpty, initialDirectory, inMsg, outMsg, errMsg, env);
51 
52  ASSERT_TRUE(ph.id());
53  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
54  ASSERT_EQ(outMsg.str(), reverseInputStream);
55  ASSERT_TRUE(errMsg.str().empty());
56  }
57  catch(std::exception& e)
58  {
59  std::cerr << "Exception: " << e.what() << std::endl;
60  noErrors = false;
61  }
62  ASSERT_EQ(noErrors, true);
63 }
64 //-----------------------------------------------------------------------------
65 TEST(ProcessTest, second)
66 {
67  bool noErrors = true;
68  try
69  {
70  std::stringstream outMsg, errMsg;
72 
73  ASSERT_TRUE(ph.id());
74  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
75  ASSERT_EQ(outMsg.str(), inMsg.str());
76  ASSERT_TRUE(errMsg.str().empty());
77 
78  outMsg.str("");
79  HCE::Process::Args argsEmpty;
80  ph = HCE::Process::launch(command, argsEmpty, inMsg, outMsg, errMsg, env);
81 
82  ASSERT_TRUE(ph.id());
83  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
84  ASSERT_EQ(outMsg.str(), reverseInputStream);
85  ASSERT_TRUE(errMsg.str().empty());
86  }
87  catch(std::exception& e)
88  {
89  std::cerr << "Exception: " << e.what() << std::endl;
90  noErrors = false;
91  }
92  ASSERT_EQ(noErrors, true);
93 }
94 //-----------------------------------------------------------------------------
95 TEST(ProcessTest, three)
96 {
97  bool noErrors = true;
98  try
99  {
100  std::stringstream outMsg, errMsg;
102 
103  ASSERT_TRUE(ph.id());
104  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
105  ASSERT_EQ(outMsg.str(), inMsg.str());
106  ASSERT_TRUE(errMsg.str().empty());
107 
108  outMsg.str("");
109  HCE::Process::Args argsEmpty;
110  ph = HCE::Process::launch(command, argsEmpty, initialDirectory, inMsg, outMsg, errMsg);
111 
112  ASSERT_TRUE(ph.id());
113  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
114  ASSERT_EQ(outMsg.str(), reverseInputStream);
115  ASSERT_TRUE(errMsg.str().empty());
116  }
117  catch(std::exception& e)
118  {
119  std::cerr << "Exception: " << e.what() << std::endl;
120  noErrors = false;
121  }
122  ASSERT_EQ(noErrors, true);
123 }
124 //-----------------------------------------------------------------------------
125 TEST(ProcessTest, four)
126 {
127  bool noErrors = true;
128  try
129  {
130  std::stringstream outMsg, errMsg;
131  HCE::ProcessHandle ph = HCE::Process::launch(command, args, inMsg, outMsg, errMsg);
132 
133  ASSERT_TRUE(ph.id());
134  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
135  ASSERT_EQ(outMsg.str(), inMsg.str());
136  ASSERT_TRUE(errMsg.str().empty());
137 
138  outMsg.str("");
139  HCE::Process::Args argsEmpty;
140  ph = HCE::Process::launch(command, argsEmpty, inMsg, outMsg, errMsg);
141 
142  ASSERT_TRUE(ph.id());
143  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
144  ASSERT_EQ(outMsg.str(), reverseInputStream);
145  ASSERT_TRUE(errMsg.str().empty());
146  }
147  catch(std::exception& e)
148  {
149  std::cerr << "Exception: " << e.what() << std::endl;
150  noErrors = false;
151  }
152  ASSERT_EQ(noErrors, true);
153 }
154 //-----------------------------------------------------------------------------
155 TEST(ProcessTest, five)
156 {
157  bool noErrors = true;
158  try
159  {
161 
162  ASSERT_TRUE(ph.id());
163  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
164 
165  HCE::Process::Args argsEmpty;
167 
168  ASSERT_TRUE(ph.id());
169  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
170  }
171  catch(std::exception& e)
172  {
173  std::cerr << "Exception: " << e.what() << std::endl;
174  noErrors = false;
175  }
176  ASSERT_EQ(noErrors, true);
177 }
178 //-----------------------------------------------------------------------------
179 TEST(ProcessTest, six)
180 {
181  bool noErrors = true;
182  try
183  {
185 
186  ASSERT_TRUE(ph.id());
187  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
188 
189  HCE::Process::Args argsEmpty;
190  ph = HCE::Process::launch(command, argsEmpty);
191 
192  ASSERT_TRUE(ph.id());
193  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
194  }
195  catch(std::exception& e)
196  {
197  std::cerr << "Exception: " << e.what() << std::endl;
198  noErrors = false;
199  }
200  ASSERT_EQ(noErrors, true);
201 }
202 //-----------------------------------------------------------------------------
203 TEST(ProcessTest, seven)
204 {
205  bool noErrors = true;
206  try
207  {
209 
210  ASSERT_TRUE(ph.id());
211  ASSERT_EQ(ph.wait(), ExitStatus::SUCCESS);
212 
215  ASSERT_TRUE(HCE::Process::id());
216  }
217  catch(std::exception& e)
218  {
219  std::cerr << "Exception: " << e.what() << std::endl;
220  noErrors = false;
221  }
222  ASSERT_EQ(noErrors, true);
223 }
224 //-----------------------------------------------------------------------------
225 TEST(ProcExecTest, exec)
226 {
227  std::string command = "sort";
228  HCE::Process::Args args = {"-r"};
229  std::stringstream inMsg("def\nabc\n123\n");
230 
231  std::stringstream outMsg, errMsg;
232  HCE::ProcExec procExec(command, args);
233  bool ret = procExec.exec(inMsg, outMsg, errMsg);
234  ASSERT_EQ(ret, true);
235  ASSERT_EQ(procExec.isError(), false);
236  ASSERT_EQ(outMsg.str(), inMsg.str());
237  ASSERT_TRUE(errMsg.str().empty());
238 }
239 //-----------------------------------------------------------------------------
240 TEST(CommandExecutorTest, execute)
241 {
242  std::string command = "sort -r";
243  std::string shell = "bash -c";
244  std::stringstream inMsg("def\nabc\n123\n");
245 
246  HCE::Command cmd(command);
247  cmd.setShell(shell);
248  cmd.setInputStream(inMsg);
249  HCE::CommandExecuter commandExecuter;
250  HCE::CommandResultData commandResultData = commandExecuter.execute(cmd);
251 
252  ASSERT_EQ(commandResultData.getIsError(), false);
253  ASSERT_EQ(commandResultData.getExitStatus(), 0);
254  ASSERT_EQ(commandResultData.getOutStream().str(), inMsg.str());
255  ASSERT_TRUE(commandResultData.getErrStream().str().empty());
256 }
257 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
259 } // end namespace tests
260 } // end namespace HCE
261 
262 int main (int argc, char** argv)
263 {
264  testing::InitGoogleTest(&argc, argv);
265  return RUN_ALL_TESTS();
266 }
267