提交 a61ad1c0 编写于 作者: L LiFeng

iSulad: add testcases for specs

Signed-off-by: NLiFeng <lifeng68@huawei.com>
上级 eb7a020b
......@@ -1053,7 +1053,7 @@ static int do_update_resources(const container_update_request *request, containe
}
backup_oci_spec = load_oci_config(cont->root_path, id);
if (oci_spec == NULL) {
if (backup_oci_spec == NULL) {
ERROR("Failed to load oci config");
ret = -1;
goto restore_hostspec;
......
......@@ -1221,6 +1221,11 @@ int merge_conf_cgroup(oci_runtime_spec *oci_spec, const host_config *host_spec)
{
int ret = 0;
if (oci_spec == NULL || host_spec == NULL) {
ret = -1;
goto out;
}
ret = merge_conf_cgroup_cpu(oci_spec, host_spec);
if (ret != 0) {
goto out;
......@@ -1764,14 +1769,24 @@ out:
return ret;
}
int merge_oci_cgroups_path(const char *id, const oci_runtime_spec *oci_spec,
const host_config *host_spec)
int merge_oci_cgroups_path(const char *id, oci_runtime_spec *oci_spec, const host_config *host_spec)
{
int ret = 0;
char cleaned[PATH_MAX] = { 0 };
char *default_cgroup_parent = NULL;
char *path = NULL;
if (id == NULL || oci_spec == NULL || host_spec == NULL) {
ERROR("Invalid arguments");
ret = -1;
goto out;
}
if (make_sure_oci_spec_linux(oci_spec) != 0) {
ERROR("Failed to make oci spec linux");
ret = -1;
goto out;
}
default_cgroup_parent = conf_get_isulad_cgroup_parent();
path = default_cgroup_parent;
if (host_spec->cgroup_parent != NULL) {
......@@ -1779,16 +1794,13 @@ int merge_oci_cgroups_path(const char *id, const oci_runtime_spec *oci_spec,
}
if (path == NULL) {
oci_spec->linux->cgroups_path = util_add_path("/isulad", id);
free(oci_spec->linux->cgroups_path);
oci_spec->linux->cgroups_path = util_path_join("/isulad", id);
return 0;
}
if (cleanpath(path, cleaned, sizeof(cleaned)) == NULL) {
ERROR("Failed to clean path: %s", path);
ret = -1;
goto out;
}
oci_spec->linux->cgroups_path = util_add_path(cleaned, id);
free(oci_spec->linux->cgroups_path);
oci_spec->linux->cgroups_path = util_path_join(path, id);
out:
UTIL_FREE_AND_SET_NULL(default_cgroup_parent);
......
......@@ -22,11 +22,14 @@
#include "oci_runtime_hooks.h"
#include "oci_runtime_spec.h"
#ifdef __cplusplus
extern "C" {
#endif
int merge_all_specs(host_config *host_spec,
const char *real_rootfs,
container_config_v2_common_config *v2_spec, oci_runtime_spec *oci_spec);
int merge_oci_cgroups_path(const char *id, const oci_runtime_spec *oci_spec,
int merge_oci_cgroups_path(const char *id, oci_runtime_spec *oci_spec,
const host_config *host_spec);
int merge_global_config(oci_runtime_spec *oci_spec);
oci_runtime_spec *load_oci_config(const char *rootpath, const char *name);
......@@ -34,5 +37,9 @@ oci_runtime_spec *default_spec(bool system_container);
int merge_conf_cgroup(oci_runtime_spec *oci_spec, const host_config *host_spec);
int save_oci_config(const char *id, const char *rootpath, const oci_runtime_spec *oci_spec);
#ifdef __cplusplus
}
#endif
#endif
......@@ -42,3 +42,4 @@ add_subdirectory(image)
add_subdirectory(path)
add_subdirectory(cmd)
add_subdirectory(runtime)
add_subdirectory(specs)
......@@ -38,4 +38,46 @@ int parse_log_opts(struct service_arguments *args, const char *key, const char *
return g_isulad_conf_mock->ParseLogopts(args, key, value);
}
return -1;
}
int conf_get_isulad_default_ulimit(host_config_ulimits_element ***ulimit)
{
if (g_isulad_conf_mock != nullptr) {
return g_isulad_conf_mock->GetUlimit(ulimit);
}
return -1;
}
int conf_get_isulad_hooks(oci_runtime_spec_hooks **phooks)
{
if (g_isulad_conf_mock != nullptr) {
return g_isulad_conf_mock->GetHooks(phooks);
}
return -1;
}
/* conf get isulad mount rootfs */
char *conf_get_isulad_mount_rootfs()
{
if (g_isulad_conf_mock != nullptr) {
return g_isulad_conf_mock->GetMountrootfs();
}
return nullptr;
}
/* conf get isulad cgroup parent for containers */
char *conf_get_isulad_cgroup_parent()
{
if (g_isulad_conf_mock != nullptr) {
return g_isulad_conf_mock->GetCgroupParent();
}
return nullptr;
}
char *conf_get_isulad_native_umask()
{
if (g_isulad_conf_mock != nullptr) {
return g_isulad_conf_mock->GetUmask();
}
return nullptr;
}
\ No newline at end of file
......@@ -24,6 +24,11 @@ public:
virtual ~MockIsuladConf() = default;
MOCK_METHOD1(GetRuntimeDir, char *(const char *name));
MOCK_METHOD3(ParseLogopts, int(struct service_arguments *args, const char *key, const char *value));
MOCK_METHOD0(GetMountrootfs, char *(void));
MOCK_METHOD1(GetHooks, int(oci_runtime_spec_hooks **phooks));
MOCK_METHOD1(GetUlimit, int(host_config_ulimits_element ***ulimit));
MOCK_METHOD0(GetCgroupParent, char *(void));
MOCK_METHOD0(GetUmask, char *(void));
};
void MockIsuladConf_SetMock(MockIsuladConf* mock);
......
project(iSulad_LLT)
add_subdirectory(specs)
project(iSulad_LLT)
SET(EXE specs_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_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec/specs.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec/specs_mount.c
${CMAKE_BINARY_DIR}/json/host_config.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec/specs_extend.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec/specs_security.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/libisulad.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json/oci_runtime_hooks.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json/parse_common.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/docker_seccomp.c
specs_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/image/oci
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/image
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils
${CMAKE_CURRENT_SOURCE_DIR}/../../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/json
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/map
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/spec
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/manager
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/events
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/services/execution/execute
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/tar
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/plugin
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/http
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/engines
${ENGINES_INCS}
#${EXECUTION_INCS}
${RUNTIME_INCS}
${IMAGE_INCS}
${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/image/oci
${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)
{
"ShmSize": 67108864,
"RestartPolicy": {
"Name": "no"
}
}
此差异已折叠。
/*
* 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: specs llt
* Author: lifeng
* Create: 2020-02-18
*/
#include <stdlib.h>
#include <stdio.h>
#include <gtest/gtest.h>
#include "mock.h"
#include "oci_runtime_spec.h"
#include "specs.h"
#include "host_config.h"
#include "container_config.h"
#include "oci_llt_common.h"
#include <gtest/gtest.h>
#include <gmock/gmock.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 SpecsUnitTest : public testing::Test {
public:
void SetUp() override
{
MockIsuladConf_SetMock(&m_isulad_conf);
::testing::Mock::AllowLeak(&m_isulad_conf);
}
void TearDown() override
{
MockIsuladConf_SetMock(nullptr);
}
NiceMock<MockIsuladConf> m_isulad_conf;
};
#define HOST_CONFIG_FILE "specs/specs/hostconfig.json"
#define OCI_RUNTIME_SPEC_FILE "specs/specs/oci_runtime_spec.json"
TEST(merge_conf_cgroup_llt, test_merge_conf_cgroup_1)
{
// All parameter NULL
ASSERT_NE(merge_conf_cgroup(NULL, NULL), 0);
}
TEST(merge_conf_cgroup_llt, test_merge_conf_cgroup_2)
{
oci_runtime_spec *oci_spec = NULL;
// Parameter host_spec is NULL
oci_spec = (oci_runtime_spec *) util_common_calloc_s(sizeof(oci_runtime_spec));
ASSERT_TRUE(oci_spec != NULL);
ASSERT_NE(merge_conf_cgroup(oci_spec, NULL), 0);
free_oci_runtime_spec(oci_spec);
oci_spec = NULL;
}
TEST(merge_conf_cgroup_llt, test_merge_conf_cgroup_3)
{
char *host_config_file = NULL;
host_config *host_spec = NULL;
char *err = NULL;
// Parameter oci_spec is NULL
host_config_file = json_path(HOST_CONFIG_FILE);
ASSERT_TRUE(host_config_file != NULL);
host_spec = host_config_parse_file(host_config_file, NULL, &err);
ASSERT_TRUE(host_spec != NULL);
free(err);
err = NULL;
free(host_config_file);
host_config_file = NULL;
ASSERT_NE(merge_conf_cgroup(NULL, host_spec), 0);
free_host_config(host_spec);
host_spec = NULL;
}
TEST(merge_conf_cgroup_llt, test_merge_conf_cgroup)
{
char *host_config_file = NULL;
host_config *host_spec = NULL;
oci_runtime_spec *oci_spec = NULL;
char *err = NULL;
// All parameter correct
host_config_file = json_path(HOST_CONFIG_FILE);
ASSERT_TRUE(host_config_file != NULL);
host_spec = host_config_parse_file(host_config_file, NULL, &err);
ASSERT_TRUE(host_spec != NULL);
free(err);
err = NULL;
free(host_config_file);
host_config_file = NULL;
oci_spec = (oci_runtime_spec *) util_common_calloc_s(sizeof(oci_runtime_spec));
ASSERT_TRUE(oci_spec != NULL);
ASSERT_EQ(merge_conf_cgroup(oci_spec, host_spec), 0);
free_host_config(host_spec);
host_spec = NULL;
free_oci_runtime_spec(oci_spec);
oci_spec = NULL;
}
TEST(merge_conf_cgroup_llt, test_merge_conf_cgroup_cpu)
{
char *host_config_file = NULL;
host_config *host_spec = NULL;
char *oci_config_file = NULL;
oci_runtime_spec *oci_spec = NULL;
char *err = NULL;
// cpu
host_config_file = json_path(HOST_CONFIG_FILE);
ASSERT_TRUE(host_config_file != NULL);
host_spec = host_config_parse_file(host_config_file, NULL, &err);
ASSERT_TRUE(host_spec != NULL);
free(err);
err = NULL;
free(host_config_file);
host_config_file = NULL;
oci_config_file = json_path(OCI_RUNTIME_SPEC_FILE);
ASSERT_TRUE(oci_config_file != NULL);
oci_spec = oci_runtime_spec_parse_file(oci_config_file, NULL, &err);
ASSERT_TRUE(oci_spec != NULL);
free(err);
err = NULL;
free(oci_config_file);
oci_config_file = NULL;
host_spec->cpu_period = 123;
host_spec->cpu_quota = 234;
host_spec->cpu_realtime_period = 456;
host_spec->cpu_realtime_runtime = 789;
host_spec->cpu_shares = 321;
free(host_spec->cpuset_cpus);
host_spec->cpuset_cpus = util_strdup_s("0-3");
free(host_spec->cpuset_mems);
host_spec->cpuset_mems = util_strdup_s("0");
ASSERT_EQ(merge_conf_cgroup(oci_spec, host_spec), 0);
ASSERT_EQ(oci_spec->linux->resources->cpu->period, 123);
ASSERT_EQ(oci_spec->linux->resources->cpu->quota, 234);
ASSERT_EQ(oci_spec->linux->resources->cpu->realtime_period, 456);
ASSERT_EQ(oci_spec->linux->resources->cpu->realtime_runtime, 789);
ASSERT_EQ(oci_spec->linux->resources->cpu->shares, 321);
ASSERT_STREQ(oci_spec->linux->resources->cpu->cpus, "0-3");
ASSERT_STREQ(oci_spec->linux->resources->cpu->mems, "0");
free_host_config(host_spec);
host_spec = NULL;
free_oci_runtime_spec(oci_spec);
oci_spec = NULL;
}
TEST(merge_conf_cgroup_llt, test_merge_conf_cgroup_mem)
{
char *host_config_file = NULL;
host_config *host_spec = NULL;
char *oci_config_file = NULL;
oci_runtime_spec *oci_spec = NULL;
char *err = NULL;
// mem
// int64_t kernel_memory;
// int64_t memory_reservation;
// int64_t memory_swap;
host_config_file = json_path(HOST_CONFIG_FILE);
ASSERT_TRUE(host_config_file != NULL);
host_spec = host_config_parse_file(host_config_file, NULL, &err);
ASSERT_TRUE(host_spec != NULL);
free(err);
err = NULL;
free(host_config_file);
host_config_file = NULL;
oci_config_file = json_path(OCI_RUNTIME_SPEC_FILE);
ASSERT_TRUE(oci_config_file != NULL);
oci_spec = oci_runtime_spec_parse_file(oci_config_file, NULL, &err);
ASSERT_TRUE(oci_spec != NULL);
free(err);
err = NULL;
free(oci_config_file);
oci_config_file = NULL;
host_spec->kernel_memory = 123;
host_spec->memory_reservation = 234;
host_spec->memory_swap = 456;
ASSERT_EQ(merge_conf_cgroup(oci_spec, host_spec), 0);
ASSERT_EQ(oci_spec->linux->resources->memory->kernel, 123);
ASSERT_EQ(oci_spec->linux->resources->memory->reservation, 234);
ASSERT_EQ(oci_spec->linux->resources->memory->swap, 456);
free_host_config(host_spec);
host_spec = NULL;
free_oci_runtime_spec(oci_spec);
oci_spec = NULL;
}
/* conf get routine rootdir */
char *invoke_conf_get_isulad_cgroup_parent_null()
{
return nullptr;
}
/* conf get routine rootdir */
char *invoke_conf_get_isulad_cgroup_parent()
{
return util_strdup_s("/var/lib/isulad/engines/lcr");
}
TEST_F(SpecsUnitTest, test_merge_oci_cgroups_path_1)
{
ASSERT_EQ(merge_oci_cgroups_path(nullptr, nullptr, nullptr), -1);
}
TEST_F(SpecsUnitTest, test_merge_oci_cgroups_path_2)
{
oci_runtime_spec *oci_spec = nullptr;
host_config *host_spec = nullptr;
oci_spec = (oci_runtime_spec *) util_common_calloc_s(sizeof(oci_runtime_spec));
ASSERT_TRUE(oci_spec != NULL);
host_spec = (host_config *) util_common_calloc_s(sizeof(host_config));
ASSERT_TRUE(host_spec != NULL);
EXPECT_CALL(m_isulad_conf, GetCgroupParent()).WillRepeatedly(Invoke(invoke_conf_get_isulad_cgroup_parent_null));
ASSERT_EQ(merge_oci_cgroups_path("123", oci_spec, host_spec), 0);
ASSERT_STREQ(oci_spec->linux->cgroups_path, "/isulad/123");
free_oci_runtime_spec(oci_spec);
free_host_config(host_spec);
testing::Mock::VerifyAndClearExpectations(&m_isulad_conf);
}
TEST_F(SpecsUnitTest, test_merge_oci_cgroups_path_3)
{
oci_runtime_spec *oci_spec = nullptr;
host_config *host_spec = nullptr;
oci_spec = (oci_runtime_spec *) util_common_calloc_s(sizeof(oci_runtime_spec));
ASSERT_TRUE(oci_spec != NULL);
host_spec = (host_config *) util_common_calloc_s(sizeof(host_config));
ASSERT_TRUE(host_spec != NULL);
host_spec->cgroup_parent = util_strdup_s("/test");
EXPECT_CALL(m_isulad_conf, GetCgroupParent()).WillRepeatedly(Invoke(invoke_conf_get_isulad_cgroup_parent_null));
ASSERT_EQ(merge_oci_cgroups_path("123", oci_spec, host_spec), 0);
ASSERT_STREQ(oci_spec->linux->cgroups_path, "/test/123");
free_oci_runtime_spec(oci_spec);
free_host_config(host_spec);
testing::Mock::VerifyAndClearExpectations(&m_isulad_conf);
}
TEST_F(SpecsUnitTest, test_merge_oci_cgroups_path_4)
{
oci_runtime_spec *oci_spec = nullptr;
host_config *host_spec = nullptr;
oci_spec = (oci_runtime_spec *) util_common_calloc_s(sizeof(oci_runtime_spec));
ASSERT_TRUE(oci_spec != NULL);
host_spec = (host_config *) util_common_calloc_s(sizeof(host_config));
ASSERT_TRUE(host_spec != NULL);
EXPECT_CALL(m_isulad_conf, GetCgroupParent()).WillRepeatedly(Invoke(invoke_conf_get_isulad_cgroup_parent));
ASSERT_EQ(merge_oci_cgroups_path("123", oci_spec, host_spec), 0);
ASSERT_STREQ(oci_spec->linux->cgroups_path, "/var/lib/isulad/engines/lcr/123");
free_oci_runtime_spec(oci_spec);
free_host_config(host_spec);
testing::Mock::VerifyAndClearExpectations(&m_isulad_conf);
}
TEST_F(SpecsUnitTest, test_merge_oci_cgroups_path_5)
{
oci_runtime_spec *oci_spec = nullptr;
host_config *host_spec = nullptr;
oci_spec = (oci_runtime_spec *) util_common_calloc_s(sizeof(oci_runtime_spec));
ASSERT_TRUE(oci_spec != NULL);
host_spec = (host_config *) util_common_calloc_s(sizeof(host_config));
ASSERT_TRUE(host_spec != NULL);
host_spec->cgroup_parent = util_strdup_s("/test");
EXPECT_CALL(m_isulad_conf, GetCgroupParent()).WillRepeatedly(Invoke(invoke_conf_get_isulad_cgroup_parent));
ASSERT_EQ(merge_oci_cgroups_path("123", oci_spec, host_spec), 0);
ASSERT_STREQ(oci_spec->linux->cgroups_path, "/test/123");
free_oci_runtime_spec(oci_spec);
free_host_config(host_spec);
testing::Mock::VerifyAndClearExpectations(&m_isulad_conf);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册