Commit 8133ee97 authored by Eric Cano's avatar Eric Cano
Browse files

Removed NULL references that made unit test segfault and create the Mocks to fill the blanks.

Refactored the Mocks to eliminate code duplication. (for Mock{Archive|Retrieve}{Job}Mount}
parent 893a03ed
......@@ -45,12 +45,6 @@ class ArchiveJob {
friend class ArchiveMount;
protected:
/**
* Empty constructor. TODO: to be removed in the future when we put in the reference to the owning mount;
*/
//ArchiveJob(): m_mount(*((ArchiveMount*)NULL)), m_ns(*((NameServer*)NULL)){}
/**
* Constructor.
*
......
......@@ -120,7 +120,7 @@ namespace cta {
*/
virtual ~ArchiveMount() throw();
private:
protected:
/**
* The database representation of this mount.
......
/*
* 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 "scheduler/RetrieveMount.hpp"
#include "scheduler/RetrieveJob.hpp"
#include <memory>
namespace cta {
class MockArchiveJob: public cta::ArchiveJob {
public:
int completes;
int failures;
MockArchiveJob(cta::ArchiveMount & am, cta::NameServer &ns): cta::ArchiveJob(am,
ns, cta::ArchiveFile(),
cta::RemotePathAndStatus(), cta::NameServerTapeFile()), completes(0), failures(0) {}
~MockArchiveJob() throw() {}
virtual void complete() {
completes++;
}
virtual void failed(const cta::exception::Exception& ex) {
failures++;
}
virtual void retry() {}
};
}
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#pragma once
#include "scheduler/ArchiveMount.hpp"
#include "scheduler/testingMocks/MockArchiveJob.hpp"
namespace cta {
class MockArchiveMount: public cta::ArchiveMount {
public:
int getJobs;
int completes;
MockArchiveMount(cta::NameServer &ns):
cta::ArchiveMount(ns), getJobs(0), completes(0) {}
~MockArchiveMount() throw() {
}
std::unique_ptr<cta::ArchiveJob> getNextJob() {
getJobs++;
if(m_jobs.empty()) {
return std::unique_ptr<cta::ArchiveJob>();
} else {
std::unique_ptr<cta::ArchiveJob> job = std::move(m_jobs.front());
m_jobs.pop_front();
return job;
}
}
void complete() {
completes++;
}
private:
std::list<std::unique_ptr<cta::ArchiveJob>> m_jobs;
public:
void createArchiveJobs(const unsigned int nbJobs) {
for(unsigned int i = 0; i < nbJobs; i++) {
m_jobs.push_back(std::unique_ptr<cta::ArchiveJob>(
new cta::MockArchiveJob(*this, ::cta::ArchiveMount::m_ns)));
}
}
}; // class MockArchiveMount
}
\ No newline at end of file
/*
* 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 "scheduler/RetrieveMount.hpp"
#include "scheduler/RetrieveJob.hpp"
#include <memory>
namespace cta {
class MockRetrieveJob: public cta::RetrieveJob {
public:
int completes;
int failures;
MockRetrieveJob(RetrieveMount & rm): cta::RetrieveJob(rm,
cta::ArchiveFile(),
std::string(), cta::NameServerTapeFile(),
cta::PositioningMethod::ByBlock), completes(0), failures(0) {}
virtual void complete() { completes++; }
virtual void failed() { failures++; };
~MockRetrieveJob() throw() {}
};
}
/*
* 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 "scheduler/RetrieveMount.hpp"
#include "scheduler/RetrieveJob.hpp"
#include "scheduler/testingMocks/MockRetrieveJob.hpp"
#include <memory>
namespace cta {
class MockRetrieveMount: public cta::RetrieveMount {
public:
int getJobs;
int completes;
MockRetrieveMount(): getJobs(0), completes(0) {}
~MockRetrieveMount() throw() {
}
std::unique_ptr<cta::RetrieveJob> getNextJob() {
getJobs++;
if(m_jobs.empty()) {
return std::unique_ptr<cta::RetrieveJob>();
} else {
std::unique_ptr<cta::RetrieveJob> job = std::move(m_jobs.front());
m_jobs.pop_front();
return job;
}
}
virtual std::string getMountTransactionId() const {
return "1234567890";
}
virtual void complete() { completes ++; }
private:
std::list<std::unique_ptr<cta::RetrieveJob>> m_jobs;
public:
void createRetrieveJobs(const unsigned int nbJobs) {
for(unsigned int i = 0; i < nbJobs; i++) {
m_jobs.push_back(std::unique_ptr<cta::RetrieveJob>(
new MockRetrieveJob(*this)));
}
}
}; // class MockRetrieveMount
}
\ No newline at end of file
......@@ -50,6 +50,10 @@
#include "scheduler/DummyScheduler.hpp"
#include "scheduler/OStoreDB/OStoreDBFactory.hpp"
#include "scheduler/MountType.hpp"
#include "nameserver/NameServer.hpp"
#include "scheduler/testingMocks/MockRetrieveMount.hpp"
#include "scheduler/testingMocks/MockArchiveJob.hpp"
#include "scheduler/testingMocks/MockArchiveMount.hpp"
#include <dirent.h>
#include <fcntl.h>
......@@ -140,27 +144,6 @@ protected:
* test by the Setup() and TearDown() methods.
*/
char m_tmpDir[100];
class MockArchiveJob: public cta::ArchiveJob {
public:
MockArchiveJob(): cta::ArchiveJob(*((cta::ArchiveMount *)NULL),
*((cta::NameServer *)NULL), cta::ArchiveFile(),
cta::RemotePathAndStatus(), cta::NameServerTapeFile()) {
}
~MockArchiveJob() throw() {
}
};
class MockRetrieveJob: public cta::RetrieveJob {
public:
MockRetrieveJob(): cta::RetrieveJob(*((cta::RetrieveMount *)NULL),
cta::ArchiveFile(),
std::string(), cta::NameServerTapeFile(),
cta::PositioningMethod::ByBlock) {}
~MockRetrieveJob() throw() {}
};
};
TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessionGooddayRecall) {
......@@ -242,10 +225,13 @@ TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessio
archiveFileMode,
archiveFileSize));
std::unique_ptr<cta::ArchiveFileStatus> status = ns.statFile(requester, archiveFilePath.str());
// Write the file to tape
std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
std::unique_ptr<cta::ArchiveJob> ftm(new MockArchiveJob());
cta::MockRetrieveMount mrm;
cta::MockNameServer mns;
cta::MockArchiveMount mam(mns);
std::unique_ptr<cta::RetrieveJob> ftr(new cta::MockRetrieveJob(mrm));
std::unique_ptr<cta::ArchiveJob> ftm(new cta::MockArchiveJob(mam, mns));
ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftm->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftr->archiveFile.fileId = status->fileId;
......@@ -404,8 +390,11 @@ TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessio
archiveFileSize));
// Write the file to tape
std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
cta::MockNameServer mns;
cta::MockArchiveMount mam(mns);
cta::MockRetrieveMount mrm;
std::unique_ptr<cta::RetrieveJob> ftr(new cta::MockRetrieveJob(mrm));
std::unique_ptr<cta::ArchiveJob> ftm_temp(new cta::MockArchiveJob(mam, mns));
ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftm_temp->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftr->archiveFile.fileId = 1000 + fseq;
......@@ -556,8 +545,11 @@ TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessio
archiveFileSize));
// Write the file to tape
std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
cta::MockNameServer mns;
cta::MockArchiveMount mam(mns);
cta::MockRetrieveMount mrm;
std::unique_ptr<cta::RetrieveJob> ftr(new cta::MockRetrieveJob(mrm));
std::unique_ptr<cta::ArchiveJob> ftm_temp(new cta::MockArchiveJob(mam, mns));
ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftm_temp->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftr->archiveFile.fileId = 1000 + fseq;
......@@ -703,8 +695,11 @@ TEST_F(castor_tape_tapeserver_daemon_DataTransferSessionTest, DataTransferSessio
archiveFileSize));
// Write the file to tape
std::unique_ptr<cta::RetrieveJob> ftr(new MockRetrieveJob());
std::unique_ptr<cta::ArchiveJob> ftm_temp(new MockArchiveJob());
cta::MockNameServer mns;
cta::MockArchiveMount mam(mns);
cta::MockRetrieveMount mrm;
std::unique_ptr<cta::RetrieveJob> ftr(new cta::MockRetrieveJob(mrm));
std::unique_ptr<cta::ArchiveJob> ftm_temp(new cta::MockArchiveJob(mam, mns));
ftr->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftm_temp->nameServerTapeFile.tapeFileLocation.fSeq = fseq;
ftr->archiveFile.fileId = 1000 + fseq;
......
......@@ -46,7 +46,7 @@ namespace daemon {
//Constructor
//------------------------------------------------------------------------------
MigrationReportPacker::MigrationReportPacker(cta::ArchiveMount *archiveMount,
castor::log::LogContext lc):
castor::log::LogContext & lc):
ReportPackerInterface<detail::Migration>(lc),
m_workerThread(*this),m_errorHappened(false),m_continue(true), m_archiveMount(archiveMount) {
}
......
......@@ -42,7 +42,7 @@ public:
* @param tg The client who is asking for a migration of his files
* and to whom we have to report to the status of the operations.
*/
MigrationReportPacker(cta::ArchiveMount *archiveMount, log::LogContext lc);
MigrationReportPacker(cta::ArchiveMount *archiveMount, log::LogContext& lc);
~MigrationReportPacker();
......
......@@ -24,6 +24,8 @@
#include "castor/log/StringLogger.hpp"
#include "castor/tape/tapeserver/daemon/MigrationReportPacker.hpp"
#include "castor/tape/tapeserver/drive/DriveInterface.hpp"
#include "nameserver/mockNS/MockNameServer.hpp"
#include "scheduler/testingMocks/MockArchiveMount.hpp"
#include "serrno.h"
//#include "scheduler/mockDB/MockSchedulerDatabase.hpp"
......@@ -44,76 +46,47 @@ namespace unitTests {
void TearDown() {
}
class MockArchiveJob: public cta::ArchiveJob {
public:
MockArchiveJob(): cta::ArchiveJob(*((cta::ArchiveMount *)NULL),
*((cta::NameServer *)NULL), cta::ArchiveFile(),
cta::RemotePathAndStatus(), cta::NameServerTapeFile()) {
}
~MockArchiveJob() throw() {
}
MOCK_METHOD0(complete, void());
MOCK_METHOD1(failed, void(const cta::exception::Exception &ex));
}; // class MockArchiveJob
class MockArchiveMount: public cta::ArchiveMount {
public:
MockArchiveMount(): cta::ArchiveMount(*((cta::NameServer *)NULL)) {
const unsigned int nbArchiveJobs = 2;
createArchiveJobs(nbArchiveJobs);
}
~MockArchiveMount() throw() {
}
std::unique_ptr<cta::ArchiveJob> getNextJob() {
internalGetNextJob();
if(m_jobs.empty()) {
return std::unique_ptr<cta::ArchiveJob>();
} else {
std::unique_ptr<cta::ArchiveJob> job = std::move(m_jobs.front());
m_jobs.pop_front();
return job;
}
}
MOCK_METHOD0(internalGetNextJob, cta::ArchiveJob*());
MOCK_METHOD0(complete, void());
private:
std::list<std::unique_ptr<cta::ArchiveJob>> m_jobs;
void createArchiveJobs(const unsigned int nbJobs) {
for(unsigned int i = 0; i < nbJobs; i++) {
m_jobs.push_back(std::unique_ptr<cta::ArchiveJob>(
new MockArchiveJob()));
}
}
}; // class MockArchiveMount
}; // class castor_tape_tapeserver_daemonTest
class MockArchiveJobExternalStats: public cta::MockArchiveJob {
public:
MockArchiveJobExternalStats(cta::ArchiveMount & am, cta::NameServer & ns,
int & completes, int &failures):
MockArchiveJob(am, ns), completesRef(completes), failuresRef(failures) {}
virtual void complete() {
completesRef++;
}
virtual void failed() {
failuresRef++;
}
private:
int & completesRef;
int & failuresRef;
};
TEST_F(castor_tape_tapeserver_daemonTest, MigrationReportPackerNominal) {
MockArchiveMount tam;
cta::MockNameServer mns;
cta::MockArchiveMount tam(mns);
::testing::InSequence dummy;
std::unique_ptr<cta::ArchiveJob> job1;
int job1completes(0), job1failures(0);
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
EXPECT_CALL(*mockJob, complete()).Times(1);
std::unique_ptr<cta::MockArchiveJob> mockJob(
new MockArchiveJobExternalStats(tam, mns, job1completes, job1failures));
job1.reset(mockJob.release());
}
std::unique_ptr<cta::ArchiveJob> job2;
int job2completes(0), job2failures(0);
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
EXPECT_CALL(*mockJob, complete()).Times(1);
std::unique_ptr<cta::MockArchiveJob> mockJob(
new MockArchiveJobExternalStats(tam, mns, job2completes, job2failures));
job2.reset(mockJob.release());
}
EXPECT_CALL(tam, complete()).Times(1);
castor::log::StringLogger log("castor_tape_tapeserver_daemon_MigrationReportPackerNominal");
castor::log::LogContext lc(log);
......@@ -130,30 +103,34 @@ namespace unitTests {
std::string temp = log.getLog();
ASSERT_NE(std::string::npos, temp.find("Reported to the client that a batch of files was written on tape"));
ASSERT_EQ(1, tam.completes);
ASSERT_EQ(1, job1completes);
ASSERT_EQ(1, job2completes);
}
TEST_F(castor_tape_tapeserver_daemonTest, MigrationReportPackerFailure) {
MockArchiveMount tam;
cta::MockNameServer mns;
cta::MockArchiveMount tam(mns);
::testing::InSequence dummy;
std::unique_ptr<cta::ArchiveJob> job1;
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
std::unique_ptr<cta::MockArchiveJob> mockJob(new cta::MockArchiveJob(tam,mns));
job1.reset(mockJob.release());
}
std::unique_ptr<cta::ArchiveJob> job2;
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
std::unique_ptr<cta::MockArchiveJob> mockJob(new cta::MockArchiveJob(tam, mns));
job2.reset(mockJob.release());
}
std::unique_ptr<cta::ArchiveJob> job3;
int job3completes(0), job3failures(0);
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
EXPECT_CALL(*mockJob, failed(_)).Times(1);
std::unique_ptr<cta::MockArchiveJob> mockJob(
new MockArchiveJobExternalStats(tam, mns, job3completes, job3failures));
job3.reset(mockJob.release());
}
EXPECT_CALL(tam, complete()).Times(1);
castor::log::StringLogger log("castor_tape_tapeserver_daemon_MigrationReportPackerFailure");
castor::log::LogContext lc(log);
tapeserver::daemon::MigrationReportPacker mrp(&tam,lc);
......@@ -173,31 +150,36 @@ namespace unitTests {
std::string temp = log.getLog();
ASSERT_NE(std::string::npos, temp.find(error_msg));
ASSERT_EQ(1, tam.completes);
ASSERT_EQ(1, job3failures);
}
TEST_F(castor_tape_tapeserver_daemonTest, MigrationReportPackerOneByteFile) {
MockArchiveMount tam;
cta::MockNameServer mns;
cta::MockArchiveMount tam(mns);
::testing::InSequence dummy;
std::unique_ptr<cta::ArchiveJob> migratedBigFile;
int migratedBigFileCompletes(0), migratedBigFileFailures(0);
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
EXPECT_CALL(*mockJob, complete()).Times(1);
std::unique_ptr<cta::MockArchiveJob> mockJob(
new MockArchiveJobExternalStats(tam, mns, migratedBigFileCompletes, migratedBigFileFailures));
migratedBigFile.reset(mockJob.release());
}
std::unique_ptr<cta::ArchiveJob> migratedFileSmall;
int migratedFileSmallCompletes(0), migratedFileSmallFailures(0);
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
EXPECT_CALL(*mockJob, complete()).Times(1);
std::unique_ptr<cta::MockArchiveJob> mockJob(
new MockArchiveJobExternalStats(tam, mns, migratedFileSmallCompletes, migratedFileSmallFailures));
migratedFileSmall.reset(mockJob.release());
}
std::unique_ptr<cta::ArchiveJob> migratedNullFile;
int migratedNullFileCompletes(0), migratedNullFileFailures(0);
{
std::unique_ptr<MockArchiveJob> mockJob(new MockArchiveJob());
EXPECT_CALL(*mockJob, complete()).Times(1);
std::unique_ptr<cta::MockArchiveJob> mockJob(
new MockArchiveJobExternalStats(tam, mns, migratedNullFileCompletes, migratedNullFileFailures));
migratedNullFile.reset(mockJob.release());
}
EXPECT_CALL(tam, complete()).Times(1);
migratedBigFile->archiveFile.size=100000;
migratedFileSmall->archiveFile.size=1;
......@@ -219,5 +201,9 @@ namespace unitTests {
std::string temp = log.getLog();
ASSERT_NE(std::string::npos, temp.find("Reported to the client that a batch of files was written on tape"));
ASSERT_EQ(1, tam.completes);
ASSERT_EQ(1, migratedBigFileCompletes);
ASSERT_EQ(1, migratedFileSmallCompletes);
ASSERT_EQ(1, migratedNullFileCompletes);
}
}
......@@ -27,6 +27,8 @@
#include "serrno.h"
#include "scheduler/OStoreDB/OStoreDBFactory.hpp"
#include "objectstore/BackendVFS.hpp"
#include "scheduler/testingMocks/MockRetrieveMount.hpp"
#include "scheduler/testingMocks/MockRetrieveJob.hpp"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
......@@ -44,75 +46,47 @@ protected:
void TearDown() {
}