1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
13 #include <anna/testing/TestManager.hpp>
15 #include <anna/xml/Compiler.hpp>
16 #include <anna/xml/Node.hpp>
17 #include <anna/core/tracing/Logger.hpp>
18 #include <anna/app/functions.hpp>
19 #include <anna/timex/Engine.hpp>
20 #include <anna/diameter/helpers/base/functions.hpp>
21 #include <anna/diameter/helpers/dcca/functions.hpp>
22 #include <anna/comm/functions.hpp>
23 #include <anna/diameter.comm/ClientSession.hpp>
24 #include <anna/diameter.comm/ServerSession.hpp>
25 #include <anna/testing/TestStep.hpp>
26 #include <anna/testing/TestClock.hpp>
27 #include <anna/diameter/codec/Message.hpp>
33 using namespace anna::testing;
36 ///////////////////////////////////////////////////////////////////////////////////////////////////
37 void TestManager::StatSummary::newTCState(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw() {
39 if ((beginState == TestCase::State::Initialized)&&(endState == TestCase::State::Initialized)) { // special case (new test case provisioning)
45 case TestCase::State::Initialized: a_initializedTcs--; break;
46 case TestCase::State::InProgress: a_inprogressTcs--; break;
47 case TestCase::State::Failed: a_failedTcs--; break;
48 case TestCase::State::Success: a_sucessTcs--; break;
52 case TestCase::State::Initialized: a_initializedTcs++; break;
53 case TestCase::State::InProgress: a_inprogressTcs++; break;
54 case TestCase::State::Failed: a_failedTcs++; break;
55 case TestCase::State::Success: a_sucessTcs++; break;
60 void TestManager::StatSummary::clear() throw() {
67 anna::xml::Node *TestManager::StatSummary::asXML(anna::xml::Node* parent) const throw() {
68 anna::xml::Node* result = parent->createChild("StatSummary");
70 anna::xml::Node* tcs = result->createChild("TestCasesCounts");
71 tcs->createAttribute("Total", a_initializedTcs + a_inprogressTcs + a_failedTcs + a_sucessTcs);
72 tcs->createAttribute("Initialized", a_initializedTcs);
73 tcs->createAttribute("InProgress", a_inprogressTcs);
74 tcs->createAttribute("Failed", a_failedTcs);
75 tcs->createAttribute("Success", a_sucessTcs);
79 ///////////////////////////////////////////////////////////////////////////////////////////////////
83 TestManager::TestManager() :
84 anna::timex::TimeEventObserver("TestManager") {
85 a_timeController = NULL;
86 a_reportsDirectory = "./";
88 a_dumpInProgressReports = false;
89 a_dumpInitializedReports = false;
90 a_dumpFailedReports = false;
91 a_dumpSuccessReports = false;
93 a_dumpHexMessages = false;
94 a_synchronousAmount = 1;
95 a_poolRepeats = 0; // repeat disabled by default
97 a_inProgressLimit = UINT_MAX; // no limit
100 //a_statSummary.clear();
102 a_autoResetHard = false;
104 a_currentTestIt = a_testPool.end();
107 void TestManager::registerKey1(const std::string &key, const TestCase *testCase) throw(anna::RuntimeException) {
109 auto it = a_key1TestCaseMap.find(key);
110 if (it != a_key1TestCaseMap.end()) { // found
111 unsigned int id = it->second->getId();
112 if (id != testCase->getId()) {
113 throw anna::RuntimeException(anna::functions::asString("There is another test case (id = %llu) which registered such key1: %s", id, key.c_str()), ANNA_FILE_LOCATION);
117 a_key1TestCaseMap[key] = const_cast<TestCase*>(testCase);
118 LOGDEBUG(anna::Logger::debug(anna::functions::asString("TestManager::registerKey1 for test case (id = %llu): %s)", testCase->getId(), key.c_str()), ANNA_FILE_LOCATION));
122 void TestManager::registerKey2(const std::string &key, const TestCase *testCase) throw(anna::RuntimeException) {
124 auto it = a_key2TestCaseMap.find(key);
125 if (it != a_key2TestCaseMap.end()) { // found
126 unsigned int id = it->second->getId();
127 if (id != testCase->getId()) {
128 throw anna::RuntimeException(anna::functions::asString("There is another test case (id = %llu) which registered such key2: %s", id, key.c_str()), ANNA_FILE_LOCATION);
132 a_key2TestCaseMap[key] = const_cast<TestCase*>(testCase);
133 LOGDEBUG(anna::Logger::debug(anna::functions::asString("TestManager::registerKey2 for test case (id = %llu): %s)", testCase->getId(), key.c_str()), ANNA_FILE_LOCATION));
137 TestTimer* TestManager::createTimer(TestCaseStep* testCaseStep, const anna::Millisecond &timeout, const TestTimer::Type::_v type)
138 throw(anna::RuntimeException) {
139 TestTimer* result(NULL);
141 if(a_timeController == NULL)
142 throw anna::RuntimeException("You must invoke 'setTimerController' with a not NULL timex engine", ANNA_FILE_LOCATION);
144 anna::Guard guard(a_timeController, "TestManager::createTimer"); // avoid interblocking
145 result = a_timers.create();
146 result->setType(type);
147 result->setId((anna::timex::TimeEvent::Id) testCaseStep);
148 result->setObserver(this);
149 result->setContext(testCaseStep);
150 result->setTimeout(timeout);
153 std::string msg("TestManager::createTimer | ");
154 msg += result->asString();
155 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
158 a_timeController->activate(result);
162 void TestManager::cancelTimer(TestTimer* timer)
168 std::string msg("TestManager::cancel | ");
169 msg += timer->asString();
170 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
174 if(a_timeController == NULL)
175 a_timeController = anna::app::functions::component <anna::timex::Engine> (ANNA_FILE_LOCATION);
177 a_timeController->cancel(timer);
178 } catch(anna::RuntimeException& ex) {
183 //------------------------------------------------------------------------------------------
184 // Se invoca automaticamente desde anna::timex::Engine
185 //------------------------------------------------------------------------------------------
186 void TestManager::release(anna::timex::TimeEvent* timeEvent)
188 TestTimer* timer = static_cast <TestTimer*>(timeEvent);
189 timer->setContext(NULL);
190 a_timers.release(timer);
193 bool TestManager::configureTTPS(int testTicksPerSecond) throw() {
195 if (testTicksPerSecond == 0) {
197 a_timeController->cancel(a_clock);
198 LOGDEBUG(anna::Logger::debug("Testing timer clock stopped !", ANNA_FILE_LOCATION));
201 LOGDEBUG(anna::Logger::debug("No testing timer started yet !", ANNA_FILE_LOCATION));
205 else if (testTicksPerSecond < 0) {
206 LOGWARNING(anna::Logger::warning("Invalid 'ttps' provided", ANNA_FILE_LOCATION));
210 anna::Millisecond applicationTimeInterval = anna::Millisecond(1000 / testTicksPerSecond);
211 a_synchronousAmount = 1;
213 if (applicationTimeInterval < anna::Millisecond(1)) {
214 LOGWARNING(anna::Logger::warning("Not allowed to configure more than 1000 events per second for for triggering testing system", ANNA_FILE_LOCATION));
218 if (applicationTimeInterval < a_timeController->getResolution()) {
219 int maximumObtained = 1000 / (int)(a_timeController->getResolution());
220 a_synchronousAmount = ceil((double)testTicksPerSecond/maximumObtained);
222 applicationTimeInterval = anna::Millisecond(a_synchronousAmount * 1000 / testTicksPerSecond);
225 if (a_synchronousAmount > 1) {
227 std::string msg = anna::functions::asString("Desired testing time trigger rate (%d events per second) requires more than one sending per event (%d every %lld milliseconds). Consider launch more instances with lower rate (for example %d ADML processes with %d ttps), or configure %d or more sockets to the remote endpoints to avoid burst sendings",
230 applicationTimeInterval.getValue(),
232 1000/applicationTimeInterval,
233 a_synchronousAmount);
235 anna::Logger::warning(msg, ANNA_FILE_LOCATION);
240 a_clock->setTimeout(applicationTimeInterval);
243 a_clock = new TestClock("Testing clock", applicationTimeInterval, this); // clock
246 if (!a_clock->isActive()) a_timeController->activate(a_clock);
251 bool TestManager::gotoTestCase(unsigned int id) throw() {
252 test_pool_it it = a_testPool.find(id);
253 if (it != a_testPool.end()) {
254 a_currentTestIt = it;
261 TestCase *TestManager::findTestCase(unsigned int id) const throw() { // id = -1 provides current test case triggered
263 if (!tests()) return NULL;
264 test_pool_it it = ((id != -1) ? a_testPool.find(id) : a_currentTestIt);
265 if (it != a_testPool.end()) return const_cast<TestCase*>(it->second);
269 TestCase *TestManager::getTestCase(unsigned int id) throw() {
271 test_pool_nc_it it = a_testPool.find(id);
272 if (it != a_testPool.end()) return it->second;
274 TestCase *result = new TestCase(id);
275 a_testPool[id] = result;
279 bool TestManager::clearPool() throw() {
280 if (!tests()) return false;
281 for (test_pool_it it = a_testPool.begin(); it != a_testPool.end(); it++) delete it->second;
282 // TODO: stop the possible command threads or there will be a core dump
285 a_key1TestCaseMap.clear();
286 a_key2TestCaseMap.clear();
287 a_currentTestIt = a_testPool.end();
289 configureTTPS(0); // stop
290 a_statSummary.clear();
294 bool TestManager::resetPool(bool hard) throw() {
295 bool result = false; // any reset
297 if (!tests()) return result;
298 for (test_pool_nc_it it = a_testPool.begin(); it != a_testPool.end(); it++) {
299 if (it->second->reset(hard))
302 //a_key1TestCaseMap.clear();
303 //a_key2TestCaseMap.clear();
307 bool TestManager::tick() throw() {
308 LOGDEBUG(anna::Logger::debug("New test clock tick !", ANNA_FILE_LOCATION));
309 return execTestCases(a_synchronousAmount);
312 bool TestManager::execTestCases(int sync_amount) throw() {
315 LOGWARNING(anna::Logger::warning("Testing pool is empty. You need programming", ANNA_FILE_LOCATION));
319 // Synchronous sendings per tick:
320 int count = sync_amount;
322 if (!nextTestCase()) return false; // stop the clock
329 bool TestManager::nextTestCase() throw() {
333 // Limit for in-progress test cases:
334 if (getInProgressCount() >= a_inProgressLimit) {
335 LOGDEBUG(anna::Logger::debug(anna::functions::asString("TestManager next case ignored (over in-progress count limit: %llu)", a_inProgressLimit), ANNA_FILE_LOCATION));
336 return true; // wait next tick to release OTA test cases
340 if (a_currentTestIt == a_testPool.end())
341 a_currentTestIt = a_testPool.begin();
346 if (a_currentTestIt == a_testPool.end()) {
347 if ((a_poolCycle > a_poolRepeats) && (a_poolRepeats != -1)) {
348 LOGWARNING(anna::Logger::warning("Testing pool cycle completed. No remaining repeat cycles left. Suspending", ANNA_FILE_LOCATION));
354 std::string nolimit = (a_poolRepeats != -1) ? "":" [no limit]";
355 anna::Logger::warning(anna::functions::asString("Testing pool cycle %d completed (repeats configured: %d%s). Restarting for the %s cycle", a_poolCycle, a_poolRepeats, nolimit.c_str(), (a_poolRepeats == a_poolCycle) ? "last":"next"), ANNA_FILE_LOCATION);
358 //a_currentTestIt = a_testPool.begin();
359 return true; // avoids infinite loop: if the cycle takes less time than test cases completion, below reset never will turns state
360 // into Initialized and this while will be infinite. It is preferable to wait one tick when the cycle is completed.
364 // Hard reset or soft reset to initialize already finished (in previous cycle) test cases:
365 a_currentTestIt->second->reset(a_autoResetHard);
367 // Process test case:
368 LOGDEBUG(anna::Logger::debug(anna::functions::asString("Processing test case id = %llu, currently '%s' state", a_currentTestIt->first, TestCase::asText(a_currentTestIt->second->getState())), ANNA_FILE_LOCATION));
369 if (a_currentTestIt->second->getState() != TestCase::State::InProgress) {
370 a_currentTestIt->second->process();
371 return true; // is not probably to reach still In-Progress test cases from previous cycles due to the whole
372 // time for complete the test cases pool regarding the single test case lifetime. You shouldn't
373 // forget to programm a test case timeout with a reasonable value
378 TestCase *TestManager::getDiameterTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw() {
380 sessionId = anna::diameter::helpers::base::functions::getSessionId(message);
382 catch (anna::RuntimeException &ex) {
384 LOGDEBUG(anna::Logger::debug("Cannot get the Session-Id from received DataBlock in order to identify the Test Case", ANNA_FILE_LOCATION));
387 auto sessionIdIt = a_key1TestCaseMap.find(sessionId);
388 if (sessionIdIt != a_key1TestCaseMap.end())
389 return sessionIdIt->second;
391 LOGDEBUG(anna::Logger::debug(anna::functions::asString("Cannot identify the Test Case for received Session-Id: %s", sessionId.c_str()), ANNA_FILE_LOCATION));
395 TestCase *TestManager::getDiameterTestCaseFromSubscriberId(const anna::DataBlock &message, std::string &subscriberId) throw() {
397 subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
398 if (subscriberId == "") // try with IMSI
399 subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
401 catch (anna::RuntimeException &ex) {
403 LOGDEBUG(anna::Logger::debug("Cannot get the Subscriber-Id from received DataBlock in order to identify the Test Case", ANNA_FILE_LOCATION));
406 auto subscriberIdIt = a_key2TestCaseMap.find(subscriberId);
407 if (subscriberIdIt != a_key2TestCaseMap.end())
408 return subscriberIdIt->second;
410 LOGDEBUG(anna::Logger::debug(anna::functions::asString("Cannot identify the Test Case for received Subscriber-Id: %s", subscriberId.c_str()), ANNA_FILE_LOCATION));
414 void TestManager::receiveDiameterMessage(const anna::DataBlock &message, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException) {
417 if (!tests()) return;
419 // Identify the test case:
420 std::string sessionId, subscriberId;
422 tc = getDiameterTestCaseFromSessionId(message, sessionId);
424 tc = getDiameterTestCaseFromSubscriberId(message, subscriberId);
426 LOGWARNING(anna::Logger::warning(anna::comm::functions::asText("Cannot identify the Test Case for the message received from server: ", message), ANNA_FILE_LOCATION)); // this should not appear
430 // Work with Test case:
431 TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, true /* comes from entity */);
432 if (!tsw) { // store as 'uncovered'
433 std::string hint = "Uncovered condition for received message from entity over Session-Id '"; hint += sessionId; hint += "'; ";
436 static anna::diameter::codec::Message codecMsg;
437 codecMsg.decode(message);
438 hint += "HEX Message: '"; hint += anna::functions::asHexString(message);
439 hint += "'; XML Message:\n"; hint += codecMsg.asXMLString();
441 catch (anna::RuntimeException &ex) {
443 hint += ex.asString();
445 hint += "\nClient Session: "; hint += clientSession->asString();
447 tc->addDebugSummaryHint(hint);
450 tsw->setClientSession(const_cast<anna::diameter::comm::ClientSession*>(clientSession));
455 void TestManager::receiveDiameterMessage(const anna::DataBlock &message, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException) {
458 if (!tests()) return;
460 // Identify the test case:
461 std::string sessionId, subscriberId;
463 tc = getDiameterTestCaseFromSessionId(message, sessionId);
465 tc = getDiameterTestCaseFromSubscriberId(message, subscriberId);
467 LOGWARNING(anna::Logger::warning(anna::comm::functions::asText("Cannot identify the Test Case for the message received from client: ", message), ANNA_FILE_LOCATION)); // this should not appear
471 // Work with Test case:
472 TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, false /* comes from client */);
473 if (!tsw) { // store as 'uncovered'
474 std::string hint = "Uncovered condition for received message from client over Session-Id '"; hint += sessionId; hint += "'; ";
477 static anna::diameter::codec::Message codecMsg;
478 codecMsg.decode(message);
479 hint += "HEX Message: '"; hint += anna::functions::asHexString(message);
480 hint += "'; XML Message:\n"; hint += codecMsg.asXMLString();
482 catch (anna::RuntimeException &ex) {
484 hint += ex.asString();
486 hint += "\nServer Session: "; hint += serverSession->asString();
488 tc->addDebugSummaryHint(hint);
491 tsw->setServerSession(const_cast<anna::diameter::comm::ServerSession*>(serverSession));
496 anna::xml::Node* TestManager::asXML(anna::xml::Node* parent) const
498 anna::xml::Node* result = parent->createChild("TestManager");
500 int poolSize = a_testPool.size();
501 result->createAttribute("NumberOfTestCases", poolSize);
502 if (a_poolRepeats) result->createAttribute("PoolRepeats", a_poolRepeats);
503 else result->createAttribute("PoolRepeats", "disabled");
504 result->createAttribute("PoolCycle", a_poolCycle);
505 a_statSummary.asXML(result);
506 if (a_inProgressLimit == UINT_MAX)
507 result->createAttribute("InProgressLimit", "<no limit>");
509 result->createAttribute("InProgressLimit", a_inProgressLimit);
510 result->createAttribute("DumpInitializedReports", (a_dumpInitializedReports ? "yes":"no"));
511 result->createAttribute("DumpInProgressReports", (a_dumpInProgressReports ? "yes":"no"));
512 result->createAttribute("DumpFailedReports", (a_dumpFailedReports ? "yes":"no"));
513 result->createAttribute("DumpSuccessReports", (a_dumpSuccessReports ? "yes":"no"));
514 result->createAttribute("DumpHexMessages", (a_dumpHexMessages ? "yes":"no"));
515 result->createAttribute("AutoResetHard", (a_autoResetHard ? "yes":"no"));
516 result->createAttribute("ReportsDirectory", a_reportsDirectory);
518 result->createAttribute("AsynchronousSendings", a_synchronousAmount);
519 int ticksPerSecond = (a_synchronousAmount * 1000) / a_clock->getTimeout();
520 result->createAttribute("TicksPerSecond", ticksPerSecond);
522 if (a_currentTestIt != a_testPool.end()) {
523 result->createAttribute("CurrentTestCaseId", (*a_currentTestIt).first);
526 anna::xml::Node* testCases = result->createChild("TestCases");
527 for (test_pool_it it = a_testPool.begin(); it != a_testPool.end(); it++) {
528 if (((*it).second->getState() == TestCase::State::Initialized) && (!getDumpInitializedReports())) continue;
529 if (((*it).second->getState() == TestCase::State::InProgress) && (!getDumpInProgressReports())) continue;
530 if (((*it).second->getState() == TestCase::State::Failed) && (!getDumpFailedReports())) continue;
531 if (((*it).second->getState() == TestCase::State::Success) && (!getDumpSuccessReports())) continue;
532 (*it).second->asXML(testCases);
539 std::string TestManager::asXMLString() const throw() {
540 anna::xml::Node root("root");
541 return anna::xml::Compiler().apply(asXML(&root));