From 98ae352fb43f3bf5aa1ac1ea2d3d6cee61cf856e Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Mon, 20 Jan 2025 22:44:56 +0800
Subject: [PATCH 182/198] fix error macro isolation/ memory leak and meanless
 reply file in sandbox ut

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 test/sandbox/CMakeLists.txt                   |  2 +-
 test/sandbox/controller/CMakeLists.txt        |  7 ++++--
 .../manager/controller_manager_ut.cc          | 17 +++++++-------
 .../sandboxer_controller_ut.cc                |  1 +
 .../controller/shim/shim_controller_ut.cc     |  3 +++
 test/sandbox/sandbox/CMakeLists.txt           | 22 ++++++++++++++-----
 test/sandbox/sandbox_manager/CMakeLists.txt   | 12 ++++++++--
 .../sandbox_manager/sandbox_manager_ut.cc     |  2 ++
 8 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/test/sandbox/CMakeLists.txt b/test/sandbox/CMakeLists.txt
index 38d7ccb9..1a9fedf6 100644
--- a/test/sandbox/CMakeLists.txt
+++ b/test/sandbox/CMakeLists.txt
@@ -1,6 +1,6 @@
 project(iSulad_UT)
 
-if (ENABLE_SANDBOXER)
+if (ENABLE_CRI_API_V1)
     add_subdirectory(controller)
     add_subdirectory(sandbox_manager)
     add_subdirectory(sandbox)
diff --git a/test/sandbox/controller/CMakeLists.txt b/test/sandbox/controller/CMakeLists.txt
index d201872b..4273c2bd 100644
--- a/test/sandbox/controller/CMakeLists.txt
+++ b/test/sandbox/controller/CMakeLists.txt
@@ -2,6 +2,9 @@ project(iSulad_UT)
 
 list(APPEND grpc_sandbox_type_srcs ${CMAKE_BINARY_DIR}/grpc/src/api/services/cri/v1/api_v1.pb.cc)
 list(APPEND grpc_sandbox_type_srcs ${CMAKE_BINARY_DIR}/grpc/src/api/services/cri/gogo.pb.cc)
-add_subdirectory(sandboxer)
-add_subdirectory(manager)
+
+if (ENABLE_SANDBOXER)
+    add_subdirectory(sandboxer)
+    add_subdirectory(manager)
+endif()
 add_subdirectory(shim)
diff --git a/test/sandbox/controller/manager/controller_manager_ut.cc b/test/sandbox/controller/manager/controller_manager_ut.cc
index 705baaca..c60f8818 100644
--- a/test/sandbox/controller/manager/controller_manager_ut.cc
+++ b/test/sandbox/controller/manager/controller_manager_ut.cc
@@ -16,12 +16,13 @@
 #include <memory>
 #include "gtest/gtest.h"
 #include "sandboxer_controller.h"
-#include "grpc_sandboxer_client_mock.h"
 #include "controller_manager.h"
 #include "controller_common.h"
 #include "utils.h"
 #include "isulad_config_mock.h"
 #include "shim_controller.h"
+#include "controller.h"
+#include "grpc_sandboxer_client_mock.h"
 
 class ControllerManagerWrapper : public sandbox::ControllerManager {
 public:
@@ -81,7 +82,7 @@ TEST_F(ControllerManagerTest, InitTestSucceed)
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(1).WillOnce(testing::Return(args));
     EXPECT_TRUE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_TRUE(err.Empty());
-    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController("vmm"), nullptr);
     FreeDummyServerconf(args);
 }
@@ -96,7 +97,7 @@ TEST_F(ControllerManagerTest, InitTestSucceedWithEmptyConfig)
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(1).WillOnce(testing::Return(args));
     EXPECT_TRUE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_TRUE(err.Empty());
-    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     EXPECT_EQ(ControllerManagerWrapper::GetInstance()->GetController("vmm"), nullptr);
     FreeDummyServerconf(args);
 }
@@ -111,7 +112,7 @@ TEST_F(ControllerManagerTest, InitTestFailedWithEmptySandboxerConfig)
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(1).WillOnce(testing::Return(args));
     EXPECT_FALSE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_THAT(err.GetCMessage(), testing::HasSubstr("Failed to load sandboxer controllers config"));
-    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     EXPECT_EQ(ControllerManagerWrapper::GetInstance()->GetController("vmm"), nullptr);
     FreeDummyServerconf(args);
 }
@@ -126,7 +127,7 @@ TEST_F(ControllerManagerTest, InitTestSucceedWithNullConfig)
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(1).WillOnce(testing::Return(args));
     EXPECT_TRUE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_TRUE(err.Empty());
-    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     EXPECT_EQ(ControllerManagerWrapper::GetInstance()->GetController("vmm"), nullptr);
     FreeDummyServerconf(args);
 }
@@ -142,7 +143,7 @@ TEST_F(ControllerManagerTest, InitTestFailedWithDupShimConfig)
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(1).WillOnce(testing::Return(args));
     EXPECT_FALSE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_THAT(err.GetCMessage(), testing::HasSubstr("Sandboxer controller already registered, sandboxer:"));
-    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     FreeDummyServerconf(args);
 }
 
@@ -191,7 +192,7 @@ TEST_F(ControllerManagerTest, InitTestFailedWithDupInit)
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(2).WillRepeatedly(testing::Return(args));
     EXPECT_TRUE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_TRUE(err.Empty());
-    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     EXPECT_NE(ControllerManagerWrapper::GetInstance()->GetController("vmm"), nullptr);
     EXPECT_FALSE(ControllerManagerWrapper::GetInstance()->Init(err));
     EXPECT_THAT(err.GetCMessage(), testing::HasSubstr("Sandboxer controller already registered,"));
@@ -211,6 +212,6 @@ TEST_F(ControllerManagerTest, InitTestFailedWithNullConf)
 TEST_F(ControllerManagerTest, FindControllerTestFailedBeforeInit)
 {
     Errors err;
-    EXPECT_EQ(ControllerManagerWrapper::GetInstance()->GetController(SHIM_CONTROLLER_NAME), nullptr);
+    EXPECT_EQ(ControllerManagerWrapper::GetInstance()->GetController(sandbox::SHIM_CONTROLLER_NAME), nullptr);
     EXPECT_EQ(ControllerManagerWrapper::GetInstance()->GetController("vmm"), nullptr);
 }
diff --git a/test/sandbox/controller/sandboxer/sandboxer_controller/sandboxer_controller_ut.cc b/test/sandbox/controller/sandboxer/sandboxer_controller/sandboxer_controller_ut.cc
index 726464e8..d31d5b95 100644
--- a/test/sandbox/controller/sandboxer/sandboxer_controller/sandboxer_controller_ut.cc
+++ b/test/sandbox/controller/sandboxer/sandboxer_controller/sandboxer_controller_ut.cc
@@ -32,6 +32,7 @@ protected:
     void TearDown() override
     {
         m_contoller.reset(nullptr);
+        MockSandboxerClient_SetMock(nullptr);
     }
 
     std::string m_sandboxer = "sandboxer";
diff --git a/test/sandbox/controller/shim/shim_controller_ut.cc b/test/sandbox/controller/shim/shim_controller_ut.cc
index e43cc645..75a51797 100644
--- a/test/sandbox/controller/shim/shim_controller_ut.cc
+++ b/test/sandbox/controller/shim/shim_controller_ut.cc
@@ -41,6 +41,9 @@ protected:
     void TearDown() override
     {
         m_contoller.reset(nullptr);
+        MockCallback_SetMock(nullptr);
+        MockServiceContainerApi_SetMock(nullptr);
+        MockImageApi_SetMock(nullptr);
     }
 
     std::string m_sandboxer = "shim";
diff --git a/test/sandbox/sandbox/CMakeLists.txt b/test/sandbox/sandbox/CMakeLists.txt
index 6dd6c3ee..d033c93f 100644
--- a/test/sandbox/sandbox/CMakeLists.txt
+++ b/test/sandbox/sandbox/CMakeLists.txt
@@ -4,8 +4,24 @@ SET(EXE sandbox_ut)
 
 aux_source_directory(${CMAKE_BINARY_DIR}/grpc/src/api/services/sandbox/sandbox/types sandbox_type_srcs)
 
