From e6263265c58f965439629890638058ec9935aa40 Mon Sep 17 00:00:00 2001 From: jingrui Date: Mon, 24 Feb 2020 10:33:05 +0800 Subject: [PATCH] isulad: add isula runtime ops llt Signed-off-by: jingrui --- src/runtime/isula/isula_rt_ops.c | 53 +++++++++- test/mocks/container_unix_mock.cc | 5 + test/mocks/isulad_config_mock.cc | 15 +++ test/runtime/CMakeLists.txt | 1 + test/runtime/isula/CMakeLists.txt | 67 +++++++++++++ test/runtime/isula/isula_rt_ops_llt.cc | 132 +++++++++++++++++++++++++ 6 files changed, 269 insertions(+), 4 deletions(-) create mode 100644 test/runtime/isula/CMakeLists.txt create mode 100644 test/runtime/isula/isula_rt_ops_llt.cc diff --git a/src/runtime/isula/isula_rt_ops.c b/src/runtime/isula/isula_rt_ops.c index 73f7a6d..ee52c39 100644 --- a/src/runtime/isula/isula_rt_ops.c +++ b/src/runtime/isula/isula_rt_ops.c @@ -247,6 +247,10 @@ static int get_runtime_args(const char *runtime, const char ***args) defs_map_string_object_runtimes *runtimes = NULL; size_t i = 0; + if (runtime == NULL) { + return 0; + } + if (isulad_server_conf_rdlock()) { ERROR("failed to lock server config"); goto out; @@ -677,14 +681,21 @@ out: int rt_isula_create(const char *id, const char *runtime, const rt_create_params_t *params) { - oci_runtime_spec *config = params->oci_config_data; + oci_runtime_spec *config = NULL; const char *cmd = NULL; const char **runtime_args = NULL; - size_t runtime_args_len = get_runtime_args(runtime, &runtime_args); + size_t runtime_args_len = 0; int ret = 0; char workdir[PATH_MAX] = {0}; shim_client_process_state p = {0}; + if (id == NULL || runtime == NULL || params == NULL) { + ERROR("nullptr arguments not allowed"); + return -1; + } + config = params->oci_config_data; + runtime_args_len = get_runtime_args(runtime, &runtime_args); + if (snprintf(workdir, sizeof(workdir), "%s/%s", params->state, id) < 0) { INFO("make full workdir failed"); ret = -1; @@ -727,6 +738,10 @@ int rt_isula_start(const char *id, const char *runtime, pid_t pid = 0; int ret = 0; + if (id == NULL || runtime == NULL || params == NULL || pid_info == NULL) { + ERROR("nullptr arguments not allowed"); + return -1; + } if (snprintf(workdir, sizeof(workdir), "%s/%s", params->state, id) < 0) { ERROR("%s: missing shim workdir", id); return -1; @@ -772,6 +787,16 @@ int rt_isula_clean_resource(const char *id, const char *runtime, { char workdir[PATH_MAX] = {0}; + if (id == NULL || runtime == NULL || params == NULL) { + ERROR("nullptr arguments not allowed"); + return -1; + } + + if (params->statepath == NULL) { + ERROR("missing state path"); + return -1; + } + if (snprintf(workdir, sizeof(workdir), "%s/%s", params->statepath, id) < 0) { ERROR("failed get shim workdir"); return -1; @@ -796,6 +821,14 @@ int rt_isula_rm(const char *id, const char *runtime, const rt_rm_params_t *param { char libdir[PATH_MAX] = {0}; + if (id == NULL || runtime == NULL || params == NULL) { + ERROR("nullptr arguments not allowed"); + return -1; + } + if (params->rootpath == NULL) { + ERROR("missing root path"); + return -1; + } if (snprintf(libdir, sizeof(libdir), "%s/%s", params->rootpath, id) < 0) { ERROR("failed get shim workdir"); return -1; @@ -845,9 +878,9 @@ int rt_isula_exec(const char *id, const char *runtime, const rt_exec_params_t *params, int *exit_code) { char *exec_id = NULL; - defs_process *process = params->spec; + defs_process *process = NULL; const char **runtime_args = NULL; - size_t runtime_args_len = get_runtime_args(runtime, &runtime_args); + size_t runtime_args_len = 0; char workdir[PATH_MAX] = {0}; const char *cmd = NULL; int ret = 0; @@ -855,6 +888,13 @@ int rt_isula_exec(const char *id, const char *runtime, int pid = 0; shim_client_process_state p = {0}; + if (id == NULL || runtime == NULL || params == NULL || exit_code == NULL) { + ERROR("nullptr arguments not allowed"); + return -1; + } + process = params->spec; + runtime_args_len = get_runtime_args(runtime, &runtime_args); + ret = snprintf(bundle, sizeof(bundle), "%s/%s", params->rootpath, id); if (ret < 0) { ERROR("failed join bundle path for exec"); @@ -926,6 +966,11 @@ int rt_isula_status(const char *id, const char *runtime, char workdir[PATH_MAX] = {0}; int ret = 0; + if (id == NULL || runtime == NULL || params == NULL || status == NULL) { + ERROR("nullptr arguments not allowed"); + return -1; + } + ret = snprintf(workdir, sizeof(workdir), "%s/%s", params->state, id); if (ret < 0) { ERROR("failed join full workdir %s/%s", params->rootpath, id); diff --git a/test/mocks/container_unix_mock.cc b/test/mocks/container_unix_mock.cc index 51e6fbc..bfabfdc 100644 --- a/test/mocks/container_unix_mock.cc +++ b/test/mocks/container_unix_mock.cc @@ -41,3 +41,8 @@ bool has_mount_for(container_t *cont, const char *mpath) } return false; } + +int container_read_proc(uint32_t pid, container_pid_t *pid_info) +{ + return 0; +} diff --git a/test/mocks/isulad_config_mock.cc b/test/mocks/isulad_config_mock.cc index a42577a..087dae3 100644 --- a/test/mocks/isulad_config_mock.cc +++ b/test/mocks/isulad_config_mock.cc @@ -97,3 +97,18 @@ char *conf_get_isulad_storage_driver() } return nullptr; } + +int isulad_server_conf_rdlock() +{ + return 0; +} + +int isulad_server_conf_unlock() +{ + return 0; +} + +struct service_arguments *conf_get_server_conf() +{ + return NULL; +} diff --git a/test/runtime/CMakeLists.txt b/test/runtime/CMakeLists.txt index 5b955bd..3d6b5df 100644 --- a/test/runtime/CMakeLists.txt +++ b/test/runtime/CMakeLists.txt @@ -1,3 +1,4 @@ project(iSulad_LLT) add_subdirectory(lcr) +add_subdirectory(isula) diff --git a/test/runtime/isula/CMakeLists.txt b/test/runtime/isula/CMakeLists.txt new file mode 100644 index 0000000..b9e44e7 --- /dev/null +++ b/test/runtime/isula/CMakeLists.txt @@ -0,0 +1,67 @@ +project(iSulad_LLT) + +SET(EXE isula_rt_ops_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_regex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/path.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/libisulad.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/image/oci/oci_llt_common.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/containers_store_mock.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/namespace_mock.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/container_unix_mock.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/engine_mock.cc + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/isulad_config_mock.cc + ${CMAKE_BINARY_DIR}/json/imagetool_image.c + ${CMAKE_BINARY_DIR}/json/oci_image_spec.c + ${CMAKE_BINARY_DIR}/json/shim_client_process_state.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_state.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/runtime/isula/isula_rt_ops.c + isula_rt_ops_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${GTEST_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/map + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/manager + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/events + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/runtime + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/runtime/isula + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/engines + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks + ) + +#set_target_properties(${EXE} PROPERTIES LINK_FLAGS) +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lz) diff --git a/test/runtime/isula/isula_rt_ops_llt.cc b/test/runtime/isula/isula_rt_ops_llt.cc new file mode 100644 index 0000000..728ea2b --- /dev/null +++ b/test/runtime/isula/isula_rt_ops_llt.cc @@ -0,0 +1,132 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v1 for more details. + * Description: isula runtime ops llt + * Author: jingrui + * Create: 2020-02-15 + */ + +#include +#include +#include +#include "mock.h" +#include "isula_rt_ops.h" +#include +#include +#include "engine_mock.h" +#include "isulad_config_mock.h" +#include "utils.h" + +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 IsulaRtOpsUnitTest : public testing::Test { +public: + void SetUp() override + { + MockEngine_SetMock(&m_engine); + ::testing::Mock::AllowLeak(&m_engine); + + MockIsuladConf_SetMock(&m_isulad_conf); + ::testing::Mock::AllowLeak(&m_isulad_conf); + } + void TearDown() override + { + MockEngine_SetMock(nullptr); + MockIsuladConf_SetMock(nullptr); + } + + NiceMock m_engine; + NiceMock m_isulad_conf; +}; + +TEST(isula_rt_ops_llt, test_rt_isula_detect) +{ + // All parameter NULL + ASSERT_FALSE(rt_isula_detect(NULL)); + + ASSERT_TRUE(rt_isula_detect("kata-runtime")); + + ASSERT_TRUE(rt_isula_detect("kata-me")); + ASSERT_TRUE(rt_isula_detect("runc")); + + ASSERT_FALSE(rt_isula_detect("lcr")); +} + + +TEST_F(IsulaRtOpsUnitTest, test_rt_isula_create) +{ + ASSERT_EQ(rt_isula_create(nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_create("123", nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_create("123", "kata-runtime", nullptr), -1); +} + +TEST_F(IsulaRtOpsUnitTest, test_rt_isula_start) +{ + rt_start_params_t params = {}; + ASSERT_EQ(rt_isula_start(nullptr, nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_start("123", nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_start("123", "kata-runtime", nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_start("123", "kata-runtime", ¶ms, nullptr), -1); +} + +TEST_F(IsulaRtOpsUnitTest, test_rt_isula_clean_resource) +{ + rt_clean_params_t params = {}; + + ASSERT_EQ(rt_isula_clean_resource(nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_clean_resource("123", nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_clean_resource("123", "kata-runtime", nullptr), -1); + ASSERT_EQ(rt_isula_clean_resource("123", "kata-runtime", ¶ms), -1); + params.statepath = "/var/run/isulad/kata-runtime/123"; + ASSERT_EQ(rt_isula_clean_resource("123", "kata-runtime", ¶ms), 0); +} + +TEST_F(IsulaRtOpsUnitTest, test_rt_isula_rm) +{ + rt_rm_params_t params = {}; + ASSERT_EQ(rt_isula_rm(nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_rm("123", nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_rm("123", "kata-runtime", nullptr), -1); + ASSERT_EQ(rt_isula_rm("123", "kata-runtime", ¶ms), -1); + params.rootpath = "/var/lib/isulad/kata-runtime/123"; + ASSERT_EQ(rt_isula_rm("123", "kata-runtime", ¶ms), 0); +} + +TEST_F(IsulaRtOpsUnitTest, test_rt_isula_exec) +{ + rt_exec_params_t params = {}; + ASSERT_EQ(rt_isula_exec(nullptr, nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_exec("123", nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_exec("123", "kata-runtime", nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_exec("123", "kata-runtime", ¶ms, nullptr), -1); +} + +TEST_F(IsulaRtOpsUnitTest, test_rt_isula_status) +{ + rt_status_params_t params = {}; + struct engine_container_status_info status = {}; + ASSERT_EQ(rt_isula_status(nullptr, nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_status("123", nullptr, nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_status("123", "kata-runtime", nullptr, nullptr), -1); + ASSERT_EQ(rt_isula_status("123", "kata-runtime", ¶ms, nullptr), -1); + params.state="/var/run/isulad/kata-runtime"; + ASSERT_EQ(rt_isula_status("123", "kata-runtime", ¶ms, &status), -1); +} -- GitLab