From 1e9031cc064f6980250287641e6b3311af755485 Mon Sep 17 00:00:00 2001
From: liuxu <liuxu156@huawei.com>
Date: Sat, 30 Nov 2024 09:50:33 +0800
Subject: [PATCH 02/11] UT: del shim_sandbox and change sandbox ops

Signed-off-by: liuxu <liuxu156@huawei.com>
---
 test/mocks/sandbox_mock.cc                  | 26 ++++++++
 test/mocks/sandbox_mock.h                   |  9 +++
 test/mocks/sandboxer_sandbox_mock.cc        | 35 +++++-----
 test/mocks/sandboxer_sandbox_mock.h         | 13 ++--
 test/mocks/shim_sandbox_mock.cc             | 72 ---------------------
 test/mocks/shim_sandbox_mock.h              | 43 ------------
 test/sandbox/sandbox/CMakeLists.txt         |  1 -
 test/sandbox/sandbox/sandbox_ut.cc          |  5 +-
 test/sandbox/sandbox_manager/CMakeLists.txt |  1 -
 9 files changed, 61 insertions(+), 144 deletions(-)
 delete mode 100644 test/mocks/shim_sandbox_mock.cc
 delete mode 100644 test/mocks/shim_sandbox_mock.h

diff --git a/test/mocks/sandbox_mock.cc b/test/mocks/sandbox_mock.cc
index 9db57a93..cce5a1b6 100644
--- a/test/mocks/sandbox_mock.cc
+++ b/test/mocks/sandbox_mock.cc
@@ -221,4 +221,30 @@ bool Sandbox::Remove(Errors &error)
     }
     return true;
 }
+
+void Sandbox::LoadSandboxTasks() {}
+
+auto Sandbox::PrepareContainer(const char *containerId, const char *baseFs,
+                               const oci_runtime_spec *ociSpec,
+                               const char *consoleFifos[]) -> int
+{
+    return 0;
+}   
+
+auto Sandbox::PrepareExec(const char *containerId, const char *execId,
+                          defs_process *processSpec, const char *consoleFifos[]) -> int
+{
+    return 0;
+}   
+
+auto Sandbox::PurgeContainer(const char *containerId) -> int
+{
+    return 0;
+}
+
+auto Sandbox::PurgeExec(const char *containerId, const char *execId) -> int
+{
+    return 0;
+}
+
 }
\ No newline at end of file
diff --git a/test/mocks/sandbox_mock.h b/test/mocks/sandbox_mock.h
index 98f40ad2..4908bcd9 100644
--- a/test/mocks/sandbox_mock.h
+++ b/test/mocks/sandbox_mock.h
@@ -58,6 +58,15 @@ public:
     MOCK_METHOD2(Stop, bool(uint32_t timeoutSecs, Errors &error));
     MOCK_METHOD1(Remove, bool(Errors &error));
     MOCK_METHOD1(Status, void(runtime::v1::PodSandboxStatus &status));
+
+    MOCK_METHOD0(LoadSandboxTasks, void());
+    MOCK_METHOD4(PrepareContainer, int(const char *containerId, const char *baseFs,
+                                       const oci_runtime_spec *ociSpec,
+                                       const char *consoleFifos[]));
+    MOCK_METHOD4(PrepareExec, int(const char *containerId, const char *execId,
+                                  defs_process *processSpec, const char *consoleFifos[]));
+    MOCK_METHOD1(PurgeContainer, int(const char *containerId));
+    MOCK_METHOD2(PurgeExec, int(const char *containerId, const char *execId));
 };
 
 void MockSandbox_SetMock(MockSandbox *mock);
diff --git a/test/mocks/sandboxer_sandbox_mock.cc b/test/mocks/sandboxer_sandbox_mock.cc
index cce58842..6ebe2820 100644
--- a/test/mocks/sandboxer_sandbox_mock.cc
+++ b/test/mocks/sandboxer_sandbox_mock.cc
@@ -33,40 +33,39 @@ void MockSandboxerSandbox_SetMock(MockSandboxerSandbox *mock)
 
 void SandboxerSandbox::LoadSandboxTasks() {}
 
-auto SandboxerSandbox::SaveSandboxTasks() -> bool
+auto SandboxerSandbox::PrepareContainer(const char *containerId, const char *baseFs,
+                               const oci_runtime_spec *ociSpec,
+                               const char *consoleFifos[]) -> int
 {
     if (g_sandboxer_sandbox_mock != nullptr) {
-        return g_sandboxer_sandbox_mock->SaveSandboxTasks();
+        return g_sandboxer_sandbox_mock->PrepareContainer(containerId, baseFs, ociSpec, consoleFifos);
     }
-    return true;
-}
+    return 0;
+}   
 
