TestManager.cpp 5.16 KB
#include "TestManager.h"

#include <iostream>
#include <fstream>
#include <unistd.h>

#include "Toolbox.h"

using namespace std;
using namespace TREPS::Common;
using namespace TREPS::XMLManager;

namespace TREPS
{
	namespace Test
	{
		TestManagerClass::TestManagerClass(void) : kernelDir("")
		{
		}

		TestManagerClass::~TestManagerClass(void)
		{

		}

		void TestManagerClass::setKernelDir(const char *kernelDir)
		{
			this->kernelDir = kernelDir;
		}

		string TestManagerClass::getKernelDir(void)
		{
			return this->kernelDir;
		}

		void TestManagerClass::setCOTSDir(const char *cotsDir)
		{
			this->cotsDir = cotsDir;
		}

		string TestManagerClass::getCOTSDir(void)
		{
			return this->cotsDir;
		}

		string TestManagerClass::getKernelPath(void)
		{
			string path = getPathCorrection(this->getKernelDir().c_str());
			path += TREPS_BIN;
			return path;
		}

		string TestManagerClass::getValgrindPath(void)
		{
			string path = getPathCorrection(this->getCOTSDir().c_str());
			path += VALGRIND_BIN;
			return path;
		}

		string TestManagerClass::setLibPath(void)
		{
			string cots = getPathCorrection(this->getCOTSDir().c_str());

			string log4cxx = cots;
			log4cxx += "log4cxx/lib";

			string apr = cots;
			apr += "apr/lib";

			string aprutil = cots;
			aprutil += "apr-util/lib";

			string cppunit = cots;
			cppunit += "cppunit/lib";

			string file = cots;
			file += "file/lib";

			string cdf = cots;
			cdf += "cdf/lib";

			string netcdf = cots;
			netcdf += "netcdf/lib";

			string gsoap = cots;
			gsoap += "gsoap/lib";

			string ldlibpath = "";
			if (getenv("LD_LIBRARY_PATH") != NULL)
			{
				ldlibpath += getenv("LD_LIBRARY_PATH");
				ldlibpath += ":";
			}
			ldlibpath += log4cxx;
			ldlibpath += ":";
			ldlibpath += apr;
			ldlibpath += ":";
			ldlibpath += aprutil;
			ldlibpath += ":";
			ldlibpath += cppunit;
			ldlibpath += ":";
			ldlibpath += file;
			ldlibpath += ":";
			ldlibpath += cdf;
			ldlibpath += ":";
			ldlibpath += netcdf;
			ldlibpath += ":";
			ldlibpath += gsoap;

			setenv("LD_LIBRARY_PATH", ldlibpath.c_str(), 1);

			if (getenv("LD_LIBRARY_PATH") == NULL)
				return "";

			return getenv("LD_LIBRARY_PATH");
		}

		bool TestManagerClass::executeRequest(const char *requestPath, bool useValgrind)
		{
			chdir(this->getKernelDir().c_str());
			string cmd = "";

			if (useValgrind)
			{
				//valgrind
				cmd += this->getValgrindPath();
				cmd += " --quiet --leak-check=full --xml=yes --xml-file=valgrind-report-%p.xml ";
			}

			//kernel
			cmd += this->getKernelPath();
			cmd += " ";
			cmd += requestPath;

			this->setLibPath();

			string output;

			cout << "REQUEST COMMAND LINE : " << cmd << endl;

			int status = executeSystemCommand(cmd.c_str(),output);

			cout << output;
			cout << "STATUS CODE : " << status << endl;

			return (!sysCmdError(status));
		}

		string TestManagerClass::createResultLoader(const char *resultFile, XMLManagerClass *&resultLoader)
		{
			resultLoader = new XMLManagerClass();

			//test if result file exist
			if (!resultLoader->isExist(resultFile))
			{
				this->deleteResultLoader(resultLoader);
				return "NO_RESULT_FILE";
			}

			//load xml file
			if (!resultLoader->loadFile(resultFile,true))
			{
				this->deleteResultLoader(resultLoader);
				return "CANNOT_LOAD_RESULT_FILE";
			}

			return "OK";
		}

		void TestManagerClass::deleteResultLoader(XMLManagerClass *&resultLoader)
		{
			if (resultLoader == NULL)
				return;

			resultLoader->close();

			delete resultLoader;
			resultLoader = NULL;
		}

		string TestManagerClass::getResultNode(XMLManagerClass *resultLoader, Node *&resultNode, bool &success)
		{
			resultNode = NULL;
			success = false;

			if (resultLoader == NULL)
				return "NO_RESULT_LOADER";

			resultNode = resultLoader->getChildFromRoot("result");

			if (resultNode == NULL)
				return "NO_RESULT_NODE";

			//get success attribute
			string successStr = resultLoader->getAttributeFromNode("success", resultNode);

			success = (successStr.compare("true") == 0);

			return "OK";
		}


		string TestManagerClass::getResultVar(const char *resultFile, const char *xpathExpr, const char *attribute)
		{
			XMLManagerClass *xmlMgr = NULL;

			string res = this->createResultLoader(resultFile,xmlMgr);
		
			if (xmlMgr == NULL)
				return res;

			//get result node
			Node *resultNode = NULL;
			bool success = false;

			res = this->getResultNode(xmlMgr,resultNode,success);

			if (resultNode == NULL)
			{
				this->deleteResultLoader(xmlMgr);
				return res;
			}

			if (!success)
			{
				//get error msg
				Node *errorNode = xmlMgr->getChildFromNode("error",resultNode);

				if (errorNode == NULL)
					res = "NO_ERROR_MSG";
				else
				{
					res = "ERROR_MSG : ";
					res += xmlMgr->getNodeContent(errorNode);
				}
			}
			else
			{
				//get var node
				NodeList resNodes = xmlMgr->evaluateXPathExpression(xpathExpr);
				if (resNodes.empty())
					res = "NOT_REACHABLE";
				else
				{
					if (resNodes.size() > 1)
						res = "TOO_MANY_MATCH";
					else
					{
						if (attribute != NULL)
							res = xmlMgr->getAttributeFromNode(attribute,*resNodes.begin());
						else
							res = xmlMgr->getNodeContent(*resNodes.begin());
					}
				}
			}

			this->deleteResultLoader(xmlMgr);

			return res;
		}
	}
}