From 8b0573a2bb1498fad4d20811c2af4c59460c8bff Mon Sep 17 00:00:00 2001
From: Eric Cano <Eric.Cano@cern.ch>
Date: Wed, 24 Feb 2016 12:47:33 +0100
Subject: [PATCH] Fixed unit test relying on existence of /etc/fstab, which
 does not exist in containers. Fixed exception failing to collect errnum.

---
 tapeserver/castor/io/IoTest.cpp               | 19 ++------
 .../castor/tape/tapeserver/file/FileTest.cpp  | 44 ++++++++++++++++---
 2 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/tapeserver/castor/io/IoTest.cpp b/tapeserver/castor/io/IoTest.cpp
index 8dc6b7b692..83db809beb 100644
--- a/tapeserver/castor/io/IoTest.cpp
+++ b/tapeserver/castor/io/IoTest.cpp
@@ -24,6 +24,7 @@
 #include "castor/io/io.hpp"
 #include "castor/utils/SmartFd.hpp"
 #include "castor/io/marshall.h"
+#include "castor/exception/Errnum.hpp"
 
 #include <fcntl.h>
 #include <gtest/gtest.h>
@@ -81,21 +82,9 @@ protected:
       strncpy(listenAddr.sun_path, listenSockPath,
         sizeof(listenAddr.sun_path) - 1);
 
-      if(0 != bind(smartListenSock.get(), (const struct sockaddr *)&listenAddr,
-        sizeof(listenAddr))) {
-        char strErrBuf[256];
-        if(0 != strerror_r(errno, strErrBuf, sizeof(strErrBuf))) {
-          memset(strErrBuf, '\0', sizeof(strErrBuf));
-          strncpy(strErrBuf, "Unknown", sizeof(strErrBuf) - 1);
-        }
-
-        std::string errorMessage("Call to bind() failed: ");
-        errorMessage += strErrBuf;
-
-        castor::exception::Exception ex(ECANCELED);
-        ex.getMessage() << errorMessage;
-        throw ex;
-      }
+      castor::exception::Errnum::throwOnNonZero(
+        bind(smartListenSock.get(), (const struct sockaddr *)&listenAddr,
+        sizeof(listenAddr)), "Call to bind() failed: ");
     }
 
     // Make the socket listen
diff --git a/tapeserver/castor/tape/tapeserver/file/FileTest.cpp b/tapeserver/castor/tape/tapeserver/file/FileTest.cpp
index de27fa6bdb..db5c16332d 100644
--- a/tapeserver/castor/tape/tapeserver/file/FileTest.cpp
+++ b/tapeserver/castor/tape/tapeserver/file/FileTest.cpp
@@ -227,17 +227,51 @@ namespace unitTests {
     EXPECT_NO_THROW(ws.validateNextFSeq(2));
     EXPECT_THROW(ws.validateNextFSeq(1), castor::exception::Exception);
   }
-    
+ 
+  // Class creating a temporary file of 1kB and deleting it 
+  // automatically
+  class TempFile {
+  public:
+    TempFile() {
+      char path[]="/tmp/testCTA-XXXXXX";
+      int fd=::mkstemp(path);
+      castor::exception::Errnum::throwOnMinusOne(fd, "In TempFile::TempFile: failed to mkstemp: ");
+      ::close(fd);
+      m_path=path;
+    }
+    TempFile(const std::string& path): m_path(path) {}
+    std::string path() { return m_path; }
+    void randomFill(size_t size) {
+      std::ofstream out(m_path,std::ios::out | std::ios::binary);
+      std::ifstream in("/dev/urandom", std::ios::in|std::ios::binary);
+      std::unique_ptr<char[]> buff(new char[size]);
+      in.read(buff.get(),size);
+      out.write(buff.get(),size);
+    }
+    ~TempFile() {
+      if (m_path.size()) {
+        ::unlink(m_path.c_str());
+      }
+    }
+  private:
+    std::string m_path;
+  };
+  
   TEST(castorTapeDiskFile, canWriteAndReadDisk) {
     const uint32_t block_size = 1024;
     char data1[block_size];
     char data2[block_size];
     castor::tape::diskFile::DiskFileFactory fileFactory("RFIO","");
+    TempFile sourceFile;
+    sourceFile.randomFill(1000);
+    TempFile destinationFile(sourceFile.path()+"_dst");
+    // host part of file location
+    std::string lh = "localhost:";
     {
       std::unique_ptr<castor::tape::diskFile::ReadFile> rf(
-        fileFactory.createReadFile("localhost:/etc/fstab"));
+        fileFactory.createReadFile(lh + sourceFile.path()));
       std::unique_ptr<castor::tape::diskFile::WriteFile> wf(
-        fileFactory.createWriteFile("localhost:/tmp/fstab"));
+        fileFactory.createWriteFile(lh + destinationFile.path()));
       size_t res=0;
       do {
         res = rf->read(data1, block_size);
@@ -246,9 +280,9 @@ namespace unitTests {
       wf->close();
     }
     std::unique_ptr<castor::tape::diskFile::ReadFile> src(
-        fileFactory.createReadFile("localhost:/tmp/fstab"));
+        fileFactory.createReadFile(sourceFile.path()));
     std::unique_ptr<castor::tape::diskFile::ReadFile> dst(
-        fileFactory.createReadFile("localhost:/etc/fstab"));
+        fileFactory.createReadFile(destinationFile.path()));
     size_t res1=0;
     size_t res2=0;
     do {
-- 
GitLab