/******************************************************************************* * ALMA - Atacama Large Millimiter Array * Copyright (c) European Southern Observatory, 2013 * * 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * who when what * -------- -------- ---------------------------------------------- * acaproni 2013-05-23 created */ #include #include #include #include #include #include #include #include #include #include "testAcsDaemonUtils.h" #include "../src/acsDaemonUtils.cpp" CPPUNIT_TEST_SUITE_REGISTRATION (TestAcsDaemonUtils); void TestAcsDaemonUtils::setUp (void) { daemonUtil_mp=new AcsDaemonUtils(); } void TestAcsDaemonUtils::tearDown (void) { delete daemonUtil_mp; } void TestAcsDaemonUtils::testLogDirectory(void) { std::string acsdataStr(ACE_OS::getenv("ACSDATA")); std::string hostStr(ACE_OS::getenv("HOST")); std::string expectsLogDir=acsdataStr+"/logs/"+hostStr+"/"; // Get the directory std::string logDir = daemonUtil_mp->getLogDirectory(); // Check if the folder is what we expect CPPUNIT_ASSERT_EQUAL (expectsLogDir.compare(logDir), 0); // Check if the folder exists CPPUNIT_ASSERT_EQUAL (ACE_OS::access(logDir.c_str(),F_OK|W_OK),0); } void TestAcsDaemonUtils::testLogDirectoryForPlainContainer(void) { // All hte logs for non hierarchical containers go in $ACSDATA/logs/ std::string expectedFolder=daemonUtil_mp->getLogDirectory(); // Non hierarchical container name - folder does not exist std::string contName="testContainer"; std::string logFolder=daemonUtil_mp->getLogDirectoryForContainer(contName); CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder), 0); // Non hierarchical container name - folder exists logFolder=""; logFolder=daemonUtil_mp->getLogDirectoryForContainer(contName); CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder), 0); // Same as previous case, but with a trailing "/" std::string logFolder2=daemonUtil_mp->getLogDirectoryForContainer(contName+'/'); CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder2), 0); // A container name with a trailing "/" (should never happen) std::string contName2="testContainer2/"; std::string logFolder3=daemonUtil_mp->getLogDirectoryForContainer(contName2); CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder3), 0); } void TestAcsDaemonUtils::testLogDirectoryForHierarchicalContainer(void) { // Non hierarchical container name - folder does not exist std::string contName="CONTROL/ACC/testContainer"; std::string logFolder=daemonUtil_mp->getLogDirectoryForContainer(contName); std::string expectedFolder=daemonUtil_mp->getLogDirectory()+"CONTROL/ACC/"; CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder), 0); // Non hierarchical container name - folder exists logFolder=""; logFolder=daemonUtil_mp->getLogDirectoryForContainer(contName); CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder), 0); // Same as previous case, but with a trailing "/" logFolder=""; logFolder=daemonUtil_mp->getLogDirectoryForContainer(contName+'/'); CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder), 0); // A container name with a trailing "/" (should never happen) std::string contName2="CONTROL/ACC/testContainer2/"; std::string logFolder2=daemonUtil_mp->getLogDirectoryForContainer(contName2); std::string expectedFolder2=daemonUtil_mp->getLogDirectory()+contName2; CPPUNIT_ASSERT_EQUAL (expectedFolder.compare(logFolder), 0); } void TestAcsDaemonUtils::testTimestamp(void) { std::string timestamp=daemonUtil_mp->getTimestamp(); // The validity of the timestamp is already tested in acsutil // This test only checks if it has been correctly reformatted std::string temp; for (unsigned int i=0;i='0' && timestamp[i]<='9') { temp+='#'; } else { temp+=timestamp[i]; } } std::string expected("####-##-##_##.##.##.###"); CPPUNIT_ASSERT_EQUAL (expected.compare(temp), 0); } void TestAcsDaemonUtils::testSimpleContainerName(void) { std::string simpleContName("containerName"); CPPUNIT_ASSERT_EQUAL (simpleContName.compare(daemonUtil_mp->getSimpleContainerName(simpleContName)),0); std::string folders("CONTROL/ACC/"); std::string hierarchicalContName(folders); hierarchicalContName=hierarchicalContName+simpleContName; std::cout<<"["<getSimpleContainerName(hierarchicalContName)<<"]"<getSimpleContainerName(hierarchicalContName)),0); } void TestAcsDaemonUtils::testWritePermission() { CPPUNIT_ASSERT_EQUAL (daemonUtil_mp->checkWritePermission(),0); } void TestAcsDaemonUtils:: testWritePermissionForContainer() { std::string contName="simpleContainer"; CPPUNIT_ASSERT_EQUAL (daemonUtil_mp->checkWritePermissionForContainer(contName),0); std::string hierContName="ACS/DAEMON/hierarchicalContainer"; CPPUNIT_ASSERT_EQUAL (daemonUtil_mp->checkWritePermissionForContainer(hierContName),0); // Check with a non-writable folder std::string acsdataStr(ACE_OS::getenv("ACSDATA")); std::string hostStr(ACE_OS::getenv("HOST")); std::string contLogDir=acsdataStr+"/logs/"+hostStr+"/"+"/EXEC"; // Let the AcsDaemonUtil create the folder for us daemonUtil_mp->getLogDirectoryForContainer("EXEC/testContainer1"); CPPUNIT_ASSERT( ACE_OS::access(contLogDir.c_str(),F_OK|W_OK) ==0 ); std::string chmod("chmod 444 "); chmod+=contLogDir; if (ACE_OS::system(chmod.c_str())!=0) { std::cout<<"Error setting permissions of "<checkWritePermissionForContainer("EXEC/testContainer1") !=0); // Restore permisisons std::string chmod2("chmod 775 "); chmod2+=contLogDir; if (ACE_OS::system(chmod2.c_str())!=0) { std::cout<<"Error setting permissions of "<