Skip to content
Snippets Groups Projects
Commit f5d2305e authored by Daniele Kruse's avatar Daniele Kruse
Browse files

Filled in the MockRemoteNS and added some simple tests for it

parent d3703697
No related branches found
No related tags found
No related merge requests found
......@@ -66,6 +66,13 @@ bool cta::RemotePath::operator==(const RemotePath &rhs) const {
return m_raw == rhs.m_raw;
}
//------------------------------------------------------------------------------
// operator<
//------------------------------------------------------------------------------
bool cta::RemotePath::operator<(const RemotePath &rhs) const {
return m_raw < rhs.m_raw;
}
//------------------------------------------------------------------------------
// getRaw
//------------------------------------------------------------------------------
......
......@@ -45,6 +45,11 @@ public:
* Equals operator.
*/
bool operator==(const RemotePath &rhs) const;
/**
* Less than operator
*/
bool operator<(const RemotePath &rhs) const;
/**
* Returns the raw path in the form "scheme:hierarchical_part".
......
......@@ -11,8 +11,9 @@ add_library (ctaremotens SHARED
add_library (ctaremotenstest SHARED
MockRemoteNS.cpp
MockRemoteNSFactory.cpp
RemoteNSDispatcherTest.cpp
MockRemoteNSFactory.cpp
MockRemoteNSTest.cpp
RemoteNSDispatcherTest.cpp
RemoteNSFactory.cpp)
target_link_libraries(ctaremotenstest
......
......@@ -16,6 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <map>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "common/exception/Exception.hpp"
#include "common/RemotePath.hpp"
#include "remotens/MockRemoteNS.hpp"
......@@ -29,30 +35,65 @@ cta::MockRemoteNS::~MockRemoteNS() throw() {
//------------------------------------------------------------------------------
// statFile
//------------------------------------------------------------------------------
cta::RemoteFileStatus cta::MockRemoteNS::statFile(const RemotePath &path)
const {
throw exception::Exception(std::string(__FUNCTION__) + " not implemented");
cta::RemoteFileStatus cta::MockRemoteNS::statFile(const RemotePath &path) const {
auto it = m_entries.find(path);
if(m_entries.end() == it) {
throw exception::Exception("MockRemoteNS: no such file or directory");
}
return m_entries.at(path);
}
//------------------------------------------------------------------------------
// regularFileExists
//------------------------------------------------------------------------------
bool cta::MockRemoteNS::regularFileExists(const RemotePath &remoteFile)
const {
throw exception::Exception(std::string(__FUNCTION__) + " not implemented");
bool cta::MockRemoteNS::regularFileExists(const RemotePath &remoteFile) const {
auto it = m_entries.find(remoteFile);
if(m_entries.end() == it) {
return false;
}
if(!S_ISREG(m_entries.at(remoteFile).getMode())) {
return false;
}
return true;
}
//------------------------------------------------------------------------------
// dirExists
//------------------------------------------------------------------------------
bool cta::MockRemoteNS::dirExists(const RemotePath &remoteFile) const {
throw exception::Exception(std::string(__FUNCTION__) + " not implemented");
auto it = m_entries.find(remoteFile);
if(m_entries.end() == it) {
return false;
}
if(!S_ISDIR(m_entries.at(remoteFile).getMode())) {
return false;
}
return true;
}
//------------------------------------------------------------------------------
// rename
//------------------------------------------------------------------------------
void cta::MockRemoteNS::rename(const RemotePath &remoteFile,
const RemotePath &newRemoteFile) {
throw exception::Exception(std::string(__FUNCTION__) + " not implemented");
void cta::MockRemoteNS::rename(const RemotePath &remoteFile, const RemotePath &newRemoteFile) {
auto it = m_entries.find(newRemoteFile);
if(m_entries.end() != it) {
throw exception::Exception("MockRemoteNS: destination path exists, cannot overwrite it");
}
it = m_entries.find(remoteFile);
if(m_entries.end() == it) {
throw exception::Exception("MockRemoteNS: source path does not exist");
}
m_entries[newRemoteFile] = m_entries[remoteFile];
m_entries.erase(remoteFile);
}
//------------------------------------------------------------------------------
// createEntry
//------------------------------------------------------------------------------
void cta::MockRemoteNS::createEntry(const RemotePath &path, const RemoteFileStatus &status){
auto it = m_entries.find(path);
if(m_entries.end() != it) {
throw exception::Exception("MockRemoteNS: path exists, cannot overwrite it");
}
m_entries[path] = status;
}
\ No newline at end of file
......@@ -18,8 +18,11 @@
#pragma once
#include "common/RemoteFileStatus.hpp"
#include "remotens/RemoteNS.hpp"
#include <map>
namespace cta {
/**
......@@ -66,6 +69,21 @@ public:
*/
void rename(const RemotePath &remoteFile,
const RemotePath &newRemoteFile);
/**
* Adds a directory or file to the remote NS with the desired status
*
* @param status The desired status of the entry to be created
* @param path The absolute path of the file or directory
*/
void createEntry(const RemotePath &path, const RemoteFileStatus &status);
private:
/**
* The fake filesystem entries (dirs and files)
*/
std::map<RemotePath, RemoteFileStatus> m_entries;
}; // class MockRemoteNS
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/RemotePath.hpp"
#include "remotens/MockRemoteNS.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class cta_MockRemoteNsTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
}; // class cta_MockRemoteNsTest
TEST_F(cta_MockRemoteNsTest, createNonExistingFile) {
using namespace cta;
MockRemoteNS rns;
const UserIdentity owner(1,2);
const mode_t mode(S_IFREG|S_IRWXU|S_IRWXG|S_IRWXO);
const uint64_t size = 50;
const RemoteFileStatus status(owner, mode, size);
const RemotePath rp("eos:/path/to/file");
bool regularFileExists = true;
ASSERT_NO_THROW(regularFileExists = rns.regularFileExists(rp));
ASSERT_FALSE(regularFileExists);
ASSERT_NO_THROW(rns.createEntry(rp, status));
regularFileExists = false;
ASSERT_NO_THROW(regularFileExists = rns.regularFileExists(rp));
ASSERT_TRUE(regularFileExists);
}
TEST_F(cta_MockRemoteNsTest, createExistingFile) {
using namespace cta;
MockRemoteNS rns;
const UserIdentity owner(1,2);
const mode_t mode(S_IFREG|S_IRWXU|S_IRWXG|S_IRWXO);
const uint64_t size = 50;
const RemoteFileStatus status(owner, mode, size);
const RemotePath rp("eos:/path/to/file");
ASSERT_NO_THROW(rns.createEntry(rp, status));
ASSERT_THROW(rns.createEntry(rp, status), std::exception);
}
TEST_F(cta_MockRemoteNsTest, createNonExistingDir) {
using namespace cta;
MockRemoteNS rns;
const UserIdentity owner(1,2);
const mode_t mode(S_IFDIR|S_IRWXU|S_IRWXG|S_IRWXO);
const uint64_t size = 50;
const RemoteFileStatus status(owner, mode, size);
const RemotePath rp("eos:/path/to/dir");
bool dirExists = true;
ASSERT_NO_THROW(dirExists = rns.dirExists(rp));
ASSERT_FALSE(dirExists);
ASSERT_NO_THROW(rns.createEntry(rp, status));
dirExists = false;
ASSERT_NO_THROW(dirExists = rns.dirExists(rp));
ASSERT_TRUE(dirExists);
}
TEST_F(cta_MockRemoteNsTest, createExistingDir) {
using namespace cta;
MockRemoteNS rns;
const UserIdentity owner(1,2);
const mode_t mode(S_IFDIR|S_IRWXU|S_IRWXG|S_IRWXO);
const uint64_t size = 50;
RemoteFileStatus status(owner, mode, size);
const RemotePath rp("eos:/path/to/dir");
ASSERT_NO_THROW(rns.createEntry(rp, status));
ASSERT_THROW(rns.createEntry(rp, status), std::exception);
}
TEST_F(cta_MockRemoteNsTest, statFile) {
using namespace cta;
MockRemoteNS rns;
const UserIdentity owner(1,2);
const mode_t mode(S_IFREG|S_IRWXU|S_IRWXG|S_IRWXO);
const uint64_t size = 50;
RemoteFileStatus status(owner, mode, size);
const RemotePath rp("eos:/path/to/file");
ASSERT_NO_THROW(rns.createEntry(rp, status));
RemoteFileStatus result;
ASSERT_NO_THROW(result = rns.statFile(rp));
ASSERT_TRUE(result.getMode()==mode);
ASSERT_TRUE(result.getOwner()==owner);
ASSERT_TRUE(result.getSize()==size);
}
TEST_F(cta_MockRemoteNsTest, rename) {
using namespace cta;
MockRemoteNS rns;
const UserIdentity owner(1,2);
const mode_t mode(S_IFREG|S_IRWXU|S_IRWXG|S_IRWXO);
const uint64_t size = 50;
RemoteFileStatus status(owner, mode, size);
const RemotePath rp("eos:/path/to/file");
ASSERT_NO_THROW(rns.createEntry(rp, status));
const RemotePath new_rp("eos:/path/to/file2");
ASSERT_NO_THROW(rns.rename(rp, new_rp));
RemoteFileStatus result;
ASSERT_NO_THROW(result = rns.statFile(new_rp));
ASSERT_TRUE(result.getMode()==mode);
ASSERT_TRUE(result.getOwner()==owner);
ASSERT_TRUE(result.getSize()==size);
}
} // namespace unitTests
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment