From 0111a575f829b946068dcb11286f0d84363cfc3d Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Thu, 28 Mar 2024 12:51:53 +0000
Subject: [PATCH 36/43] add ci cases for oomkilled monitor

Signed-off-by: jikai <jikai11@huawei.com>
---
 CI/test_cases/container_cases/inspect.sh      | 14 ++++++++
 test/cgroup/cpu/CMakeLists.txt                |  2 ++
 .../image/oci/oci_config_merge/CMakeLists.txt |  1 +
 test/image/oci/registry/CMakeLists.txt        |  1 +
 test/mocks/sender_mock.cc                     | 34 +++++++++++++++++++
 test/mocks/sender_mock.h                      | 31 +++++++++++++++++
 test/network/network_ns/CMakeLists.txt        |  1 +
 test/runtime/isula/CMakeLists.txt             |  1 +
 test/runtime/lcr/CMakeLists.txt               |  1 +
 test/specs/specs/CMakeLists.txt               |  1 +
 test/specs/specs_extend/CMakeLists.txt        |  1 +
 test/specs/verify/CMakeLists.txt              |  1 +
 test/volume/CMakeLists.txt                    |  3 +-
 13 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 test/mocks/sender_mock.cc
 create mode 100644 test/mocks/sender_mock.h

diff --git a/CI/test_cases/container_cases/inspect.sh b/CI/test_cases/container_cases/inspect.sh
index cde9ea1f..b4f4a785 100755
--- a/CI/test_cases/container_cases/inspect.sh
+++ b/CI/test_cases/container_cases/inspect.sh
@@ -27,6 +27,7 @@ function test_inspect_spec()
 {
     local ret=0
     local image="busybox"
+    local ubuntu_image="ubuntu"
     local test="container inspect test => (${FUNCNAME[@]})"
 
     msg_info "${test} starting..."
@@ -37,6 +38,12 @@ function test_inspect_spec()
     isula images | grep busybox
     [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - missing list image: ${image}" && ((ret++))
 
+    isula pull ${ubuntu_image}
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to pull image: ${ubuntu_image}" && return ${FAILURE}
+
+    isula images | grep ubuntu
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - missing list image: ${ubuntu_image}" && ((ret++))
+
     containername=test_inspect
 
     isula create --name $containername --ipc host --pid host --uts host --restart=on-failure:10 --hook-spec ${test_data_path}/test-hookspec.json --cpu-shares 100 --memory 5MB --memory-reservation 4MB --cpu-period 1000000 --cpu-quota 200000  --cpuset-cpus 1 --cpuset-mems 0 --kernel-memory 50M --pids-limit=10000 --volume /home:/root --env a=1 $image /bin/sh ls
@@ -139,6 +146,13 @@ function test_inspect_spec()
 
     isula rm -f $containername
 
+    isula run -it -m 4m --name $containername $ubuntu_image perl -e 'for ($i = 0; $i < 100000000; $i++) { $a .= " " x 1024 }'
+
+    isula inspect -f "{{json .State.OOMKilled}} {{.Name}}" $containername 2>&1 | sed -n '1p' | grep "true"
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check container with image: ${ubuntu_image}" && ((ret++))
+
+    isula rm -f $containername
+
     msg_info "${test} finished with return ${ret}..."
     return ${ret}
 }
diff --git a/test/cgroup/cpu/CMakeLists.txt b/test/cgroup/cpu/CMakeLists.txt
index 30bfc417..9c3cfa12 100644
--- a/test/cgroup/cpu/CMakeLists.txt
+++ b/test/cgroup/cpu/CMakeLists.txt
@@ -13,6 +13,7 @@ add_executable(${EXE}
     ${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
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sender_mock.cc
     cgroup_cpu_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
@@ -23,6 +24,7 @@ target_include_directories(${EXE} PUBLIC
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/config
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/common/cgroup
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/modules/api
     ${CMAKE_BINARY_DIR}/conf
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/config
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd
diff --git a/test/image/oci/oci_config_merge/CMakeLists.txt b/test/image/oci/oci_config_merge/CMakeLists.txt
index d13ec738..ffd3999d 100644
--- a/test/image/oci/oci_config_merge/CMakeLists.txt
+++ b/test/image/oci/oci_config_merge/CMakeLists.txt
@@ -35,6 +35,7 @@ add_executable(${EXE}
     ${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/sender_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/modules/spec/parse_volume.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/modules/spec/specs.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/modules/spec/parse_volume.c
diff --git a/test/image/oci/registry/CMakeLists.txt b/test/image/oci/registry/CMakeLists.txt
index 5b5bc3f5..6166c2d0 100644
--- a/test/image/oci/registry/CMakeLists.txt
+++ b/test/image/oci/registry/CMakeLists.txt
@@ -44,6 +44,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/storage_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/oci_image_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/http_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/sender_mock.cc
     registry_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/mocks/sender_mock.cc b/test/mocks/sender_mock.cc
new file mode 100644
index 00000000..26028d7f
--- /dev/null
+++ b/test/mocks/sender_mock.cc
@@ -0,0 +1,34 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020. 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: jikai
+ * Create: 2024-03-29
+ * Description: provide collector mock
+ ******************************************************************************/
+
+#include "sender_mock.h"
+
+namespace {
+MockEventSender *g_sender_mock = nullptr;
+}
+
+void MockEventSender_SetMock(MockEventSender *mock)
+{
+    g_sender_mock = mock;
+}
+
+int isulad_monitor_send_container_event(const char *name, runtime_state_t state, int pid, int exit_code,
+                                        const char *args, const char *extra_annations)
+{
+    if (g_sender_mock != nullptr) {
+        return g_sender_mock->IsuladMonitorEventSendContainerEvent(name, state, pid, exit_code, args, extra_annations);
+    }
+    return 0;
+}
diff --git a/test/mocks/sender_mock.h b/test/mocks/sender_mock.h
new file mode 100644
index 00000000..f4fe75f0
--- /dev/null
+++ b/test/mocks/sender_mock.h
@@ -0,0 +1,31 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020. 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: jikai
+ * Create: 2024-03-30
+ * Description: provide sender mock
+ ******************************************************************************/
+
+#ifndef _ISULAD_TEST_MOCKS_SENDER_MOCK_H
+#define _ISULAD_TEST_MOCKS_SENDER_MOCK_H
+
+#include <gmock/gmock.h>
+#include "events_sender_api.h"
+
+class MockEventSender {
+public:
+    MOCK_METHOD6(IsuladMonitorEventSendContainerEvent, int(const char *name, runtime_state_t state, int pid, int exit_code,
+                                                       const char *args, const char *extra_annations));
+};
+
+void MockEventSender_SetMock(MockEventSender *mock);
+
+#endif
+
diff --git a/test/network/network_ns/CMakeLists.txt b/test/network/network_ns/CMakeLists.txt
index 71b8039d..6f3f36a0 100644
--- a/test/network/network_ns/CMakeLists.txt
+++ b/test/network/network_ns/CMakeLists.txt
@@ -43,6 +43,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/selinux_label_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/isulad_config_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/storage_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sender_mock.cc
 network_ns_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/runtime/isula/CMakeLists.txt b/test/runtime/isula/CMakeLists.txt
index c1f0a5cc..15636623 100644
--- a/test/runtime/isula/CMakeLists.txt
+++ b/test/runtime/isula/CMakeLists.txt
@@ -31,6 +31,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/engine_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/isulad_config_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/modules/runtime/isula/isula_rt_ops.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sender_mock.cc
     isula_rt_ops_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/runtime/lcr/CMakeLists.txt b/test/runtime/lcr/CMakeLists.txt
index c3b93d67..5b2ed11a 100644
--- a/test/runtime/lcr/CMakeLists.txt
+++ b/test/runtime/lcr/CMakeLists.txt
@@ -29,6 +29,7 @@ add_executable(${EXE}
     ${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/sender_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/isulad_config_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/daemon/modules/runtime/engines/lcr/lcr_rt_ops.c
     lcr_rt_ops_ut.cc)
diff --git a/test/specs/specs/CMakeLists.txt b/test/specs/specs/CMakeLists.txt
index 45f688f9..12c11f51 100644
--- a/test/specs/specs/CMakeLists.txt
+++ b/test/specs/specs/CMakeLists.txt
@@ -43,6 +43,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/isulad_config_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/storage_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/image_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sender_mock.cc
     specs_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/specs/specs_extend/CMakeLists.txt b/test/specs/specs_extend/CMakeLists.txt
index 1b737089..2fd37e1c 100644
--- a/test/specs/specs_extend/CMakeLists.txt
+++ b/test/specs/specs_extend/CMakeLists.txt
@@ -43,6 +43,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/isulad_config_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/storage_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/image_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sender_mock.cc
     specs_extend_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/specs/verify/CMakeLists.txt b/test/specs/verify/CMakeLists.txt
index b0602127..7f000cd1 100644
--- a/test/specs/verify/CMakeLists.txt
+++ b/test/specs/verify/CMakeLists.txt
@@ -38,6 +38,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/storage_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/image_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/storage_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/sender_mock.cc
     verify_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/volume/CMakeLists.txt b/test/volume/CMakeLists.txt
index 27d07330..1f9dac03 100644
--- a/test/volume/CMakeLists.txt
+++ b/test/volume/CMakeLists.txt
@@ -24,6 +24,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../src/daemon/common/cgroup/cgroup_v1.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../src/daemon/common/cgroup/cgroup_v2.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../src/daemon/common/cgroup/cgroup_common.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../test/mocks/sender_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cmd/command_parser.c
     volume_ut.cc)
 
@@ -43,6 +44,6 @@ target_include_directories(${EXE} PUBLIC
     ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/console
     )
 
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
 add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
 set_tests_properties(${EXE} PROPERTIES TIMEOUT 120)
-- 
2.34.1

