Commit 4a796daa authored by Eric Cano's avatar Eric Cano
Browse files

Replaced the ad-hoc container castor::tape::drives::Drive by a factory...

Replaced the ad-hoc container castor::tape::drives::Drive by a factory function castor::tape::drives::DriveFactory.

Adapted the use in all the tests (with auto_ptr).
parent e89cfb76
......@@ -27,6 +27,21 @@
using namespace castor::tape;
drives::DriveInterface * drives::DriveFactory(SCSI::DeviceInfo di,
System::virtualWrapper& sw) {
if (std::string::npos != di.product.find("T10000")) {
return new DriveT10000(di, sw);
} else if (std::string::npos != di.product.find("ULT" || std::string::npos != di.product.find("Ultrium"))) {
return new DriveLTO(di, sw);
} 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();
} else {
throw Exception(std::string("Unsupported drive type: ") + di.product);
}
}
drives::DriveGeneric::DriveGeneric(SCSI::DeviceInfo di, System::virtualWrapper& sw) : m_SCSIInfo(di),
m_tapeFD(-1), m_sysWrapper(sw) {
/* Open the device files */
......
......@@ -167,6 +167,16 @@ namespace drives {
virtual bool isAtEOD() throw(Exception) = 0;
};
/**
* Factory function that allocated the proper drive type, based on device info
* @param di deviceInfo, as found in a DeviceVector.
* @param sw reference to the system wrapper.
* @return pointer to the newly allocated drive object
*/
DriveInterface * DriveFactory(SCSI::DeviceInfo di,
System::virtualWrapper & sw);
/**
* Fake drive class used for unit testing
*/
......@@ -519,31 +529,6 @@ namespace drives {
virtual compressionStats getCompression() throw (Exception);
};
class Drive {
public:
Drive(SCSI::DeviceInfo di, System::virtualWrapper & sw): m_drive(NULL) {
if (std::string::npos != di.product.find("T10000")) {
m_drive = new DriveT10000(di, sw);
} else if (std::string::npos != di.product.find("ULT" || std::string::npos != di.product.find("Ultrium"))) {
m_drive = new DriveLTO(di, sw);
} else if (std::string::npos != di.product.find("03592")) {
m_drive = new DriveIBM3592(di, sw);
} else if (std::string::npos != di.product.find("VIRTUAL")) {
m_drive = new FakeDrive();
} else {
throw Exception(std::string("Unsupported drive type: ")+di.product);
}
}
~Drive() {
delete m_drive;
}
operator DriveInterface &() {
return *m_drive;
}
private:
DriveInterface * m_drive;
};
} // namespace drives
} // namespace tape
} // namespace castor
......@@ -28,6 +28,7 @@
#include <gtest/gtest.h>
#include <gmock/gmock-cardinalities.h>
#include <typeinfo>
#include <memory>
using ::testing::AtLeast;
using ::testing::Return;
......@@ -67,10 +68,10 @@ TEST(castor_tape_drives_Drive, OpensCorrectly) {
for (std::vector<castor::tape::SCSI::DeviceInfo>::iterator i = dl.begin();
i != dl.end(); i++) {
if (castor::tape::SCSI::Types::tape == i->type) {
castor::tape::drives::Drive drive(*i, sysWrapper);
std::string expected_classid (typeid(castor::tape::drives::DriveT10000).name());
castor::tape::drives::DriveInterface & drv = drive;
std::string found_classid (typeid(drv).name());
std::auto_ptr<castor::tape::drives::DriveInterface>drive(
castor::tape::drives::DriveFactory(*i, sysWrapper));
std::string found_classid (typeid(*drive).name());
ASSERT_EQ(expected_classid, found_classid);
}
}
......@@ -100,14 +101,12 @@ TEST(castor_tape_drives_Drive, getPositionInfoAndPositionToLogicalObject) {
for (std::vector<castor::tape::SCSI::DeviceInfo>::iterator i = dl.begin();
i != dl.end(); i++) {
if (castor::tape::SCSI::Types::tape == i->type) {
castor::tape::drives::Drive dContainer(*i, sysWrapper);
/* Compiler cannot implicitly use the conversion operator. Create an
* intermediate reference*/
castor::tape::drives::DriveInterface & drive = dContainer;
std::auto_ptr<castor::tape::drives::DriveInterface> drive (
castor::tape::drives::DriveFactory(*i, sysWrapper));
castor::tape::drives::positionInfo posInfo;
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(1);
posInfo = drive.getPositionInfo();
posInfo = drive->getPositionInfo();
ASSERT_EQ(0xABCDEF12U,posInfo.currentPosition);
ASSERT_EQ(0x12EFCDABU,posInfo.oldestDirtyObject);
......@@ -115,10 +114,10 @@ TEST(castor_tape_drives_Drive, getPositionInfoAndPositionToLogicalObject) {
ASSERT_EQ(0x12EFCDABU,posInfo.dirtyBytesCount);
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(1);
drive.positionToLogicalObject(0xABCDEF0);
drive->positionToLogicalObject(0xABCDEF0);
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(1);
posInfo = drive.getPositionInfo();
posInfo = drive->getPositionInfo();
ASSERT_EQ(0xABCDEF0U,posInfo.currentPosition);
ASSERT_EQ(0xABCDEF0U,posInfo.oldestDirtyObject);
......@@ -151,25 +150,23 @@ TEST(castor_tape_drives_Drive, setDensityAndCompression) {
for (std::vector<castor::tape::SCSI::DeviceInfo>::iterator i = dl.begin();
i != dl.end(); i++) {
if (castor::tape::SCSI::Types::tape == i->type) {
castor::tape::drives::Drive dContainer(*i, sysWrapper);
/* Compiler cannot implicitly use the conversion operator. Create an
* intermediate reference*/
castor::tape::drives::DriveInterface & drive = dContainer;
std::auto_ptr<castor::tape::drives::DriveInterface> drive (
castor::tape::drives::DriveFactory(*i, sysWrapper));
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(2);
drive.setDensityAndCompression();
drive->setDensityAndCompression();
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(2);
drive.setDensityAndCompression(true);
drive->setDensityAndCompression(true);
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(2);
drive.setDensityAndCompression(false);
drive->setDensityAndCompression(false);
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(2);
drive.setDensityAndCompression(0x42,true);
drive->setDensityAndCompression(0x42,true);
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(2);
drive.setDensityAndCompression(0x46,false);
drive->setDensityAndCompression(0x46,false);
}
}
}
......@@ -197,16 +194,14 @@ TEST(castor_tape_drives_Drive, setStDriverOptions) {
castor::tape::SCSI::DeviceVector dl(sysWrapper);
for (std::vector<castor::tape::SCSI::DeviceInfo>::iterator i = dl.begin(); i != dl.end(); i++) {
if (castor::tape::SCSI::Types::tape == i->type) {
castor::tape::drives::Drive dContainer(*i, sysWrapper);
/* Compiler cannot implicitly use the conversion operator. Create an
* intermediate reference*/
castor::tape::drives::DriveInterface & drive = dContainer;
std::auto_ptr<castor::tape::drives::DriveInterface> drive (
castor::tape::drives::DriveFactory(*i, sysWrapper));
EXPECT_CALL(sysWrapper, ioctl(_,_,An<struct mtop *>())).Times(1);
drive.setSTBufferWrite(true);
drive->setSTBufferWrite(true);
EXPECT_CALL(sysWrapper, ioctl(_,_,An<struct mtop *>())).Times(1);
drive.setSTBufferWrite(false);
drive->setSTBufferWrite(false);
}
}
}
......@@ -235,14 +230,12 @@ TEST(castor_tape_drives_Drive, getDeviceInfo) {
for (std::vector<castor::tape::SCSI::DeviceInfo>::iterator i = dl.begin();
i != dl.end(); i++) {
if (castor::tape::SCSI::Types::tape == i->type) {
castor::tape::drives::Drive dContainer(*i, sysWrapper);
/* Compiler cannot implicitly use the conversion operator. Create an
* intermediate reference*/
castor::tape::drives::DriveInterface & drive = dContainer;
std::auto_ptr<castor::tape::drives::DriveInterface> drive (
castor::tape::drives::DriveFactory(*i, sysWrapper));
castor::tape::drives::deviceInfo devInfo;
EXPECT_CALL(sysWrapper, ioctl(_,_,An<sg_io_hdr_t*>())).Times(2);
devInfo = drive.getDeviceInfo();
devInfo = drive->getDeviceInfo();
ASSERT_EQ("STK ",devInfo.vendor);
ASSERT_EQ("T10000B ",devInfo.product);
......@@ -375,12 +368,10 @@ TEST(castor_tape_drives_Drive, getTapeAlerts) {
for (std::vector<castor::tape::SCSI::DeviceInfo>::iterator i = dl.begin();
i != dl.end(); i++) {
if (castor::tape::SCSI::Types::tape == i->type) {
castor::tape::drives::Drive dContainer(*i, sysWrapper);
/* Compiler cannot implicitly use the conversion operator. Create an
* intermediate reference*/
castor::tape::drives::DriveInterface & drive = dContainer;
std::auto_ptr<castor::tape::drives::DriveInterface> drive (
castor::tape::drives::DriveFactory(*i, sysWrapper));
EXPECT_CALL(sysWrapper, ioctl(_, _, An<sg_io_hdr_t*>())).Times(1);
std::vector<std::string> alerts = drive.getTapeAlerts();
std::vector<std::string> alerts = drive->getTapeAlerts();
ASSERT_EQ(3U, alerts.size());
ASSERT_FALSE(alerts.end() ==
find(alerts.begin(), alerts.end(),
......
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