提交 c108e9f6 编写于 作者: L leizhongkai 提交者: lifeng68

iSulad: add testcase for isulad-shim

Signed-off-by: Nleizhongkai <leizhongkai@huawei.com>
上级 3d017330
......@@ -18,6 +18,10 @@
#include <stdbool.h>
#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
......@@ -21,6 +21,10 @@
#include <stdbool.h>
#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
project(iSulad_LLT)
add_subdirectory(isula)
add_subdirectory(isulad-shim)
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)
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
* Description: isulad-shim llt
* Author: leizhongkai
* Create: 2020-02-25
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册