-auto SandboxerSandbox::AddSandboxTasks(sandbox_task *task) -> bool
+auto SandboxerSandbox::PrepareExec(const char *containerId, const char *execId,
+                          defs_process *processSpec, const char *consoleFifos[]) -> int
 {
     if (g_sandboxer_sandbox_mock != nullptr) {
-        return g_sandboxer_sandbox_mock->AddSandboxTasks(task);
+        return g_sandboxer_sandbox_mock->PrepareExec(containerId, execId, processSpec, consoleFifos);
     }
-    return true;
-}
+    return 0;
+}   
 
-auto SandboxerSandbox::GetAnySandboxTasks() -> std::string
+auto SandboxerSandbox::PurgeContainer(const char *containerId) -> int
 {
     if (g_sandboxer_sandbox_mock != nullptr) {
-        return g_sandboxer_sandbox_mock->GetAnySandboxTasks();
+        return g_sandboxer_sandbox_mock->PurgeContainer(containerId);
     }
-    return std::string("Nothing for sandboxer.");
+    return 0;
 }
 
-void SandboxerSandbox::DeleteSandboxTasks(const char *containerId) {}
-
-auto SandboxerSandbox::AddSandboxTasksProcess(const char *containerId, sandbox_process *processes) -> bool
+auto SandboxerSandbox::PurgeExec(const char *containerId, const char *execId) -> int
 {
     if (g_sandboxer_sandbox_mock != nullptr) {
-        return g_sandboxer_sandbox_mock->AddSandboxTasksProcess(containerId, processes);
+        return g_sandboxer_sandbox_mock->PurgeExec(containerId, execId);
     }
-   return true;
+    return 0;
 }
 
-void SandboxerSandbox::DeleteSandboxTasksProcess(const char *containerId, const char *execId) {}
-
 }
\ No newline at end of file
diff --git a/test/mocks/sandboxer_sandbox_mock.h b/test/mocks/sandboxer_sandbox_mock.h
index 4f76e5fc..020fe4d6 100644
--- a/test/mocks/sandboxer_sandbox_mock.h
+++ b/test/mocks/sandboxer_sandbox_mock.h
@@ -28,12 +28,13 @@ public:
     virtual ~MockSandboxerSandbox() = default;
 
     MOCK_METHOD0(LoadSandboxTasks, void());
-    MOCK_METHOD0(SaveSandboxTasks, bool());
-    MOCK_METHOD1(AddSandboxTasks, bool(sandbox_task *task));
-    MOCK_METHOD0(GetAnySandboxTasks, std::string());
-    MOCK_METHOD1(DeleteSandboxTasks, void(const char *containerId));
-    MOCK_METHOD2(AddSandboxTasksProcess, bool(const char *containerId, sandbox_process *processes));
-    MOCK_METHOD2(DeleteSandboxTasksProcess, void(const char *containerId, const char *execId));
+    MOCK_METHOD4(PrepareContainer, int(const char *containerId, const char *baseFs,
+                                       const oci_runtime_spec *ociSpec,
+                                       const char *consoleFifos[]));
+    MOCK_METHOD4(PrepareExec, int(const char *containerId, const char *execId,
+                                  defs_process *processSpec, const char *consoleFifos[]));
+    MOCK_METHOD1(PurgeContainer, int(const char *containerId));
+    MOCK_METHOD2(PurgeExec, int(const char *containerId, const char *execId));
 };
 
 void MockSandboxerSandbox_SetMock(MockSandboxerSandbox *mock);
