Ignore ECHILD during system call on thread
[anna.git] / example / diameter / launcher / testing / TestStep.cpp
index c4ec2e5..b1da8fb 100644 (file)
@@ -8,6 +8,9 @@
 
 // Standard
 #include <string>
+#include <iostream>
+#include <stdio.h>
+#include <errno.h>
 
 // Project
 #include <anna/xml/Compiler.hpp>
 #include <TestTimer.hpp>
 
 
+namespace {
+  void cmdRunOnThread (TestStepCmd *step, const std::string &cmd) {
+    step->setThreadRunning(true);
+    int rc = system(cmd.c_str());
+    if (rc < 0 && errno == ECHILD) rc = 0; // ignore, it could happens
+    // I know one reason for this is that SICCHLD is set to SIG_IGN but this
+    // should not be the case here. SIGCHLD is explicity set to SIG_DFL
+    // using a sigaction before the call to system(). (Although it is
+    // normally set to SIG_IGN). There should not be any other threads
+    // messing about with SIGCHLD.
+
+    if (rc < 0) {
+      step->setErrorMsg(anna::functions::asString("errno = %d", errno));
+      //std::terminate;
+    }
+    else {
+      rc >>= 8; // divide by 256
+    }
+
+    step->setResultCode(rc);
+    step->complete();
+    // TODO: timeout the system call
+  }
+}
+
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 // TestStep
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -42,7 +71,7 @@ void TestStep::initialize(TestCase *testCase) {
 
 const char* TestStep::asText(const Type::_v type)
 throw() {
-  static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait" };
+  static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Cmd" };
   return text [type];
 }
 
@@ -161,15 +190,19 @@ anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepSendxml");
+  std::string msg = "", xmlmsg = "";
 
   // Message
-  std::string msg = "", xmlmsg = "";
-  if (a_message.isEmpty()) {
-    msg = "<empty>";
+  if (TestManager::instantiate().getDumpHex()) {
+    if (a_message.isEmpty()) {
+      msg = "<empty>";
+    }
+    else {
+      msg = "\n"; msg += a_message.asString(); msg += "\n";
+    }
   }
-  else {
-    msg = "\n"; msg += a_message.asString(); msg += "\n";
-    // Helper
+
+  if (!a_message.isEmpty()) {
     try {
       Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
       static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
@@ -181,9 +214,11 @@ throw() {
     }
   }
 
-  result->createAttribute("Message", msg);
+  if (msg != "") result->createAttribute("Message", msg);
   if (xmlmsg != "") result->createAttribute("XMLMessage", xmlmsg);
   result->createAttribute("Expired", (a_expired ? "yes":"no"));
+  if (a_waitForRequestStepNumber != -1)
+    result->createAttribute("WaitForRequestStepNumber", a_waitForRequestStepNumber);
 
   return result;
 }
@@ -292,7 +327,7 @@ bool TestStepDelay::do_execute() throw() {
 
 void TestStepDelay::do_complete() throw() {
   a_timer = NULL;
-  next(); // next() invoked here because execute() is always false for delay and never dvance the iterator
+  next(); // next() invoked here because execute() is always false for delay and never advance the iterator
 }
 
 void TestStepDelay::do_reset() throw() {
@@ -334,25 +369,36 @@ anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
 throw() {
   anna::xml::Node* result = TestStep::asXML(parent);
   //parent->createChild("TestStepWait");
+  std::string msg = "", xmlmsg = "";
 
   // Condition
   a_condition.asXML(result);
 
+  // Message
+  if (TestManager::instantiate().getDumpHex()) {
+    if (a_message.isEmpty()) {
+      msg = "<empty>";
+    }
+    else {
+      msg = "\n"; msg += a_message.asString(); msg += "\n";
+    }
+  }
+
   if (!a_message.isEmpty()) {
-    // Message
-    result->createAttribute("MatchedMessage", a_message.asString());
-    // Helper
     try {
       Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
       static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
       codecMsg.decode(a_message);
-      result->createAttribute("MatchedXMLMessage", codecMsg.asXMLString());
+      xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
     }
     catch (anna::RuntimeException &ex) {
       ex.trace();
     }
   }
 
+  if (msg != "") result->createAttribute("MatchedMessage", msg);
+  if (xmlmsg != "") result->createAttribute("MatchedXMLMessage", xmlmsg);
+
   return result;
 }
 
@@ -361,7 +407,7 @@ bool TestStepWait::do_execute() throw() {
 }
 
 void TestStepWait::do_complete() throw() {
-  next(); // next() invoked here because execute() never do this.
+  a_testCase->process(); // next() not invoked; we only want to reactivate the test case
 }
 
 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
@@ -380,3 +426,70 @@ void TestStepWait::do_reset() throw() {
   a_serverSession = NULL;
 }
 
+////////////////////////////////////////////////////////////////////////////////////////////////////////
+// TestStepCmd
+////////////////////////////////////////////////////////////////////////////////////////////////////////
+anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent) const
+throw() {
+  anna::xml::Node* result = TestStep::asXML(parent);
+  //parent->createChild("TestStepCmd");
+
+  result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
+  result->createAttribute("Parameters", (a_parameters != "") ? a_parameters:"<no parameters>");
+  result->createAttribute("CommandInProgress", a_threadRunning ? "yes":"no");
+  if (a_errorMsg != "") result->createAttribute("ErrorMessage", a_errorMsg);
+  if (!a_threadRunning && a_resultCode != -2) {
+    result->createAttribute("ResultCode", a_resultCode);
+    //if (a_output != "") result->createAttribute("Output", a_output);
+  }
+
+  return result;
+}
+
+bool TestStepCmd::do_execute() throw() {
+  if (!a_threadRunning) {
+    // Special tags to replace:
+    std::string cmd = getScript();
+    cmd += " ";
+    cmd += getParameters();
+    size_t index;
+    while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID)) != std::string::npos)
+      cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID), anna::functions::asString(TestManager::instantiate().getPoolCycle()));
+    while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID)) != std::string::npos)
+      cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID), anna::functions::asString(a_testCase->getId()));
+    while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID)) != std::string::npos)
+      cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID), anna::functions::asString(getNumber()));
+
+    a_thread = std::thread(cmdRunOnThread, this, cmd);
+    a_thread.detach();
+  }
+
+  return false; // don't go next (wait complete)
+}
+
+void TestStepCmd::do_complete() throw() {
+
+  a_threadRunning = false;
+  if (a_threadDeprecated) {
+    a_threadDeprecated = false;
+    return; // ignore TODO: interrupt the thread to avoid execution of the script
+  }
+
+  if (getResultCode() != 0)
+    a_testCase->setState(TestCase::State::Failed);
+  else
+    next(); // next() invoked here because execute() is always false for delay and never advance the iterator
+}
+
+void TestStepCmd::do_reset() throw() {
+
+  if (a_threadRunning) {
+    std::string s_warn = anna::functions::asString("Thread still in progress: deprecating step %d for Test Case %llu", getNumber(), a_testCase->getId());
+    LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
+    a_threadDeprecated = true;
+  }
+
+  a_resultCode = -2;
+  //a_output = "";
+}
+