Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Unit Driven tests using Google Test frameworks #26

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@

[submodule "submodules/fastlog"]
path = submodules/fastlog
url = https://github.com/gabrielefronze/fastlog.git
url = https://github.com/gabrielefronze/fastlog.git
[submodule "submodules/googletest"]
path = submodules/googletest
url = https://github.com/google/googletest
3 changes: 0 additions & 3 deletions source/REST-API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ std::vector<std::string> rucio_get_replicas_metalinks(const std::string& path){
auto beg_pfn = rses.find('[', 0);
auto end_pfn = rses.find(']', beg_pfn + 1);

std::cout << beg_pfn << std::endl;
std::cout << end_pfn << std::endl;

std::vector<std::string> pfns;

while(beg_pfn != std::string::npos && end_pfn != std::string::npos){
Expand Down
1 change: 1 addition & 0 deletions submodules/googletest
Submodule googletest added at 011959
28 changes: 27 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.8)

add_executable(
test_curl-REST
test_curl-REST.cpp
Expand All @@ -14,4 +16,28 @@ add_executable(
test_REST-API
test_REST-API.cpp
)
target_link_libraries(test_REST-API PUBLIC rucio-rest-api-wrapper globals)
target_link_libraries(test_REST-API PUBLIC rucio-rest-api-wrapper globals)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link gtest_utils with what we want to test and the GTest and pthread library
add_executable(
gtest_utils
gtest_utils.cpp)

target_link_libraries(gtest_utils PUBLIC utils ${GTEST_LIBRARIES} pthread)

add_executable(
gtest_curl-REST
gtest_curl-REST.cpp)

target_link_libraries(gtest_curl-REST PUBLIC curl-wrapper ${GTEST_LIBRARIES} pthread)

add_executable(
gtest_REST-API
gtest_REST-API.cpp
)

target_link_libraries(gtest_REST-API PUBLIC rucio-rest-api-wrapper globals ${GTEST_LIBRARIES} pthread)
86 changes: 86 additions & 0 deletions tests/gtest_REST-API.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Authors:
- Vivek Nigam <[email protected]>, 2020
*/

#include <REST-API.h>
#include <globals.h>
#include <iostream>
#include <vector>
#include <gtest/gtest.h>

using namespace std;
using namespace fastlog;

std::string server_name="rucio-dev-server";

TEST(RESTAPI_Test, Test_rucio_ping){
ASSERT_TRUE(rucio_ping("https://localhost/"));
}

TEST(RESTAPI_Test, Test_server_connection){
auto servers = rucio_list_servers();
if (servers.empty()){
fastlog(ERROR, "No servers found!");
exit(-1);
}
ASSERT_EQ(server_name, servers[0]);
ASSERT_NO_THROW(rucio_get_auth_token_userpass(server_name));
ASSERT_TRUE(rucio_is_token_valid(server_name));
}

TEST(RESTAPI_Test, Test_server_scopes){
auto scopes = rucio_list_scopes(server_name);
ASSERT_TRUE(scopes.size()==5);
ASSERT_EQ("data13_hip", scopes[0]);
ASSERT_EQ("test", scopes[4]);
}

TEST(RESTAPI_Test, Test_scope_dids){
auto ret = rucio_list_dids("mock", server_name);
ASSERT_TRUE(ret.empty());

ret = rucio_list_dids("archive", server_name);
ASSERT_TRUE(ret.empty());

ret = rucio_list_dids("test", server_name);
ASSERT_TRUE(ret.size()>=2);
ASSERT_EQ("container", ret[0].name);
ASSERT_EQ("dataset3", ret[1].name);
ASSERT_NE("dataset1", ret[1].name);
}

TEST(RESTAPI_Test, Test_is_container){
EXPECT_TRUE(rucio_is_container("/"+server_name+"/test/container"));
//EXPECT_FALSE(rucio_is_container("/"+server_name+"/test/container/dataset1"));
//EXPECT_FALSE(rucio_is_container("/"+server_name+"/test/dataset3"));
}

TEST(RESTAPI_Test, Test_rucio_get_replicas_metalinks){
ASSERT_TRUE(not rucio_get_replicas_metalinks("/"+server_name+"/test/container/dataset1/file1").empty());
ASSERT_FALSE(rucio_get_replicas_metalinks("/"+server_name+"/test/container/dataset1/file1").empty());
ASSERT_TRUE(not rucio_get_replicas_metalinks("/"+server_name+"/test/container/dataset1/file2").empty());
}

TEST(RESTAPI_Test, Test_rucio_list_container_dids){
auto ret = rucio_list_container_dids("test", "container", server_name);
ASSERT_TRUE(ret.size()==2);
ASSERT_EQ("dataset1", ret[0].name);
ASSERT_EQ("dataset2", ret[1].name);
ASSERT_NE("dataser3", ret[1].name);

ret = rucio_list_container_dids("test", "dataset3", server_name);
ASSERT_TRUE(ret.size()==1);
ASSERT_EQ("file4", ret[0].name);

ret = rucio_list_container_dids("test", "dataset2", server_name);
ASSERT_TRUE(ret.size()==2);
ASSERT_EQ("file3", ret[0].name);
ASSERT_EQ("file4", ret[1].name);
}

int main(int argc, char **argv) {
parse_settings();
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
18 changes: 18 additions & 0 deletions tests/gtest_curl-REST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Authors:
- Vivek Nigam <[email protected]>, 2020
*/

#include <curl-REST.h>
#include <gtest/gtest.h>
#include <iostream>

TEST(curl_Test, Test){
auto res = GET("www.example.com");
ASSERT_EQ(1, res.payload.size());
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
95 changes: 95 additions & 0 deletions tests/gtest_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Authors:
- Vivek Nigam <[email protected]>, 2020
*/

#include <utils.h>
#include <vector>
#include <gtest/gtest.h>

using namespace std;

TEST(Utils_Test, Test_is_root_path){
EXPECT_EQ(1, (bool)is_root_path("/"));
EXPECT_EQ(0, (bool)is_root_path("/a"));
EXPECT_EQ(0, (bool)is_root_path("/a/b"));
}

TEST(Utils_Test, Test_path_depth){
EXPECT_EQ(0, path_depth("/", '/'));
EXPECT_EQ(1, path_depth("/a",'/'));
EXPECT_EQ(1, path_depth("/a/",'/'));
EXPECT_EQ(2, path_depth("/a/b",'/'));
EXPECT_EQ(6, path_depth("/a/b/c/d/e/f/",'/'));
EXPECT_EQ(0, path_depth("a", 'a'));
}

TEST(Utils_Test, Test_is_server_mountpoint){
EXPECT_TRUE(is_server_mountpoint("/server1"));
EXPECT_TRUE(is_server_mountpoint("/server1/"));
EXPECT_FALSE(is_server_mountpoint("/"));
EXPECT_FALSE(is_server_mountpoint("/server1/scope1"));
EXPECT_FALSE(is_server_mountpoint("/server1/scope1/"));
EXPECT_FALSE(is_server_mountpoint("/server1/scope1/name1"));
}

TEST(Utils_Test, Test_is_main_scope){
EXPECT_FALSE(is_main_scope("/server1"));
EXPECT_FALSE(is_main_scope("/server1/"));
EXPECT_FALSE(is_main_scope("/"));
EXPECT_TRUE(is_main_scope("/server1/scope1"));
EXPECT_TRUE(is_main_scope("/server1/scope1/"));
EXPECT_FALSE(is_main_scope("/server1/scope1/name1"));
}

TEST(Utils_Test, Test_string_manipulation){
string test_string = "/server/scope/container/dataset/filename/";
string exp_string = "/server/scope/container/dataset/filename";
remove_trailing_token(test_string);

EXPECT_EQ(test_string, exp_string);
EXPECT_EQ("/server/scope/dataset/filename", remove_substring(test_string, "container/"));
EXPECT_EQ("server/scope/container/dataset/filename", remove_root_path(test_string));
EXPECT_EQ("server", extract_server_name(test_string));
EXPECT_EQ("scope", extract_scope(test_string));
EXPECT_EQ("filename", extract_name(test_string));
EXPECT_EQ("scope:filename", get_did(test_string));
}

TEST(Utils_Test, Test_structurize_DID){
std::string test_did = R"(
{
"scope": "user.root",
"type": "FILE",
"name": "test-19102019.txt",
"parent": null,
"level": 0
})";
std::vector<rucio_did> did_v;
structurize_did(test_did, did_v);
EXPECT_EQ("user.root", did_v[0].scope);
EXPECT_EQ(rucio_data_type::rucio_file, did_v[0].type);
EXPECT_EQ("test-19102019.txt", did_v[0].name);
EXPECT_EQ("null", did_v[0].parent);
EXPECT_EQ(0, did_v[0].level);
}

TEST(Utils_Test, Test_tokenize_python_list){
std::vector<std::string> expected_scopes = {"a","b","c","d"};
std::vector<std::string> scopes;
tokenize_python_list(R"(["a","b","c","d"])", scopes);
EXPECT_EQ("a", scopes[0]);
EXPECT_EQ("b", scopes[1]);
EXPECT_EQ("c", scopes[2]);
EXPECT_EQ("d", scopes[3]);
}

TEST(Utils_Test, Test_is_hidden){
EXPECT_TRUE(is_hidden("/server/scope/container/dataset/.filename/"));
EXPECT_FALSE(is_hidden("/server/scope/container/dataset/"));
}

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}