#include <dpl/test/test_runner.h>
+#include <algorithm>
#include <sstream>
#include <unistd.h>
#include <vector>
}
-ServiceManager::ServiceManager(const std::string &serviceName)
+ServiceManager::ServiceManager(const std::string &serviceName,
+ const std::vector<std::string>& socketsNames)
: m_connection(DBUS_BUS_SYSTEM, true)
, m_serviceName(serviceName)
+ , m_socketsNames(socketsNames)
{
addBusMatch(MATCH_JOB_REMOVED);
addBusMatch(MATCH_JOB_NEW);
return iteratorSub.getArgUint64();
}
-void ServiceManager::sendToService(const std::string &method)
+void ServiceManager::sendToService(const std::string &method, const std::string &unit)
{
DBus::MessageOut messageOut = newMethodCall(method);
- messageOut.append(m_serviceName);
+ messageOut.append(unit);
messageOut.append("fail");
DBus::MessageIn messageIn = m_connection.sendWithReplyAndBlock(messageOut);
m_runningJobs.insert(handleObjectPathMsgReply(messageIn));
iterator.expectArgType(DBUS_TYPE_STRING);
std::string result = iterator.getArgString();
- if(m_serviceName == unit) {
+ if (unit == m_serviceName
+ || std::count(m_socketsNames.begin(), m_socketsNames.end(), unit) > 0) {
RUNNER_ASSERT_MSG(result == "done" || result == "canceled",
"RemoveJob signal delivered bad news. Job wasn't completed successfully: "
<< "expected job results = {done, canceled}, "
m_connection.readWriteDispatch();
}
-void ServiceManager::startService()
+void ServiceManager::executeMethod(const std::string &method, const std::string &unit)
{
- sendToService("StartUnit");
+ sendToService(method, unit);
waitForRunningJobsFinish();
sendResetFailedToService();
}
-void ServiceManager::stopService()
+void ServiceManager::startService(bool withSockets)
{
- sendToService("StopUnit");
- waitForRunningJobsFinish();
- sendResetFailedToService();
+ executeMethod("StartUnit", m_serviceName);
+ if (withSockets)
+ for (const auto &socket : m_socketsNames)
+ executeMethod("StartUnit", socket);
}
-void ServiceManager::restartService()
+void ServiceManager::stopService(bool withSockets)
{
- sendToService("RestartUnit");
- waitForRunningJobsFinish();
- sendResetFailedToService();
+ if (withSockets)
+ for (const auto &socket : m_socketsNames)
+ executeMethod("StopUnit", socket);
+ executeMethod("StopUnit", m_serviceName);
+}
+
+void ServiceManager::restartService(bool withSockets)
+{
+ if (withSockets)
+ for (const auto &socket : m_socketsNames)
+ executeMethod("StopUnit", socket);
+
+ executeMethod("RestartUnit", m_serviceName);
+
+ if (withSockets)
+ for (const auto &socket : m_socketsNames)
+ executeMethod("StartUnit", socket);
}
pid_t ServiceManager::getServicePid()
class ServiceManager {
public:
ServiceManager() = delete;
- ServiceManager(const std::string &serviceName);
+ ServiceManager(const std::string &serviceName,
+ const std::vector<std::string>& socketsNames = {});
~ServiceManager() = default;
- void startService();
- void stopService();
- void restartService();
+ void startService(bool withSockets = false);
+ void stopService(bool withSockets = false);
+ void restartService(bool withSockets = false);
pid_t getServicePid();
timeval getServiceStartTimestamp();
void maskService();
uint32_t handleVariantUIntMsgReply(DBus::MessageIn &messageIn);
uint64_t handleVariantUInt64MsgReply(DBus::MessageIn &messageIn);
- void sendToService(const std::string &method);
+ void sendToService(const std::string &method, const std::string &unit);
void sendMaskToService();
void sendUnmaskToService();
DBus::MessageIn sendPropertyGetMsg(const std::string &interface, const std::string &property);
void signalReloadingHandler(DBus::MessageIn &messageIn);
void waitForRunningJobsFinish();
+ void executeMethod(const std::string &method, const std::string &unit);
+
DBus::Connection m_connection;
const std::string m_serviceName;
+ const std::vector<std::string> m_socketsNames;
std::string m_unitPath;
std::set<std::string> m_runningJobs;
const std::string USER("cynara");
const std::string LABEL("System");
const std::string SERVICE("cynara.service");
+const std::string SOCKET_CLIENT("cynara.socket");
+const std::string SOCKET_ADMIN("cynara-admin.socket");
+const std::string SOCKET_AGENT("cynara-agent.socket");
+
const std::string SERVICE_PLUGINS_DIR("/usr/lib/cynara/plugin/service/");
}
for (const auto &dir : pluginDirectories)
FileOperations::copyCynaraFiles(dir.c_str(), CynaraTestConsts::SERVICE_PLUGINS_DIR);
}
+
+void restartCynaraService()
+{
+ ServiceManager service(CynaraTestConsts::SERVICE);
+ service.restartService();
+}
+
+void restartCynaraServiceAndSockets()
+{
+ ServiceManager service(CynaraTestConsts::SERVICE, { CynaraTestConsts::SOCKET_CLIENT,
+ CynaraTestConsts::SOCKET_ADMIN,
+ CynaraTestConsts::SOCKET_AGENT });
+
+ service.restartService(true);
+}