From c43fdc706f07a5664466089f20510e8269b1cc2b Mon Sep 17 00:00:00 2001
From: Steven Murray <Steven.Murray@cern.ch>
Date: Tue, 16 Feb 2021 11:46:43 +0100
Subject: [PATCH] cta/CTA#966 Unable to distinguish empty and wrong tape pool

Fixed.
---
 catalogue/Catalogue.hpp      |  3 +++
 catalogue/CatalogueTest.cpp  | 22 ++++++++++++++++++++++
 catalogue/RdbmsCatalogue.cpp |  6 ++++++
 3 files changed, 31 insertions(+)

diff --git a/catalogue/Catalogue.hpp b/catalogue/Catalogue.hpp
index dea47dc6b6..14b7091e21 100644
--- a/catalogue/Catalogue.hpp
+++ b/catalogue/Catalogue.hpp
@@ -91,6 +91,7 @@ CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedANonEmptyLogicalLibrary);
 CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedANonEmptyTape);
 CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedANonExistentLogicalLibrary);
 CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedANonExistentTape);
+CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedANonExistentTapePool);
 CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedAnEmptyStringComment);
 CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedAnEmptyStringDiskSystemName);
 CTA_GENERATE_USER_EXCEPTION_CLASS(UserSpecifiedAnEmptyStringFileRegexp);
@@ -580,6 +581,8 @@ public:
    *
    * @param searchCriteria The search criteria.
    * @return The list of tapes.
+   * @throw UserSpecifiedANonExistentTapePool if the user specified a
+   * non-existent tape pool.
    */
   virtual std::list<common::dataStructures::Tape> getTapes(
     const TapeSearchCriteria &searchCriteria = TapeSearchCriteria()) const = 0;
diff --git a/catalogue/CatalogueTest.cpp b/catalogue/CatalogueTest.cpp
index 96056b61be..4987884735 100644
--- a/catalogue/CatalogueTest.cpp
+++ b/catalogue/CatalogueTest.cpp
@@ -1136,6 +1136,28 @@ TEST_P(cta_catalogue_CatalogueTest, deleteMediaType_usedByTapes) {
   ASSERT_THROW(m_catalogue->deleteMediaType(m_tape1.mediaType), exception::UserError);
 }
 
+TEST_P(cta_catalogue_CatalogueTest, getTapes_non_existent_tape_pool) {
+  using namespace cta;
+
+  log::LogContext dummyLc(m_dummyLog);
+  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);
+   
+  {
+    cta::catalogue::TapeSearchCriteria criteria;
+    criteria.tapePool = "non_existent";
+    ASSERT_THROW(m_catalogue->getTapes(criteria), catalogue::UserSpecifiedANonExistentTapePool);
+  }
+}
+
 TEST_P(cta_catalogue_CatalogueTest, createTape_deleteStorageClass) {
   // TO BE DONE
 }
diff --git a/catalogue/RdbmsCatalogue.cpp b/catalogue/RdbmsCatalogue.cpp
index 8897028818..903451754f 100644
--- a/catalogue/RdbmsCatalogue.cpp
+++ b/catalogue/RdbmsCatalogue.cpp
@@ -3667,6 +3667,12 @@ std::list<common::dataStructures::Tape> RdbmsCatalogue::getTapes(rdbms::Conn &co
   if(isSetAndEmpty(searchCriteria.diskFileIds)) throw exception::UserError("Disk file ID list cannot be empty");
 
   try {
+    if(searchCriteria.tapePool && !tapePoolExists(conn, searchCriteria.tapePool.value())) {
+      UserSpecifiedANonExistentTapePool ex;
+      ex.getMessage() << "Cannot list tapes because tape pool " + searchCriteria.tapePool.value() + " does not exist";
+      throw ex;
+    }
+
     std::list<common::dataStructures::Tape> tapes;
     std::string sql =
       "SELECT "
-- 
GitLab