From c798e4f1abb27d950af6698903891bd2f4dbc63e Mon Sep 17 00:00:00 2001
From: songbuhuang <544824346@qq.com>
Date: Mon, 30 Jan 2023 15:34:34 +0800
Subject: [PATCH 03/19] add cpu-rt ut

Signed-off-by: songbuhuang <544824346@qq.com>
---
 test/CMakeLists.txt              |  1 +
 test/cgroup/CMakeLists.txt       |  3 ++
 test/cgroup/cpu/CMakeLists.txt   | 28 +++++++++++++++
 test/cgroup/cpu/cgroup_cpu_ut.cc | 61 ++++++++++++++++++++++++++++++++
 4 files changed, 93 insertions(+)
 create mode 100644 test/cgroup/CMakeLists.txt
 create mode 100644 test/cgroup/cpu/CMakeLists.txt
 create mode 100644 test/cgroup/cpu/cgroup_cpu_ut.cc

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 06adb602..ad348131 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -47,6 +47,7 @@ IF(ENABLE_UT)
     add_subdirectory(sha256)
     add_subdirectory(buffer)
     add_subdirectory(console)
+    add_subdirectory(cgroup)
 ENDIF(ENABLE_UT)
 
 IF(ENABLE_FUZZ)
diff --git a/test/cgroup/CMakeLists.txt b/test/cgroup/CMakeLists.txt
new file mode 100644
index 00000000..d904a6a0
--- /dev/null
+++ b/test/cgroup/CMakeLists.txt
@@ -0,0 +1,3 @@
+project(iSulad_UT)
+
+add_subdirectory(cpu)
diff --git a/test/cgroup/cpu/CMakeLists.txt b/test/cgroup/cpu/CMakeLists.txt
new file mode 100644
index 00000000..159b0d85
--- /dev/null
+++ b/test/cgroup/cpu/CMakeLists.txt
@@ -0,0 +1,28 @@
+project(iSulad_UT)
+
+SET(EXE cgroup_cpu_ut)
+
+add_executable(${EXE}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/path.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/err_msg.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/sysinfo.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/command_parser.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config/daemon_arguments.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config/isulad_config.c
+    cgroup_cpu_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+    ${GTEST_INCLUDE_DIR}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common
+    ${CMAKE_BINARY_DIR}/conf
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/config
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad
+    )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lgrpc++ -lprotobuf -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cgroup/cpu/cgroup_cpu_ut.cc b/test/cgroup/cpu/cgroup_cpu_ut.cc
new file mode 100644
index 00000000..1cfdf86f
--- /dev/null
+++ b/test/cgroup/cpu/cgroup_cpu_ut.cc
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * 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 v2 for more details.
+ * Description: specs unit test
+ * Author: huangsong
+ * Create: 2023-01-29
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "daemon_arguments.h"
+#include "isulad_config.h"
+#include "utils.h"
+
+struct service_arguments *new_args(int64_t cpu_rt_period, int64_t cpu_rt_runtime)
+{
+    struct service_arguments *args = (struct service_arguments *)util_common_calloc_s(sizeof(struct service_arguments));
+    if (args == nullptr) {
+        std::cerr << "Out of memory" << std::endl;
+        return nullptr;
+    }
+
+    args->json_confs = (isulad_daemon_configs *)util_common_calloc_s(sizeof(isulad_daemon_configs));
+    if (args->json_confs == nullptr) {
+        std::cerr << "Out of memory" << std::endl;
+        free(args);
+        return nullptr;
+    }
+
+    args->json_confs->cpu_rt_period = cpu_rt_period;
+    args->json_confs->cpu_rt_runtime = cpu_rt_runtime;
+
+    return args;
+}
+
+TEST(CgroupCpuUnitTest, test_conf_get_cgroup_cpu_rt)
+{
+    int64_t cpu_rt_period = 0;
+    int64_t cpu_rt_runtime = 0;
+
+    ASSERT_EQ(conf_get_cgroup_cpu_rt(nullptr, nullptr), -1);
+    ASSERT_EQ(conf_get_cgroup_cpu_rt(&cpu_rt_period, nullptr), -1);
+    ASSERT_EQ(conf_get_cgroup_cpu_rt(nullptr, &cpu_rt_runtime), -1);
+    
+    struct service_arguments *args = new_args(cpu_rt_period, cpu_rt_runtime);
+    ASSERT_EQ(save_args_to_conf(args), 0);
+    ASSERT_EQ(conf_get_cgroup_cpu_rt(&cpu_rt_period, &cpu_rt_runtime), 0);
+    ASSERT_EQ(cpu_rt_period, 0);
+    ASSERT_EQ(cpu_rt_runtime, 0);
+
+}
+
+
-- 
2.25.1

