Commit 56edd12f authored by Cedric Caffy's avatar Cedric Caffy
Browse files

[catalogue] Added unit tests for Catalogue::modifyTapeState()

parent 1a4f647c
......@@ -5482,6 +5482,101 @@ TEST_P(cta_catalogue_CatalogueTest, modifyTapeEncryptionKeyName_nonExistentTape)
ASSERT_THROW(m_catalogue->modifyTapeEncryptionKeyName(m_admin, m_tape1.vid, encryptionKeyName), exception::UserError);
}
 
TEST_P(cta_catalogue_CatalogueTest, modifyTapeState_nonExistentTape) {
using namespace cta;
common::dataStructures::Tape::State state = common::dataStructures::Tape::State::ACTIVE;
ASSERT_THROW(m_catalogue->modifyTapeState("DOES_NOT_EXIST",state,cta::nullopt,m_admin.username),cta::catalogue::UserSpecifiedANonExistentTape);
}
TEST_P(cta_catalogue_CatalogueTest, modifyTapeState_nonInitializedState) {
using namespace cta;
const bool logicalLibraryIsDisabled= false;
const uint64_t nbPartialTapes = 2;
const bool isEncrypted = true;
const cta::optional<std::string> supply("value for the supply pool mechanism");
m_catalogue->createMediaType(m_admin, m_mediaType);
m_catalogue->createLogicalLibrary(m_admin, m_tape1.logicalLibraryName, logicalLibraryIsDisabled, "Create logical library");
m_catalogue->createVirtualOrganization(m_admin, m_vo);
m_catalogue->createTapePool(m_admin, m_tape1.tapePoolName, m_vo.name, nbPartialTapes, isEncrypted, supply, "Create tape pool");
m_catalogue->createTape(m_admin, m_tape1);
common::dataStructures::Tape::State state;
ASSERT_THROW(m_catalogue->modifyTapeState(m_tape1.vid,state,cta::nullopt,m_admin.username),cta::catalogue::UserSpecifiedANonExistentTapeState);
}
TEST_P(cta_catalogue_CatalogueTest, modifyTapeState_noReasonWhenNotActive) {
using namespace cta;
const bool logicalLibraryIsDisabled= false;
const uint64_t nbPartialTapes = 2;
const bool isEncrypted = true;
const cta::optional<std::string> supply("value for the supply pool mechanism");
m_catalogue->createMediaType(m_admin, m_mediaType);
m_catalogue->createLogicalLibrary(m_admin, m_tape1.logicalLibraryName, logicalLibraryIsDisabled, "Create logical library");
m_catalogue->createVirtualOrganization(m_admin, m_vo);
m_catalogue->createTapePool(m_admin, m_tape1.tapePoolName, m_vo.name, nbPartialTapes, isEncrypted, supply, "Create tape pool");
m_catalogue->createTape(m_admin, m_tape1);
std::string reason = "";
ASSERT_THROW(m_catalogue->modifyTapeState(m_tape1.vid,common::dataStructures::Tape::State::BROKEN,reason,m_admin.username),cta::catalogue::UserSpecifiedAnEmptyStringReasonWhenTapeStateNotActive);
ASSERT_THROW(m_catalogue->modifyTapeState(m_tape1.vid,common::dataStructures::Tape::State::DISABLED,cta::nullopt,m_admin.username),cta::catalogue::UserSpecifiedAnEmptyStringReasonWhenTapeStateNotActive);
}
TEST_P(cta_catalogue_CatalogueTest, modifyTapeState) {
using namespace cta;
const bool logicalLibraryIsDisabled= false;
const uint64_t nbPartialTapes = 2;
const bool isEncrypted = true;
const cta::optional<std::string> supply("value for the supply pool mechanism");
m_catalogue->createMediaType(m_admin, m_mediaType);
m_catalogue->createLogicalLibrary(m_admin, m_tape1.logicalLibraryName, logicalLibraryIsDisabled, "Create logical library");
m_catalogue->createVirtualOrganization(m_admin, m_vo);
m_catalogue->createTapePool(m_admin, m_tape1.tapePoolName, m_vo.name, nbPartialTapes, isEncrypted, supply, "Create tape pool");
m_catalogue->createTape(m_admin, m_tape1);
std::string reason = "tape broken";
ASSERT_NO_THROW(m_catalogue->modifyTapeState(m_tape1.vid,common::dataStructures::Tape::State::BROKEN,reason,m_admin.username));
//TODO: Get the tape back to see if the state has been changed with the reason
}
TEST_P(cta_catalogue_CatalogueTest, modifyTapeStateResetReasonWhenBackToActiveState) {
using namespace cta;
const bool logicalLibraryIsDisabled= false;
const uint64_t nbPartialTapes = 2;
const bool isEncrypted = true;
const cta::optional<std::string> supply("value for the supply pool mechanism");
m_catalogue->createMediaType(m_admin, m_mediaType);
m_catalogue->createLogicalLibrary(m_admin, m_tape1.logicalLibraryName, logicalLibraryIsDisabled, "Create logical library");
m_catalogue->createVirtualOrganization(m_admin, m_vo);
m_catalogue->createTapePool(m_admin, m_tape1.tapePoolName, m_vo.name, nbPartialTapes, isEncrypted, supply, "Create tape pool");
m_catalogue->createTape(m_admin, m_tape1);
std::string reason = "Broken tape";
ASSERT_NO_THROW(m_catalogue->modifyTapeState(m_tape1.vid,common::dataStructures::Tape::State::BROKEN,reason,m_admin.username));
ASSERT_NO_THROW(m_catalogue->modifyTapeState(m_tape1.vid,common::dataStructures::Tape::State::ACTIVE,cta::nullopt,m_admin.username));
//TODO: Get the tape back to see if the state has been changed with the reason reset to null
}
TEST_P(cta_catalogue_CatalogueTest, tapeLabelled) {
using namespace cta;
 
......
......@@ -4470,18 +4470,26 @@ void RdbmsCatalogue::modifyTapeState(const std::string &vid, const common::dataS
using namespace common::dataStructures;
const time_t now = time(nullptr);
cta::optional<std::string> stateReasonCopy = stateReason;
std::string stateStr;
try {
stateStr = Tape::STATE_TO_STRING_MAP.at(state);
} catch(std::out_of_range & ex){
std::string errorMsg = "The state provided in parameter (" + std::to_string(state) + ") is not known or has not been initialized";
throw cta::exception::Exception(errorMsg);
std::string errorMsg = "The state provided in parameter (" + std::to_string(state) + ") is not known or has not been initialized existing states are:";
for(const auto & kv: Tape::STRING_TO_STATE_MAP){
errorMsg += " " + kv.first;
}
throw UserSpecifiedANonExistentTapeState(errorMsg);
}
//Check the reason is set for all the status except the ACTIVE one, this is the only status that allows the reason to be set to null.
if(state != Tape::State::ACTIVE){
if(!stateReason){
throw exception::UserError(std::string("Cannot modify the state of the tape ") + vid + " to " + stateStr + " because the reason has not been provided.");
if(!stateReason || (stateReason && cta::utils::trimString(stateReason.value()).empty())){
if(state != Tape::State::ACTIVE){
throw UserSpecifiedAnEmptyStringReasonWhenTapeStateNotActive(std::string("Cannot modify the state of the tape ") + vid + " to " + stateStr + " because the reason has not been provided.");
} else {
//State is active, but no reason provided: we will reset the field so we assign nullopt to the state reason
stateReasonCopy = cta::nullopt;
}
}
......@@ -4497,13 +4505,14 @@ void RdbmsCatalogue::modifyTapeState(const std::string &vid, const common::dataS
auto stmt = conn.createStmt(sql);
stmt.bindString(":TAPE_STATE", stateStr);
stmt.bindString(":STATE_REASON", stateReason);
stmt.bindString(":STATE_REASON", stateReasonCopy);
stmt.bindUint64(":STATE_UPDATE_TIME", now);
stmt.bindString(":STATE_MODIFIED_BY",stateModifiedBy);
stmt.bindString(":VID",vid);
stmt.executeNonQuery();
if (0 == stmt.getNbAffectedRows()) {
throw exception::UserError(std::string("Cannot modify the state of the tape ") + vid + " because it does not exist");
throw UserSpecifiedANonExistentTape(std::string("Cannot modify the state of the tape ") + vid + " because it does not exist");
}
} catch(exception::UserError &) {
......
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