API
 
Loading...
Searching...
No Matches
MagAOXAppExecute_test.cpp
Go to the documentation of this file.
1// #define CATCH_CONFIG_MAIN
2#include "../../../tests/catch2/catch.hpp"
3
4#include <filesystem>
5
6#include <mx/sys/timeUtils.hpp>
7
8#include "../MagAOXApp.hpp"
9#include "MagAOXApp_test.hpp"
10
11#undef app_MagAOXApp_hpp
12#undef app_tests_MagAOXApp_test_hpp
13#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_PID_LOCKED_ns
14#define XWCTEST_MAGAOXAPP_PID_LOCKED
15#include "../MagAOXApp.hpp"
16#include "MagAOXApp_test.hpp"
17#undef XWCTEST_NAMESPACE
18#undef XWCTEST_MAGAOXAPP_PID_LOCKED
19
20#undef app_MagAOXApp_hpp
21#undef app_tests_MagAOXApp_test_hpp
22#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_PID_WRITE_FAIL_ns
23#define XWCTEST_MAGAOXAPP_PID_WRITE_FAIL
24#include "../MagAOXApp.hpp"
25#include "MagAOXApp_test.hpp"
26#undef XWCTEST_NAMESPACE
27#undef XWCTEST_MAGAOXAPP_PID_WRITE_FAIL
28
29#undef app_MagAOXApp_hpp
30#undef app_tests_MagAOXApp_test_hpp
31#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_NORM_ns
32#define XWCTEST_MAGAOXAPP_EXEC_NORM
33#include "../MagAOXApp.hpp"
34#include "MagAOXApp_test.hpp"
35#undef XWCTEST_NAMESPACE
36#undef XWCTEST_MAGAOXAPP_EXEC_NORM
37
38#undef app_MagAOXApp_hpp
39#undef app_tests_MagAOXApp_test_hpp
40#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_WRONG_USER_ns
41#define XWCTEST_MAGAOXAPP_EXEC_WRONG_USER
42#include "../MagAOXApp.hpp"
43#include "MagAOXApp_test.hpp"
44#undef XWCTEST_NAMESPACE
45#undef XWCTEST_MAGAOXAPP_EXEC_WRONG_USER
46
47#undef app_MagAOXApp_hpp
48#undef app_tests_MagAOXApp_test_hpp
49#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_LOG_START_ns
50#define XWCTEST_MAGAOXAPP_EXEC_LOG_START
51#define XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
52#include "../MagAOXApp.hpp"
53#include "MagAOXApp_test.hpp"
54#undef XWCTEST_NAMESPACE
55#undef XWCTEST_MAGAOXAPP_EXEC_LOG_START
56#undef XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
57
58
59#undef app_MagAOXApp_hpp
60#undef app_tests_MagAOXApp_test_hpp
61#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_SIGTERM_ns
62#define XWCTEST_MAGAOXAPP_SIGTERMH_ERR
63#define XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
64#include "../MagAOXApp.hpp"
65#include "MagAOXApp_test.hpp"
66#undef XWCTEST_NAMESPACE
67#undef XWCTEST_MAGAOXAPP_SIGTERMH_ERR
68#undef XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
69
70
71#undef app_MagAOXApp_hpp
72#undef app_tests_MagAOXApp_test_hpp
73#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_NORM_PID_UNLOCK_ns
74#define XWCTEST_MAGAOXAPP_EXEC_NORM
75#define XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
76#include "../MagAOXApp.hpp"
77#include "MagAOXApp_test.hpp"
78#undef XWCTEST_NAMESPACE
79#undef XWCTEST_MAGAOXAPP_EXEC_NORM
80#undef XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
81
82#undef app_MagAOXApp_hpp
83#undef app_tests_MagAOXApp_test_hpp
84#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_NORM_APPSTARTUP_ns
85#define XWCTEST_MAGAOXAPP_EXEC_NORM
86#define XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
87#include "../MagAOXApp.hpp"
88#include "MagAOXApp_test.hpp"
89#undef XWCTEST_NAMESPACE
90#undef XWCTEST_MAGAOXAPP_EXEC_NORM
91#undef XWCTEST_MAGAOXAPP_PID_UNLOCK_ERR
92
93#undef app_MagAOXApp_hpp
94#undef app_tests_MagAOXApp_test_hpp
95#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_NORM_APPLOGIC_ns
96#define XWCTEST_MAGAOXAPP_EXEC_NORM
97#include "../MagAOXApp.hpp"
98#include "MagAOXApp_test.hpp"
99#undef XWCTEST_NAMESPACE
100#undef XWCTEST_MAGAOXAPP_EXEC_NORM
101
102#undef app_MagAOXApp_hpp
103#undef app_tests_MagAOXApp_test_hpp
104#define XWCTEST_NAMESPACE XWCTEST_MAGAOXAPP_EXEC_NORM_APPSHUTDOWN_ns
105#define XWCTEST_MAGAOXAPP_EXEC_NORM
106#include "../MagAOXApp.hpp"
107#include "MagAOXApp_test.hpp"
108#undef XWCTEST_NAMESPACE
109#undef XWCTEST_MAGAOXAPP_EXEC_NORM
110
111
112namespace libXWCTest
113{
114namespace appTest
115{
116namespace MagAOXAppTest
117{
118
119/// running execute
120/**
121 * \ingroup MagAOXApp_unit_test
122 */
123TEST_CASE( "running execute", "[app::MagAOXApp]" )
124{
125 SECTION( "complete run-through" )
126 {
127 std::vector<const char *> argv;
128 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
129
130 argv.resize( argvstr.size() + 1, NULL );
131 for( size_t index = 0; index < argvstr.size(); ++index )
132 {
133 argv[index] = argvstr[index].c_str();
134 }
135
136 char ppath[1024];
137 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
138 putenv( ppath );
139
140 // First delete the directory and files in case this is a repeat call
141 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
142
143 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
144 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
145 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
146 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
147
148 std::ofstream fout;
149 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
150 fout.close();
151
152 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
153 { "", "power", "power", "power", "power", "power" },
154 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
155 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
156
157 XWCTEST_MAGAOXAPP_EXEC_NORM_ns::MagAOXApp_test app( false );
158 app.setPowerMgtEnabled( true );
159 app.invokedName() = argv[0];
160
161 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
162
163 int rv = app.execute();
164 REQUIRE( rv == 0 );
165 }
166
167 SECTION( "No log directory" )
168 {
169 std::vector<const char *> argv;
170 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
171
172 argv.resize( argvstr.size() + 1, NULL );
173 for( size_t index = 0; index < argvstr.size(); ++index )
174 {
175 argv[index] = argvstr[index].c_str();
176 }
177
178 char ppath[1024];
179 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
180 putenv( ppath );
181
182 // First delete the directory and files in case this is a repeat call
183 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
184
185 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
186 //don't create logs so the user check fails
187 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
188 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
189
190 std::ofstream fout;
191 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
192 fout.close();
193
194 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
195 { "", "power", "power", "power", "power", "power" },
196 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
197 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
198
199 MagAOXApp_test app( false );
200 app.setPowerMgtEnabled( true );
201 app.invokedName() = argv[0];
202
203 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
204
205 int rv = app.execute();
206 REQUIRE( rv == -1 );
207 }
208
209 SECTION( "wrong user" )
210 {
211 std::vector<const char *> argv;
212 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
213
214 argv.resize( argvstr.size() + 1, NULL );
215 for( size_t index = 0; index < argvstr.size(); ++index )
216 {
217 argv[index] = argvstr[index].c_str();
218 }
219
220 char ppath[1024];
221 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
222 putenv( ppath );
223
224 // First delete the directory and files in case this is a repeat call
225 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
226
227 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
228 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
229 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
230 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
231
232 std::ofstream fout;
233 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
234 fout.close();
235
236 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
237 { "", "power", "power", "power", "power", "power" },
238 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
239 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
240
241 XWCTEST_MAGAOXAPP_EXEC_WRONG_USER_ns::MagAOXApp_test app( false );
242 app.setPowerMgtEnabled( true );
243 app.invokedName() = argv[0];
244
245 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
246
247 int rv = app.execute();
248 REQUIRE( rv == -1 );
249 }
250
251 SECTION( "PID Lock Error" )
252 {
253 std::vector<const char *> argv;
254 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
255
256 argv.resize( argvstr.size() + 1, NULL );
257 for( size_t index = 0; index < argvstr.size(); ++index )
258 {
259 argv[index] = argvstr[index].c_str();
260 }
261
262 char ppath[1024];
263 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
264 putenv( ppath );
265
266 // First delete the directory and files in case this is a repeat call
267 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
268
269 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
270 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
271 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
272 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
273
274 std::ofstream fout;
275 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
276 fout.close();
277
278 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
279 { "", "power", "power", "power", "power", "power" },
280 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
281 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
282
283 XWCTEST_MAGAOXAPP_PID_WRITE_FAIL_ns::MagAOXApp_test app( false );
284 app.setPowerMgtEnabled( true );
285 app.invokedName() = argv[0];
286
287 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
288
289 int rv = app.execute();
290 REQUIRE( rv == -1 );
291 }
292
293 SECTION( "Log fails to start" )
294 {
295 std::vector<const char *> argv;
296 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
297
298 argv.resize( argvstr.size() + 1, NULL );
299 for( size_t index = 0; index < argvstr.size(); ++index )
300 {
301 argv[index] = argvstr[index].c_str();
302 }
303
304 char ppath[1024];
305 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
306 putenv( ppath );
307
308 // First delete the directory and files in case this is a repeat call
309 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
310
311 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
312 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
313 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
314 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
315
316 std::ofstream fout;
317 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
318 fout.close();
319
320 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
321 { "", "power", "power", "power", "power", "power" },
322 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
323 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
324
325 XWCTEST_MAGAOXAPP_EXEC_LOG_START_ns::MagAOXApp_test app( false );
326 app.setPowerMgtEnabled( true );
327 app.invokedName() = argv[0];
328
329 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
330
331 int rv = app.execute();
332 REQUIRE( rv == -1 );
333 }
334
335 SECTION( "Setting sigterm handler fails" )
336 {
337 std::vector<const char *> argv;
338 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
339
340 argv.resize( argvstr.size() + 1, NULL );
341 for( size_t index = 0; index < argvstr.size(); ++index )
342 {
343 argv[index] = argvstr[index].c_str();
344 }
345
346 char ppath[1024];
347 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
348 putenv( ppath );
349
350 // First delete the directory and files in case this is a repeat call
351 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
352
353 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
354 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
355 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
356 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
357
358 std::ofstream fout;
359 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
360 fout.close();
361
362 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
363 { "", "power", "power", "power", "power", "power" },
364 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
365 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
366
367 XWCTEST_MAGAOXAPP_EXEC_SIGTERM_ns::MagAOXApp_test app( false );
368 app.setPowerMgtEnabled( true );
369 app.invokedName() = argv[0];
370
371 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
372
373 int rv = app.execute();
374 REQUIRE( rv == -1 );
375 }
376
377 SECTION( "appStartup failure" )
378 {
379 std::vector<const char *> argv;
380 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
381
382 argv.resize( argvstr.size() + 1, NULL );
383 for( size_t index = 0; index < argvstr.size(); ++index )
384 {
385 argv[index] = argvstr[index].c_str();
386 }
387
388 char ppath[1024];
389 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
390 putenv( ppath );
391
392 // First delete the directory and files in case this is a repeat call
393 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
394
395 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
396 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
397 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
398 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
399
400 std::ofstream fout;
401 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
402 fout.close();
403
404 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
405 { "", "power", "power", "power", "power", "power" },
406 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
407 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
408
409 XWCTEST_MAGAOXAPP_EXEC_NORM_APPSTARTUP_ns::MagAOXApp_test app( false );
410 app.setPowerMgtEnabled( true );
411 app.invokedName() = argv[0];
412
413 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
414
415 app.appStartupFail = true;
416
417 int rv = app.execute();
418 REQUIRE( rv == -1 );
419 }
420
421 SECTION( "appLogic failure" )
422 {
423 std::vector<const char *> argv;
424 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
425
426 argv.resize( argvstr.size() + 1, NULL );
427 for( size_t index = 0; index < argvstr.size(); ++index )
428 {
429 argv[index] = argvstr[index].c_str();
430 }
431
432 char ppath[1024];
433 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
434 putenv( ppath );
435
436 // First delete the directory and files in case this is a repeat call
437 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
438
439 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
440 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
441 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
442 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
443
444 std::ofstream fout;
445 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
446 fout.close();
447
448 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
449 { "", "power", "power", "power", "power", "power" },
450 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
451 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
452
453 XWCTEST_MAGAOXAPP_EXEC_NORM_APPLOGIC_ns::MagAOXApp_test app( false );
454 app.setPowerMgtEnabled( true );
455 app.invokedName() = argv[0];
456
457 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
458 app.appLogicFail = true;
459 int rv = app.execute();
460 REQUIRE( rv == -4 );
461 }
462
463 SECTION( "appShutdown failure" )
464 {
465 std::vector<const char *> argv;
466 std::vector<std::string> argvstr( { "./execname", "-n", "testapp" } );
467
468 argv.resize( argvstr.size() + 1, NULL );
469 for( size_t index = 0; index < argvstr.size(); ++index )
470 {
471 argv[index] = argvstr[index].c_str();
472 }
473
474 char ppath[1024];
475 snprintf( ppath, sizeof( ppath ), "%s=/tmp/MagAOXApp_test", MAGAOX_env_path );
476 putenv( ppath );
477
478 // First delete the directory and files in case this is a repeat call
479 std::filesystem::remove_all( "/tmp/MagAOXApp_test" );
480
481 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/config" );
482 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/logs" );
483 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/sys/testapp" );
484 mx::ioutils::createDirectories( "/tmp/MagAOXApp_test/drivers/fifos" );
485
486 std::ofstream fout;
487 fout.open( "/tmp/MagAOXApp_test/config/magaox.conf" );
488 fout.close();
489
490 mx::app::writeConfigFile( "/tmp/MagAOXApp_test/config/testapp.conf",
491 { "", "power", "power", "power", "power", "power" },
492 { "loopPause", "device", "channel", "element", "targetElement", "powerOnWait" },
493 { "2500", "pdu9", "thisch", "thisel", "thistgtel", "5" } );
494
495 XWCTEST_MAGAOXAPP_EXEC_NORM_APPSHUTDOWN_ns::MagAOXApp_test app( false );
496 app.setPowerMgtEnabled( true );
497 app.invokedName() = argv[0];
498
499 app.setup( argv.size() - 1, const_cast<char **>( argv.data() ) );
500 app.appShutdownFail = true;
501 int rv = app.execute();
502 REQUIRE( rv == -5 );
503 }
504}
505
506
507} // namespace MagAOXAppTest
508} // namespace appTest
509} // namespace libXWCTest
TEST_CASE("MagAOXApp 2nd instance", "[app::MagAOXApp]")
MagAOXApp 2nd instance.
#define MAGAOX_env_path
Environment variable setting the MagAO-X path.
Namespace for all libXWC tests.
Definition MagAOXApp.hpp:49