Commit 5c251fa3 authored by Anastasia Karachaliou's avatar Anastasia Karachaliou
Browse files

Acsd daemon running with unittests for parsing the arguments

parent 362729e1
......@@ -241,6 +241,7 @@ Unit tests and system tests with virtual tape drives
%{_libdir}/libctatapeserverscsiunittests.so*
%{_libdir}/libctadaemonunittests.so*
%{_libdir}/libctamediachangerunittests.so*
%{_libdir}/libctamediachangeracsdaemonunittests.so*
%{_bindir}/cta-systemTests
%{_libdir}/libctadaemonunittests-multiprocess.so*
%attr(0644,root,root) %{_datadir}/%{name}-%{ctaVersion}/unittest/*.suppr
......
......@@ -26,6 +26,7 @@
#include "AcsdConfiguration.hpp"
#include "mediachanger/acs/daemon/AcsMessageHandler.hpp"
//#include "AcsPendingRequests.hpp"
#include "mediachanger/acs/daemon/AcsdCmdLine.hpp"
#include "common/exception/Errnum.hpp"
#include "common/exception/BadAlloc.hpp"
#include "common/exception/Exception.hpp"
......@@ -71,7 +72,6 @@ std::string cta::mediachanger::acs::daemon::AcsDaemon::getHostName() const {
if(gethostname(nameBuf, sizeof(nameBuf))) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to get host name: "<<ex.getMessage().str();
//cta::log::write(LOG_ERR, ex.getMessage().str());
m_log(LOG_ERR, ex.getMessage().str());
throw ex;
}
......@@ -136,54 +136,7 @@ int AcsDaemon::main() throw() {
void AcsDaemon::exceptionThrowingMain(
const int argc, char **const argv) {
logStartOfDaemon(argc, argv);
////parseCommandLine(argc, argv);
bool foreground = false; ///< Prevents daemonisation
std::string configFileLocation = "/etc/cta/cta-acsd.conf"; ///< Location of the configuration file. Defaults to /etc/cta/cta-taped.conf
bool helpRequested = false; ///< Help requested: will print out help and exit.
// bool logToStdout =false;
static struct option longopts[] = {
// { .name, .has_args, .flag, .val } (see getopt.h))
{ "foreground", no_argument, NULL, 'f' },
{ "config", required_argument, NULL, 'c' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, '\0' }
};
char c;
// Reset getopt's global variables to make sure we start fresh
optind=0;
// Prevent getopt from printing out errors on stdout
opterr=0;
// We ask getopt to not reshuffle argv ('+')
while ((c = getopt_long(argc, argv, "+fsc:l:h", longopts, NULL)) != -1) {
m_log(LOG_INFO, "Usage: [options]\n");
switch (c) {
case 'f':
foreground = true;
m_foreground = foreground;
break;
case 'c':
configFileLocation = optarg;
break;
case 'h':
helpRequested = true;
std::cout<<"Usage: "<<m_programName<<" [options]\n"
"where options can be:\n"
"--foreground or -f Remain in the Foreground\n"
"--config <config-file> or -c Configuration file\n"
"--help or -h Print this help and exit\n";
break;
default:
break;
}
}
//Convert the command line into set of parameters for logging.
std::list<cta::log::Param> ret;
ret.push_back({"foreground", foreground});
ret.push_back({"configFileLocation", configFileLocation});
ret.push_back({"helpRequested", helpRequested});
AcsdCmdLine(argc,argv); //parse command line
const std::string runAsStagerSuperuser = "ACSD";
const std::string runAsStagerSupergroup = "CTA";
......
......@@ -28,7 +28,7 @@
#include "AcsPendingRequests.hpp"
#include "common/Constants.hpp"
#include "common/log/SyslogLogger.hpp"
#include "AcsdCmdLine.hpp"
#include "common/processCap/ProcessCap.hpp"
#include <signal.h>
......@@ -60,8 +60,6 @@ public:
AcsDaemon(
const int argc,
char **const argv,
//std::ostream &stdOut,
//std::ostream &stdErr,
log::Logger& log,
cta::mediachanger::reactor::ZMQReactor &reactor,
const AcsdConfiguration &config);
......@@ -214,7 +212,7 @@ private:
}; // class AcsDaemon
}
}
} // namespace daemon
} // namespace acs
} // namespace castor
} // namespace mediachanger
} // namespace cta
......@@ -26,7 +26,7 @@
//#include "common/processCap/ProcessCap.hpp"
#include "../Constants.hpp"
#include "AcsDaemon.hpp"
//#include "AcsDaemonConfig.hpp"
#include "AcsdCmdLine.hpp"
#include "mediachanger/reactor/ZMQReactor.hpp"
#include "AcsdConfiguration.hpp"
//#include "common/utils/utils.hpp"
......
/*
* The CERN Tape Archive(CTA) project
* Copyright(C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AcsdCmdLine.hpp"
#include "common/exception/Exception.hpp"
#include "common/log/log.hpp"
#include <iostream>
#include <getopt.h>
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
cta::mediachanger::acs::daemon::AcsdCmdLine::AcsdCmdLine() throw():
foreground(false),
help(false),
configLocation(""){
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::mediachanger::acs::daemon::AcsdCmdLine::AcsdCmdLine(const int argc,
char *const *const argv):
foreground(false), //< Prevents daemonisation
help(false), //< Help requested: will print out help and exit.
configLocation(""){ //< Location of the configuration file. Defaults to /etc/cta/cta-taped.conf
static struct option longopts[] = {
// { .name, .has_args, .flag, .val } (see getopt.h))
{ "foreground", no_argument, NULL, 'f' },
{ "config", required_argument, NULL, 'c' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, '\0' }
};
char c;
// Reset getopt's global variables to make sure we start fresh
optind=0;
// Prevent getopt from printing out errors on stdout
opterr=0;
// We ask getopt to not reshuffle argv ('+')
while ((c = getopt_long(argc, argv, "+fc:h", longopts, NULL)) != -1) {
//log:write(LOG_INFO, "Usage: [options]\n");
switch (c) {
case 'f':
foreground = true;
break;
case 'c':
configLocation = optarg;
break;
case 'h':
help = true;
std::cout<<"Usage: "<<"cta-acsd"<<" [options]\n"
"where options can be:\n"
"--foreground or -f Remain in the Foreground\n"
"--config <config-file> or -c Configuration file\n"
"--help or -h Print this help and exit\n";
break;
default:
break;
}
}
}
/*
* The CERN Tape Archive(CTA) project
* Copyright(C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "mediachanger/CmdLine.hpp"
#include <string.h>
namespace cta{
namespace mediachanger{
namespace acs{
namespace daemon{
class AcsdCmdLine: public CmdLine{
public:
/**
* Constructor.
*
* Initialises all BOOLEAN member-variables to FALSE, all integer
* member-variables to 0 and the volume identifier to an empty string.
*/
AcsdCmdLine() throw();
/**
* Constructor.
*
* Parses the specified command-line arguments.
*
* @param argc Argument count from the executable's entry function: main().
* @param argv Argument vector from the executable's entry function: main().
*/
AcsdCmdLine(const int argc, char *const *const argv);
bool foreground;
bool help;
std::string configLocation;
};
}
}
}
}
/*
* The CERN Tape Archive(CTA) project
* Copyright(C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/exception/Exception.hpp"
#include "mediachanger/acs/daemon/AcsdCmdLine.hpp"
#include <gtest/gtest.h>
#include <list>
#include <memory>
namespace unitTests {
class cta_mediachanger_acs_daemon_AcsdCmdLineTest : public ::testing::Test {
protected:
struct Argcv {
int argc;
char **argv;
Argcv(): argc(0), argv(NULL) {
}
};
typedef std::list<Argcv*> ArgcvList;
ArgcvList m_argsList;
/**
* Creates a duplicate string using the new operator.
*/
char *dupString(const char *str) {
const size_t len = strlen(str);
char *duplicate = new char[len+1];
strncpy(duplicate, str, len);
duplicate[len] = '\0';
return duplicate;
}
virtual void SetUp() {
}
virtual void TearDown() {
// Allow getopt_long to be called again
optind = 0;
for(ArgcvList::const_iterator itor = m_argsList.begin();
itor != m_argsList.end(); itor++) {
for(int i=0; i < (*itor)->argc; i++) {
delete[] (*itor)->argv[i];
}
delete[] (*itor)->argv;
delete *itor;
}
}
};
TEST_F(cta_mediachanger_acs_daemon_AcsdCmdLineTest, constructor_no_command_line_args) {
using namespace cta::mediachanger::acs::daemon;
Argcv *args = new Argcv();
m_argsList.push_back(args);
args->argc = 1;
args->argv = new char *[2];
args->argv[0] = dupString("cta-acsd");
args->argv[1] = NULL;
cta::mediachanger::acs::daemon::AcsdCmdLine acsdcmdLine1(args->argc, args->argv);
ASSERT_FALSE(acsdcmdLine1.foreground);
ASSERT_TRUE(acsdcmdLine1.configLocation.empty());
ASSERT_FALSE(acsdcmdLine1.help);
}
TEST_F(cta_mediachanger_acs_daemon_AcsdCmdLineTest, default_constructor) {
using namespace cta::mediachanger::acs::daemon;
Argcv *args = new Argcv();
m_argsList.push_back(args);
args->argc = 0;
cta::mediachanger::acs::daemon::AcsdCmdLine acsdcmdLine1(args->argc, args->argv);
ASSERT_FALSE(acsdcmdLine1.foreground);
ASSERT_TRUE(acsdcmdLine1.configLocation.empty());
ASSERT_FALSE(acsdcmdLine1.help);
}
TEST_F(cta_mediachanger_acs_daemon_AcsdCmdLineTest, constructor_foreground) {
using namespace cta::mediachanger::acs::daemon;
Argcv *args = new Argcv();
m_argsList.push_back(args);
args->argc = 2;
args->argv = new char *[3];
args->argv[0] = dupString("cta-acsd");
args->argv[1] = dupString("-f");
args->argv[2] = NULL;
cta::mediachanger::acs::daemon::AcsdCmdLine acsdcmdLine1(args->argc, args->argv);
ASSERT_TRUE(acsdcmdLine1.foreground);
}
TEST_F(cta_mediachanger_acs_daemon_AcsdCmdLineTest, constructor_help) {
using namespace cta::mediachanger::acs::daemon;
Argcv *args = new Argcv();
m_argsList.push_back(args);
args->argc = 2;
args->argv = new char *[3];
args->argv[0] = dupString("cta-mediachanger-acs-daemon");
args->argv[1] = dupString("-h");
args->argv[2] = NULL;
cta::mediachanger::acs::daemon::AcsdCmdLine acsdcmdLine2(args->argc, args->argv);
ASSERT_TRUE(acsdcmdLine2.help);
}
TEST_F(cta_mediachanger_acs_daemon_AcsdCmdLineTest, constructor_configLocation) {
using namespace cta::mediachanger::acs::daemon;
Argcv *args1 = new Argcv();
m_argsList.push_back(args1);
args1->argc = 2;
args1->argv = new char *[3];
args1->argv[0] = dupString("cta-mediachanger-acs-daemon");
args1->argv[1] = dupString("-c");
args1->argv[2] = NULL;
cta::mediachanger::acs::daemon::AcsdCmdLine acsdcmdLine3(args1->argc, args1->argv);
ASSERT_EQ(acsdcmdLine3.configLocation,"");
}
} // namespace unitTests
......@@ -20,6 +20,7 @@ set (ACS_DAEMON_SRC_FILES
AcsDaemonMain.cpp
AcsdConfiguration.cpp
AcsPendingRequests.cpp
AcsdCmdLine.cpp
#AcsDismountTape.cpp
#AcsMessageHandler.cpp
#AcsMountTapeReadWrite.cpp
......@@ -43,7 +44,12 @@ target_link_libraries(
zmq
${STK_LIBRARIES})
install (FILES cta.conf
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/cta
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
add_library (ctamediachangeracsdaemonunittests SHARED AcsdCmdLineTest.cpp AcsdCmdLine.cpp)
set_property(TARGET ctamediachangeracsdaemonunittests PROPERTY SOVERSION "${CTA_SOVERSION}")
set_property(TARGET ctamediachangeracsdaemonunittests PROPERTY VERSION "${CTA_LIBVERSION}")
target_link_libraries (ctamediachangeracsdaemonunittests
ctamediachangerunittests)
install (TARGETS ctamediachangeracsdaemonunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
......@@ -40,6 +40,7 @@ target_link_libraries(cta-unitTests
ctaschedulerunittests
ctadaemonunittests
ctamediachangerunittests
ctamediachangeracsdaemonunittests
${GMOCK_LIB}
gtest
pthread)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment