diff --git a/akonadi/tests/testrunner/setup.cpp b/akonadi/tests/testrunner/setup.cpp index d36d6c3a8..1abb50757 100644 --- a/akonadi/tests/testrunner/setup.cpp +++ b/akonadi/tests/testrunner/setup.cpp @@ -1,207 +1,208 @@ /* * Copyright (c) 2008 Igor Trindade Oliveira * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "setup.h" #include "config.h" #include "symbols.h" #include #include #include #include #include #include #include #include QMap SetupTest::getEnvironment() { QMap env; foreach (const QString& val, QProcess::systemEnvironment()) { int p = val.indexOf('='); if (p > 0) { env[val.left(p).toUpper()] = val.mid(p+1); } } return env; } bool SetupTest::clearEnvironment() { QMap environment = getEnvironment(); foreach(const QString& s, environment.keys()) { if (s != "HOME") { if ( !unsetenv( s.toAscii() )) { return false; } } } return true; } int SetupTest::addDBusToEnvironment(QIODevice& io) { QByteArray data = io.readLine(); int pid = -1; Symbols *symbol = Symbols::getInstance(); while (data.size()) { if (data[data.size()-1] == '\n') { data.resize(data.size()-1); } QString val(data); int p = val.indexOf('='); if (p > 0) { QString name = val.left(p).toUpper(); val = val.mid(p+1); if (name == "DBUS_SESSION_BUS_PID") { pid = val.toInt(); setenv(name.toAscii(), val.toAscii(), 1); symbol->insertSymbol(name, val); } else if (name == "DBUS_SESSION_BUS_ADDRESS") { setenv(name.toAscii(), val.toAscii(), 1); symbol->insertSymbol(name, val); } } data = io.readLine(); } return pid; } int SetupTest::startDBusDaemon() { QProcess dbusprocess; QStringList dbusargs; dbusprocess.start("/usr/bin/dbus-launch", dbusargs); bool ok = dbusprocess.waitForStarted() && dbusprocess.waitForFinished(); if (!ok) { kDebug() << "error starting dbus-launch"; dbusprocess.kill(); return -1; } int dbuspid = addDBusToEnvironment(dbusprocess); return dbuspid; } void SetupTest::stopDBusDaemon(int dbuspid) { kDebug() << dbuspid; if (dbuspid) kill(dbuspid, 15); sleep(1); if (dbuspid) kill(dbuspid, 9); } void SetupTest::registerWithInternalDBus( const QString &address ) { mInternalBus = new QDBusConnection( QDBusConnection::connectToBus( address, QLatin1String( "InternalBus" ) ) ); mInternalBus->registerService( QLatin1String( "org.kde.Akonaditest" ) ); mInternalBus->registerObject(QLatin1String("/MainApplication"), KApplication::kApplication(), QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableProperties | QDBusConnection::ExportAdaptors); QDBusConnectionInterface *busInterface = mInternalBus->interface(); connect( busInterface, SIGNAL( serviceOwnerChanged( QString, QString, QString ) ), this, SLOT( dbusNameOwnerChanged( QString, QString, QString ) ) ); } void SetupTest::startAkonadiDaemon() { QString akonadiDaemon = "akonadi_control"; QStringList args; akonadiDaemonProcess->start(akonadiDaemon, args); akonadiDaemonProcess->waitForStarted(5000); kDebug()<pid(); } void SetupTest::stopAkonadiDaemon() { akonadiDaemonProcess->terminate(); if (!akonadiDaemonProcess->waitForFinished(5000)) { kDebug() << "Problem finishing process."; } akonadiDaemonProcess->close(); } void SetupTest::setupAgents() { Config *config = Config::getInstance(); QDBusInterface agentDBus( "org.freedesktop.Akonadi.Control", "/AgentManager", "org.freedesktop.Akonadi.AgentManager", *mInternalBus ); kDebug() << "available agent types:" << agentDBus.call( QLatin1String( "agentTypes" ) ); kDebug() << config->getAgents(); foreach(const QString &agent, config->getAgents()){ kDebug() << "inserting resource:"<getKdeHome().toAscii() , 1 ); setenv("XDG_DATA_HOME", config->getXdgDataHome().toAscii() , 1 ); setenv("XDG_CONFIG_HOME", config->getXdgConfigHome().toAscii() , 1 ); Symbols *symbol = Symbols::getInstance(); symbol->insertSymbol("KDEHOME", config->getKdeHome()); symbol->insertSymbol("XDG_DATA_HOME", config->getXdgDataHome()); symbol->insertSymbol("XDG_CONFIG_HOME", config->getXdgConfigHome()); + symbol->insertSymbol("AKONADI_TESTRUNNER_PID", QString::number( QCoreApplication::instance()->applicationPid() ) ); dpid = startDBusDaemon(); #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 2)) const QString dbusAddress = symbol->getSymbols()[ "DBUS_SESSION_BUS_ADDRESS" ]; registerWithInternalDBus( dbusAddress ); #endif akonadiDaemonProcess = new QProcess(); } SetupTest::~SetupTest() { stopAkonadiDaemon(); stopDBusDaemon(dpid); delete akonadiDaemonProcess; } diff --git a/akonadi/tests/testrunner/shellscript.cpp b/akonadi/tests/testrunner/shellscript.cpp index 1d25b4799..bac2fa66b 100644 --- a/akonadi/tests/testrunner/shellscript.cpp +++ b/akonadi/tests/testrunner/shellscript.cpp @@ -1,62 +1,80 @@ /* * Copyright (c) 2008 Igor Trindade Oliveira * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "config.h" #include "shellscript.h" #include #include #include shellScript::shellScript() { - symbol = Symbols::getInstance(); + symbol = Symbols::getInstance(); } void shellScript::writeEnvironmentVariables() { QHashIterator i( symbol->getSymbols() ); - + while( i.hasNext() ) { i.next(); script.append( i.key() ); script.append( "=" ); script.append( i.value() ); - script.append( "\n" ); - + script.append( "\n" ); + script.append("export "); script.append( i.key() ); - script.append("\n"); + script.append("\n"); } - script.append("\n\n"); + script.append("\n\n"); +} + +void shellScript::writeShutdownFunction() +{ + const QString s = + "function shutdown-testenvironment()\n" + "{\n" + " echo Stopping Akonadi server\n" + " qdbus org.freedesktop.Akonadi.Control /ControlManager org.freedesktop.Akonadi.ControlManager.shutdown\n" + " echo \"Stopping testrunner with PID \" $TESTRUNNER_PID\n" + " kill $AKONADI_TESTRUNNER_PID\n" + " # wait a bit before killing D-Bus\n" + " echo \"Waiting 10 seconds before killing D-Bus\"\n" + " sleep 10\n" + " echo \"Killing D-Bus with PID \" $DBUS_SESSION_BUS_PID\n" + " kill $DBUS_SESSION_BUS_PID\n" + "}\n\n"; + script.append( s ); } void shellScript::makeShellScript() { QFile file("testenvironment.sh"); //can user define the file name/location? file.open( QIODevice::WriteOnly ); - + writeEnvironmentVariables(); - + writeShutdownFunction(); + //script.append("exec /usr/bin/dbus-launch \n"); - //script.append("exec akonadiconsole\n"); + //script.append("exec akonadiconsole\n"); file.write(script.toAscii(), qstrlen(script.toAscii()) ); file.close(); } - diff --git a/akonadi/tests/testrunner/shellscript.h b/akonadi/tests/testrunner/shellscript.h index f202c1682..63d5d27f8 100644 --- a/akonadi/tests/testrunner/shellscript.h +++ b/akonadi/tests/testrunner/shellscript.h @@ -1,33 +1,34 @@ /* * Copyright (c) 2008 Igor Trindade Oliveira * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include #include "symbols.h" class shellScript { private: Symbols *symbol; - QString script; + QString script; - void writeEnvironmentVariables(); + void writeEnvironmentVariables(); + void writeShutdownFunction(); public: shellScript(); void makeShellScript(); - + }; - +