1 /***********************************************************************************************************************************
2 Test Exit Routines
3 ***********************************************************************************************************************************/
4 #include "common/error.h"
5 #include "common/log.h"
6 #include "config/config.h"
7 #include "version.h"
8 
9 #include "common/harnessConfig.h"
10 #include "common/harnessError.h"
11 #include "common/harnessFork.h"
12 
13 /***********************************************************************************************************************************
14 Test Run
15 ***********************************************************************************************************************************/
16 void
testRun(void)17 testRun(void)
18 {
19     FUNCTION_HARNESS_VOID();
20 
21     // *****************************************************************************************************************************
22     if (testBegin("exitSignalName()"))
23     {
24         TEST_RESULT_Z(exitSignalName(signalTypeHup), "HUP", "SIGHUP name");
25         TEST_RESULT_Z(exitSignalName(signalTypeInt), "INT", "SIGINT name");
26         TEST_RESULT_Z(exitSignalName(signalTypeTerm), "TERM", "SIGTERM name");
27         TEST_ERROR(exitSignalName(signalTypeNone), AssertError, "no name for signal none");
28     }
29 
30     // *****************************************************************************************************************************
31     if (testBegin("exitInit() and exitOnSignal()"))
32     {
33         HRN_CFG_LOAD(cfgCmdHelp, strLstNew());
34 
35         HRN_FORK_BEGIN()
36         {
37             HRN_FORK_CHILD_BEGIN(.expectedExitStatus = errorTypeCode(&TermError))
38             {
39                 exitInit();
40                 raise(SIGTERM);
41             }
42             HRN_FORK_CHILD_END();                               // {uncoverable - signal is raised in block}
43         }
44         HRN_FORK_END();
45     }
46 
47     // *****************************************************************************************************************************
48     if (testBegin("exitSafe()"))
49     {
50         HRN_CFG_LOAD(cfgCmdHelp, strLstNew());
51         cfgCommandSet(cfgCmdNone, cfgCmdRoleMain);
52 
53         TEST_RESULT_INT(exitSafe(0, false, signalTypeNone), 0, "exit with no command");
54 
55         // -------------------------------------------------------------------------------------------------------------------------
56         StringList *argList = strLstNew();
57         hrnCfgArgRawZ(argList, cfgOptStanza, "test");
58         hrnCfgArgRawNegate(argList, cfgOptLogTimestamp);
59         HRN_CFG_LOAD(cfgCmdArchivePush, argList);
60 
61         TEST_RESULT_INT(exitSafe(0, false, signalTypeNone), 0, "exit with no error");
62         TEST_RESULT_LOG("P00   INFO: archive-push command end: completed successfully");
63 
64         TEST_RESULT_INT(exitSafe(1, false, signalTypeNone), 1, "exit with no error");
65         TEST_RESULT_LOG("P00   INFO: archive-push command end: completed successfully");
66 
67         // -------------------------------------------------------------------------------------------------------------------------
68         TRY_BEGIN()
69         {
70             THROW(RuntimeError, "test error message");
71         }
72         CATCH_ANY()
73         {
74             exitSafe(0, true, signalTypeNone);
75             TEST_RESULT_LOG(
76                 "P00  ERROR: [122]: test error message\n"
77                 "P00   INFO: archive-push command end: aborted with exception [122]");
78         }
79         TRY_END();
80 
81         // -------------------------------------------------------------------------------------------------------------------------
82         argList = strLstNew();
83         strLstAddZ(argList, "--" CFGOPT_STANZA "=test");
84         strLstAddZ(argList, "--" CFGOPT_PROCESS_MAX "=4");
85         HRN_CFG_LOAD(cfgCmdArchivePush, argList, .role = cfgCmdRoleAsync, .noStd = true);
86 
87         harnessLogLevelSet(logLevelDebug);
88 
89         TRY_BEGIN()
90         {
91             hrnErrorThrowP(.errorType = &RuntimeError, .message = "test debug error message");
92         }
93         CATCH_ANY()
94         {
95             exitSafe(0, true, signalTypeNone);
96             TEST_RESULT_LOG(
97                 "P00  DEBUG:     " TEST_PGB_PATH "/src/common/exit::exitSafe: (result: 0, error: true, signalType: 0)\n"
98                 "P00  ERROR: [122]: test debug error message\n"
99                 "            --------------------------------------------------------------------\n"
100                 "            If SUBMITTING AN ISSUE please provide the following information:\n"
101                 "            \n"
102                 "            version: " PROJECT_VERSION "\n"
103                 "            command: archive-push:async\n"
104                 "            options: --exec-id=1-test --process-max=4 --stanza=test\n"
105                 "            \n"
106                 "            stack trace:\n"
107                 "            ERR_STACK_TRACE\n"
108                 "            --------------------------------------------------------------------\n"
109                 "P00   INFO: archive-push:async command end: aborted with exception [122]\n"
110                 "P00  DEBUG:     " TEST_PGB_PATH "/src/common/lock::lockRelease: (failOnNoLock: false)\n"
111                 "P00  DEBUG:     " TEST_PGB_PATH "/src/common/lock::lockRelease: => false\n"
112                 "P00  DEBUG:     " TEST_PGB_PATH "/src/common/exit::exitSafe: => 122");
113         }
114         TRY_END();
115 
116         harnessLogLevelReset();
117 
118         // -------------------------------------------------------------------------------------------------------------------------
119         TRY_BEGIN()
120         {
121             hrnErrorThrowP(.message = "test assert message");
122         }
123         CATCH_ANY()
124         {
125             exitSafe(0, true, signalTypeNone);
126             TEST_RESULT_LOG(
127                 "P00 ASSERT: [025]: test assert message\n"
128                 "            --------------------------------------------------------------------\n"
129                 "            If SUBMITTING AN ISSUE please provide the following information:\n"
130                 "            \n"
131                 "            version: " PROJECT_VERSION "\n"
132                 "            command: archive-push:async\n"
133                 "            options: --exec-id=1-test --process-max=4 --stanza=test\n"
134                 "            \n"
135                 "            stack trace:\n"
136                 "            ERR_STACK_TRACE\n"
137                 "            --------------------------------------------------------------------\n"
138                 "P00   INFO: archive-push:async command end: aborted with exception [025]");
139         }
140         TRY_END();
141 
142         // -------------------------------------------------------------------------------------------------------------------------
143         TEST_RESULT_INT(
144             exitSafe(errorTypeCode(&TermError), false, signalTypeNone), errorTypeCode(&TermError), "exit on term with no signal");
145         TEST_RESULT_LOG("P00   INFO: archive-push:async command end: terminated on signal from child process");
146 
147         // -------------------------------------------------------------------------------------------------------------------------
148         TEST_RESULT_INT(
149             exitSafe(errorTypeCode(&TermError), false, signalTypeTerm), errorTypeCode(&TermError), "exit on term with SIGTERM");
150         TEST_RESULT_LOG("P00   INFO: archive-push:async command end: terminated on signal [SIGTERM]");
151     }
152 
153     FUNCTION_HARNESS_RETURN_VOID();
154 }
155