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