Commit a23fef4c authored by Eric Cano's avatar Eric Cano
Browse files

Added option to hook a pre-allocated drive to the system wrapper.

This options allows us to prepare the contents of a drive for a unit test.
This function is only implemented for the fake systems and the drives of type VIRTUAL, so it is only available
for test environments.
parent 2652dbcc
......@@ -36,7 +36,14 @@ drives::DriveInterface * drives::DriveFactory(SCSI::DeviceInfo di,
} else if (std::string::npos != di.product.find("03592")) {
return new DriveIBM3592(di, sw);
} else if (std::string::npos != di.product.find("VIRTUAL")) {
return new FakeDrive();
/* In case of a VIRTUAL drive, it could have been pre-allocated
* for testing purposes (with "pre-cooked" contents). */
drives::DriveInterface * ret = sw.getDriveByPath(di.nst_dev);
if (ret) {
return ret;
} else {
return new FakeDrive();
}
} else {
throw Exception(std::string("Unsupported drive type: ") + di.product);
}
......
......@@ -191,6 +191,20 @@ int System::fakeWrapper::stat(const char* path, struct stat* buf) {
return 0;
}
castor::tape::drives::DriveInterface *
System::fakeWrapper::getDriveByPath(const std::string & path) {
std::map<std::string, castor::tape::drives::DriveInterface *>::iterator drive =
m_pathToDrive.find(path);
if (m_pathToDrive.end() == drive) {
return NULL;
} else {
/* The drive will be deleted by the user, so we remove references to it */
castor::tape::drives::DriveInterface * ret = drive->second;
m_pathToDrive.erase(drive);
return ret;
}
}
/**
* Function merging all types of files into a single pointer
* based map. This allows usage of polymorphic
......@@ -229,6 +243,7 @@ void System::mockWrapper::delegateToFake() {
static_cast<int(fakeWrapper::*)(int , unsigned long int , sg_io_hdr_t*)>(&fakeWrapper::ioctl)));
ON_CALL(*this, close(_)).WillByDefault(Invoke(&fake, &fakeWrapper::close));
ON_CALL(*this, stat(_, _)).WillByDefault(Invoke(&fake, &fakeWrapper::stat));
ON_CALL(*this, getDriveByPath(_)).WillByDefault(Invoke(&fake, &fakeWrapper::getDriveByPath));
}
void System::mockWrapper::disableGMockCallsCounting() {
......@@ -246,6 +261,7 @@ void System::mockWrapper::disableGMockCallsCounting() {
EXPECT_CALL(*this, ioctl(_, _, A<struct mtop *>())).Times(AnyNumber());
EXPECT_CALL(*this, ioctl(_, _, A<struct mtget *>())).Times(AnyNumber());
EXPECT_CALL(*this, ioctl(_, _, A<struct sg_io_hdr *>())).Times(AnyNumber());
EXPECT_CALL(*this, getDriveByPath(_)).Times(AnyNumber());
}
void System::fakeWrapper::setupSLC5() {
......
......@@ -41,11 +41,20 @@
namespace castor {
namespace tape {
/**
* Forward declaration for pointer type in virutalWrapper.
*/
namespace drives {
class DriveInterface;
}
namespace System {
/**
* Interface class definition, allowing common ancestor between
* realWrapper, mockWrapper and fakeWrapper
*/
class virtualWrapper {
public:
virtual DIR* opendir(const char *name) = 0;
......@@ -64,6 +73,10 @@ namespace System {
virtual int close(int fd) = 0;
virtual int stat(const char * path, struct stat *buf) = 0;
virtual ~virtualWrapper() {};
/** Hook allowing the pre-allocation of a tape drive in the test environment.
* will */
virtual castor::tape::drives::DriveInterface *
getDriveByPath(const std::string & path) = 0;
};
......@@ -96,6 +109,8 @@ namespace System {
virtual ssize_t write(int fd, const void *buf, size_t nbytes) { return ::write(fd, buf, nbytes); }
virtual int close(int fd) { return ::close(fd); }
virtual int stat(const char * path, struct stat *buf) { return ::stat(path, buf); }
virtual castor::tape::drives::DriveInterface *
getDriveByPath(const std::string &) { return NULL; }
};
/**
......@@ -121,13 +136,16 @@ namespace System {
virtual ssize_t write(int fd, const void *buf, size_t nbytes);
virtual int close(int fd);
virtual int stat(const char * path, struct stat *buf);
virtual castor::tape::drives::DriveInterface *
getDriveByPath(const std::string & path);
std::map<std::string, std::vector<std::string> > m_directories;
std::map<std::string, std::string> m_links;
std::map<std::string, std::string> m_realpathes;
std::map<std::string, vfsFile *> m_files;
std::map<std::string, struct stat> m_stats;
std::map<std::string, regularFile> m_regularFiles;
std::map<std::string, stDeviceFile> m_stFiles;
std::map<std::string, stDeviceFile> m_stFiles;
std::map<std::string, castor::tape::drives::DriveInterface *> m_pathToDrive;
void setupSLC5();
void setupSLC6();
void setupForVirtualDriveSLC6();
......@@ -163,6 +181,7 @@ namespace System {
MOCK_METHOD3(ioctl, int(int fd, unsigned long int request, sg_io_hdr_t * sgh));
MOCK_METHOD1(close, int(int fd));
MOCK_METHOD2(stat, int(const char *, struct stat *));
MOCK_METHOD1(getDriveByPath, castor::tape::drives::DriveInterface *(const std::string &));
DIR* m_DIR;
int m_DIRfake;
void delegateToFake();
......
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