diff --git a/test/mocks/shim_sandbox_mock.cc b/test/mocks/shim_sandbox_mock.cc
deleted file mode 100644
index ccefb424..00000000
--- a/test/mocks/shim_sandbox_mock.cc
+++ /dev/null
@@ -1,72 +0,0 @@
-/******************************************************************************
- * 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.
- * Author: liuxu
- * Create: 2024-11-21
- * Description: provide sandbox mock
- ******************************************************************************/
-
-#include <gmock/gmock.h>
-#include "shim_sandbox_mock.h"
-
-namespace sandbox {
-MockShimSandbox *g_shim_sandbox_mock = nullptr;
-
-ShimSandbox::ShimSandbox(const std::string id, const std::string &rootdir, const std::string &statedir, const std::string name,
-                 const RuntimeInfo info, std::string netMode, std::string netNsPath, const runtime::v1::PodSandboxConfig sandboxConfig,
-                 std::string image):Sandbox(id, rootdir, statedir, name, info, netMode,
-					 								  netNsPath, sandboxConfig, image)
-{
-}
-
-void MockShimSandbox_SetMock(MockShimSandbox *mock)
-{
-    g_shim_sandbox_mock = mock;
-}
-
-void ShimSandbox::LoadSandboxTasks() {}
-
-auto ShimSandbox::SaveSandboxTasks() -> bool
-{
-    if (g_shim_sandbox_mock != nullptr) {
-        return g_shim_sandbox_mock->SaveSandboxTasks();
-    }
-    return true;
-}
-
-auto ShimSandbox::AddSandboxTasks(sandbox_task *task) -> bool
-{
-    if (g_shim_sandbox_mock != nullptr) {
-        return g_shim_sandbox_mock->AddSandboxTasks(task);
-    }
-    return true;
-}
-
-auto ShimSandbox::GetAnySandboxTasks() -> std::string
-{
-    if (g_shim_sandbox_mock != nullptr) {
-        return g_shim_sandbox_mock->GetAnySandboxTasks();
-    }
-    return std::string("Nothing for shim.");
-}
-
-void ShimSandbox::DeleteSandboxTasks(const char *containerId) {}
-
-auto ShimSandbox::AddSandboxTasksProcess(const char *containerId, sandbox_process *processes) -> bool
-{
-    if (g_shim_sandbox_mock != nullptr) {
-        return g_shim_sandbox_mock->AddSandboxTasksProcess(containerId, processes);
-    }
-   return true;
-}
-
-void ShimSandbox::DeleteSandboxTasksProcess(const char *containerId, const char *execId) {}
-
-}
\ No newline at end of file
diff --git a/test/mocks/shim_sandbox_mock.h b/test/mocks/shim_sandbox_mock.h
deleted file mode 100644
index 1b16a4cc..00000000
--- a/test/mocks/shim_sandbox_mock.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/******************************************************************************
- * 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.
- * Author: liuxu
- * Create: 2024-11-21
- * Description: provide sandbox mock
- ******************************************************************************/
-
-#ifndef _ISULAD_TEST_MOCKS_SHIM_SANDBOX_MOCK_H
-#define _ISULAD_TEST_MOCKS_SHIM_SANDBOX_MOCK_H
-
-#include <gmock/gmock.h>
-#include "sandbox_mock.h"
-#include "shim_sandbox.h"
-
-namespace sandbox {
-
-class MockShimSandbox : public MockSandbox {
-public:
-    MockShimSandbox() = default;
-    virtual ~MockShimSandbox() = default;
-
-    MOCK_METHOD0(LoadSandboxTasks, void());
-    MOCK_METHOD0(SaveSandboxTasks, bool());
-    MOCK_METHOD1(AddSandboxTasks, bool(sandbox_task *task));
-    MOCK_METHOD0(GetAnySandboxTasks, std::string());
-    MOCK_METHOD1(DeleteSandboxTasks, void(const char *containerId));
-    MOCK_METHOD2(AddSandboxTasksProcess, bool(const char *containerId, sandbox_process *processes));
-    MOCK_METHOD2(DeleteSandboxTasksProcess, void(const char *containerId, const char *execId));
-};
-
-void MockShimSandbox_SetMock(MockShimSandbox *mock);
-
-}
-
-#endif
diff --git a/test/sandbox/sandbox/CMakeLists.txt b/test/sandbox/sandbox/CMakeLists.txt
index 9ee67033..6dd6c3ee 100644
--- a/test/sandbox/sandbox/CMakeLists.txt
+++ b/test/sandbox/sandbox/CMakeLists.txt
@@ -16,7 +16,6 @@ add_executable(${EXE}
     ${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/sandbox/shim/shim_sandbox.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
diff --git a/test/sandbox/sandbox/sandbox_ut.cc b/test/sandbox/sandbox/sandbox_ut.cc
index 192d46ef..dd84d8fb 100644
--- a/test/sandbox/sandbox/sandbox_ut.cc
+++ b/test/sandbox/sandbox/sandbox_ut.cc
@@ -16,7 +16,6 @@
 #include <gtest/gtest.h>
 
 #include "sandbox.h"
-#include "shim_sandbox.h"
 
 namespace sandbox {
 
@@ -41,7 +40,7 @@ TEST_F(SandboxTest, TestDefaultGetters)
     std::string name = "test";
     RuntimeInfo info = {"runc", "shim", "kuasar"};
 
-    auto sandbox = new ShimSandbox(id, rootdir, statedir, name, info);
+    auto sandbox = new Sandbox(id, rootdir, statedir, name, info);
     ASSERT_NE(sandbox, nullptr);
 
     ASSERT_EQ(sandbox->IsReady(), false);
@@ -67,7 +66,7 @@ TEST_F(SandboxTest, TestGettersAndSetters)
     std::string statedir = "/test2/statedir";
     std::string mode = "host";
 
-    auto sandbox = new ShimSandbox(id, rootdir, statedir);
+    auto sandbox = new Sandbox(id, rootdir, statedir);
     ASSERT_NE(sandbox, nullptr);
 
     sandbox->SetNetMode(mode);
diff --git a/test/sandbox/sandbox_manager/CMakeLists.txt b/test/sandbox/sandbox_manager/CMakeLists.txt
index 9254263c..a7dd8c9d 100644
--- a/test/sandbox/sandbox_manager/CMakeLists.txt
+++ b/test/sandbox/sandbox_manager/CMakeLists.txt
@@ -12,7 +12,6 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cpputils/read_write_lock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cpputils/transform.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sandbox_mock.cc
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/shim_sandbox_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sandboxer_sandbox_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/sandbox/sandbox_manager.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/id_name_manager.c
-- 
2.23.0

