From 51f5e1c28ad14c68436bc52ce882b6c95640fe54 Mon Sep 17 00:00:00 2001
From: Eric Cano <Eric.Cano@cern.ch>
Date: Fri, 29 May 2015 18:10:52 +0200
Subject: [PATCH] Disabled the tests for objectstore middle tier which will not
 be updated for a while, pending the middle tier interface review. Remove the
 now useless (following the move to generic test) printouts of the type in
 backend store tests. Added tests for RootEntry's admin hosts and admin users
 interface.

---
 objectstore/BackendTest.cpp   |   8 +--
 objectstore/RootEntryTest.cpp | 124 +++++++++++++++++++++++++++++++++-
 objectstore/UserIdentity.hpp  |   5 +-
 tests/CMakeLists.txt          |   2 +-
 4 files changed, 130 insertions(+), 9 deletions(-)

diff --git a/objectstore/BackendTest.cpp b/objectstore/BackendTest.cpp
index 3698a35d09..1b9fd8b833 100644
--- a/objectstore/BackendTest.cpp
+++ b/objectstore/BackendTest.cpp
@@ -24,7 +24,7 @@
 namespace unitTests {
 
 TEST_P(BackendAbstractTest, BasicReadWrite) {
-  std::cout << "Type=" << m_os->typeName() << std::endl;
+  //std::cout << "Type=" << m_os->typeName() << std::endl;
   const std::string testValue = "1234";
   const std::string testSecondValue = "1234";
   const std::string testObjectName = "testObject";
@@ -52,7 +52,7 @@ TEST_P(BackendAbstractTest, BasicReadWrite) {
 }
 
 TEST_P(BackendAbstractTest, LockingInterface) {
-  std::cout << "Type=" << m_os->typeName() << std::endl;
+  //std::cout << "Type=" << m_os->typeName() << std::endl;
   const std::string testObjectName = "testObject";
   m_os->create(testObjectName, "");
   {
@@ -70,10 +70,10 @@ TEST_P(BackendAbstractTest, LockingInterface) {
 }
 
 TEST_P(BackendAbstractTest, ParametersInterface) {
-  std::cout << "Type=" << m_os->typeName() << std::endl;
+  //std::cout << "Type=" << m_os->typeName() << std::endl;
   std::unique_ptr<cta::objectstore::Backend::Parameters> params(
     m_os->getParams());
-  std::cout << params->toStr() << std::endl;
+  //std::cout << params->toStr() << std::endl;
 }
 
 cta::objectstore::BackendVFS osVFS;
diff --git a/objectstore/RootEntryTest.cpp b/objectstore/RootEntryTest.cpp
index 380b2c2e27..03b9be160c 100644
--- a/objectstore/RootEntryTest.cpp
+++ b/objectstore/RootEntryTest.cpp
@@ -38,7 +38,7 @@ TEST(RootEntry, BasicAccess) {
     ASSERT_THROW(re.fetch(), cta::exception::Exception);
     cta::objectstore::ScopedSharedLock lock(re);
     ASSERT_NO_THROW(re.fetch());
-    std::cout << re.dump();
+    re.dump();
   }
   {
     // Try to allocate the agent register
@@ -63,4 +63,126 @@ TEST(RootEntry, BasicAccess) {
   ASSERT_EQ(false, re.exists());
 }
 
+TEST(RootEntry, AdminHosts) {
+  cta::objectstore::BackendVFS be;
+  { 
+    // Try to create the root entry
+    cta::objectstore::RootEntry re(be);
+    re.initialize();
+    re.insert();
+  }
+  {
+    // Add 2 admin hosts to the root entry
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedExclusiveLock lock(re);
+    ASSERT_NO_THROW(re.fetch());
+    cta::objectstore::CreationLog cl(99, "dummyUser", 99, "dummyGroup", 
+      "unittesthost", time(NULL), "Creation of unit test agent register");
+    re.addAdminHost("adminHost1", cl);
+    re.addAdminHost("adminHost2", cl);
+    re.commit();
+  }
+  {
+    // Check that the admin hosts made it
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedSharedLock lock(re);
+    re.fetch();
+    ASSERT_TRUE(re.isAdminHost("adminHost1"));
+    ASSERT_TRUE(re.isAdminHost("adminHost2"));
+    ASSERT_FALSE(re.isAdminHost("adminHost3"));
+  }
+  {
+    // Check that we can remove existing and non-existing hosts
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedExclusiveLock lock(re);
+    re.fetch();
+    re.removeAdminHost("adminHost1");
+    re.removeAdminHost("noSuch");
+    re.commit();
+  }
+  {
+    // Check that we got the expected result
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedSharedLock lock(re);
+    re.fetch();
+    ASSERT_FALSE(re.isAdminHost("adminHost1"));
+    ASSERT_TRUE(re.isAdminHost("adminHost2"));
+    ASSERT_FALSE(re.isAdminHost("adminHost3"));
+    ASSERT_EQ(1, re.dumpAdminHosts().size());
+    ASSERT_EQ("adminHost2", re.dumpAdminHosts().front().hostname);
+  }
+  // Delete the root entry
+  cta::objectstore::RootEntry re(be);
+  cta::objectstore::ScopedExclusiveLock lock(re);
+  re.remove();
+  ASSERT_EQ(false, re.exists());
+}
+  
+TEST(RootEntry, AdminUsers) {
+  cta::objectstore::BackendVFS be;
+  { 
+    // Try to create the root entry
+    cta::objectstore::RootEntry re(be);
+    re.initialize();
+    re.insert();
+  }
+  cta::objectstore::UserIdentity user1(123, "user123", 456, "group456");
+  cta::objectstore::UserIdentity user1prime(123, "somethingelse", 789, "group789");
+  cta::objectstore::UserIdentity user2(234, "user234", 345, "group345");
+  cta::objectstore::UserIdentity user2prime(234, "somethingwrong", 567, "group567");
+  cta::objectstore::UserIdentity user3(345, "user234", 345, "group345");
+  {
+    // Add 2 admin users to the root entry
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedExclusiveLock lock(re);
+    ASSERT_NO_THROW(re.fetch());
+    cta::objectstore::CreationLog cl(99, "dummyUser", 99, "dummyGroup", 
+      "unittesthost", time(NULL), "Creation of unit test agent register");
+    re.addAdminUser(user1, cl);
+    re.addAdminUser(user2, cl);
+    re.commit();
+  }
+  {
+    // Check that the admin hosts made it and that uid is the only meaningful
+    // criteria
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedSharedLock lock(re);
+    re.fetch();
+    ASSERT_TRUE(re.isAdminUser(user1));
+    ASSERT_TRUE(re.isAdminUser(user1prime));
+    ASSERT_TRUE(re.isAdminUser(user2));
+    ASSERT_TRUE(re.isAdminUser(user2prime));
+    ASSERT_FALSE(re.isAdminUser(user3));
+
+  }
+  {
+    // Check that we can remove existing and non-existing hosts
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedExclusiveLock lock(re);
+    re.fetch();
+    re.removeAdminUser(user1prime);
+    re.removeAdminUser(user3);
+    re.commit();
+  }
+  {
+    // Check that we got the expected result
+    cta::objectstore::RootEntry re(be);
+    cta::objectstore::ScopedSharedLock lock(re);
+    re.fetch();
+    ASSERT_FALSE(re.isAdminUser(user1));
+    ASSERT_FALSE(re.isAdminUser(user1prime));
+    ASSERT_TRUE(re.isAdminUser(user2));
+    ASSERT_TRUE(re.isAdminUser(user2prime));
+    ASSERT_FALSE(re.isAdminUser(user3));
+    ASSERT_EQ(1, re.dumpAdminUsers().size());
+    ASSERT_EQ(user2.uid, re.dumpAdminUsers().front().user.uid);
+  }
+  
+  // Delete the root entry
+  cta::objectstore::RootEntry re(be);
+  cta::objectstore::ScopedExclusiveLock lock(re);
+  re.remove();
+  ASSERT_EQ(false, re.exists());
+}
+
 }
diff --git a/objectstore/UserIdentity.hpp b/objectstore/UserIdentity.hpp
index 8577db2555..56b0233122 100644
--- a/objectstore/UserIdentity.hpp
+++ b/objectstore/UserIdentity.hpp
@@ -28,9 +28,8 @@ public:
   UserIdentity (): uid(std::numeric_limits<decltype(uid)>::max()),
           gid(std::numeric_limits<decltype(gid)>::max()) {}
   UserIdentity (uint32_t ui, const std::string & un,
-    uint32_t gi, const std::string & gn,
-    const std::string & hn, uint64_t t,
-    const std::string & c): uid(ui), uname(un), gid(gi), gname(gn) {}
+    uint32_t gi, const std::string & gn): 
+    uid(ui), uname(un), gid(gi), gname(gn) {}
   uint32_t uid;
   std::string uname;
   uint32_t gid;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b397b8860d..354d719cab 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -15,7 +15,7 @@ target_link_libraries(unittests
   ctamiddletierunittests
   ctamiddletiersqlite
   ctamiddletiersqliteunittests
-  ctamiddletierobjectstoreunittests
+  #ctamiddletierobjectstoreunittests
   ctamiddletierobjectstore
   ctanameservertest
   ctaschedulertest
-- 
GitLab