From baa75b2db9a020c0e0552c7236fbbf08ede07f20 Mon Sep 17 00:00:00 2001 From: jikai Date: Sat, 27 Jan 2024 15:30:05 +0800 Subject: [PATCH 195/198] improve dt for oci device update Signed-off-by: jikai --- .../container_cases/dev_cgroup_rule.sh | 2 +- test/specs/specs/CMakeLists.txt | 1 + test/specs/specs/specs_ut.cc | 90 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/CI/test_cases/container_cases/dev_cgroup_rule.sh b/CI/test_cases/container_cases/dev_cgroup_rule.sh index 5616d37a..f6a58d15 100755 --- a/CI/test_cases/container_cases/dev_cgroup_rule.sh +++ b/CI/test_cases/container_cases/dev_cgroup_rule.sh @@ -60,7 +60,7 @@ function test_cpu_dev_cgoup_rule_spec() def_cid=$(isula run -tid --runtime $runtime -m 10m $image /bin/sh) [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - run container failed" && ((ret++)) cp $default_config $default_config_bak - sed -i '/"linux": {/a \ \t\t"devices": [\n\t\t{\n\t\t\t"type": "c",\n\t\t\t"path": "\/dev\/testABC",\n\t\t\t"major": 88,\n\t\t\t"minor": 88\n\t\t}\n\t\t],' $default_config + sed -i '/"linux": {/a \ \t\t"devices": [\n\t\t{\n\t\t\t"type": "c",\n\t\t\t"path": "\/dev\/testA",\n\t\t\t"major": 88,\n\t\t\t"minor": 88\n\t\t}\n\t\t],' $default_config stop_isulad_without_valgrind start_isulad_with_valgrind --cgroup-parent $test_cgroup_parent [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - start isulad failed" && ((ret++)) diff --git a/test/specs/specs/CMakeLists.txt b/test/specs/specs/CMakeLists.txt index c4b36c5f..22505a97 100644 --- a/test/specs/specs/CMakeLists.txt +++ b/test/specs/specs/CMakeLists.txt @@ -81,6 +81,7 @@ target_include_directories(${EXE} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks ) +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,util_common_calloc_s -Wl,--wrap,util_smart_calloc_s -Wl,--wrap,get_readonly_default_oci_spec") target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lgrpc++ -lprotobuf -lcrypto -lyajl -lz) add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml) set_tests_properties(${EXE} PROPERTIES TIMEOUT 120) diff --git a/test/specs/specs/specs_ut.cc b/test/specs/specs/specs_ut.cc index a2bd3890..8fbaa337 100644 --- a/test/specs/specs/specs_ut.cc +++ b/test/specs/specs/specs_ut.cc @@ -19,6 +19,7 @@ #include "mock.h" #include "isula_libutils/oci_runtime_spec.h" #include "specs_api.h" +#include "specs_mount.h" #include "specs_namespace.h" #include "isula_libutils/host_config.h" #include "isula_libutils/container_config.h" @@ -41,6 +42,46 @@ using ::testing::_; using namespace std; +static int g_malloc_count = 0; +static int g_malloc_match = 1; + +extern "C" { + DECLARE_WRAPPER_V(util_common_calloc_s, void *, (size_t size)); + DEFINE_WRAPPER_V(util_common_calloc_s, void *, (size_t size), (size)); + + DECLARE_WRAPPER_V(util_smart_calloc_s, void *, (size_t size, size_t len)); + DEFINE_WRAPPER_V(util_smart_calloc_s, void *, (size_t size, size_t len), (size, len)); + + DECLARE_WRAPPER(get_readonly_default_oci_spec, const oci_runtime_spec *, (bool system_container)); + DEFINE_WRAPPER(get_readonly_default_oci_spec, const oci_runtime_spec *, (bool system_container), (system_container)); +} + +void *util_common_calloc_s_fail(size_t size) +{ + g_malloc_count++; + + if (g_malloc_count == g_malloc_match) { + g_malloc_match++; + g_malloc_count = 0; + return nullptr; + } else { + return __real_util_common_calloc_s(size); + } +} + +void *util_smart_calloc_s_fail(size_t size, size_t len) +{ + g_malloc_count++; + + if (g_malloc_count == g_malloc_match) { + g_malloc_match++; + g_malloc_count = 0; + return nullptr; + } else { + return __real_util_smart_calloc_s(size, len); + } +} + class SpecsUnitTest : public testing::Test { public: void SetUp() override @@ -324,3 +365,52 @@ TEST_F(SpecsUnitTest, test_merge_container_cgroups_path_5) testing::Mock::VerifyAndClearExpectations(&m_isulad_conf); } + +TEST_F(SpecsUnitTest, SpecsUnitTest_test_update_devcies_for_oci_spec) +{ + parser_error err = nullptr; + oci_runtime_spec *readonly_spec = oci_runtime_spec_parse_data("{\"ociVersion\": \"1.0.1\", \"linux\": \ + { \"devices\": \ + [ { \"type\": \"c\", \"path\": \"/dev/testA\", \ + \"fileMode\": 8612, \"major\": 99, \"minor\": 99} ], \ + \"resources\": { \"devices\": [ { \"allow\": false, \ + \"type\": \"a\", \"major\": -1, \ + \"minor\": -1, \"access\": \"rwm\" } ] } } }", nullptr, &err); + ASSERT_NE(readonly_spec, nullptr); + free(err); + err = nullptr; + host_config *hostspec = static_cast(util_common_calloc_s(sizeof(host_config))); + ASSERT_NE(hostspec, nullptr); + + oci_runtime_spec *ocispec = oci_runtime_spec_parse_data("{\"ociVersion\": \"1.0.1\", \"linux\": \ + { \"devices\": [ ], \ + \"resources\": { \"devices\": [ ] } } }", nullptr, &err); + ASSERT_NE(ocispec, nullptr); + + MOCK_SET(get_readonly_default_oci_spec, readonly_spec); + MOCK_SET_V(util_smart_calloc_s, util_smart_calloc_s_fail); + MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail); + + ASSERT_EQ(update_devcies_for_oci_spec(ocispec, hostspec), -1); + ASSERT_EQ(update_devcies_for_oci_spec(ocispec, hostspec), -1); + ASSERT_EQ(update_devcies_for_oci_spec(ocispec, hostspec), -1); + free(ocispec->linux->devices[0]); + free(ocispec->linux->devices); + ocispec->linux->devices = NULL; + ocispec->linux->devices_len = 0; + ASSERT_EQ(update_devcies_for_oci_spec(ocispec, hostspec), -1); + free(ocispec->linux->devices[0]); + free(ocispec->linux->devices); + ocispec->linux->devices = NULL; + ocispec->linux->devices_len = 0; + ASSERT_EQ(update_devcies_for_oci_spec(ocispec, hostspec), 0); + + MOCK_CLEAR(get_readonly_default_oci_spec); + MOCK_CLEAR(util_smart_calloc_s); + MOCK_CLEAR(util_common_calloc_s); + + free_oci_runtime_spec(readonly_spec); + free_oci_runtime_spec(ocispec); + free_host_config(hostspec); + free(err); +} -- 2.25.1