+if(ENABLE_SANDBOXER)
+    set(sandboxer_srcs
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller/sandboxer_controller.cc
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/grpc_sandboxer_client_mock.cc
+        )
+    set(sandbox_dir
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller/client
+    )
+else()
+    set(sandboxer_srcs "")
+    set(sandbox_dir "")
+endif()
+
 add_executable(${EXE}
     ${sandbox_type_srcs}
+    ${sandboxer_srcs}
     ${CMAKE_BINARY_DIR}/grpc/src/api/services/cri/v1/api_v1.pb.cc
     ${CMAKE_BINARY_DIR}/grpc/src/api/services/cri/gogo.pb.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cpputils/errors.cc
@@ -15,12 +31,10 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandbox.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandbox_task.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/controller_manager.cc
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller/sandboxer_controller.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/id_name_manager.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config/isulad_config.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/sandbox/controller/controller_common.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config/daemon_arguments.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/grpc_sandboxer_client_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/shim_controller_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/mailbox_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/err_msg.c
@@ -28,6 +42,7 @@ add_executable(${EXE}
 
 target_include_directories(${EXE} PUBLIC
     ${GTEST_INCLUDE_DIR}
+    ${sandbox_dir}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config
@@ -37,9 +52,6 @@ target_include_directories(${EXE} PUBLIC
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/shim
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/shim/controller
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller/client
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cpputils
diff --git a/test/sandbox/sandbox_manager/CMakeLists.txt b/test/sandbox/sandbox_manager/CMakeLists.txt
index a7dd8c9d..45c55300 100644
--- a/test/sandbox/sandbox_manager/CMakeLists.txt
+++ b/test/sandbox/sandbox_manager/CMakeLists.txt
@@ -4,8 +4,18 @@ SET(EXE sandbox_manager_ut)
 
 aux_source_directory(${CMAKE_BINARY_DIR}/grpc/src/api/services/sandbox/sandbox/types sandbox_type_srcs)
 
+if(ENABLE_SANDBOXER)
+    set(sandboxer_srcs
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandboxer/controller/sandboxer_controller.cc
+        ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/grpc_sandboxer_client_mock.cc
+        )
+else()
+    set(sandboxer_srcs "")
+endif()
+
 add_executable(${EXE}
     ${sandbox_type_srcs}
+    ${sandboxer_srcs}
     ${CMAKE_BINARY_DIR}/grpc/src/api/services/cri/v1/api_v1.pb.cc
     ${CMAKE_BINARY_DIR}/grpc/src/api/services/cri/gogo.pb.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cpputils/errors.cc
@@ -19,8 +29,6 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/err_msg.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config/daemon_arguments.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/controller_manager.cc
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/grpc_sandboxer_client_mock.cc
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sandboxer_controller_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/shim_controller_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/sandbox_manager_ut.cc)
 
diff --git a/test/sandbox/sandbox_manager/sandbox_manager_ut.cc b/test/sandbox/sandbox_manager/sandbox_manager_ut.cc
index 9e762039..276b6f4c 100644
--- a/test/sandbox/sandbox_manager/sandbox_manager_ut.cc
+++ b/test/sandbox/sandbox_manager/sandbox_manager_ut.cc
@@ -154,7 +154,9 @@ TEST_F(SandboxManagerTest, TestCreateSandbox)
         "{\"cri-sandboxers\": {\"kuasar\": {\"name\": \"vmm\",\"address\": \"/run/vmm-sandboxer.sock\"}}}";
     struct service_arguments *args = CreateDummyServerConf(daemonConfig);
     ASSERT_NE(args, nullptr);
+#ifdef ENABLE_SANDBOXER
     EXPECT_CALL(*isuladConfMock, ConfGetServerConf()).Times(1).WillOnce(testing::Return(args));
+#endif
     EXPECT_TRUE(ControllerManager::GetInstance()->Init(error));
     EXPECT_TRUE(error.Empty());
 
-- 
2.34.1

