From c108e9f6c4d6c0596b793ee3cc031f561c9f1ced Mon Sep 17 00:00:00 2001 From: leizhongkai Date: Tue, 25 Feb 2020 23:38:37 +0800 Subject: [PATCH] iSulad: add testcase for isulad-shim Signed-off-by: leizhongkai --- src/cmd/isulad-shim/common.h | 8 ++ src/cmd/isulad-shim/process.h | 7 ++ test/cmd/CMakeLists.txt | 1 + test/cmd/isulad-shim/CMakeLists.txt | 22 +++++ test/cmd/isulad-shim/isulad-shim_llt.cc | 122 ++++++++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 test/cmd/isulad-shim/CMakeLists.txt create mode 100644 test/cmd/isulad-shim/isulad-shim_llt.cc diff --git a/src/cmd/isulad-shim/common.h b/src/cmd/isulad-shim/common.h index a1ee0db..2a6c914 100644 --- a/src/cmd/isulad-shim/common.h +++ b/src/cmd/isulad-shim/common.h @@ -18,6 +18,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + // error code #define SHIM_ERR_BASE (-10000) #define SHIM_SYS_ERR(err) (SHIM_ERR_BASE-err) @@ -60,5 +64,9 @@ void close_fd(int *pfd); int open_no_inherit(const char *path, int flag, mode_t mode); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/cmd/isulad-shim/process.h b/src/cmd/isulad-shim/process.h index e641813..71eca2e 100644 --- a/src/cmd/isulad-shim/process.h +++ b/src/cmd/isulad-shim/process.h @@ -21,6 +21,10 @@ #include #include "shim_client_process_state.h" +#ifdef __cplusplus +extern "C" { +#endif + enum { stdid_in = 0, stdid_out, @@ -88,6 +92,9 @@ int create_process(process_t *p); int process_signal_handle_routine(process_t *p); void process_delete(process_t *p); +#ifdef __cplusplus +} +#endif #endif diff --git a/test/cmd/CMakeLists.txt b/test/cmd/CMakeLists.txt index 69a7c3d..69dac71 100644 --- a/test/cmd/CMakeLists.txt +++ b/test/cmd/CMakeLists.txt @@ -1,3 +1,4 @@ project(iSulad_LLT) add_subdirectory(isula) +add_subdirectory(isulad-shim) diff --git a/test/cmd/isulad-shim/CMakeLists.txt b/test/cmd/isulad-shim/CMakeLists.txt new file mode 100644 index 0000000..8757c7e --- /dev/null +++ b/test/cmd/isulad-shim/CMakeLists.txt @@ -0,0 +1,22 @@ +project(iSulad_LLT) + +SET(EXE isulad-shim_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim/process.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim/common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json/schema/src/read_file.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_BINARY_DIR}/json/shim_client_process_state.c + isulad-shim_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${GTEST_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json/schema/src/ + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ) +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lyajl) diff --git a/test/cmd/isulad-shim/isulad-shim_llt.cc b/test/cmd/isulad-shim/isulad-shim_llt.cc new file mode 100644 index 0000000..e129519 --- /dev/null +++ b/test/cmd/isulad-shim/isulad-shim_llt.cc @@ -0,0 +1,122 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. + * Description: isulad-shim llt + * Author: leizhongkai + * Create: 2020-02-25 + */ +#include +#include +#include +#include +#include +#include + +#include "process.h" +#include "common.h" + +int g_log_fd = -1; + +using ::testing::Args; +using ::testing::ByRef; +using ::testing::SetArgPointee; +using ::testing::DoAll; +using ::testing::NiceMock; +using ::testing::Return; +using ::testing::NotNull; +using ::testing::AtLeast; +using ::testing::Invoke; +using ::testing::_; + +using namespace std; + +class IsuladShimUnitTest : public testing::Test { +public: + void SetUp() override + { + } + void TearDown() override + { + } +}; + +TEST_F(IsuladShimUnitTest, test_new_process) +{ + string id="aaaabbbbccccdddd"; + string bundle = "/home/isulad/bundle"; + string runtime = "kata-runtime"; + + process_t *p = new_process((char*)id.c_str(), (char*)bundle.c_str(), (char*)runtime.c_str()); + ASSERT_TRUE(p == NULL); +} + +TEST_F(IsuladShimUnitTest, test_open_no_inherit) +{ + string exist_file = "/tmp/test_open_no_inherit_exist"; + string non_file = "/tmp/test_open_no_inherit_non"; + int fd_exist = -1; + + fd_exist = open_no_inherit(exist_file.c_str(), O_CREAT | O_WRONLY | O_APPEND | O_SYNC, 0640); + EXPECT_GT(fd_exist, 0); + EXPECT_EQ(open_no_inherit(non_file.c_str(), O_WRONLY, -1), -1); + + close(fd_exist); + unlink(exist_file.c_str()); +} + +TEST_F(IsuladShimUnitTest, test_read_write_nointr) +{ + char buf[32] = { 0 }; + string test_file = "/tmp/test_read_nointr"; + string test_string = "hello"; + int fd_wr = -1; + int fd_rd = -1; + int nwrite = -1; + int nread = -1; + + EXPECT_EQ(read_nointr(-1, NULL, 32), -1); + EXPECT_EQ(read_nointr(0, NULL, 32), -1); + EXPECT_EQ(read_nointr(1, NULL, 32), -1); + + fd_wr = open_no_inherit(test_file.c_str(), O_CREAT | O_RDWR | O_APPEND | O_SYNC , 0640); + EXPECT_GT(fd_wr, 0); + nwrite = write_nointr(fd_wr, test_string.c_str(), 5); + EXPECT_EQ(nwrite, 5); + fd_rd = open(test_file.c_str(), O_RDONLY); + nread = read_nointr(fd_rd, buf, 32); + EXPECT_EQ(nread, 5); + + close(fd_wr); + close(fd_rd); + unlink(test_file.c_str()); +} + +TEST_F(IsuladShimUnitTest, test_file_exist) +{ + string exist_file = "/tmp/test_exist_exist"; + string non_file = "/tmp/test_exist_non"; + int fd_exist = -1; + + fd_exist = open_no_inherit(exist_file.c_str(), O_CREAT | O_WRONLY | O_APPEND | O_SYNC, 0640); + EXPECT_GT(fd_exist, 0); + EXPECT_TRUE(file_exists(exist_file.c_str())); + EXPECT_FALSE(file_exists(non_file.c_str())); + + close(fd_exist); + unlink(exist_file.c_str()); +} + + +TEST_F(IsuladShimUnitTest, test_combined_output) +{ + string exist_cmd = "ls"; + string non_cmd = "aaa"; + const char *params[MAX_RUNTIME_ARGS] = { NULL }; + char output[BUFSIZ] = { 0 }; + int output_len = BUFSIZ; + + params[0] = exist_cmd.c_str(); + EXPECT_EQ(cmd_combined_output(exist_cmd.c_str(), params, output, &output_len), 0); + + params[0] = non_cmd.c_str(); + EXPECT_EQ(cmd_combined_output(non_cmd.c_str(), params, output, &output_len), -1); +} -- GitLab