Skip to content

Commit

Permalink
!2548 add image storage unit test
Browse files Browse the repository at this point in the history
From: @taotao-sauce 
Reviewed-by: @xuxuepeng 
Signed-off-by: @xuxuepeng
  • Loading branch information
openeuler-ci-bot authored and gitee-org committed Dec 15, 2024
2 parents d33698c + 7dfa691 commit 7883dcc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <isula_libutils/oci_image_manifest.h>
#include <isula_libutils/image_manifest_v1_compatibility.h>

#include <isula_libutils/utils_macro.h>

#include "utils.h"
#include "utils_file.h"
#include "utils_images.h"
Expand Down Expand Up @@ -3003,7 +3005,7 @@ static int do_append_image(storage_image *im)
return 0;
}

static void strip_host_prefix(char **name)
STATIC void strip_host_prefix(char **name)
{
char *new_image_name = NULL;

Expand Down
2 changes: 2 additions & 0 deletions test/image/oci/storage/images/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ project(iSulad_UT)

SET(EXE storage_images_ut)

add_definitions(-DUNIT_TEST=ON)

add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/utils_regex.c
Expand Down
51 changes: 51 additions & 0 deletions test/image/oci/storage/images/storage_images_ut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::_;

extern "C" {
void strip_host_prefix(char **name);
}

std::string GetDirectory()
{
char abs_path[PATH_MAX] { 0x00 };
Expand Down Expand Up @@ -299,11 +303,13 @@ class StorageImagesUnitTest : public testing::Test {
ASSERT_EQ(image_store_init(&opts), 0);
free(opts.storage_root);
free(opts.driver_name);
MockIsuladConf_SetMock(&m_isulad_conf);
}

void TearDown() override
{
image_store_free();
MockIsuladConf_SetMock(nullptr);
}

void BackUp()
Expand All @@ -325,6 +331,7 @@ class StorageImagesUnitTest : public testing::Test {
std::vector<std::string> ids { "39891ff67da98ab8540d71320915f33d2eb80ab42908e398472cab3c1ce7ac10",
"e4db68de4ff27c2adfea0c54bbb73a61a42f5b667c326de4d7d5b19ab71c6a3b" };
char store_real_path[PATH_MAX] = { 0x00 };
NiceMock<MockIsuladConf> m_isulad_conf;
};

TEST_F(StorageImagesUnitTest, test_images_load)
Expand Down Expand Up @@ -714,3 +721,47 @@ TEST_F(StorageImagesUnitTest, test_image_store_remove_multi_name)

Restore();
}

static isulad_daemon_constants *g_test_isulad_daemon_constants = NULL;

isulad_daemon_constants *invoke_get_isulad_daemon_constants(void)
{
g_test_isulad_daemon_constants = (isulad_daemon_constants *)util_common_calloc_s(sizeof(isulad_daemon_constants));
if (g_test_isulad_daemon_constants == NULL) {
return NULL;
}
g_test_isulad_daemon_constants->default_host = util_strdup_s("docker.io");

return g_test_isulad_daemon_constants;
}

TEST_F(StorageImagesUnitTest, test_strip_host_prefix)
{
char *name = util_strdup_s("docker.io/test_image");
std::string test_name = "test_image";
std::string test_name_origin = "docker.io/test_image";
char *null_name = NULL;

strip_host_prefix(&name);
ASSERT_STREQ(name, test_name_origin.c_str());

EXPECT_CALL(m_isulad_conf, GetIsuladDaemonConstants()).WillRepeatedly(Invoke(invoke_get_isulad_daemon_constants));

strip_host_prefix(&name);
ASSERT_STREQ(name, test_name.c_str());

strip_host_prefix(&null_name);
ASSERT_EQ(null_name, nullptr);

free(name);
free_isulad_daemon_constants(g_test_isulad_daemon_constants);
}

#ifdef ENABLE_REMOTE_LAYER_STORE
TEST_F(StorageImagesUnitTest, test_remote_layer_common)
{
ASSERT_EQ(remote_append_image_by_directory_with_lock(NULL), -1);
ASSERT_EQ(remote_remove_image_from_memory_with_lock(NULL), -1);
ASSERT_EQ(remote_image_get_top_layer_from_json(NULL), nullptr);
}
#endif

0 comments on commit 7883dcc

Please sign in to comment.