Blame view

server/kernel/test/Fitnesse/CSlimServer/src/TestManager.cpp 5.16 KB
346b85c6   Benjamin Renard   First commit with...
1
2
3
4
#include "TestManager.h"

#include <iostream>
#include <fstream>
c377ca43   Benjamin Renard   Update tests suite
5
#include <unistd.h>
346b85c6   Benjamin Renard   First commit with...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

#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;
		}
	}
}