398 lines
12 KiB
Diff
398 lines
12 KiB
Diff
From 678baec08a80c65950180dd0e4c403c03d628693 Mon Sep 17 00:00:00 2001
|
|
From: haozi007 <liuhao27@huawei.com>
|
|
Date: Thu, 16 Sep 2021 04:57:01 +0100
|
|
Subject: [PATCH] add testcase for clibcni
|
|
|
|
Signed-off-by: haozi007 <liuhao27@huawei.com>
|
|
---
|
|
CMakeLists.txt | 5 +
|
|
clibcni.spec | 8 +-
|
|
tests/CMakeLists.txt | 48 ++++++++++
|
|
tests/api_llt.cpp | 185 +++++++++++++++++++++++++++++++++++++
|
|
tests/main.cpp | 22 +++++
|
|
tests/utils/CMakeLists.txt | 7 ++
|
|
tests/utils/constants.h | 37 ++++++++
|
|
7 files changed, 310 insertions(+), 2 deletions(-)
|
|
create mode 100644 tests/CMakeLists.txt
|
|
create mode 100644 tests/api_llt.cpp
|
|
create mode 100644 tests/main.cpp
|
|
create mode 100644 tests/utils/CMakeLists.txt
|
|
create mode 100644 tests/utils/constants.h
|
|
|
|
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
|
index a5c7fec..fad4472 100644
|
|
--- a/CMakeLists.txt
|
|
+++ b/CMakeLists.txt
|
|
@@ -58,6 +58,11 @@ endif()
|
|
|
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
|
|
|
|
+option(ENABLE_UT "enble ut testcase" OFF)
|
|
+if (ENABLE_UT STREQUAL "ON")
|
|
+ add_subdirectory(tests)
|
|
+endif()
|
|
+
|
|
# install all files
|
|
install(FILES ${CMAKE_BINARY_DIR}/conf/clibcni.pc
|
|
DESTINATION ${LIB_INSTALL_DIR_DEFAULT}/pkgconfig PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE)
|
|
diff --git a/clibcni.spec b/clibcni.spec
|
|
index fa2046b..784538a 100644
|
|
--- a/clibcni.spec
|
|
+++ b/clibcni.spec
|
|
@@ -12,7 +12,7 @@ BuildRoot: %{_tmppath}/%{name}-%{version}
|
|
|
|
BuildRequires: gcc
|
|
BuildRequires: cmake
|
|
-BuildRequires: lcr-devel yajl-devel
|
|
+BuildRequires: lcr-devel yajl-devel gtest-devel
|
|
|
|
Requires: lcr
|
|
|
|
@@ -44,9 +44,13 @@ the %{name}-libs package contains libraries for running %{name} applications.
|
|
%build
|
|
mkdir -p build
|
|
cd build
|
|
-%cmake -DDEBUG=ON -DLIB_INSTALL_DIR=%{_libdir} ../
|
|
+%cmake -DDEBUG=ON -DENABLE_UT=ON -DLIB_INSTALL_DIR=%{_libdir} ../
|
|
%make_build
|
|
|
|
+pushd tests
|
|
+ctest -V
|
|
+popd
|
|
+
|
|
%install
|
|
rm -rf %{buildroot}
|
|
cd build
|
|
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
|
|
new file mode 100644
|
|
index 0000000..6cb2c7b
|
|
--- /dev/null
|
|
+++ b/tests/CMakeLists.txt
|
|
@@ -0,0 +1,48 @@
|
|
+enable_testing()
|
|
+find_package(GTest REQUIRED)
|
|
+
|
|
+set(CMAKE_C_COMPILER "g++" CACHE PATH "c compiler")
|
|
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
|
|
+
|
|
+include_directories(
|
|
+ ${GTEST_INCLUDE_DIR}
|
|
+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
|
|
+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/utils
|
|
+ PUBLIC ${CMAKE_SOURCE_DIR}/src
|
|
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/version/
|
|
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/types/
|
|
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/invoke/
|
|
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/json
|
|
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/json/schema/src
|
|
+ PUBLIC ${CMAKE_BINARY_DIR}/json
|
|
+ PUBLIC ${CMAKE_BINARY_DIR}/conf
|
|
+ )
|
|
+
|
|
+add_subdirectory(utils)
|
|
+
|
|
+macro(_DEFINE_NEW_TEST)
|
|
+ add_executable(${ARGV0}
|
|
+ ${TESTS_UTILS_SRCS}
|
|
+ main.cpp
|
|
+ ${ARGV0}.cpp
|
|
+ )
|
|
+
|
|
+ target_link_libraries(${ARGV0}
|
|
+ clibcni
|
|
+ gtest
|
|
+ -lyajl
|
|
+ pthread
|
|
+ )
|
|
+
|
|
+ add_test(
|
|
+ NAME ${ARGV1}
|
|
+ COMMAND ${ARGV0}
|
|
+ )
|
|
+endmacro()
|
|
+
|
|
+# --------------- testcase add here -----------------
|
|
+# api testcase
|
|
+_DEFINE_NEW_TEST(api_llt api_testcase)
|
|
+
|
|
+# --------------- testcase add finish -----------------
|
|
+
|
|
diff --git a/tests/api_llt.cpp b/tests/api_llt.cpp
|
|
new file mode 100644
|
|
index 0000000..d21ec01
|
|
--- /dev/null
|
|
+++ b/tests/api_llt.cpp
|
|
@@ -0,0 +1,185 @@
|
|
+/*
|
|
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
|
+ * clibcni 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: haozi007
|
|
+ * Create: 2021-09-16
|
|
+ * Description: provide cni api functions
|
|
+ */
|
|
+#include <gtest/gtest.h>
|
|
+
|
|
+#include <iostream>
|
|
+
|
|
+#include <string.h>
|
|
+#include <unistd.h>
|
|
+
|
|
+#include "api.h"
|
|
+#include "version.h"
|
|
+#include "conf.h"
|
|
+#include "constants.h"
|
|
+
|
|
+
|
|
+void api_check_network_config_list(struct cni_network_list_conf *conf, const char *target_name, bool check_plugin_name)
|
|
+{
|
|
+ /* check network_config_list */
|
|
+ ASSERT_NE(conf, nullptr);
|
|
+ ASSERT_NE(conf->first_plugin_name, nullptr);
|
|
+ ASSERT_NE(conf->first_plugin_type, nullptr);
|
|
+ EXPECT_STREQ(target_name, conf->name);
|
|
+ ASSERT_NE(conf->bytes, nullptr);
|
|
+
|
|
+ struct network_config_list *tmp = NULL;
|
|
+ char *err = NULL;
|
|
+ int ret = conflist_from_bytes(conf->bytes, &tmp, &err);
|
|
+ if (ret != 0) {
|
|
+ std::cout << "conflist parse failed:" << err << std::endl;
|
|
+ }
|
|
+ free(err);
|
|
+ ASSERT_EQ(ret , 0);
|
|
+
|
|
+ /* check net_conf_list */
|
|
+ EXPECT_STREQ("0.3.0", tmp->list->cni_version);
|
|
+ EXPECT_STREQ(target_name, tmp->list->name);
|
|
+ ASSERT_NE(tmp->list->plugins, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins_len, 2);
|
|
+ EXPECT_STREQ("0.3.0", tmp->list->plugins[0]->cni_version);
|
|
+ if (check_plugin_name) {
|
|
+ EXPECT_STREQ(target_name, tmp->list->plugins[0]->name);
|
|
+ }
|
|
+ EXPECT_STREQ("bridge", tmp->list->plugins[0]->type);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->dns, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->runtime_config, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->capabilities, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->prev_result, nullptr);
|
|
+ EXPECT_STREQ("10.1.0.1", tmp->list->plugins[1]->dns->nameservers[0]);
|
|
+ EXPECT_STREQ("bridge", tmp->list->plugins[1]->type);
|
|
+
|
|
+ free_network_config_list(tmp);
|
|
+}
|
|
+
|
|
+TEST(api_testcases, cni_conflist_from_bytes)
|
|
+{
|
|
+ int ret;
|
|
+ struct cni_network_list_conf *new_list = NULL;
|
|
+ char *err = NULL;
|
|
+
|
|
+ ret = cni_conflist_from_bytes(COMMON_CONF_LIST, &new_list, &err);
|
|
+ if (ret != 0) {
|
|
+ std::cout << "conflist parse failed:" << err << std::endl;
|
|
+ }
|
|
+ free(err);
|
|
+ std::cout << new_list->bytes << std::endl;
|
|
+
|
|
+ api_check_network_config_list(new_list, "default", true);
|
|
+
|
|
+ free(err);
|
|
+ free_cni_network_list_conf(new_list);
|
|
+}
|
|
+
|
|
+void api_check_network_config_list_from_conf(struct cni_network_list_conf *conf, const char *target_name, bool check_plugin_name)
|
|
+{
|
|
+ /* check network_config_list */
|
|
+ ASSERT_NE(conf, nullptr);
|
|
+ ASSERT_NE(conf->first_plugin_name, nullptr);
|
|
+ ASSERT_NE(conf->first_plugin_type, nullptr);
|
|
+ EXPECT_STREQ(target_name, conf->name);
|
|
+ ASSERT_NE(conf->bytes, nullptr);
|
|
+
|
|
+ struct network_config_list *tmp = NULL;
|
|
+ char *err = NULL;
|
|
+ int ret = conflist_from_bytes(conf->bytes, &tmp, &err);
|
|
+ if (ret != 0) {
|
|
+ std::cout << "conflist parse failed:" << err << std::endl;
|
|
+ }
|
|
+ free(err);
|
|
+ ASSERT_EQ(ret , 0);
|
|
+
|
|
+ /* check net_conf_list */
|
|
+ EXPECT_STREQ("0.3.0", tmp->list->cni_version);
|
|
+ EXPECT_STREQ(target_name, tmp->list->name);
|
|
+ ASSERT_NE(tmp->list->plugins, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins_len, 1);
|
|
+ EXPECT_STREQ("0.3.0", tmp->list->plugins[0]->cni_version);
|
|
+ if (check_plugin_name) {
|
|
+ EXPECT_STREQ(target_name, tmp->list->plugins[0]->name);
|
|
+ }
|
|
+ EXPECT_STREQ("bridge", tmp->list->plugins[0]->type);
|
|
+ ASSERT_NE(tmp->list->plugins[0]->dns, nullptr);
|
|
+ EXPECT_STREQ("10.1.0.1", tmp->list->plugins[0]->dns->nameservers[0]);
|
|
+ EXPECT_STREQ("bridge", tmp->list->plugins[0]->type);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->runtime_config, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->capabilities, nullptr);
|
|
+ ASSERT_EQ(tmp->list->plugins[0]->prev_result, nullptr);
|
|
+ ASSERT_NE(tmp->list->plugins[0]->ipam, nullptr);
|
|
+ EXPECT_STREQ("host-local", tmp->list->plugins[0]->ipam->type);
|
|
+ EXPECT_STREQ("10.1.0.0/16", tmp->list->plugins[0]->ipam->subnet);
|
|
+ EXPECT_STREQ("10.1.0.1", tmp->list->plugins[0]->ipam->gateway);
|
|
+
|
|
+ free_network_config_list(tmp);
|
|
+}
|
|
+
|
|
+TEST(api_testcases, cni_conflist_from_conf)
|
|
+{
|
|
+ int ret;
|
|
+ struct cni_network_list_conf *new_list = NULL;
|
|
+ char *err = NULL;
|
|
+ struct cni_network_conf test = {
|
|
+ .name = "default",
|
|
+ .type = "bridge",
|
|
+ .bytes = COMMON_CONF,
|
|
+ };
|
|
+
|
|
+ ret = cni_conflist_from_conf(&test, &new_list, &err);
|
|
+ if (ret != 0) {
|
|
+ std::cout << "conflist parse failed:" << err << std::endl;
|
|
+ }
|
|
+ free(err);
|
|
+ std::cout << new_list->bytes << std::endl;
|
|
+
|
|
+ api_check_network_config_list_from_conf(new_list, "default", true);
|
|
+
|
|
+ free(err);
|
|
+ free_cni_network_list_conf(new_list);
|
|
+}
|
|
+
|
|
+TEST(api_testcases, get_version_info)
|
|
+{
|
|
+ const std::string CNI_PLUGIN_PATH = "/opt/cni/bin/";
|
|
+ char *err = nullptr;
|
|
+ struct plugin_info *pinfo = nullptr;
|
|
+ size_t i = 0;
|
|
+ int ret = 0;
|
|
+ char *paths[] = {strdup(CNI_PLUGIN_PATH.c_str()), nullptr};
|
|
+ const std::string bridge_name = "bridge";
|
|
+
|
|
+ ret = cni_get_version_info(bridge_name.c_str(), paths, &pinfo, &err);
|
|
+ if (ret != 0) {
|
|
+ if (strstr(err, "No such file or directory") != nullptr) {
|
|
+ std::cout << "Skip: cni_get_version_info api cause by no bridge plugin found" << std::endl;
|
|
+ return;
|
|
+ }
|
|
+ std::cout << "Get version failed:" << err << std::endl;
|
|
+ }
|
|
+ ASSERT_EQ(ret, 0);
|
|
+
|
|
+ /* check plugin info */
|
|
+ EXPECT_STREQ("0.4.0", pinfo->cniversion);
|
|
+ ASSERT_LE(0, pinfo->supported_versions_len);
|
|
+ for (i = 0; i < pinfo->supported_versions_len; i++) {
|
|
+ if (strcmp(pinfo->supported_versions[i], CURRENT_VERSION) == 0) {
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ ASSERT_LE(i, pinfo->supported_versions_len);
|
|
+
|
|
+ free_plugin_info(pinfo);
|
|
+ free(paths[0]);
|
|
+ paths[0] = nullptr;
|
|
+}
|
|
+
|
|
diff --git a/tests/main.cpp b/tests/main.cpp
|
|
new file mode 100644
|
|
index 0000000..7a4bc09
|
|
--- /dev/null
|
|
+++ b/tests/main.cpp
|
|
@@ -0,0 +1,22 @@
|
|
+/*
|
|
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
|
+ * clibcni 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: haozi007
|
|
+ * Create: 2021-09-16
|
|
+ * Description: provide cni api functions
|
|
+ */
|
|
+
|
|
+#include "gtest/gtest.h"
|
|
+
|
|
+int main(int argc, char **argv) {
|
|
+ testing::InitGoogleTest(&argc, argv);
|
|
+
|
|
+ return RUN_ALL_TESTS();
|
|
+}
|
|
diff --git a/tests/utils/CMakeLists.txt b/tests/utils/CMakeLists.txt
|
|
new file mode 100644
|
|
index 0000000..9b01eb6
|
|
--- /dev/null
|
|
+++ b/tests/utils/CMakeLists.txt
|
|
@@ -0,0 +1,7 @@
|
|
+# get current directory sources files
|
|
+aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} local_cutils_srcs)
|
|
+
|
|
+set(TESTS_UTILS_SRCS
|
|
+ ${local_cutils_srcs}
|
|
+ PARENT_SCOPE
|
|
+ )
|
|
diff --git a/tests/utils/constants.h b/tests/utils/constants.h
|
|
new file mode 100644
|
|
index 0000000..1f06bfc
|
|
--- /dev/null
|
|
+++ b/tests/utils/constants.h
|
|
@@ -0,0 +1,37 @@
|
|
+/*
|
|
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
|
+ * clibcni 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: haozi007
|
|
+ * Create: 2021-09-16
|
|
+ * Description: provide cni api functions
|
|
+ */
|
|
+#ifndef _TESTS_CONSTANTS_H
|
|
+#define _TESTS_CONSTANTS_H
|
|
+
|
|
+#ifdef __cplusplus
|
|
+extern "C" {
|
|
+#endif
|
|
+
|
|
+#define DEFAULT_CNI_BIN_PATH "/opt/cni/bin"
|
|
+
|
|
+#define COMMON_CONF_LIST "{\"cniVersion\":\"0.3.0\",\"name\":\"default\", \
|
|
+ \"plugins\":[{\"cniVersion\":\"0.3.0\", \"name\":\"default\",\"type\":\"bridge\"}, \
|
|
+ {\"name\": \"exist\",\"type\": \"bridge\", \"dns\": {\"nameservers\": [\"10.1.0.1\"]}}]}"
|
|
+
|
|
+#define COMMON_CONF "{\"cniVersion\":\"0.3.0\",\"name\":\"default\", \
|
|
+ \"type\": \"bridge\", \"bridge\": \"cni0\", \"isGateway\": \"true\", \
|
|
+ \"ipam\": {\"type\": \"host-local\", \"subnet\": \"10.1.0.0/16\", \"gateway\": \"10.1.0.1\"},\
|
|
+ \"dns\": {\"nameservers\": [\"10.1.0.1\"]}}"
|
|
+
|
|
+#ifdef __cplusplus
|
|
+}
|
|
+#endif
|
|
+
|
|
+#endif
|
|
--
|
|
2.20.1
|
|
|