From 7bc32be8a87b09481ef6a898506ebe4b7f91aad8 Mon Sep 17 00:00:00 2001 From: zhuchunyi Date: Wed, 6 Nov 2019 19:33:20 +0800 Subject: [PATCH] update code --- CMakeLists.txt | 4 +- cmake/options.cmake | 2 +- docs/install_guide.md | 125 +-- iSulad.spec | 18 +- src/cmd/commander.c | 7 +- src/cmd/lcrc/base/create.c | 66 +- src/cmd/lcrc/base/rm.c | 6 +- src/cmd/lcrc/commands.c | 7 +- src/cmd/lcrc/images/images.c | 2 +- src/cmd/lcrc/images/load.c | 2 +- src/cmd/lcrc/images/login.c | 4 +- src/cmd/lcrc/images/logout.c | 2 +- src/cmd/lcrc/images/pull.c | 2 +- src/cmd/lcrc/images/rmi.c | 2 +- src/cmd/lcrc/information/info.c | 2 +- src/cmd/lcrc/information/ps.c | 4 +- src/cmd/lcrc/information/version.c | 2 +- src/cmd/lcrc/stream/exec.c | 5 +- src/cmd/lcrd/commands.c | 10 +- src/cmd/lcrd/main.c | 3 +- src/connect/client/grpc/grpc_client.h | 2 +- .../client/grpc/grpc_containers_client.h | 2 +- src/connect/client/grpc/grpc_images_client.h | 2 +- .../client/rest/rest_containers_client.c | 1 + src/connect/client/rest/rest_images_client.c | 1 + .../service/grpc/grpc_containers_service.h | 2 +- .../service/rest/rest_containers_service.c | 1 + .../service/rest/rest_images_service.c | 1 + src/container_def.c | 2 + src/container_def.h | 5 +- src/contrib/config/config.json | 29 +- src/contrib/config/daemon.json | 1 - .../config/systemcontainer_config.json | 321 ++++++++ src/cpputils/stoppable_thread.h | 2 +- src/cutils/utils.c | 39 +- src/cutils/utils.h | 2 +- src/cutils/utils_array.c | 7 +- src/cutils/utils_array.h | 3 +- src/cutils/utils_convert.c | 3 +- src/cutils/utils_file.c | 5 +- src/cutils/utils_verify.c | 3 +- src/filters.c | 2 +- src/image/embedded/embedded_config_merge.c | 2 +- src/image/oci/oci_auth.c | 1 + src/image/oci/oci_config_merge.c | 7 +- src/image/oci/oci_image.c | 1 + src/image/oci/oci_rootfs_prepare.c | 52 +- src/image/oci/oci_rootfs_prepare.h | 4 +- src/json/schema/schema/docker/seccomp.json | 6 +- .../schema/oci/image/content-descriptor.json | 2 +- .../schema/oci/image/defs-descriptor.json | 2 +- src/json/schema/schema/oci/image/defs.json | 308 -------- src/json/schema/schema/oci/image/index.json | 2 +- src/json/schema/schema/oci/image/spec.json | 6 +- .../schema/oci/runtime/config-linux.json | 38 +- .../schema/schema/oci/runtime/defs-linux.json | 22 +- src/json/schema/schema/oci/runtime/defs.json | 1 - src/json/schema/schema/oci/runtime/pspec.json | 6 +- src/json/schema/schema/oci/runtime/spec.json | 32 +- src/json/schema/schema/oci/runtime/state.json | 4 +- src/json/schema/src/common_h.py | 1 - src/liblcrc.h | 2 +- src/liblcrd.h | 1 - src/pack_config.c | 8 +- src/path.c | 8 + src/plugin/plugin.c | 18 +- src/plugin/pspec.c | 1 + src/services/cri/cni_network_plugin.cc | 5 +- src/services/cri/network_plugin.cc | 7 +- src/services/cri/request_cache.h | 4 +- .../execution/execute/execution_create.c | 251 +++--- .../execution/execute/execution_extend.c | 2 +- .../execution/execute/execution_information.c | 18 +- src/services/execution/execute/list.c | 6 +- .../execution/manager/container_unix.c | 2 +- src/services/execution/manager/health_check.c | 5 +- src/services/execution/manager/restore.c | 4 +- src/services/execution/manager/restore.h | 2 +- src/services/execution/manager/supervisor.h | 2 +- src/services/execution/spec/specs.c | 55 +- src/services/execution/spec/specs_mount.c | 59 +- src/services/execution/spec/specs_mount.h | 3 +- src/services/execution/spec/sysinfo.c | 5 +- .../graphdriver/overlay2/driver_overlay2.c | 2 + src/tar/lcrdtar.h | 2 +- src/websocket/service/attach_serve.h | 4 + src/websocket/service/exec_serve.h | 4 + .../service/route_callback_register.h | 12 +- src/websocket/service/ws_server.cc | 1 + src/websocket/service/ws_server.h | 2 +- test/CMakeLists.txt | 3 +- test/cutils/CMakeLists.txt | 2 + test/cutils/utils_array/CMakeLists.txt | 27 + test/cutils/utils_array/utils_array_llt.cc | 235 ++++++ test/cutils/utils_convert/CMakeLists.txt | 26 + .../cutils/utils_convert/utils_convert_llt.cc | 242 ++++++ test/cutils/utils_string/CMakeLists.txt | 1 + test/cutils/utils_string/utils_string_llt.cc | 10 +- test/image/CMakeLists.txt | 3 + test/image/oci/CMakeLists.txt | 8 + .../image/oci/oci_config_merge/CMakeLists.txt | 58 ++ .../oci/oci_config_merge/imagetool_image.json | 51 ++ .../oci_config_merge/oci_config_merge_llt.cc | 479 +++++++++++ .../oci_config_merge/oci_runtime_spec.json | 741 ++++++++++++++++++ test/image/oci/oci_llt_common.cc | 73 ++ test/image/oci/oci_llt_common.h | 88 +++ .../oci/oci_rootfs_export/CMakeLists.txt | 60 ++ .../oci_rootfs_export_llt.cc | 180 +++++ .../image/oci/oci_rootfs_mount/CMakeLists.txt | 60 ++ .../oci_rootfs_mount/oci_rootfs_mount_llt.cc | 167 ++++ .../oci/oci_rootfs_prepare/CMakeLists.txt | 62 ++ .../oci_rootfs_prepare_llt.cc | 268 +++++++ .../oci_rootfs_prepare/prepare_response.json | 1 + .../oci/oci_rootfs_remove/CMakeLists.txt | 60 ++ .../oci_rootfs_remove_llt.cc | 173 ++++ .../oci/oci_rootfs_umount/CMakeLists.txt | 60 ++ .../oci_rootfs_umount_llt.cc | 162 ++++ test/include/mock.h | 10 +- test/llt.sh | 39 +- test/path/CMakeLists.txt | 26 + test/path/path_llt.cc | 372 +++++++++ tools/static_check | 4 +- 122 files changed, 4603 insertions(+), 851 deletions(-) create mode 100644 src/contrib/config/systemcontainer_config.json delete mode 100644 src/json/schema/schema/oci/image/defs.json delete mode 120000 src/json/schema/schema/oci/runtime/defs.json create mode 100644 test/cutils/utils_array/CMakeLists.txt create mode 100644 test/cutils/utils_array/utils_array_llt.cc create mode 100644 test/cutils/utils_convert/CMakeLists.txt create mode 100644 test/cutils/utils_convert/utils_convert_llt.cc create mode 100644 test/image/CMakeLists.txt create mode 100644 test/image/oci/CMakeLists.txt create mode 100644 test/image/oci/oci_config_merge/CMakeLists.txt create mode 100644 test/image/oci/oci_config_merge/imagetool_image.json create mode 100644 test/image/oci/oci_config_merge/oci_config_merge_llt.cc create mode 100644 test/image/oci/oci_config_merge/oci_runtime_spec.json create mode 100644 test/image/oci/oci_llt_common.cc create mode 100644 test/image/oci/oci_llt_common.h create mode 100644 test/image/oci/oci_rootfs_export/CMakeLists.txt create mode 100644 test/image/oci/oci_rootfs_export/oci_rootfs_export_llt.cc create mode 100644 test/image/oci/oci_rootfs_mount/CMakeLists.txt create mode 100644 test/image/oci/oci_rootfs_mount/oci_rootfs_mount_llt.cc create mode 100644 test/image/oci/oci_rootfs_prepare/CMakeLists.txt create mode 100644 test/image/oci/oci_rootfs_prepare/oci_rootfs_prepare_llt.cc create mode 100644 test/image/oci/oci_rootfs_prepare/prepare_response.json create mode 100644 test/image/oci/oci_rootfs_remove/CMakeLists.txt create mode 100644 test/image/oci/oci_rootfs_remove/oci_rootfs_remove_llt.cc create mode 100644 test/image/oci/oci_rootfs_umount/CMakeLists.txt create mode 100644 test/image/oci/oci_rootfs_umount/oci_rootfs_umount_llt.cc mode change 100755 => 100644 test/llt.sh create mode 100644 test/path/CMakeLists.txt create mode 100644 test/path/path_llt.cc mode change 100755 => 100644 tools/static_check diff --git a/CMakeLists.txt b/CMakeLists.txt index 25b9689..b0cc2cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,7 @@ OPTION(ENABLE_COVERAGE "coverage switch" OFF) IF(ENABLE_COVERAGE) MESSAGE(STATUS "Enable coverage compile option") SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage") - SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage") + SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage -fkeep-inline-functions -fkeep-static-functions") SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage -lgcov") ENDIF(ENABLE_COVERAGE) @@ -111,7 +111,7 @@ if (CMAKE_INSTALL_SYSCONFDIR) endif() install(FILES src/contrib/config/daemon.json DESTINATION ${conf_prefix}/isulad PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE) -install(FILES src/contrib/config/config.json +install(FILES src/contrib/config/config.json src/contrib/config/systemcontainer_config.json DESTINATION ${conf_prefix}/default/lcrd PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE) install(FILES src/contrib/config/seccomp_default.json DESTINATION ${conf_prefix}/isulad PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE) diff --git a/cmake/options.cmake b/cmake/options.cmake index c13c2f4..49183da 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -33,7 +33,7 @@ endif() option(VERSION "set lcrd version" ON) if (VERSION STREQUAL "ON") - set(LCRD_VERSION "1.0.31") + set(LCRD_VERSION "1.0.33") endif() option(DEBUG "set lcrd gcc option" ON) diff --git a/docs/install_guide.md b/docs/install_guide.md index f6934bc..177962c 100644 --- a/docs/install_guide.md +++ b/docs/install_guide.md @@ -7,10 +7,10 @@ This project depends on gRPC (need protobuf at least v3.1.0, gRPC at least v1.1. ### Initialization ```sh -$ # for ubuntu -$ sudo apt-get install unzip libtool automake autoconf g++ cmake curl zlib1g-dev libcap-dev libseccomp-dev libyajl-dev libsqlite3-dev libwebsockets-dev -$ # for centos/RTOS -$ sudo yum install gcc-c++ autoconf libtool unzip automake cmake curl zlib-devel libcap-devel libseccomp-devel yajl-devel sqlite-devel libwebsockets-devel +$ sudo yum install -y go gcc gcc-c++ autoconf libtool unzip automake cmake curl zlib-devel libcap-devel libseccomp-devel \ +$ yajl-devel sqlite-devel libwebsockets-devel openssl-devel c-ares-devel zlib-devel python3-devel python3-setuptools libsecurec-devel +$ sudo sh -c "echo /usr/local/lib >> /etc/ld.so.conf" +$ sudo sh -c 'echo "export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH" >> /etc/bashrc' ``` ### protobuf v3.5.0 @@ -19,8 +19,8 @@ Compile protobuf from source code: ```sh $ git clone http://dgggit09-rd.huawei.com/a/euleros/third_party/open_source/userspace/protobuf $ cd protobuf -$ git checkout -b next origin/next -$ tar -xf protobuf-3.5.0.tar.gz +$ git checkout -b open origin/next_openeuler +$ tar -xf v3.5.0.tar.gz $ cp googlemock-1.7.0.tar.gz googletest-1.7.0.tar.gz 0001-fix-build-on-s390x.patch protobuf-3.5.0 $ cd protobuf-3.5.0 $ tar -xf googlemock-1.7.0.tar.gz @@ -29,7 +29,7 @@ $ mv googlemock-release-1.7.0 gmock $ tar -xf googletest-1.7.0.tar.gz -C gmock $ mv gmock/googletest-release-1.7.0 gmock/gtest $ patch -p1 < 0001-fix-build-on-s390x.patch -$ ./autogen.sh # Because of internal network issue, we need to change curl to allow insecure connections (curl -k) +$ ./autogen.sh $ ./configure $ make -j $ sudo make install @@ -42,13 +42,13 @@ Compile the gRPC C Core library ```sh $ git clone http://dgggit09-rd.huawei.com/a/euleros/third_party/open_source/userspace/grpc $ cd grpc -$ tar xf grpc-1.17.1.tar.gz +$ git checkout -b open origin/next_openeuler +$ tar xf v1.17.1.tar.gz $ cd grpc-1.17.1 -$ git checkout -b next origin/next -$ patch -p1 < ../0001-Do-not-build-the-Ruby-plugin.patch $ patch -p1 < ../0001-enforce-system-crypto-policies.patch $ patch -p1 < ../0002-patch-from-15532.patch -$ patch -p1 < ../cxx-Arg-List-Too-Long.patch +$ patch -p1 < ../0003-Do-not-build-the-Ruby-plugin.patch +$ patch -p1 < ../0001-cxx-Arg-List-Too-Long.patch $ make -j $ sudo make install $ sudo ldconfig @@ -60,7 +60,7 @@ Compile clibcni from source code: ```sh $ git clone http://dgggit09-rd.huawei.com/a/euleros/self_src/userspace/clibcni $ cd clibcni -$ git checkout -b next_docker origin/next_docker +$ git checkout -b open origin/next_openeuler $ rm -rf build $ mkdir build && cd build $ cmake .. @@ -68,17 +68,6 @@ $ make -j $ sudo make install $ sudo ldconfig ``` -if enbale testcase -```sh -$ rm -rf build -$ mkdir build && cd build -$ cmake -DENABLE_TESTS=ON .. -$ make -j -$ sudo make install -$ sudo ldconfig -$ cd tests && ./cni_test -$ cd - -``` ### containernetworking plugins @@ -96,16 +85,10 @@ $ cp bin/* /opt/cni/bin/ Compile iSulad-kit from source code: ```sh +$ yum install -y gpgme-devel $ git clone http://dgggit09-rd.huawei.com/a/euleros/self_src/userspace/iSulad-kit -$ git clone http://dgggit09-rd.huawei.com/a/euleros/third_party/open_source/userspace/skopeo -$ cd skopeo -$ git checkout -b next_docker origin/next_docker -$ mkdir ./tmp -$ tar -zxf skopeo-e814f96.tar.gz --strip-components 1 -C ./tmp -$ cp -r ./tmp/vendor ../iSulad-kit/ -$ cd ../iSulad-kit -$ git checkout -b next_docker origin/next_docker -$ patch -p1 -F1 -s < ../skopeo/backport-update-vendor-to-e96a9b0e1b9019f9.patch +$ cd iSulad-kit +$ git checkout -b open origin/next_openeuler # apply the patchs $ cp ./patch/* ./ $ cat series-patch.conf | while read line @@ -116,7 +99,7 @@ $ cat series-patch.conf | while read line patch -p1 -F1 -s < $line done $ make -j -$ make install +$ sudo make install ``` ### LXC @@ -125,56 +108,34 @@ Compile lxc from source code: ```sh $ git clone http://dgggit09-rd.huawei.com/a/euleros/third_party/open_source/userspace/lxc $ cd lxc -$ git checkout -b next_docker origin/next_docker +$ git checkout -b open origin/next_openeuler $ tar xf lxc-3.0.3.tar.gz $ cd lxc-3.0.3 $ mv ../*.patch . # official patch $ for var in $(ls lxc-*.patch | sort -n) do - if [[ "$var" =~ "CVE-2019-5736" ]]; then - echo "ignoring CVE patch cause valgrind can not work" - continue - fi patch -p1 < ${var} done # self-developing patch -$ for var in $(ls huawei-*.patch | sort -n) +$ for var in $(ls *.patch | grep -v "^lxc-" | sort -n) do patch -p1 < ${var} done $ ./autogen.sh $ ./configure -$ make -j (If the GCC version on the system is greater than 7, please add CFLAGS="-Wno-error" option) +$ make -j # If the GCC version on the system is greater than 7, please add CFLAGS="-Wno-error" option $ sudo make install $ sudo ldconfig ``` -### huawei securec library - -Compile huawei securec library from source code: -```sh -$ git clone git@code-sh.huawei.com:containers/securec.git -$ cd securec -$ ./autogen.sh -$ ./configue -$ make -j $(nproc) -$ sudo make install -$ sudo ldconfig -``` ### LCR -Note: If you encounter an error like "You must install [project] >= [version]" during executing "./configure", -please export the environment variable -```sh -$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig -``` - Compile lcr from source code: ```sh $ git clone http://dgggit09-rd.huawei.com/a/euleros/self_src/userspace/lcr $ cd lcr -$ git checkout -b next_docker origin/next_docker +$ git checkout -b open origin/next_openeuler $ mkdir -p build $ cd build $ cmake ../ @@ -183,23 +144,17 @@ $ sudo make install $ sudo ldconfig ``` -## Build LCRD -Note: If you encounter an error like "not found libcurl" during executing "./configure" on the ubuntu system, -please execute the following command: -```sh -$ sudo apt-get install libcurl4-gnutls-dev -``` +## Build iSulad -In most cases, if we do not need to change the interface API(container.proto), just build the server and client like this: ```sh +$ yum install -y libcurl-devel http-parser-devel systemd-devel libevent-devel libevhtp-devel $ git clone http://dgggit09-rd.huawei.com/a/euleros/self_src/userspace/iSulad $ cd iSulad -$ git checkout -b next_docker origin/next_docker -$ rm -rf build -$ mkdir build && cd build -# To enable gRPC, configure lcrd by default +$ git checkout -b open origin/next_openeuler +$ mkdir -p build && cd build +# To enable gRPC, configure iSulad by default $ cmake ../ -$ make -j (If the GCC version on the system is greater than 7, please add CFLAGS="-Wno-error" option) +$ make -j # If the GCC version on the system is greater than 7, please add CFLAGS="-Wno-error" option $ sudo make install $ sudo ldconfig ``` @@ -207,35 +162,19 @@ $ sudo ldconfig ## Run ### Start daemon -Note: if you encounter an error like "error while loading shared libraries" when start the daemon , -please execute the following command: -```sh -$ sudo echo "/usr/local/lib" >> /etc/ld.so.conf -``` You should have built and installed lcrd and lcrc. To run the daemon: ```sh -$ sudo lcrd # run the lcrd server with default socket name and default log level and images manage function +$ sudo lcrd # run the iSulad server with default socket name and default log level and images manage function ``` -### Download rootfs - -To create a container, you should have downloaded rootfs to your platform like this: -```sh -$ mkdir $HOME/myrootfs -$ sudo lcr-pull --name ubuntu --rootfs $HOME/myrootfs --dist ubuntu -r xenial -a amd64 -``` -If lcrd started with the images manage function you can download images from registry (e.g., docker.io) - ### Operations on containers: ```sh $ sudo lcrc ps -a # list containers -# create a container 'ubuntu1' with the directory -$ sudo lcrc create -n ubuntu1 --external-rootfs $HOME/myrootfs/ none -# or, you can create a container with OverlayFS -$ sudo mkdir $HOME/upperdir/ # create the upperdir for OverlayFS -$ sudo lcrc create -n 'ubuntu1' --external-rootfs overlayfs:$HOME/myrootfs:$HOME/upperdir none -$ sudo lcrc start ubuntu1 # start the container 'ubuntu1' -$ sudo lcrc kill ubuntu1 # kill the container 'ubuntu1' +# create a container 'test' with image busybox +$ sudo lcrc create -t -n test busybox +$ sudo lcrc start test # start the container 'test' +$ sudo lcrc kill test # kill the container 'test' +$ sudo lcrc rm test # remove the container 'test' ``` diff --git a/iSulad.spec b/iSulad.spec index 11b9afc..94772ca 100644 --- a/iSulad.spec +++ b/iSulad.spec @@ -1,17 +1,17 @@ -%global _version 1.0.31 -%global _release 20190919.232053.gitf0f8c706 +%global _version 1.0.33 +%global _release 20190930.052413.gitd2956279 %global is_systemd 1 %global debug_package %{nil} Name: iSulad Version: %{_version} -Release: %{_release}%{?dist} +Release: %{_release} Summary: Lightweight Container Runtime Daemon License: Mulan PSL v1 -BuildRoot: {_tmppath}/%{name}-%{version} -ExclusiveArch: x86_64 aarch64 URL: http://code.huawei.com/containers/lcrd -Source: %{name}-1.0.tar.gz +Source: iSulad-1.0.tar.gz +BuildRoot: {_tmppath}/iSulad-%{version} +ExclusiveArch: x86_64 aarch64 %ifarch x86_64 aarch64 Provides: libhttpclient.so()(64bit) @@ -48,7 +48,7 @@ This is a umbrella project for gRPC-services based Lightweight Container Runtime Daemon, written by C. %prep -%autosetup -c -n %{name}-%{version} +%autosetup -c -n iSulad-%{version} %build mkdir -p build @@ -84,7 +84,8 @@ install -m 0640 ../src/contrib/config/seccomp_default.json %{buildroot}/%{_sysc install -d $RPM_BUILD_ROOT/%{_sysconfdir}/default/lcrd install -m 0640 ../src/contrib/config/config.json %{buildroot}/%{_sysconfdir}/default/lcrd/config.json -install -m 0550 ../src/contrib/sysmonitor/isulad-check.sh %{buildroot}/%{_sysconfdir}/default/lcrd/isulad-check.sh +install -m 0640 ../src/contrib/config/systemcontainer_config.json %{buildroot}/%{_sysconfdir}/default/lcrd/systemcontainer_config.json +install -m 0550 ../src/contrib/sysmonitor/isulad-check.sh %{buildroot}/%{_sysconfdir}/default/lcrd/isulad-check.sh mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/sysmonitor/process cp ../src/contrib/sysmonitor/isulad-monit $RPM_BUILD_ROOT/etc/sysmonitor/process @@ -159,6 +160,7 @@ fi %attr(0600,root,root) %{_sysconfdir}/sysmonitor/process/isulad-monit %attr(0550,root,root) %{_sysconfdir}/default/lcrd/isulad-check.sh %defattr(0640,root,root,0750) +%{_sysconfdir}/isulad %{_sysconfdir}/isulad/* %{_sysconfdir}/default/* %defattr(-,root,root,-) diff --git a/src/cmd/commander.c b/src/cmd/commander.c index 059ffcb..c3b8c08 100644 --- a/src/cmd/commander.c +++ b/src/cmd/commander.c @@ -17,7 +17,6 @@ #include "commander.h" #include -#include #include #include #include @@ -299,10 +298,6 @@ static int command_parse_short_arg(command_t *self, const char *arg) do { found = false; - if (opt_arg[0] == 'h' && have_short_options(self, 'h') < 0) { - command_help(self); - exit(0); - } if (command_parse_options(self, &opt_arg, &found)) { return -1; } @@ -459,7 +454,7 @@ static void get_default_ulimit_split_parts(const char *val, char ***parts, size_ ERROR("Out of memory"); return; } - *parts_len = util_array_len(*parts); + *parts_len = util_array_len((const char **)(*parts)); } static int parse_soft_hard_default_ulimit(const char *val, char **limitvals, size_t limitvals_len, int64_t *soft, diff --git a/src/cmd/lcrc/base/create.c b/src/cmd/lcrc/base/create.c index 2e3657c..c2dba48 100644 --- a/src/cmd/lcrc/base/create.c +++ b/src/cmd/lcrc/base/create.c @@ -76,7 +76,7 @@ static int request_pack_host_config_storage_opts(const struct client_arguments * return -1; } - len = util_array_len(args->custom_conf.storage_opts); + len = util_array_len((const char **)(args->custom_conf.storage_opts)); for (i = 0; i < len; i++) { char *p = NULL; p = strchr(args->custom_conf.storage_opts[i], '='); @@ -111,7 +111,7 @@ static int request_pack_host_config_sysctls(const struct client_arguments *args, return -1; } - len = util_array_len(args->custom_conf.sysctls); + len = util_array_len((const char **)(args->custom_conf.sysctls)); for (i = 0; i < len; i++) { char *p = NULL; p = strchr(args->custom_conf.sysctls[i], '='); @@ -232,7 +232,7 @@ static int request_pack_custom_env(struct client_arguments *args, lcrc_container if (args->custom_conf.env != NULL) { conf->env = args->custom_conf.env; - conf->env_len = util_array_len(conf->env); + conf->env_len = util_array_len((const char **)(conf->env)); } if (args->custom_conf.accel != NULL) { @@ -246,10 +246,10 @@ static int request_pack_custom_env(struct client_arguments *args, lcrc_container } args->custom_conf.env = conf->env; /* make sure args->custom_conf.env point to valid memory. */ - conf->env_len = util_array_len(conf->env); + conf->env_len = util_array_len((const char **)(conf->env)); conf->accel = args->custom_conf.accel; - conf->accel_len = util_array_len(args->custom_conf.accel); + conf->accel_len = util_array_len((const char **)(args->custom_conf.accel)); if (util_env_set_lcrd_enable_plugins(&conf->env, &conf->env_len, LCRD_ISULA_ADAPTER)) { COMMAND_ERROR("init accel env failed"); ret = -1; @@ -308,7 +308,7 @@ static void request_pack_custom_system_container(const struct client_arguments * static void request_pack_custom_mounts(const struct client_arguments *args, lcrc_container_config_t *conf) { if (args->custom_conf.mounts != NULL) { - conf->mounts_len = util_array_len(args->custom_conf.mounts); + conf->mounts_len = util_array_len((const char **)(args->custom_conf.mounts)); conf->mounts = args->custom_conf.mounts; } return; @@ -535,12 +535,12 @@ static void request_pack_host_caps(const struct client_arguments *args, lcrc_hos { /* cap add */ if (args->custom_conf.cap_adds != NULL) { - hostconfig->cap_add_len = util_array_len(args->custom_conf.cap_adds); + hostconfig->cap_add_len = util_array_len((const char **)(args->custom_conf.cap_adds)); hostconfig->cap_add = args->custom_conf.cap_adds; } /* cap drop */ if (args->custom_conf.cap_drops != NULL) { - hostconfig->cap_drop_len = util_array_len(args->custom_conf.cap_drops); + hostconfig->cap_drop_len = util_array_len((const char **)(args->custom_conf.cap_drops)); hostconfig->cap_drop = args->custom_conf.cap_drops; } } @@ -549,7 +549,7 @@ static void request_pack_host_group_add(const struct client_arguments *args, lcr { /* group add */ if (args->custom_conf.group_add != NULL) { - hostconfig->group_add_len = util_array_len(args->custom_conf.group_add); + hostconfig->group_add_len = util_array_len((const char **)(args->custom_conf.group_add)); hostconfig->group_add = args->custom_conf.group_add; } } @@ -558,7 +558,7 @@ static void request_pack_host_extra_hosts(const struct client_arguments *args, l { /* extra hosts */ if (args->custom_conf.extra_hosts != NULL) { - hostconfig->extra_hosts_len = util_array_len(args->custom_conf.extra_hosts); + hostconfig->extra_hosts_len = util_array_len((const char **)(args->custom_conf.extra_hosts)); hostconfig->extra_hosts = args->custom_conf.extra_hosts; } } @@ -567,19 +567,19 @@ static void request_pack_host_dns(const struct client_arguments *args, lcrc_host { /* dns */ if (args->custom_conf.dns != NULL) { - hostconfig->dns_len = util_array_len(args->custom_conf.dns); + hostconfig->dns_len = util_array_len((const char **)(args->custom_conf.dns)); hostconfig->dns = args->custom_conf.dns; } /* dns options */ if (args->custom_conf.dns_options != NULL) { - hostconfig->dns_options_len = util_array_len(args->custom_conf.dns_options); + hostconfig->dns_options_len = util_array_len((const char **)(args->custom_conf.dns_options)); hostconfig->dns_options = args->custom_conf.dns_options; } /* dns search */ if (args->custom_conf.dns_search != NULL) { - hostconfig->dns_search_len = util_array_len(args->custom_conf.dns_search); + hostconfig->dns_search_len = util_array_len((const char **)(args->custom_conf.dns_search)); hostconfig->dns_search = args->custom_conf.dns_search; } } @@ -588,7 +588,7 @@ static void request_pack_host_ulimit(const struct client_arguments *args, lcrc_h { /* ulimit options */ if (args->custom_conf.ulimits != NULL) { - hostconfig->ulimits_len = util_array_len(args->custom_conf.ulimits); + hostconfig->ulimits_len = util_array_len((const char **)(args->custom_conf.ulimits)); hostconfig->ulimits = args->custom_conf.ulimits; } } @@ -597,7 +597,7 @@ static void request_pack_host_weight_devices(const struct client_arguments *args { /* blkio weight devices */ if (args->custom_conf.weight_devices != NULL) { - hostconfig->blkio_weight_device_len = util_array_len(args->custom_conf.weight_devices); + hostconfig->blkio_weight_device_len = util_array_len((const char **)(args->custom_conf.weight_devices)); hostconfig->blkio_weight_device = args->custom_conf.weight_devices; } } @@ -606,7 +606,7 @@ static void request_pack_host_device_read_bps(const struct client_arguments *arg { if (args->custom_conf.blkio_throttle_read_bps_device != NULL) { hostconfig->blkio_throttle_read_bps_device_len = - util_array_len(args->custom_conf.blkio_throttle_read_bps_device); + util_array_len((const char **)(args->custom_conf.blkio_throttle_read_bps_device)); hostconfig->blkio_throttle_read_bps_device = args->custom_conf.blkio_throttle_read_bps_device; } } @@ -615,7 +615,7 @@ static void request_pack_host_device_write_bps(const struct client_arguments *ar { if (args->custom_conf.blkio_throttle_write_bps_device != NULL) { hostconfig->blkio_throttle_write_bps_device_len = - util_array_len(args->custom_conf.blkio_throttle_write_bps_device); + util_array_len((const char **)(args->custom_conf.blkio_throttle_write_bps_device)); hostconfig->blkio_throttle_write_bps_device = args->custom_conf.blkio_throttle_write_bps_device; } } @@ -631,7 +631,7 @@ static void request_pack_host_devices(const struct client_arguments *args, lcrc_ { /* devices */ if (args->custom_conf.devices != NULL) { - hostconfig->devices_len = util_array_len(args->custom_conf.devices); + hostconfig->devices_len = util_array_len((const char **)(args->custom_conf.devices)); hostconfig->devices = args->custom_conf.devices; } } @@ -640,7 +640,7 @@ static void request_pack_host_hugepage_limits(const struct client_arguments *arg { /* hugepage limits*/ if (args->custom_conf.hugepage_limits != NULL) { - hostconfig->hugetlbs_len = util_array_len(args->custom_conf.hugepage_limits); + hostconfig->hugetlbs_len = util_array_len((const char **)(args->custom_conf.hugepage_limits)); hostconfig->hugetlbs = args->custom_conf.hugepage_limits; } } @@ -649,7 +649,7 @@ static void request_pack_host_binds(const struct client_arguments *args, lcrc_ho { /* volumes to binds */ if (args->custom_conf.volumes != NULL) { - hostconfig->binds_len = (size_t)util_array_len(args->custom_conf.volumes); + hostconfig->binds_len = (size_t)util_array_len((const char **)(args->custom_conf.volumes)); hostconfig->binds = args->custom_conf.volumes; } } @@ -696,7 +696,7 @@ static void request_pack_host_security(const struct client_arguments *args, lcrc { /* security opt */ if (args->custom_conf.security != NULL) { - hostconfig->security_len = util_array_len(args->custom_conf.security); + hostconfig->security_len = util_array_len((const char **)(args->custom_conf.security)); hostconfig->security = args->custom_conf.security; } } @@ -1162,7 +1162,7 @@ static bool check_devices_conf_valid(const char *devices) ret = false; goto out; } - tmp_str_len = util_array_len(tmp_str); + tmp_str_len = util_array_len((const char **)tmp_str); // device format: x:x:x or x:x or x switch (tmp_str_len) { @@ -1217,7 +1217,7 @@ static bool check_volumes_valid(const char *volume) ret = false; goto free_out; } - alen = util_array_len(array); + alen = util_array_len((const char **)array); // volume format: src:dst:mode switch (alen) { @@ -1453,7 +1453,7 @@ static int parse_mounts_conf(const char *mount, struct valid_mounts_state *state goto out; } - items_len = util_array_len(items); + items_len = util_array_len((const char **)items); for (i = 0; i < items_len; i++) { key_val = util_string_split(items[i], '='); @@ -1621,7 +1621,7 @@ static int create_check_hugetlbs(const struct client_arguments *args) int ret = 0; size_t len, i; - len = util_array_len(args->custom_conf.hugepage_limits); + len = util_array_len((const char **)(args->custom_conf.hugepage_limits)); for (i = 0; i < len; i++) { char *limit = NULL; int64_t limitvalue; @@ -1666,7 +1666,7 @@ static int create_check_network(const struct client_arguments *args) size_t len, i; struct sockaddr_in sa; - len = util_array_len(args->custom_conf.extra_hosts); + len = util_array_len((const char **)(args->custom_conf.extra_hosts)); for (i = 0; i < len; i++) { char **items = NULL; items = util_string_split(args->custom_conf.extra_hosts[i], ':'); @@ -1674,7 +1674,7 @@ static int create_check_network(const struct client_arguments *args) COMMAND_ERROR("split extra hosts '%s' failed.", args->custom_conf.extra_hosts[i]); return -1; } - if (util_array_len(items) != 2) { + if (util_array_len((const char **)items) != 2) { util_free_array(items); COMMAND_ERROR("Invalid extra hosts specification '%s'. unsupported format", args->custom_conf.extra_hosts[i]); @@ -1687,7 +1687,7 @@ static int create_check_network(const struct client_arguments *args) } util_free_array(items); } - len = util_array_len(args->custom_conf.dns); + len = util_array_len((const char **)(args->custom_conf.dns)); for (i = 0; i < len; i++) { if (!inet_pton(AF_INET, args->custom_conf.dns[i], &sa.sin_addr)) { COMMAND_ERROR("Invalid dns ip address '%s'.", args->custom_conf.dns[i]); @@ -1733,21 +1733,21 @@ static int create_devices_volumes_checker(const struct client_arguments *args) size_t i; size_t len = 0; - len = util_array_len(args->custom_conf.devices); + len = util_array_len((const char **)(args->custom_conf.devices)); for (i = 0; i < len; i++) { if (!check_devices_conf_valid(args->custom_conf.devices[i])) { ret = -1; goto out; } } - len = util_array_len(args->custom_conf.volumes); + len = util_array_len((const char **)(args->custom_conf.volumes)); for (i = 0; i < len; i++) { if (!check_volumes_conf_valid(args->custom_conf.volumes[i])) { ret = -1; goto out; } } - len = util_array_len(args->custom_conf.mounts); + len = util_array_len((const char **)(args->custom_conf.mounts)); for (i = 0; i < len; i++) { if (!check_mounts_conf_valid(args->custom_conf.mounts[i])) { ret = -1; @@ -1812,7 +1812,7 @@ static int create_check_nschangeopt(const struct client_arguments *args) ERROR("Out of memory"); return EINVALIDARGS; } - array_str_len = util_array_len(array_str); + array_str_len = util_array_len((const char **)array_str); if (array_str_len != 1 && array_str_len != 2) { ERROR("invalid ns-change-opt pararm:%s\n", args->custom_conf.ns_change_opt); util_free_array(array_str); @@ -1886,7 +1886,7 @@ static int create_check_sysctl(const struct client_arguments *args) return 0; } - len = util_array_len(args->custom_conf.sysctls); + len = util_array_len((const char **)(args->custom_conf.sysctls)); for (i = 0; i < len; i++) { if (!do_create_check_sysctl((const char *)args->custom_conf.sysctls[i])) { return -1; diff --git a/src/cmd/lcrc/base/rm.c b/src/cmd/lcrc/base/rm.c index c64464d..01e1ce3 100644 --- a/src/cmd/lcrc/base/rm.c +++ b/src/cmd/lcrc/base/rm.c @@ -62,8 +62,10 @@ static int client_delete(const struct client_arguments *args) } if (response->name != NULL) { + free(g_cmd_delete_args.name); g_cmd_delete_args.name = util_strdup_s(response->name); } + out: lcrc_delete_response_free(response); return ret; @@ -141,7 +143,8 @@ int cmd_delete_main(int argc, const char **argv) } for (i = 0; i < g_cmd_delete_args.argc; i++) { - g_cmd_delete_args.name = g_cmd_delete_args.argv[i]; + free(g_cmd_delete_args.name); + g_cmd_delete_args.name = util_strdup_s(g_cmd_delete_args.argv[i]); if (client_delete(&g_cmd_delete_args)) { ERROR("Container \"%s\" rm failed", g_cmd_delete_args.name); status = true; @@ -156,3 +159,4 @@ int cmd_delete_main(int argc, const char **argv) } exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/commands.c b/src/cmd/lcrc/commands.c index e81730b..85cfde0 100644 --- a/src/cmd/lcrc/commands.c +++ b/src/cmd/lcrc/commands.c @@ -174,8 +174,8 @@ int run_command(struct command *commands, int argc, const char **argv) return command_default_help(argv[0], commands, argc - 1, (const char **)(argv + 1)); } - if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { - // lcrc help command format: lcrc [-h|help|--help] args + if (strcmp(argv[1], "--help") == 0) { + // lcrc help command format: lcrc --help args return command_default_help(argv[0], commands, argc - 2, (const char **)(argv + 2)); } @@ -191,7 +191,7 @@ int run_command(struct command *commands, int argc, const char **argv) } printf("%s: command \"%s\" not found\n", argv[0], argv[1]); - printf("run `%s --help` or `run -h` for a list of sub-commands\n", argv[0]); + printf("run `%s --help` for a list of sub-commands\n", argv[0]); return 1; } @@ -430,3 +430,4 @@ int start_client_console_thread(struct command_fifo_config *console_fifos, bool return 0; } + diff --git a/src/cmd/lcrc/images/images.c b/src/cmd/lcrc/images/images.c index 6b81832..89739e7 100644 --- a/src/cmd/lcrc/images/images.c +++ b/src/cmd/lcrc/images/images.c @@ -15,7 +15,6 @@ #include "images.h" #include -#include #include #include #include @@ -278,3 +277,4 @@ int cmd_images_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/images/load.c b/src/cmd/lcrc/images/load.c index fa913f9..1118a82 100644 --- a/src/cmd/lcrc/images/load.c +++ b/src/cmd/lcrc/images/load.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "load.h" #include -#include #include #include #include @@ -172,3 +171,4 @@ int cmd_load_main(int argc, const char **argv) printf("Load image from \"%s\" success\n", g_cmd_load_args.file); exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/images/login.c b/src/cmd/lcrc/images/login.c index 5f82f89..5b02317 100644 --- a/src/cmd/lcrc/images/login.c +++ b/src/cmd/lcrc/images/login.c @@ -15,7 +15,6 @@ #include "login.h" #include -#include #include #include #include @@ -95,7 +94,7 @@ static int get_password_from_notty(struct client_arguments *args) // Try get password from notty input. if (g_cmd_login_args.password_stdin) { char password[LOGIN_PASSWORD_LEN + 1] = { 0 }; - int n = util_input_notty(password, sizeof(password)); + int n = util_input_readall(password, sizeof(password)); if (n == 0) { COMMAND_ERROR("Error: Password Required"); return -1; @@ -230,3 +229,4 @@ int cmd_login_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/images/logout.c b/src/cmd/lcrc/images/logout.c index d09b239..56b86f2 100644 --- a/src/cmd/lcrc/images/logout.c +++ b/src/cmd/lcrc/images/logout.c @@ -15,7 +15,6 @@ #include "logout.h" #include -#include #include #include #include @@ -113,3 +112,4 @@ int cmd_logout_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/images/pull.c b/src/cmd/lcrc/images/pull.c index 8eca845..fe9153d 100644 --- a/src/cmd/lcrc/images/pull.c +++ b/src/cmd/lcrc/images/pull.c @@ -15,7 +15,6 @@ #include "pull.h" #include -#include #include #include #include @@ -112,3 +111,4 @@ int cmd_pull_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/images/rmi.c b/src/cmd/lcrc/images/rmi.c index a9272af..d95b17c 100644 --- a/src/cmd/lcrc/images/rmi.c +++ b/src/cmd/lcrc/images/rmi.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "rmi.h" #include -#include #include #include #include @@ -130,3 +129,4 @@ int cmd_rmi_main(int argc, const char **argv) } exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/information/info.c b/src/cmd/lcrc/information/info.c index b99b3e8..7503442 100644 --- a/src/cmd/lcrc/information/info.c +++ b/src/cmd/lcrc/information/info.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "info.h" #include -#include #include "securec.h" #include "utils.h" @@ -148,3 +147,4 @@ int cmd_info_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/information/ps.c b/src/cmd/lcrc/information/ps.c index faeb2d6..224f432 100644 --- a/src/cmd/lcrc/information/ps.c +++ b/src/cmd/lcrc/information/ps.c @@ -339,7 +339,8 @@ static int client_list(const struct client_arguments *args) } if (args->filters != NULL) { - request.filters = lcrc_filters_parse_args((const char **)args->filters, util_array_len(args->filters)); + request.filters = lcrc_filters_parse_args((const char **)args->filters, + util_array_len((const char **)(args->filters))); if (!request.filters) { ERROR("Failed to parse filters args"); ret = -1; @@ -409,3 +410,4 @@ int cmd_list_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/information/version.c b/src/cmd/lcrc/information/version.c index 03581ac..78f56cf 100644 --- a/src/cmd/lcrc/information/version.c +++ b/src/cmd/lcrc/information/version.c @@ -14,7 +14,6 @@ ******************************************************************************/ #include "version.h" #include -#include #include "securec.h" #include "utils.h" @@ -122,3 +121,4 @@ int cmd_version_main(int argc, const char **argv) exit(EXIT_SUCCESS); } + diff --git a/src/cmd/lcrc/stream/exec.c b/src/cmd/lcrc/stream/exec.c index a78d58c..29ea7ea 100644 --- a/src/cmd/lcrc/stream/exec.c +++ b/src/cmd/lcrc/stream/exec.c @@ -68,7 +68,7 @@ static int client_exec(const struct client_arguments *args, const struct command request.argv = (char **)args->argv; /* environment variables */ - request.env_len = util_array_len(args->extra_env); + request.env_len = util_array_len((const char **)(args->extra_env)); request.env = args->extra_env; ops = get_connect_client_ops(); @@ -258,7 +258,7 @@ static int remote_cmd_exec(const struct client_arguments *args, uint32_t *exit_c request.argv = (char **)args->argv; /* environment variables */ - request.env_len = util_array_len(args->extra_env); + request.env_len = util_array_len((const char **)(args->extra_env)); request.env = args->extra_env; if (remote_cmd_exec_setup_tty(args, &reset_tty, &oldtios) < 0) { @@ -356,3 +356,4 @@ int cmd_exec_main(int argc, const char **argv) out: exit(exit_code ? (int)exit_code : ret); } + diff --git a/src/cmd/lcrd/commands.c b/src/cmd/lcrd/commands.c index 9f8adee..4a8f8d4 100644 --- a/src/cmd/lcrd/commands.c +++ b/src/cmd/lcrd/commands.c @@ -237,10 +237,10 @@ int check_args(struct service_arguments *args) goto out; } - args->hosts_len = util_array_len(args->hosts); - args->json_confs->storage_opts_len = util_array_len(args->json_confs->storage_opts); - args->json_confs->registry_mirrors_len = util_array_len(args->json_confs->registry_mirrors); - args->json_confs->insecure_registries_len = util_array_len(args->json_confs->insecure_registries); + args->hosts_len = util_array_len((const char **)(args->hosts)); + args->json_confs->storage_opts_len = util_array_len((const char **)(args->json_confs->storage_opts)); + args->json_confs->registry_mirrors_len = util_array_len((const char **)(args->json_confs->registry_mirrors)); + args->json_confs->insecure_registries_len = util_array_len((const char **)(args->json_confs->insecure_registries)); /* validate log-file-mode */ if (check_args_log_conf(args) != 0) { @@ -522,7 +522,7 @@ out: int update_hosts(struct service_arguments *args) { - args->hosts_len = util_array_len(args->hosts); + args->hosts_len = util_array_len((const char **)(args->hosts)); if (check_hosts_specified_conflict(args) != 0) { return -1; diff --git a/src/cmd/lcrd/main.c b/src/cmd/lcrd/main.c index d6d8b51..3defc8d 100644 --- a/src/cmd/lcrd/main.c +++ b/src/cmd/lcrd/main.c @@ -388,7 +388,7 @@ static void send_dump_req(void) ERROR("Failed to read /proc/self/task' subdirectory"); goto out; } - subdir_num = util_array_len(subdir); + subdir_num = util_array_len((const char **)subdir); if (subdir_num == 0) { goto out; } @@ -1700,3 +1700,4 @@ failure: DAEMON_CLEAR_ERRMSG(); exit(1); } + diff --git a/src/connect/client/grpc/grpc_client.h b/src/connect/client/grpc/grpc_client.h index 874a82b..0d6c33e 100644 --- a/src/connect/client/grpc/grpc_client.h +++ b/src/connect/client/grpc/grpc_client.h @@ -12,7 +12,7 @@ * Create: 2018-11-08 * Description: provide grpc client definition ******************************************************************************/ -#ifndef __GPRC_CLIENT_H +#ifndef __GRPC_CLIENT_H #define __GRPC_CLIENT_H #include "lcrc_connect.h" diff --git a/src/connect/client/grpc/grpc_containers_client.h b/src/connect/client/grpc/grpc_containers_client.h index 0110bc7..547654e 100644 --- a/src/connect/client/grpc/grpc_containers_client.h +++ b/src/connect/client/grpc/grpc_containers_client.h @@ -12,7 +12,7 @@ * Create: 2018-11-08 * Description: provide container grpc client definition ******************************************************************************/ -#ifndef __GPRC_CONTAINERS_CLIENT_H +#ifndef __GRPC_CONTAINERS_CLIENT_H #define __GRPC_CONTAINERS_CLIENT_H #include "lcrc_connect.h" diff --git a/src/connect/client/grpc/grpc_images_client.h b/src/connect/client/grpc/grpc_images_client.h index 594524c..4fc88a6 100644 --- a/src/connect/client/grpc/grpc_images_client.h +++ b/src/connect/client/grpc/grpc_images_client.h @@ -12,7 +12,7 @@ * Create: 2018-11-08 * Description: provide grpc image client definition ******************************************************************************/ -#ifndef __GPRC_IMAGES_CLIENT_H +#ifndef __GRPC_IMAGES_CLIENT_H #define __GRPC_IMAGES_CLIENT_H #include "lcrc_connect.h" diff --git a/src/connect/client/rest/rest_containers_client.c b/src/connect/client/rest/rest_containers_client.c index 1cc9ab1..8b82f02 100644 --- a/src/connect/client/rest/rest_containers_client.c +++ b/src/connect/client/rest/rest_containers_client.c @@ -1883,3 +1883,4 @@ int rest_containers_client_ops_init(lcrc_connect_ops *ops) return 0; } + diff --git a/src/connect/client/rest/rest_images_client.c b/src/connect/client/rest/rest_images_client.c index c6725fb..3ae0862 100644 --- a/src/connect/client/rest/rest_images_client.c +++ b/src/connect/client/rest/rest_images_client.c @@ -474,3 +474,4 @@ int rest_images_client_ops_init(lcrc_connect_ops *ops) return 0; } + diff --git a/src/connect/service/grpc/grpc_containers_service.h b/src/connect/service/grpc/grpc_containers_service.h index 3a06655..727239b 100644 --- a/src/connect/service/grpc/grpc_containers_service.h +++ b/src/connect/service/grpc/grpc_containers_service.h @@ -101,7 +101,7 @@ public: ServerWriter *writer) override; Status CopyToContainer(ServerContext *context, ServerReaderWriter *stream); + CopyToContainerRequest> *stream) override; Status Logs(ServerContext *context, const LogsRequest* request, ServerWriter* writer) override; diff --git a/src/connect/service/rest/rest_containers_service.c b/src/connect/service/rest/rest_containers_service.c index a565e49..c3a259d 100644 --- a/src/connect/service/rest/rest_containers_service.c +++ b/src/connect/service/rest/rest_containers_service.c @@ -1225,3 +1225,4 @@ int rest_register_containers_handler(evhtp_t *htp) } return 0; } + diff --git a/src/connect/service/rest/rest_images_service.c b/src/connect/service/rest/rest_images_service.c index fc24cb8..55bb7e5 100644 --- a/src/connect/service/rest/rest_images_service.c +++ b/src/connect/service/rest/rest_images_service.c @@ -444,3 +444,4 @@ int rest_register_images_handler(evhtp_t *htp) return 0; } + diff --git a/src/container_def.c b/src/container_def.c index 7fc497a..0111e95 100644 --- a/src/container_def.c +++ b/src/container_def.c @@ -13,6 +13,7 @@ * Description: provide container definition ******************************************************************************/ #include +#include #include "container_def.h" @@ -30,3 +31,4 @@ void container_cgroup_resources_free(container_cgroup_resources_t *cr) free(cr); } + diff --git a/src/container_def.h b/src/container_def.h index 95ecd6f..b167ada 100644 --- a/src/container_def.h +++ b/src/container_def.h @@ -16,7 +16,6 @@ #define __CONTAINER_DEF_H_ #include -#include #include #include "types_def.h" @@ -34,6 +33,9 @@ extern "C" { #ifndef OCICONFIG_PATH #define OCICONFIG_PATH "/etc/default/lcrd/config.json" #endif +#ifndef OCI_SYSTEM_CONTAINER_CONFIG_PATH +#define OCI_SYSTEM_CONTAINER_CONFIG_PATH "/etc/default/lcrd/systemcontainer_config.json" +#endif #ifndef SECCOMP_DEFAULT_PATH #define SECCOMP_DEFAULT_PATH "/etc/isulad/seccomp_default.json" #endif @@ -137,3 +139,4 @@ typedef void (*container_events_callback_t)(const container_events_format_t *eve #endif #endif + diff --git a/src/contrib/config/config.json b/src/contrib/config/config.json index b20495e..351d082 100644 --- a/src/contrib/config/config.json +++ b/src/contrib/config/config.json @@ -253,26 +253,27 @@ "maskedPaths": [ "/proc/acpi", "/proc/config.gz", + "/proc/cpuirqstat", + "/proc/fdenable", + "/proc/fdstat", + "/proc/fdthreshold", + "/proc/files_panic_enable", + "/proc/iomem_ext", + "/proc/kbox", "/proc/kcore", "/proc/keys", "/proc/latency_stats", - "/proc/timer_list", - "/proc/timer_stats", + "/proc/livepatch", + "/proc/memstat", + "/proc/net_namespace", + "/proc/oom_extend", "/proc/sched_debug", "/proc/scsi", - "/proc/signo", "/proc/sig_catch", - "/proc/kbox", - "/proc/oom_extend", - "/proc/fdthreshold", - "/proc/fdstat", - "/proc/fdenable", - "/proc/files_panic_enable", - "/sys/firmware", - "/proc/cpuirqstat", - "/proc/memstat", - "/proc/iomem_ext", - "/proc/livepatch" + "/proc/signo", + "/proc/timer_list", + "/proc/timer_stats", + "/sys/firmware" ], "readonlyPaths": [ "/proc/asound", diff --git a/src/contrib/config/daemon.json b/src/contrib/config/daemon.json index 392b6d8..0be3db6 100644 --- a/src/contrib/config/daemon.json +++ b/src/contrib/config/daemon.json @@ -22,7 +22,6 @@ "docker.io" ], "insecure-registries": [ - "rnd-dockerhub.huawei.com" ], "pod-sandbox-image": "", "image-opt-timeout": "5m", diff --git a/src/contrib/config/systemcontainer_config.json b/src/contrib/config/systemcontainer_config.json new file mode 100644 index 0000000..229929b --- /dev/null +++ b/src/contrib/config/systemcontainer_config.json @@ -0,0 +1,321 @@ +{ + "ociVersion": "1.0.0-rc5-dev", + "process": { + "terminal": true, + "consoleSize": { + "height": 0, + "width": 0 + }, + "user": { + "uid": 0, + "gid": 0 + }, + "args": [ + "/bin/bash" + ], + "env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "TERM=xterm" + ], + "cwd": "/", + "capabilities": { + "bounding": [ + "CAP_CHOWN", + "CAP_DAC_OVERRIDE", + "CAP_FSETID", + "CAP_FOWNER", + "CAP_MKNOD", + "CAP_NET_RAW", + "CAP_SETGID", + "CAP_SETUID", + "CAP_SETFCAP", + "CAP_SETPCAP", + "CAP_NET_BIND_SERVICE", + "CAP_SYS_CHROOT", + "CAP_KILL", + "CAP_AUDIT_WRITE" + ], + "effective": [ + ], + "inheritable": [ + ], + "permitted": [ + ], + "ambient": [ + ] + } + }, + "root": { + "path": "rootfs", + "readonly": false + }, + "hostname": "ubuntu", + "mounts": [ + { + "destination": "/proc", + "type": "proc", + "source": "proc", + "options": [ + "nosuid", + "noexec", + "nodev" + ] + }, + { + "destination": "/dev", + "type": "tmpfs", + "source": "tmpfs", + "options": [ + "nosuid", + "strictatime", + "mode=755", + "size=65536k" + ] + }, + { + "destination": "/dev/pts", + "type": "devpts", + "source": "devpts", + "options": [ + "nosuid", + "noexec", + "newinstance", + "ptmxmode=0666", + "mode=0620", + "gid=5" + ] + }, + { + "destination": "/sys", + "type": "sysfs", + "source": "sysfs", + "options": [ + "nosuid", + "noexec", + "nodev", + "ro" + ] + }, + { + "destination": "/dev/shm", + "type": "tmpfs", + "source": "shm", + "options": [ + "nosuid", + "noexec", + "nodev", + "mode=1777", + "size=65536k" + ] + }, + { + "destination": "/sys/fs/cgroup", + "type": "cgroup", + "source": "cgroup", + "options": [ + "nosuid", + "noexec", + "nodev", + "ro" + ] + }, + { + "destination": "/dev/mqueue", + "type": "mqueue", + "source": "mqueue", + "options": [ + "nosuid", + "noexec", + "nodev" + ] + }, + { + "destination": "/run", + "type": "tmpfs", + "source": "tmpfs", + "options": [ + "nosuid", + "noexec", + "nodev", + "mode=755" + ] + }, + { + "destination": "/run/lock", + "type": "tmpfs", + "source": "tmpfs", + "options": [ + "nosuid", + "noexec", + "nodev", + "mode=755" + ] + }, + { + "destination": "/tmp", + "type": "tmpfs", + "source": "tmpfs", + "options": [ + "nosuid", + "noexec", + "nodev", + "mode=1777" + ] + } + ], + "linux": { + "resources": { + "devices": [ + { + "allow": false, + "type": "a", + "major": -1, + "minor": -1, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": -1, + "minor": -1, + "access": "m" + }, + { + "allow": true, + "type": "b", + "major": -1, + "minor": -1, + "access": "m" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 3, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 5, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 7, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 0, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 1, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 2, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 8, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 9, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 136, + "minor": -1, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 10, + "minor": 200, + "access": "rwm" + }, + { + "allow": false, + "type": "c", + "major": 10, + "minor": 229, + "access": "rwm" + } + ] + }, + "namespaces": [ + { + "type": "pid" + }, + { + "type": "network" + }, + { + "type": "ipc" + }, + { + "type": "uts" + }, + { + "type": "mount" + } + ], + "maskedPaths": [ + "/proc/acpi", + "/proc/config.gz", + "/proc/cpuirqstat", + "/proc/fdenable", + "/proc/fdstat", + "/proc/fdthreshold", + "/proc/files_panic_enable", + "/proc/iomem_ext", + "/proc/kbox", + "/proc/kcore", + "/proc/keys", + "/proc/latency_stats", + "/proc/livepatch", + "/proc/memstat", + "/proc/net_namespace", + "/proc/oom_extend", + "/proc/sched_debug", + "/proc/scsi", + "/proc/sig_catch", + "/proc/signo", + "/proc/timer_list", + "/proc/timer_stats", + "/sys/firmware" + ], + "readonlyPaths": [ + "/proc/asound", + "/proc/bus", + "/proc/fs", + "/proc/irq", + "/proc/sys", + "/proc/sysrq-trigger", + "/proc/sysrq-region-size" + ] + } +} diff --git a/src/cpputils/stoppable_thread.h b/src/cpputils/stoppable_thread.h index 4b8a5e2..aff403c 100644 --- a/src/cpputils/stoppable_thread.h +++ b/src/cpputils/stoppable_thread.h @@ -16,7 +16,6 @@ #define __STOPPABLE_THREAD_H_ #include -#include #include #include #include @@ -50,3 +49,4 @@ private: }; #endif /* __STOPPABLE_THREAD_H_ */ + diff --git a/src/cutils/utils.c b/src/cutils/utils.c index c95d0a0..a7bb12a 100644 --- a/src/cutils/utils.c +++ b/src/cutils/utils.c @@ -1080,7 +1080,7 @@ int util_parse_user_remap(const char *user_remap, unsigned int *host_uid, unsign ret = -1; goto out; } - args_len = util_array_len(items); + args_len = util_array_len((const char **)items); switch (args_len) { case 3: @@ -1349,6 +1349,43 @@ int util_input_notty(char *buf, size_t maxlen) return ret ? ret : (int)i; } +int util_input_readall(char *buf, size_t maxlen) +{ + size_t i = 0; + int ret = 0; + + for (;;) { + int c = getchar(); + if (c == EOF) { + break; + } + if (c < 0) { + ret = -1; + break; + } + // Skip chars larger than maxlen + if (i + 1 >= maxlen) { + continue; + } + buf[i] = (char) c; + i++; + } + buf[i] = 0; + + // Strip last '\n' + if (i > 0 && buf[i - 1] == '\n') { + buf[i - 1] = 0; + i--; + } + // Strip last '\r' + if (i > 0 && buf[i - 1] == '\r') { + buf[i - 1] = 0; + i--; + } + + return ret ? ret : (int)i; +} + static int util_input(char *buf, size_t maxlen, bool echo_back) { int ret = 0; diff --git a/src/cutils/utils.h b/src/cutils/utils.h index 1376672..98d6b19 100644 --- a/src/cutils/utils.h +++ b/src/cutils/utils.h @@ -398,7 +398,7 @@ void free_sensitive_string(char *str); void memset_sensitive_string(char *str); bool detect_mount(const char *path); -int util_input_notty(char *buf, size_t maxlen); +int util_input_readall(char *buf, size_t maxlen); int util_input_echo(char *buf, size_t maxlen); int util_input_noecho(char *buf, size_t maxlen); diff --git a/src/cutils/utils_array.c b/src/cutils/utils_array.c index d6aefe2..6c3096d 100644 --- a/src/cutils/utils_array.c +++ b/src/cutils/utils_array.c @@ -21,9 +21,9 @@ #include "log.h" #include "utils.h" -size_t util_array_len(char **array) +size_t util_array_len(const char **array) { - char **pos; + const char **pos; size_t len = 0; for (pos = array; pos != NULL && *pos != NULL; pos++) { @@ -53,7 +53,7 @@ int util_array_append(char ***array, const char *element) } // let new len to len + 2 for element and null - len = util_array_len(*array); + len = util_array_len((const char **)(*array)); if (len > SIZE_MAX / sizeof(char *) - 2) { ERROR("Too many array elements!"); @@ -123,3 +123,4 @@ int util_grow_array(char ***orig_array, size_t *orig_capacity, size_t size, return 0; } + diff --git a/src/cutils/utils_array.h b/src/cutils/utils_array.h index 7e667a5..e534735 100644 --- a/src/cutils/utils_array.h +++ b/src/cutils/utils_array.h @@ -24,7 +24,7 @@ extern "C" { #endif -size_t util_array_len(char **array); +size_t util_array_len(const char **array); void util_free_array(char **array); @@ -38,3 +38,4 @@ int util_array_append(char ***array, const char *element); #endif #endif /* __UTILS_H */ + diff --git a/src/cutils/utils_convert.c b/src/cutils/utils_convert.c index e190c19..f94a6f6 100644 --- a/src/cutils/utils_convert.c +++ b/src/cutils/utils_convert.c @@ -169,7 +169,7 @@ static inline bool is_valid_str_bool_false(const char *str) int util_str_to_bool(const char *boolstr, bool *converted) { - if (boolstr == NULL || boolstr[0] == '\0') { + if (boolstr == NULL || converted == NULL) { return -EINVAL; } if (is_valid_str_bool_true(boolstr)) { @@ -181,3 +181,4 @@ int util_str_to_bool(const char *boolstr, bool *converted) } return 0; } + diff --git a/src/cutils/utils_file.c b/src/cutils/utils_file.c index 22304cd..ed774b0 100644 --- a/src/cutils/utils_file.c +++ b/src/cutils/utils_file.c @@ -786,10 +786,11 @@ char *look_path(const char *file, char **err) /* if slash in file, directly use file and do not try PATH. */ if (strings_contains_any(file, "/")) { - if (find_executable(file) == 0) { + int en = find_executable(file); + if (en == 0) { return util_strdup_s(file); } - if (asprintf(err, "find exec %s : %s", file, strerror(errno)) < 0) { + if (asprintf(err, "find exec %s : %s", file, strerror(en)) < 0) { *err = util_strdup_s("Out of memory"); } return NULL; diff --git a/src/cutils/utils_verify.c b/src/cutils/utils_verify.c index e358082..72778f5 100644 --- a/src/cutils/utils_verify.c +++ b/src/cutils/utils_verify.c @@ -607,7 +607,7 @@ bool util_valid_mount_mode(const char *mode) ERROR("Out of memory"); return false; } - mlen = util_array_len(modes); + mlen = util_array_len((const char **)modes); for (i = 0; i < mlen; i++) { if (util_valid_rw_mode(modes[i])) { @@ -664,3 +664,4 @@ cleanup: free(copy); return bret; } + diff --git a/src/filters.c b/src/filters.c index 734d3fe..1bd702c 100644 --- a/src/filters.c +++ b/src/filters.c @@ -213,7 +213,7 @@ static bool do_filters_args_match_kv_list(const map_t *field_values_map, const m goto cleanup; } - if (util_array_len(test_kv) == 2 && strcmp(test_kv[1], sources_value) != 0) { + if (util_array_len((const char **)test_kv) == 2 && strcmp(test_kv[1], sources_value) != 0) { util_free_array(test_kv); goto cleanup; } diff --git a/src/image/embedded/embedded_config_merge.c b/src/image/embedded/embedded_config_merge.c index 6bf2878..c118dbf 100644 --- a/src/image/embedded/embedded_config_merge.c +++ b/src/image/embedded/embedded_config_merge.c @@ -205,7 +205,7 @@ int embedded_image_merge_config(oci_runtime_spec *oci_spec, } } - ret = merge_volumes(oci_spec, mounts, util_array_len(mounts), NULL, parse_mount); + ret = merge_volumes(oci_spec, mounts, util_array_len((const char **)mounts), NULL, parse_mount); if (ret) { ERROR("Failed to merge layer into mounts"); goto out; diff --git a/src/image/oci/oci_auth.c b/src/image/oci/oci_auth.c index c5e9c4b..3fd035d 100644 --- a/src/image/oci/oci_auth.c +++ b/src/image/oci/oci_auth.c @@ -62,3 +62,4 @@ out: return auth_string; } + diff --git a/src/image/oci/oci_config_merge.c b/src/image/oci/oci_config_merge.c index 319bc62..fb1b2e5 100644 --- a/src/image/oci/oci_config_merge.c +++ b/src/image/oci/oci_config_merge.c @@ -229,11 +229,6 @@ static int oci_image_merge_health_check(const defs_health_check *image_health_ch { int ret = 0; - if (custom_spec == NULL) { - ERROR("Invalid input arguments"); - return -1; - } - if (image_health_check == NULL) { return 0; } @@ -264,7 +259,7 @@ int oci_image_merge_config(imagetool_image *image_conf, oci_runtime_spec *oci_sp { int ret = 0; - if (image_conf == NULL || oci_spec == NULL) { + if (image_conf == NULL || oci_spec == NULL || custom_spec == NULL) { ERROR("Invalid input arguments"); return -1; } diff --git a/src/image/oci/oci_image.c b/src/image/oci/oci_image.c index aa33ad0..f6ad1fa 100644 --- a/src/image/oci/oci_image.c +++ b/src/image/oci/oci_image.c @@ -729,3 +729,4 @@ int oci_init(const char *rootpath) out: return ret; } + diff --git a/src/image/oci/oci_rootfs_prepare.c b/src/image/oci/oci_rootfs_prepare.c index b275130..4589a2b 100644 --- a/src/image/oci/oci_rootfs_prepare.c +++ b/src/image/oci/oci_rootfs_prepare.c @@ -145,57 +145,6 @@ pack_response: return ret; } -int prepare_rootfs(rootfs_prepare_request *request, - rootfs_prepare_response **response) -{ - int ret = 0; - char *name = NULL; - char *image = NULL; - imagetool_prepare_response *tool_response = NULL; - - if (response == NULL) { - ERROR("Invalid input arguments"); - return -1; - } - - *response = util_common_calloc_s(sizeof(rootfs_prepare_response)); - if (*response == NULL) { - ERROR("Out of memory"); - return -1; - } - - if (check_prepare_request_valid(request) != 0) { - ret = -1; - goto pack_response; - } - - image = request->image; - name = request->name; - - EVENT("Event: {Object: %s, Type: preparing rootfs with image %s}", name, image); - - if (!do_prepare(request, &tool_response)) { - ERROR("Failed to prepare rootfs"); - ret = -1; - goto pack_response; - } - - EVENT("Event: {Object: %s, Type: prepared rootfs with image %s}", name, image); - -pack_response: - if (g_lcrd_errmsg != NULL) { - (*response)->errmsg = util_strdup_s(g_lcrd_errmsg); - } - - if (tool_response != NULL) { - (*response)->rootfs = tool_response->mount_point; - tool_response->mount_point = NULL; - free_imagetool_prepare_response(tool_response); - } - - return ret; -} - void free_rootfs_prepare_request(rootfs_prepare_request *ptr) { if (ptr == NULL) { @@ -228,3 +177,4 @@ void free_rootfs_prepare_and_get_image_conf_response(rootfs_prepare_and_get_imag free(ptr); } + diff --git a/src/image/oci/oci_rootfs_prepare.h b/src/image/oci/oci_rootfs_prepare.h index 8ab9681..79ff074 100644 --- a/src/image/oci/oci_rootfs_prepare.h +++ b/src/image/oci/oci_rootfs_prepare.h @@ -40,9 +40,6 @@ typedef struct { imagetool_prepare_response *raw_response; } rootfs_prepare_and_get_image_conf_response; -int prepare_rootfs(rootfs_prepare_request *request, - rootfs_prepare_response **response); - void free_rootfs_prepare_request(rootfs_prepare_request *ptr); int prepare_rootfs_and_get_image_conf(rootfs_prepare_request *request, @@ -54,3 +51,4 @@ void free_rootfs_prepare_and_get_image_conf_response(rootfs_prepare_and_get_imag #endif #endif + diff --git a/src/json/schema/schema/docker/seccomp.json b/src/json/schema/schema/docker/seccomp.json index dff02b3..1318ad3 100644 --- a/src/json/schema/schema/docker/seccomp.json +++ b/src/json/schema/schema/docker/seccomp.json @@ -46,13 +46,13 @@ "type": "object", "properties": { "index": { - "$ref": "../oci/runtime/defs.json#/definitions/uint32" + "$ref": "../defs.json#/definitions/uint32" }, "value": { - "$ref": "../oci/runtime/defs.json#/definitions/uint64" + "$ref": "../defs.json#/definitions/uint64" }, "valueTwo": { - "$ref": "../oci/runtime/defs.json#/definitions/uint64" + "$ref": "../defs.json#/definitions/uint64" }, "op": { "$ref": "../oci/runtime/defs-linux.json#/definitions/SeccompOperators" diff --git a/src/json/schema/schema/oci/image/content-descriptor.json b/src/json/schema/schema/oci/image/content-descriptor.json index 69fcea9..54fd0d8 100644 --- a/src/json/schema/schema/oci/image/content-descriptor.json +++ b/src/json/schema/schema/oci/image/content-descriptor.json @@ -10,7 +10,7 @@ }, "size": { "description": "the size in bytes of the referenced object", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "digest": { "description": "the cryptographic checksum digest of the object, in the pattern ':'", diff --git a/src/json/schema/schema/oci/image/defs-descriptor.json b/src/json/schema/schema/oci/image/defs-descriptor.json index feaea00..1d4e4dc 100644 --- a/src/json/schema/schema/oci/image/defs-descriptor.json +++ b/src/json/schema/schema/oci/image/defs-descriptor.json @@ -21,7 +21,7 @@ }, "annotations": { "id": "https://opencontainers.org/schema/image/descriptor/annotations", - "$ref": "defs.json#/definitions/mapStringString" + "$ref": "../../defs.json#/definitions/mapStringString" } } } diff --git a/src/json/schema/schema/oci/image/defs.json b/src/json/schema/schema/oci/image/defs.json deleted file mode 100644 index 72bdad7..0000000 --- a/src/json/schema/schema/oci/image/defs.json +++ /dev/null @@ -1,308 +0,0 @@ -{ - "description": "Definitions used throughout the OpenContainer Specification", - "definitions": { - "int8": { - "type": "integer", - "minimum": -128, - "maximum": 127 - }, - "int16": { - "type": "integer", - "minimum": -32768, - "maximum": 32767 - }, - "int32": { - "type": "integer", - "minimum": -2147483648, - "maximum": 2147483647 - }, - "int64": { - "type": "integer", - "minimum": -9223372036854776000, - "maximum": 9223372036854776000 - }, - "uint8": { - "type": "integer", - "minimum": 0, - "maximum": 255 - }, - "uint16": { - "type": "integer", - "minimum": 0, - "maximum": 65535 - }, - "uint32": { - "type": "integer", - "minimum": 0, - "maximum": 4294967295 - }, - "uint64": { - "type": "integer", - "minimum": 0, - "maximum": 18446744073709552000 - }, - "int32Pointer": { - "oneOf": [ - { - "$ref": "#/definitions/int32" - }, - { - "type": "null" - } - ] - }, - "uint16Pointer": { - "oneOf": [ - { - "$ref": "#/definitions/uint16" - }, - { - "type": "null" - } - ] - }, - "uint64Pointer": { - "oneOf": [ - { - "$ref": "#/definitions/uint64" - }, - { - "type": "null" - } - ] - }, - "stringPointer": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "null" - } - ] - }, - "percent": { - "type": "integer", - "minimum": 0, - "maximum": 100 - }, - "UID": { - "$ref": "#/definitions/uint32" - }, - "GID": { - "$ref": "#/definitions/uint32" - }, - "ArrayOfGIDs": { - "type": "array", - "items": { - "$ref": "#/definitions/GID" - } - }, - "FilePath": { - "type": "string" - }, - "Env": { - "$ref": "#/definitions/ArrayOfStrings" - }, - "Hook": { - "type": "object", - "properties": { - "path": { - "$ref": "#/definitions/FilePath" - }, - "args": { - "$ref": "#/definitions/ArrayOfStrings" - }, - "env": { - "$ref": "#/definitions/Env" - }, - "timeout": { - "type": "integer", - "minimum": 1 - } - }, - "required": [ - "path" - ] - }, - "ArrayOfHooks": { - "type": "array", - "items": { - "$ref": "#/definitions/Hook" - } - }, - "IDMapping": { - "type": "object", - "properties": { - "hostID": { - "$ref": "#/definitions/uint32" - }, - "containerID": { - "$ref": "#/definitions/uint32" - }, - "size": { - "$ref": "#/definitions/uint32" - } - }, - "required": [ - "hostID", - "containerID", - "size" - ] - }, - "Mount": { - "type": "object", - "properties": { - "source": { - "$ref": "#/definitions/FilePath" - }, - "destination": { - "$ref": "#/definitions/FilePath" - }, - "options": { - "$ref": "#/definitions/ArrayOfStrings" - }, - "type": { - "type": "string" - } - }, - "required": [ - "destination" - ] - }, - "ArrayOfStrings": { - "type": "array", - "items": { - "type": "string" - } - }, - "mapStringString": { - "type": "object", - "patternProperties": { - ".{1,}": { - "type": "string" - } - } - }, - "mapStringInt": { - "type": "object", - "patternProperties": { - ".{1,}": { - "type": "integer" - } - } - }, - "mapStringBool": { - "type": "object", - "patternProperties": { - ".{1,}": { - "type": "boolean" - } - } - }, - "mapIntString": { - "type": "object", - "patternProperties": { - ".{2,}": { - "type": "string" - } - } - }, - "mapIntInt": { - "type": "object", - "patternProperties": { - ".{2,}": { - "type": "integer" - } - } - }, - "mapIntBool": { - "type": "object", - "patternProperties": { - ".{2,}": { - "type": "boolean" - } - } - }, - "mapStringObject": { - "type": "object", - "patternProperties": { - ".{1,}": { - "type": "object" - } - } - }, - "ociVersion": { - "description": "The version of Open Container Runtime Specification that the document complies with", - "type": "string" - }, - "annotations": { - "$ref": "#/definitions/mapStringString" - }, - "HealthCheck": { - "type": "object", - "properties": { - "Test": { - "type": "array", - "items": { - "type": "string" - } - }, - "Interval": { - "type": "int64" - }, - "Timeout": { - "type": "int64" - }, - "StartPeriod": { - "type": "int64" - }, - "Retries": { - "type": "integer" - }, - "ExitOnUnhealthy": { - "type": "boolean" - } - } - }, - "Health": { - "type": "object", - "properties": { - "Status": { - "type": "string" - }, - "FailingStreak": { - "type": "integer" - }, - "Log": { - "type": "array", - "items": { - "type": "object", - "properties": { - "Start": { - "type": "string" - }, - "End": { - "type": "string" - }, - "ExitCode": { - "type": "integer" - }, - "Output": { - "type": "string" - } - } - } - } - } - }, - "filters": { - "type": "object", - "patternProperties": { - ".{1,}": { - "$ref": "#/definitions/mapStringBool" - } - } - } - } -} diff --git a/src/json/schema/schema/oci/image/index.json b/src/json/schema/schema/oci/image/index.json index 8a962aa..7d9e451 100644 --- a/src/json/schema/schema/oci/image/index.json +++ b/src/json/schema/schema/oci/image/index.json @@ -28,7 +28,7 @@ }, "size": { "description": "the size in bytes of the referenced object", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "digest": { "description": "the cryptographic checksum digest of the object, in the pattern ':'", diff --git a/src/json/schema/schema/oci/image/spec.json b/src/json/schema/schema/oci/image/spec.json index 15bccd0..7cd7562 100644 --- a/src/json/schema/schema/oci/image/spec.json +++ b/src/json/schema/schema/oci/image/spec.json @@ -24,7 +24,7 @@ "type": "string" }, "ExposedPorts": { - "$ref": "defs.json#/definitions/mapStringObject" + "$ref": "../../defs.json#/definitions/mapStringObject" }, "Env": { "type": "array", @@ -61,7 +61,7 @@ "Volumes": { "oneOf": [ { - "$ref": "defs.json#/definitions/mapStringObject" + "$ref": "../../defs.json#/definitions/mapStringObject" }, { "type": "null" @@ -74,7 +74,7 @@ "Labels": { "oneOf": [ { - "$ref": "defs.json#/definitions/mapStringString" + "$ref": "../../defs.json#/definitions/mapStringString" }, { "type": "null" diff --git a/src/json/schema/schema/oci/runtime/config-linux.json b/src/json/schema/schema/oci/runtime/config-linux.json index 95935f5..70b05e0 100644 --- a/src/json/schema/schema/oci/runtime/config-linux.json +++ b/src/json/schema/schema/oci/runtime/config-linux.json @@ -15,14 +15,14 @@ "id": "https://opencontainers.org/schema/bundle/linux/uidMappings", "type": "array", "items": { - "$ref": "defs.json#/definitions/IDMapping" + "$ref": "../../defs.json#/definitions/IDMapping" } }, "gidMappings": { "id": "https://opencontainers.org/schema/bundle/linux/gidMappings", "type": "array", "items": { - "$ref": "defs.json#/definitions/IDMapping" + "$ref": "../../defs.json#/definitions/IDMapping" } }, "namespaces": { @@ -53,7 +53,7 @@ "properties": { "limit": { "id": "https://opencontainers.org/schema/bundle/linux/resources/pids/limit", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" } }, "required": [ @@ -123,23 +123,23 @@ }, "period": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/period", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "quota": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/quota", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "realtimePeriod": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/realtimePeriod", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "realtimeRuntime": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/realtimeRuntime", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "shares": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/shares", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } } }, @@ -153,7 +153,7 @@ "type": "string" }, "limit": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } }, "required": [ @@ -168,27 +168,27 @@ "properties": { "kernel": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/kernel", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "kernelTCP": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/kernelTCP", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "limit": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/limit", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "reservation": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/reservation", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "swap": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/swap", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "swappiness": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/swappiness", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "disableOOMKiller": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/disableOOMKiller", @@ -202,7 +202,7 @@ "properties": { "classID": { "id": "https://opencontainers.org/schema/bundle/linux/resources/network/classId", - "$ref": "defs.json#/definitions/uint32" + "$ref": "../../defs.json#/definitions/uint32" }, "priorities": { "id": "https://opencontainers.org/schema/bundle/linux/resources/network/priorities", @@ -252,15 +252,15 @@ }, "sysctl": { "id": "https://opencontainers.org/schema/bundle/linux/sysctl", - "$ref": "defs.json#/definitions/mapStringString" + "$ref": "../../defs.json#/definitions/mapStringString" }, "maskedPaths": { "id": "https://opencontainers.org/schema/bundle/linux/maskedPaths", - "$ref": "defs.json#/definitions/ArrayOfStrings" + "$ref": "../../defs.json#/definitions/ArrayOfStrings" }, "readonlyPaths": { "id": "https://opencontainers.org/schema/bundle/linux/readonlyPaths", - "$ref": "defs.json#/definitions/ArrayOfStrings" + "$ref": "../../defs.json#/definitions/ArrayOfStrings" }, "mountLabel": { "id": "https://opencontainers.org/schema/bundle/linux/mountLabel", diff --git a/src/json/schema/schema/oci/runtime/defs-linux.json b/src/json/schema/schema/oci/runtime/defs-linux.json index 4d9620a..ccbe3c7 100644 --- a/src/json/schema/schema/oci/runtime/defs-linux.json +++ b/src/json/schema/schema/oci/runtime/defs-linux.json @@ -58,13 +58,13 @@ "type": "object", "properties": { "index": { - "$ref": "defs.json#/definitions/uint32" + "$ref": "../../defs.json#/definitions/uint32" }, "value": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "valueTwo": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "op": { "$ref": "#/definitions/SeccompOperators" @@ -103,11 +103,11 @@ }, "Major": { "description": "major device number", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "Minor": { "description": "minor device number", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "FileMode": { "description": "File permissions mode (typically an octal value)", @@ -131,7 +131,7 @@ "$ref": "#/definitions/FileType" }, "path": { - "$ref": "defs.json#/definitions/FilePath" + "$ref": "../../defs.json#/definitions/FilePath" }, "fileMode": { "$ref": "#/definitions/FileMode" @@ -143,10 +143,10 @@ "$ref": "#/definitions/Minor" }, "uid": { - "$ref": "defs.json#/definitions/UID" + "$ref": "../../defs.json#/definitions/UID" }, "gid": { - "$ref": "defs.json#/definitions/GID" + "$ref": "../../defs.json#/definitions/GID" } } }, @@ -196,7 +196,7 @@ "type": "object", "properties": { "rate": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } } } @@ -232,7 +232,7 @@ "type": "string" }, "priority": { - "$ref": "defs.json#/definitions/uint32" + "$ref": "../../defs.json#/definitions/uint32" } }, "required": [ @@ -259,7 +259,7 @@ "$ref": "#/definitions/NamespaceType" }, "path": { - "$ref": "defs.json#/definitions/FilePath" + "$ref": "../../defs.json#/definitions/FilePath" } }, "required": [ diff --git a/src/json/schema/schema/oci/runtime/defs.json b/src/json/schema/schema/oci/runtime/defs.json deleted file mode 120000 index 78180b5..0000000 --- a/src/json/schema/schema/oci/runtime/defs.json +++ /dev/null @@ -1 +0,0 @@ -../../defs.json \ No newline at end of file diff --git a/src/json/schema/schema/oci/runtime/pspec.json b/src/json/schema/schema/oci/runtime/pspec.json index cac6fd9..5cefc40 100644 --- a/src/json/schema/schema/oci/runtime/pspec.json +++ b/src/json/schema/schema/oci/runtime/pspec.json @@ -3,7 +3,7 @@ "type": "object", "properties": { "annotations": { - "$ref": "defs.json#/definitions/annotations" + "$ref": "../../defs.json#/definitions/annotations" }, "root": { "$ref": "spec.json#/properties/root" @@ -12,12 +12,12 @@ "id": "https://opencontainers.org/schema/bundle/mounts", "type": "array", "items": { - "$ref": "defs.json#/definitions/Mount" + "$ref": "../../defs.json#/definitions/Mount" } }, "env": { "id": "https://opencontainers.org/schema/bundle/process/env", - "$ref": "defs.json#/definitions/Env" + "$ref": "../../defs.json#/definitions/Env" }, "devices": { "id": "https://opencontainers.org/schema/bundle/linux/devices", diff --git a/src/json/schema/schema/oci/runtime/spec.json b/src/json/schema/schema/oci/runtime/spec.json index d083274..61eacbd 100644 --- a/src/json/schema/schema/oci/runtime/spec.json +++ b/src/json/schema/schema/oci/runtime/spec.json @@ -6,25 +6,25 @@ "properties": { "ociVersion": { "id": "https://opencontainers.org/schema/bundle/ociVersion", - "$ref": "defs.json#/definitions/ociVersion" + "$ref": "../../defs.json#/definitions/ociVersion" }, "hooks": { "id": "https://opencontainers.org/schema/bundle/hooks", "type": "object", "properties": { "prestart": { - "$ref": "defs.json#/definitions/ArrayOfHooks" + "$ref": "../../defs.json#/definitions/ArrayOfHooks" }, "poststart": { - "$ref": "defs.json#/definitions/ArrayOfHooks" + "$ref": "../../defs.json#/definitions/ArrayOfHooks" }, "poststop": { - "$ref": "defs.json#/definitions/ArrayOfHooks" + "$ref": "../../defs.json#/definitions/ArrayOfHooks" } } }, "annotations": { - "$ref": "defs.json#/definitions/annotations" + "$ref": "../../defs.json#/definitions/annotations" }, "hostname": { "id": "https://opencontainers.org/schema/bundle/hostname", @@ -34,7 +34,7 @@ "id": "https://opencontainers.org/schema/bundle/mounts", "type": "array", "items": { - "$ref": "defs.json#/definitions/Mount" + "$ref": "../../defs.json#/definitions/Mount" } }, "root": { @@ -47,7 +47,7 @@ "properties": { "path": { "id": "https://opencontainers.org/schema/bundle/root/path", - "$ref": "defs.json#/definitions/FilePath" + "$ref": "../../defs.json#/definitions/FilePath" }, "readonly": { "id": "https://opencontainers.org/schema/bundle/root/readonly", @@ -65,7 +65,7 @@ "properties": { "args": { "id": "https://opencontainers.org/schema/bundle/process/args", - "$ref": "defs.json#/definitions/ArrayOfStrings" + "$ref": "../../defs.json#/definitions/ArrayOfStrings" }, "consoleSize": { "id": "https://opencontainers.org/schema/bundle/process/consoleSize", @@ -77,11 +77,11 @@ "properties": { "height": { "id": "https://opencontainers.org/schema/bundle/process/consoleSize/height", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "width": { "id": "https://opencontainers.org/schema/bundle/process/consoleSize/width", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } } }, @@ -91,7 +91,7 @@ }, "env": { "id": "https://opencontainers.org/schema/bundle/process/env", - "$ref": "defs.json#/definitions/Env" + "$ref": "../../defs.json#/definitions/Env" }, "terminal": { "id": "https://opencontainers.org/schema/bundle/process/terminal", @@ -103,15 +103,15 @@ "properties": { "uid": { "id": "https://opencontainers.org/schema/bundle/process/user/uid", - "$ref": "defs.json#/definitions/UID" + "$ref": "../../defs.json#/definitions/UID" }, "gid": { "id": "https://opencontainers.org/schema/bundle/process/user/gid", - "$ref": "defs.json#/definitions/GID" + "$ref": "../../defs.json#/definitions/GID" }, "additionalGids": { "id": "https://opencontainers.org/schema/bundle/process/user/additionalGids", - "$ref": "defs.json#/definitions/ArrayOfGIDs" + "$ref": "../../defs.json#/definitions/ArrayOfGIDs" }, "username": { "id": "https://opencontainers.org/schema/bundle/process/user/username", @@ -190,11 +190,11 @@ "properties": { "hard": { "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/hard", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "soft": { "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/soft", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "type": { "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/type", diff --git a/src/json/schema/schema/oci/runtime/state.json b/src/json/schema/schema/oci/runtime/state.json index 563a619..93b8160 100644 --- a/src/json/schema/schema/oci/runtime/state.json +++ b/src/json/schema/schema/oci/runtime/state.json @@ -6,7 +6,7 @@ "properties": { "ociVersion": { "id": "https://opencontainers.org/schema/runtime/state/ociVersion", - "$ref": "defs.json#/definitions/ociVersion" + "$ref": "../../defs.json#/definitions/ociVersion" }, "id": { "id": "https://opencontainers.org/schema/runtime/state/id", @@ -33,7 +33,7 @@ "type": "string" }, "annotations": { - "$ref": "defs.json#/definitions/annotations" + "$ref": "../../defs.json#/definitions/annotations" } }, "required": [ diff --git a/src/json/schema/src/common_h.py b/src/json/schema/src/common_h.py index 5c45daf..ce0b665 100644 --- a/src/json/schema/src/common_h.py +++ b/src/json/schema/src/common_h.py @@ -33,7 +33,6 @@ CODE = '''// Auto generated file. Do not edit! # include # include # include -# include # include # include # include diff --git a/src/liblcrc.h b/src/liblcrc.h index 7b0decb..17912d1 100644 --- a/src/liblcrc.h +++ b/src/liblcrc.h @@ -16,7 +16,6 @@ #define __LIB_LCRC_H #include -#include #include #include "container_def.h" @@ -840,3 +839,4 @@ void lcrc_logs_response_free(struct lcrc_logs_response *response); #endif #endif + diff --git a/src/liblcrd.h b/src/liblcrd.h index 685a862..c328f1a 100644 --- a/src/liblcrd.h +++ b/src/liblcrd.h @@ -16,7 +16,6 @@ #define __LIB_LCRD_H #include -#include #include #include "container_def.h" diff --git a/src/pack_config.c b/src/pack_config.c index d2edf28..15d580a 100644 --- a/src/pack_config.c +++ b/src/pack_config.c @@ -379,7 +379,7 @@ static host_config_devices_element *parse_device(const char *devices) } tmp_str = util_string_split(devices, ':'); - tmp_str_len = util_array_len(tmp_str); + tmp_str_len = util_array_len((const char **)tmp_str); switch (tmp_str_len) { case 3: @@ -448,7 +448,7 @@ static void get_ulimit_split_parts(const char *val, char ***parts, size_t *parts COMMAND_ERROR("Out of memory"); return; } - *parts_len = util_array_len(*parts); + *parts_len = util_array_len((const char **)(*parts)); } static int parse_soft_hard_ulimit(const char *val, char **limitvals, size_t limitvals_len, int64_t *soft, int64_t *hard) @@ -748,7 +748,7 @@ static host_config_blkio_weight_device_element *pack_blkio_weight_devices(const COMMAND_ERROR("String split failed"); goto erro_out; } - tmp_str_len = util_array_len(tmp_str); + tmp_str_len = util_array_len((const char **)tmp_str); if (tmp_str_len != 2) { COMMAND_ERROR("Bad blkio weight device format: %s", devices); @@ -789,7 +789,7 @@ static int parse_blkio_throttle_bps_device(const char *device, char **path, cons char **split = NULL; split = util_string_split_multi(device, ':'); - if (split == NULL || util_array_len(split) != 2) { + if (split == NULL || util_array_len((const char **)split) != 2) { COMMAND_ERROR("bad format: %s", device); ret = -1; goto out; diff --git a/src/path.c b/src/path.c index 3c4c357..6e235d6 100644 --- a/src/path.c +++ b/src/path.c @@ -507,6 +507,14 @@ char *preserve_trailing_dot_or_separator(const char *cleanedpath, const char *or int nret; char respath[PATH_MAX + 3] = { 0 }; + if (cleanedpath == NULL || originalpath == NULL) { + return NULL; + } + + if (cleanedpath[0] == '\0' || originalpath[0] == '\0') { + return NULL; + } + nret = sprintf_s(respath, PATH_MAX, "%s", cleanedpath); if (nret < 0) { ERROR("Failed to print string"); diff --git a/src/plugin/plugin.c b/src/plugin/plugin.c index 9f5b996..cc3f715 100644 --- a/src/plugin/plugin.c +++ b/src/plugin/plugin.c @@ -165,8 +165,8 @@ static char *get_uniq_enable_plugins(const oci_runtime_spec *oci) UTIL_FREE_AND_SET_NULL(names); UTIL_FREE_AND_SET_NULL(full); - for (i = 0; i < util_array_len(raw); i++) { - if (strings_in_slice((const char **)arr, util_array_len(arr), raw[i])) { + for (i = 0; i < util_array_len((const char **)raw); i++) { + if (strings_in_slice((const char **)arr, util_array_len((const char **)arr), raw[i])) { continue; } if (util_array_append(&arr, raw[i]) != 0) { @@ -175,7 +175,7 @@ static char *get_uniq_enable_plugins(const oci_runtime_spec *oci) } } - names = util_string_join(LCRD_ENABLE_PLUGINS_SEPERATOR, (const char **)arr, util_array_len(arr)); + names = util_string_join(LCRD_ENABLE_PLUGINS_SEPERATOR, (const char **)arr, util_array_len((const char **)arr)); if (names == NULL) { ERROR("join uniq plugin name failed"); goto failed; @@ -251,7 +251,7 @@ static char **get_enable_plugins(const char *plugins) if (arr == NULL) { ERROR("Out of memory"); } - arr_len = util_array_len(arr); + arr_len = util_array_len((const char **)arr); for (i = 0; i < arr_len; i++) { if (strings_in_slice((const char **)dst, dst_len, arr[i])) { @@ -262,7 +262,7 @@ static char **get_enable_plugins(const char *plugins) dst = NULL; goto out; } - dst_len = util_array_len(dst); + dst_len = util_array_len((const char **)dst); } if (arr_len != dst_len) { @@ -956,7 +956,7 @@ static bool plugin_useby_container(const plugin_t *plugin, const container_t *co plugin_names = container_get_env_nolock(cont, LCRD_ENABLE_PLUGINS); pnames = get_enable_plugins(plugin_names); - for (i = 0; i < util_array_len(pnames); i++) { + for (i = 0; i < util_array_len((const char **)pnames); i++) { if (strcmp(pnames[i], plugin->name) == 0) { ok = true; break; @@ -1090,7 +1090,7 @@ static int pm_init_plugin(const plugin_t *plugin) size_t i = 0; cnames = containers_store_list_ids(); - container_num = util_array_len(cnames); + container_num = util_array_len((const char **)cnames); /* * send init request no matter containers exist or not, plugin should @@ -1331,7 +1331,7 @@ static int plugin_event_handle_dispath_impl(const char *cid, const char *plugins } pm_rdlock(); - for (i = 0; i < util_array_len(pnames); i++) { + for (i = 0; i < util_array_len((const char **)pnames); i++) { if (pm_get_plugin(pnames[i], &plugin)) { /* plugin not found */ ERROR("plugin %s not registered.", pnames[i]); ret = -1; @@ -1548,7 +1548,7 @@ int plugin_event_container_pre_create(const char *cid, oci_runtime_spec *ocic) goto out; } pm_rdlock(); - for (i = 0; i < util_array_len(pnames); i++) { + for (i = 0; i < util_array_len((const char **)pnames); i++) { if (pm_get_plugin(pnames[i], &plugin)) { /* plugin not found */ ERROR("plugin %s not registered.", pnames[i]); ret = -1; diff --git a/src/plugin/pspec.c b/src/plugin/pspec.c index 07f5a1e..2b69196 100644 --- a/src/plugin/pspec.c +++ b/src/plugin/pspec.c @@ -254,3 +254,4 @@ revert_free_old: return dst; } + diff --git a/src/services/cri/cni_network_plugin.cc b/src/services/cri/cni_network_plugin.cc index 2809a70..d52ed59 100644 --- a/src/services/cri/cni_network_plugin.cc +++ b/src/services/cri/cni_network_plugin.cc @@ -146,13 +146,13 @@ int CniNetworkPlugin::GetCNIConfFiles(const std::string &pluginDir, std::vector< goto out; } - if (util_array_len(files) == 0) { + if (util_array_len((const char **)files) == 0) { err.Errorf("No networks found in %s", usePluginDir.c_str()); ret = -1; goto out; } - vect_files = std::vector(files, files + util_array_len(files)); + vect_files = std::vector(files, files + util_array_len((const char **)files)); out: free(serr); @@ -717,3 +717,4 @@ void CniNetworkPlugin::UnlockNetworkMap(Errors &error) } } // namespace Network + diff --git a/src/services/cri/network_plugin.cc b/src/services/cri/network_plugin.cc index f3c373d..fe074f1 100644 --- a/src/services/cri/network_plugin.cc +++ b/src/services/cri/network_plugin.cc @@ -39,7 +39,7 @@ static void runGetIP(void *cmdArgs) char *args[ARGS_NUM]; char **tmpArgs = reinterpret_cast(cmdArgs); - if (util_array_len(tmpArgs) != CMD_ARGS_NUM) { + if (util_array_len((const char **)tmpArgs) != CMD_ARGS_NUM) { COMMAND_ERROR("need four args"); exit(1); } @@ -102,7 +102,7 @@ static std::string GetOnePodIP(std::string nsenterPath, std::string netnsPath, s error.SetError("Out of memory"); goto free_out; } - if (util_array_len(lines) < 1) { + if (util_array_len((const char **)lines) < 1) { error.Errorf("Unexpected command output %s", stdout_str); goto free_out; } @@ -112,7 +112,7 @@ static std::string GetOnePodIP(std::string nsenterPath, std::string netnsPath, s error.SetError("Out of memory"); goto free_out; } - if (util_array_len(fields) < 4) { + if (util_array_len((const char **)fields) < 4) { error.Errorf("Unexpected address output %s ", lines[0]); goto free_out; } @@ -519,3 +519,4 @@ void NoopNetworkPlugin::Status(Errors &error) } } // namespace Network + diff --git a/src/services/cri/request_cache.h b/src/services/cri/request_cache.h index 622ae54..ef86f7f 100644 --- a/src/services/cri/request_cache.h +++ b/src/services/cri/request_cache.h @@ -44,9 +44,7 @@ private: RequestCache() = default; RequestCache(const RequestCache &) = delete; RequestCache &operator=(const RequestCache &) = delete; - ~RequestCache() = default; - // clock is used to obtain the current time - std::time_t m_clock; + virtual ~RequestCache() = default; // tokens maps the generate token to the request for fast retrieval. std::unordered_map m_tokens; // ll maintains an age-ordered request list for faster garbage collection of expired requests. diff --git a/src/services/execution/execute/execution_create.c b/src/services/execution/execute/execution_create.c index e68db9e..30b4f27 100644 --- a/src/services/execution/execute/execution_create.c +++ b/src/services/execution/execute/execution_create.c @@ -693,6 +693,159 @@ static int verify_merged_custom_config(const container_custom_config *custom_spe return 0; } +static int response_allocate_memory(container_create_response **response) +{ + if (response == NULL) { + ERROR("Invalid NULL input"); + return -1; + } + + *response = util_common_calloc_s(sizeof(container_create_response)); + if (*response == NULL) { + ERROR("Out of memory"); + return -1; + } + + return 0; +} + +static int get_request_container_info(const container_create_request *request, char **id, char **name, uint32_t *cc) +{ + if (create_request_check(request) != 0) { + ERROR("Invalid create container request"); + *cc = LCRD_ERR_INPUT; + return -1; + } + + if (maintain_container_id(request, id, name) != 0) { + *cc = LCRD_ERR_EXEC; + return -1; + } + + return 0; +} + +static int get_request_image_info(const container_create_request *request, char **image_type, + const char **ext_config_image, const char **image_name) +{ + *image_type = im_get_image_type(request->image, request->rootfs); + if (*image_type == NULL) { + return -1; + } + + if (request->rootfs != NULL) { + *image_name = request->rootfs; + // Do not use none image because none image has no config. + if (strcmp(request->image, "none") && strcmp(request->image, "none:latest")) { + *ext_config_image = request->image; + } + } else { + *image_name = request->image; + } + + // Check if config image exist if provided. + if (*ext_config_image != NULL) { + if (!im_config_image_exist(*ext_config_image)) { + return -1; + } + } + + return 0; +} + +static int preparate_runtime_environment(const container_create_request *request, const char *id, + char **runtime, char **runtime_root, uint32_t *cc) +{ + *runtime = get_runtime_from_request(request); + if (*runtime == NULL) { + *cc = LCRD_ERR_INPUT; + return -1; + } + + *runtime_root = conf_get_routine_rootdir(*runtime); + if (*runtime_root == NULL) { + *cc = LCRD_ERR_EXEC; + return -1; + } + + if (create_container_root_dir(id, *runtime_root) != 0) { + *cc = LCRD_ERR_EXEC; + return -1; + } + + return 0; +} + +static int get_basic_spec(const container_create_request *request, const char *id, const char *runtime_root, + host_config **host_spec, container_custom_config **custom_spec) +{ + *host_spec = get_host_spec(request); + if (*host_spec == NULL) { + return -1; + } + + *custom_spec = get_custom_spec(id, runtime_root, request); + if (*custom_spec == NULL) { + return -1; + } + + return 0; +} + +static int get_v2_spec(const container_create_request *request, const char *id, const char *name, + const char *runtime_root, const host_config *host_spec, const char *image_type, + container_config_v2_common_config **v2_spec) +{ + *v2_spec = get_config_v2_spec(id, runtime_root, host_spec); + if (*v2_spec == NULL) { + ERROR("Failed to malloc container_config_v2_common_config"); + return -1; + } + + if (v2_spec_make_basic_info(id, name, request->image, image_type, *v2_spec) != 0) { + ERROR("Failed to malloc container_config_v2_common_config"); + return -1; + } + + return 0; +} + +static int v2_spec_merge_config(const container_custom_config *custom_spec, const oci_runtime_spec *oci_spec, + container_config_v2_common_config *v2_spec) +{ + if (v2_spec_merge_custom_spec(custom_spec, v2_spec) != 0) { + ERROR("Failed to malloc container_config_v2_common_config"); + return -1; + } + + if (v2_spec_merge_oci_spec(oci_spec, v2_spec) != 0) { + ERROR("Failed to malloc container_config_v2_common_config"); + return -1; + } + + return 0; +} + +static int generate_oci_config_json(const char *id, const container_create_request *request, + const host_config *host_spec, const container_custom_config *custom_spec, + oci_runtime_spec *oci_spec, char **oci_config_data) +{ + if (verify_merged_custom_config(custom_spec)) { + return -1; + } + + if (merge_config_for_syscontainer(request, host_spec, custom_spec, oci_spec) != 0) { + ERROR("Failed to merge config for syscontainer"); + return -1; + } + + if (generate_merged_oci_config_json(id, oci_spec, oci_config_data) != 0) { + return -1; + } + + return 0; +} + int container_create_cb(const container_create_request *request, container_create_response **response) { @@ -714,90 +867,29 @@ int container_create_cb(const container_create_request *request, DAEMON_CLEAR_ERRMSG(); - if (response == NULL) { - ERROR("Invalid NULL input"); + if (response_allocate_memory(response) != 0) { return -1; } - *response = util_common_calloc_s(sizeof(container_create_response)); - if (*response == NULL) { - ERROR("Out of memory"); - return -1; - } - - if (create_request_check(request) != 0) { - ERROR("Invalid create container request"); - cc = LCRD_ERR_INPUT; + if (get_request_container_info(request, &id, &name, &cc) != 0) { goto pack_response; } - if (maintain_container_id(request, &id, &name) != 0) { - cc = LCRD_ERR_EXEC; - goto pack_response; - } - - image_type = im_get_image_type(request->image, request->rootfs); - if (image_type == NULL) { + if (get_request_image_info(request, &image_type, &ext_config_image, &image_name) != 0) { cc = LCRD_ERR_EXEC; goto clean_nameindex; } - if (request->rootfs != NULL) { - image_name = request->rootfs; - // Do not use none image because none image has no config. - if (strcmp(request->image, "none") && strcmp(request->image, "none:latest")) { - ext_config_image = request->image; - } - } else { - image_name = request->image; - } - - // Check if config image exist if provided. - if (ext_config_image != NULL) { - if (!im_config_image_exist(ext_config_image)) { - cc = LCRD_ERR_EXEC; - goto clean_nameindex; - } - } - - runtime = get_runtime_from_request(request); - if (runtime == NULL) { - cc = LCRD_ERR_INPUT; + if (preparate_runtime_environment(request, id, &runtime, &runtime_root, &cc) != 0) { goto clean_nameindex; } - runtime_root = conf_get_routine_rootdir(runtime); - if (runtime_root == NULL) { - cc = LCRD_ERR_EXEC; - goto clean_nameindex; - } - - if (create_container_root_dir(id, runtime_root) != 0) { - cc = LCRD_ERR_EXEC; - goto clean_nameindex; - } - - host_spec = get_host_spec(request); - if (host_spec == NULL) { + if (get_basic_spec(request, id, runtime_root, &host_spec, &custom_spec) != 0) { cc = LCRD_ERR_INPUT; goto clean_container_root_dir; } - custom_spec = get_custom_spec(id, runtime_root, request); - if (custom_spec == NULL) { - cc = LCRD_ERR_INPUT; - goto clean_container_root_dir; - } - - v2_spec = get_config_v2_spec(id, runtime_root, host_spec); - if (v2_spec == NULL) { - ERROR("Failed to malloc container_config_v2_common_config"); - cc = LCRD_ERR_EXEC; - goto clean_container_root_dir; - } - - if (v2_spec_make_basic_info(id, name, request->image, image_type, v2_spec) != 0) { - ERROR("Failed to malloc container_config_v2_common_config"); + if (get_v2_spec(request, id, name, runtime_root, host_spec, image_type, &v2_spec) != 0) { cc = LCRD_ERR_EXEC; goto clean_container_root_dir; } @@ -808,12 +900,12 @@ int container_create_cb(const container_create_request *request, } host_channel = dup_host_channel(host_spec->host_channel); - if (prepare_host_channel(host_channel, host_spec->user_remap)) { ERROR("Failed to prepare host channel with '%s'", host_spec->host_channel); cc = LCRD_ERR_EXEC; goto clean_container_root_dir; } + oci_spec = merge_config(id, image_type, image_name, ext_config_image, host_spec, custom_spec, v2_spec, &real_rootfs); if (oci_spec == NULL) { @@ -826,36 +918,18 @@ int container_create_cb(const container_create_request *request, goto clean_rootfs; } - if (verify_merged_custom_config(custom_spec)) { + if (generate_oci_config_json(id, request, host_spec, custom_spec, oci_spec, &oci_config_data) != 0) { cc = LCRD_ERR_INPUT; goto clean_rootfs; } - if (merge_config_for_syscontainer(request, host_spec, custom_spec, oci_spec) != 0) { - ERROR("Failed to merge config for syscontainer"); - cc = LCRD_ERR_EXEC; - goto clean_rootfs; - } - - if (generate_merged_oci_config_json(id, oci_spec, &oci_config_data) != 0) { - cc = LCRD_ERR_EXEC; - goto clean_rootfs; - } - if (runtime_create(id, runtime, real_rootfs, oci_config_data) != 0) { ERROR("Runtime create container failed"); cc = LCRD_ERR_EXEC; goto clean_rootfs; } - if (v2_spec_merge_custom_spec(custom_spec, v2_spec) != 0) { - ERROR("Failed to malloc container_config_v2_common_config"); - cc = LCRD_ERR_EXEC; - goto clean_on_error; - } - - if (v2_spec_merge_oci_spec(oci_spec, v2_spec) != 0) { - ERROR("Failed to malloc container_config_v2_common_config"); + if (v2_spec_merge_config(custom_spec, oci_spec, v2_spec) != 0) { cc = LCRD_ERR_EXEC; goto clean_on_error; } @@ -868,6 +942,7 @@ int container_create_cb(const container_create_request *request, EVENT("Event: {Object: %s, Type: Created %s}", id, name); goto pack_response; + clean_on_error: (void)runtime_rm(id, runtime, runtime_root); diff --git a/src/services/execution/execute/execution_extend.c b/src/services/execution/execute/execution_extend.c index 65dd90a..50f11e8 100644 --- a/src/services/execution/execute/execution_extend.c +++ b/src/services/execution/execute/execution_extend.c @@ -389,7 +389,7 @@ static int stats_get_all_containers_id(const container_stats_request *request, c } else { array = containers_store_list_ids(); } - *ids_len = util_array_len(array); + *ids_len = util_array_len((const char **)array); *idsarray = array; array = NULL; ret = 0; diff --git a/src/services/execution/execute/execution_information.c b/src/services/execution/execute/execution_information.c index 7215f2e..46afc5e 100644 --- a/src/services/execution/execute/execution_information.c +++ b/src/services/execution/execute/execution_information.c @@ -314,7 +314,7 @@ int get_stime(const char *title_line) char **title_element = NULL; title_element = util_string_split(title_line, ' '); - for (i = 0; i < util_array_len(title_element); i++) { + for (i = 0; i < util_array_len((const char **)title_element); i++) { if (strcmp(title_element[i], "STIME") == 0) { stime = (int)i; break; @@ -333,7 +333,7 @@ int get_pid_num(const char *title_line) char **title_element = NULL; title_element = util_string_split(title_line, ' '); - for (i = 0; i < util_array_len(title_element); i++) { + for (i = 0; i < util_array_len((const char **)title_element); i++) { if (strcmp(title_element[i], "PID") == 0) { num = (int)i; break; @@ -372,7 +372,7 @@ static int parse_output_by_lines(char **process, char **tmp, int pid_num, int st char **pid_s = NULL; char **pid_s_pre = NULL; - for (i = 1; i < util_array_len(tmp); i++) { + for (i = 1; i < util_array_len((const char **)tmp); i++) { bool flag = false; int tmp_num = 0; if (i > 1) { @@ -429,12 +429,12 @@ int parse_output(char **title, char ***process, const char *output, const pid_t pid_num = get_pid_num(*title); stime = get_stime(*title); - if (util_array_len(tmp) > SIZE_MAX / sizeof(char *)) { + if (util_array_len((const char **)tmp) > SIZE_MAX / sizeof(char *)) { ERROR("Invalid array length"); ret = -1; goto out; } - *process = util_common_calloc_s(util_array_len(tmp) * sizeof(char *)); + *process = util_common_calloc_s(util_array_len((const char **)tmp) * sizeof(char *)); if (*process == NULL) { ERROR("Out of memory"); ret = -1; @@ -757,12 +757,12 @@ static int container_top_cb(container_top_request *request, container_top_respon cc = LCRD_ERR_EXEC; goto pack_response; } - if (util_array_len(processes) > SIZE_MAX / sizeof(char *)) { + if (util_array_len((const char **)processes) > SIZE_MAX / sizeof(char *)) { ERROR("invalid processe size"); cc = LCRD_ERR_EXEC; goto pack_response; } - (*response)->processes = util_common_calloc_s(util_array_len(processes) * sizeof(char *)); + (*response)->processes = util_common_calloc_s(util_array_len((const char **)processes) * sizeof(char *)); if ((*response)->processes == NULL) { ERROR("Out of memory"); cc = LCRD_ERR_EXEC; @@ -771,10 +771,10 @@ static int container_top_cb(container_top_request *request, container_top_respon (*response)->titles = titles; titles = NULL; - for (i = 0; i < util_array_len(processes); i++) { + for (i = 0; i < util_array_len((const char **)processes); i++) { (*response)->processes[i] = util_strdup_s(processes[i]); } - (*response)->processes_len = util_array_len(processes); + (*response)->processes_len = util_array_len((const char **)processes); pack_response: if (*response != NULL) { diff --git a/src/services/execution/execute/list.c b/src/services/execution/execute/list.c index 0f2b72f..e97c90c 100644 --- a/src/services/execution/execute/list.c +++ b/src/services/execution/execute/list.c @@ -199,10 +199,10 @@ static char **filter_by_name_id_matches(const struct list_context *ctx, const ma map_t *matches = NULL; names = filters_args_get(ctx->ps_filters, "name"); - names_len = util_array_len(names); + names_len = util_array_len((const char **)names); ids = filters_args_get(ctx->ps_filters, "id"); - ids_len = util_array_len(ids); + ids_len = util_array_len((const char **)ids); if (names_len == 0 && ids_len == 0) { if (append_ids(map_id_name, &filtered_ids) != 0) { goto cleanup; @@ -618,7 +618,7 @@ static int pack_list_containers(char **idsarray, const struct list_context *ctx, int j = 0; size_t container_nums = 0; - container_nums = util_array_len(idsarray); + container_nums = util_array_len((const char **)idsarray); if (container_nums == 0) { goto out; } diff --git a/src/services/execution/manager/container_unix.c b/src/services/execution/manager/container_unix.c index 4532983..1e5ff16 100644 --- a/src/services/execution/manager/container_unix.c +++ b/src/services/execution/manager/container_unix.c @@ -1098,7 +1098,7 @@ char *container_get_command(const container_t *cont) } } - cmd = util_string_join(" ", (const char **)args, util_array_len(args)); + cmd = util_string_join(" ", (const char **)args, util_array_len((const char **)args)); cleanup: util_free_array(args); diff --git a/src/services/execution/manager/health_check.c b/src/services/execution/manager/health_check.c index 3ce42a7..d7827f3 100644 --- a/src/services/execution/manager/health_check.c +++ b/src/services/execution/manager/health_check.c @@ -253,7 +253,7 @@ static char **health_check_cmds(const container_config *config) goto out; } - shell_len = util_array_len(shell); + shell_len = util_array_len((const char **)shell); if (shell_len > (SIZE_MAX / sizeof(char *)) - config->health_check->test_len) { ERROR("Invalid shell length"); goto out; @@ -574,7 +574,7 @@ void *health_check_run(void *arg) container_req->timeout = timeout_with_default(config->health_check->timeout, DEFAULT_PROBE_TIMEOUT) / Time_Second; container_req->container_id = util_strdup_s(cont->common_config->id); container_req->argv = cmd_slice; - container_req->argv_len = util_array_len(cmd_slice); + container_req->argv_len = util_array_len((const char **)cmd_slice); cmd_slice = NULL; EVENT("EVENT: {Object: %s, Type: Health checking}", cont->common_config->id); @@ -856,3 +856,4 @@ out: return; } + diff --git a/src/services/execution/manager/restore.c b/src/services/execution/manager/restore.c index 53ebd07..d309a1e 100644 --- a/src/services/execution/manager/restore.c +++ b/src/services/execution/manager/restore.c @@ -664,7 +664,7 @@ static int restore_container_by_runtime(const char *runtime) ret = -1; goto out; } - subdir_num = util_array_len(subdir); + subdir_num = util_array_len((const char **)subdir); if (subdir_num == 0) { goto out; } @@ -706,7 +706,7 @@ void containers_restore(void) ERROR("Failed to list engines"); goto out; } - subdir_num = util_array_len(subdir); + subdir_num = util_array_len((const char **)subdir); for (i = 0; i < subdir_num; i++) { DEBUG("Restore the containers by runtime:%s", subdir[i]); diff --git a/src/services/execution/manager/restore.h b/src/services/execution/manager/restore.h index 6546fb1..453ae9f 100644 --- a/src/services/execution/manager/restore.h +++ b/src/services/execution/manager/restore.h @@ -16,7 +16,6 @@ #define __LCRD_RESTORE_H #include -#include #include #include "engine.h" @@ -32,3 +31,4 @@ extern void containers_restore(void); #endif #endif + diff --git a/src/services/execution/manager/supervisor.h b/src/services/execution/manager/supervisor.h index ca91dd4..a6f61bf 100644 --- a/src/services/execution/manager/supervisor.h +++ b/src/services/execution/manager/supervisor.h @@ -16,7 +16,6 @@ #define __LCRD_SUPERVISOR_H #include #include -#include #include "container_unix.h" extern char *exit_fifo_create(const char *cont_state_path); @@ -31,3 +30,4 @@ extern int supervisor_add_exit_monitor(int fd, const container_pid_t *pid_info, extern int new_supervisor(); #endif + diff --git a/src/services/execution/spec/specs.c b/src/services/execution/spec/specs.c index 02f967f..e5a9b96 100644 --- a/src/services/execution/spec/specs.c +++ b/src/services/execution/spec/specs.c @@ -328,9 +328,12 @@ out: } /* default_spec returns default oci spec used by lcrd. */ -oci_runtime_spec *default_spec() +oci_runtime_spec *default_spec(bool system_container) { const char *oci_file = OCICONFIG_PATH; + if (system_container) { + oci_file = OCI_SYSTEM_CONTAINER_CONFIG_PATH; + } oci_runtime_spec *oci_spec = NULL; parser_error err = NULL; @@ -1453,6 +1456,44 @@ out: return ret; } +static int change_tmpfs_mount_size(const oci_runtime_spec *oci_spec, int64_t memory_limit) +{ + int ret = 0; + size_t i = 0; + char size_opt[MOUNT_PROPERTIES_SIZE] = { 0 }; + + if (oci_spec->mounts == NULL) { + goto out; + } + if (memory_limit <= 0) { + goto out; + } + /*set tmpfs mount size to half of container memory limit*/ + if (sprintf_s(size_opt, sizeof(size_opt), "size=%lldk", (long long int)(memory_limit / 2048)) < 0) { + ERROR("Out of memory"); + ret = -1; + goto out; + } + for (i = 0; i < oci_spec->mounts_len; i++) { + if (strcmp("tmpfs", oci_spec->mounts[i]->type) != 0) { + continue; + } + if (strcmp("/run", oci_spec->mounts[i]->destination) == 0 || \ + strcmp("/run/lock", oci_spec->mounts[i]->destination) == 0 || \ + strcmp("/tmp", oci_spec->mounts[i]->destination) == 0) { + ret = util_array_append(&oci_spec->mounts[i]->options, size_opt); + if (ret != 0) { + ERROR("append mount size option failed"); + goto out; + } + oci_spec->mounts[i]->options_len++; + } + } + +out: + return ret; +} + static int merge_settings_for_system_container(oci_runtime_spec *oci_spec, host_config *host_spec, container_custom_config *custom_spec) { @@ -1471,14 +1512,9 @@ static int merge_settings_for_system_container(oci_runtime_spec *oci_spec, host_ ERROR("Failed to adapt settings for system container"); goto out; } - if (!mount_run_tmpfs(oci_spec, "/run")) { + if (change_tmpfs_mount_size(oci_spec, host_spec->memory) != 0) { ret = -1; - ERROR("Failed to add /run mount to system container"); - goto out; - } - if (!mount_run_tmpfs(oci_spec, "/run/lock")) { - ret = -1; - ERROR("Failed to add /run/lock mount to system container"); + ERROR("Failed to change tmpfs mount size for system container"); goto out; } @@ -1674,7 +1710,7 @@ oci_runtime_spec *merge_container_config(const char *id, const char *image_type, parser_error err = NULL; int ret = 0; - oci_spec = default_spec(); + oci_spec = default_spec(host_spec->system_container); if (oci_spec == NULL) { goto out; } @@ -1814,3 +1850,4 @@ out: free(err); return ociconfig; } + diff --git a/src/services/execution/spec/specs_mount.c b/src/services/execution/spec/specs_mount.c index 572539b..6e9b470 100644 --- a/src/services/execution/spec/specs_mount.c +++ b/src/services/execution/spec/specs_mount.c @@ -147,7 +147,7 @@ int adapt_settings_for_mounts(oci_runtime_spec *oci_spec, container_custom_confi ret = -1; goto out; } - array_str_len = util_array_len(array_str); + array_str_len = util_array_len((const char **)array_str); for (i = 0; i < array_str_len; i++) { if (strcmp(array_str[i], "net") == 0) { @@ -543,7 +543,7 @@ defs_mount *parse_mount(const char *mount) goto out; } - items_len = util_array_len(items); + items_len = util_array_len((const char **)items); for (i = 0; i < items_len; i++) { kv = util_string_split(items[i], '='); @@ -635,7 +635,7 @@ static int get_src_dst_mode_by_volume(const char *volume, defs_mount *mount_elem goto free_out; } - alen = util_array_len(array); + alen = util_array_len((const char **)array); switch (alen) { case 1: ERROR("Not supported volume format '%s'", volume); @@ -716,7 +716,7 @@ defs_mount *parse_volume(const char *volume) goto free_out; } - mlen = util_array_len(modes); + mlen = util_array_len((const char **)modes); for (i = 0; i < mlen; i++) { if (util_valid_rw_mode(modes[i])) { rw = modes[i]; @@ -1820,57 +1820,6 @@ static bool mounts_expand(oci_runtime_spec *container, size_t add_len) return true; } -bool mount_run_tmpfs(oci_runtime_spec *container, const char *path) -{ - char **options = NULL; - size_t options_len = 5; - bool ret = false; - defs_mount *tmp_mounts = NULL; - - if (options_len > SIZE_MAX / sizeof(char *)) { - ERROR("Invalid option size"); - return ret; - } - options = util_common_calloc_s(options_len * sizeof(char *)); - if (options == NULL) { - ERROR("Out of memory"); - goto out_free; - } - options[0] = util_strdup_s("nosuid"); - options[1] = util_strdup_s("noexec"); - options[2] = util_strdup_s("nodev"); - options[3] = util_strdup_s("relatime"); - options[4] = util_strdup_s("mode=755"); - tmp_mounts = util_common_calloc_s(sizeof(defs_mount)); - if (tmp_mounts == NULL) { - ERROR("Malloc tmp_mounts memory failed"); - goto out_free; - } - - tmp_mounts->destination = util_strdup_s(path); - tmp_mounts->source = util_strdup_s("tmpfs"); - tmp_mounts->type = util_strdup_s("tmpfs"); - tmp_mounts->options = options; - tmp_mounts->options_len = options_len; - options = NULL; - - /*expand mount array*/ - if (!mounts_expand(container, 1)) { - goto out_free; - } - /*add a new mount node*/ - container->mounts[container->mounts_len - 1] = tmp_mounts; - - ret = true; -out_free: - - if (!ret) { - util_free_array(options); - free_defs_mount(tmp_mounts); - } - return ret; -} - static bool mount_file(oci_runtime_spec *container, const char *src_path, const char *dst_path) { char **options = NULL; diff --git a/src/services/execution/spec/specs_mount.h b/src/services/execution/spec/specs_mount.h index 1921839..8e2d444 100644 --- a/src/services/execution/spec/specs_mount.h +++ b/src/services/execution/spec/specs_mount.h @@ -44,8 +44,9 @@ int set_mounts_readwrite_option(const oci_runtime_spec *oci_spec); int merge_all_devices_and_all_permission(oci_runtime_spec *oci_spec); -bool mount_run_tmpfs(oci_runtime_spec *container, const char *path); +bool mount_run_tmpfs(oci_runtime_spec *container, const host_config *host_spec, const char *path); int merge_conf_device(oci_runtime_spec *oci_spec, host_config *host_spec); #endif + diff --git a/src/services/execution/spec/sysinfo.c b/src/services/execution/spec/sysinfo.c index ecd5e45..3abf5d7 100644 --- a/src/services/execution/spec/sysinfo.c +++ b/src/services/execution/spec/sysinfo.c @@ -1061,7 +1061,7 @@ static bool is_huge_pagesize_valid(const char *pagesize) ERROR("Hugetlb cgroup not supported"); goto free_out; } - hps_len = util_array_len(hps); + hps_len = util_array_len((const char **)hps); if (hps_len == 0) { ERROR("Hugetlb cgroup not supported"); goto free_out; @@ -1245,7 +1245,7 @@ mountinfo_t *get_mount_info(const char *pline) ret = -1; goto free_out; } - length = util_array_len(list); + length = util_array_len((const char **)list); if (length < 8) { ERROR("Invalid mountinfo '%s'", pline); ret = -1; @@ -1339,3 +1339,4 @@ free_out: } return minfos; } + diff --git a/src/services/graphdriver/overlay2/driver_overlay2.c b/src/services/graphdriver/overlay2/driver_overlay2.c index 59d1d62..4ea9753 100644 --- a/src/services/graphdriver/overlay2/driver_overlay2.c +++ b/src/services/graphdriver/overlay2/driver_overlay2.c @@ -14,6 +14,7 @@ ******************************************************************************/ #include "driver_overlay2.h" #include +#include #include "liblcrd.h" #include "utils.h" @@ -79,3 +80,4 @@ int overlay2_parse_options(struct graphdriver *driver, const char **options, siz return 0; } + diff --git a/src/tar/lcrdtar.h b/src/tar/lcrdtar.h index 91d2922..700763d 100644 --- a/src/tar/lcrdtar.h +++ b/src/tar/lcrdtar.h @@ -19,7 +19,6 @@ #include #include -#include #include #include @@ -75,3 +74,4 @@ int archive_path(const char *srcdir, const char *srcbase, const char *rebase_nam #endif #endif + diff --git a/src/websocket/service/attach_serve.h b/src/websocket/service/attach_serve.h index 413c736..ada2a55 100644 --- a/src/websocket/service/attach_serve.h +++ b/src/websocket/service/attach_serve.h @@ -29,6 +29,10 @@ class AttachServe : public StreamingServeInterface { public: + AttachServe() = default; + AttachServe(const AttachServe &) = delete; + AttachServe &operator=(const AttachServe &) = delete; + virtual ~AttachServe() = default; int Execute(struct lws *wsi, const std::string &token, int read_pipe_fd) override; private: int RequestFromCri(const runtime::AttachRequest *grequest, diff --git a/src/websocket/service/exec_serve.h b/src/websocket/service/exec_serve.h index 2b4ab12..5eea0b0 100644 --- a/src/websocket/service/exec_serve.h +++ b/src/websocket/service/exec_serve.h @@ -33,6 +33,10 @@ class ExecServe : public StreamingServeInterface { public: + ExecServe() = default; + ExecServe(const ExecServe &) = delete; + ExecServe &operator=(const ExecServe &) = delete; + virtual ~ExecServe() = default; int Execute(struct lws *wsi, const std::string &token, int read_pipe_fd) override; private: int RequestFromCri(const runtime::ExecRequest *grequest, diff --git a/src/websocket/service/route_callback_register.h b/src/websocket/service/route_callback_register.h index dd86e73..a131dfe 100644 --- a/src/websocket/service/route_callback_register.h +++ b/src/websocket/service/route_callback_register.h @@ -24,11 +24,19 @@ #include "log.h" class StreamingServeInterface { public: + StreamingServeInterface() = default; + StreamingServeInterface(const StreamingServeInterface &) = delete; + StreamingServeInterface &operator=(const StreamingServeInterface &) = delete; + virtual ~StreamingServeInterface() = default; virtual int Execute(struct lws *wsi, const std::string &token, int read_pipe_fd) = 0; }; class RouteCallbackRegister { public: + RouteCallbackRegister() = default; + RouteCallbackRegister(const RouteCallbackRegister &) = delete; + RouteCallbackRegister &operator=(const RouteCallbackRegister &) = delete; + virtual ~RouteCallbackRegister() = default; bool IsValidMethod(const std::string &method) { return static_cast(m_registeredcallbacks.count(method)); @@ -66,7 +74,9 @@ public: const std::string &token, int read_pipe_fd) : m_invoker(invoker), m_wsi(wsi), m_method(method), m_token(token), m_read_pipe_fd(read_pipe_fd) {} - ~StreamTask() = default; + StreamTask(const StreamTask &) = delete; + StreamTask &operator=(const StreamTask &) = delete; + virtual ~StreamTask() = default; int Run() { return m_invoker->HandleCallback(m_wsi, m_method, m_token, m_read_pipe_fd); diff --git a/src/websocket/service/ws_server.cc b/src/websocket/service/ws_server.cc index 281a019..6120da6 100644 --- a/src/websocket/service/ws_server.cc +++ b/src/websocket/service/ws_server.cc @@ -413,6 +413,7 @@ void WebsocketServer::Wait() lws_context_destroy(m_context); } + ssize_t WsWriteToClient(void *context, const void *data, size_t len) { const int RETRIES = 10; diff --git a/src/websocket/service/ws_server.h b/src/websocket/service/ws_server.h index 1a67baa..169c4ed 100644 --- a/src/websocket/service/ws_server.h +++ b/src/websocket/service/ws_server.h @@ -90,7 +90,7 @@ private: WebsocketServer(); WebsocketServer(const WebsocketServer &) = delete; WebsocketServer &operator=(const WebsocketServer &) = delete; - ~WebsocketServer(); + virtual ~WebsocketServer(); int InitRWPipe(int read_fifo[]); std::vector split(std::string str, char r); static void EmitLog(int level, const char *line); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9bf54af..baea137 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,6 +3,7 @@ project(iSulad_LLT) # setup testing find_package(Threads REQUIRED) find_package(GTest REQUIRED) -include_directories(${GTEST_INCLUDE_DIR}) add_subdirectory(cutils) +add_subdirectory(image) +add_subdirectory(path) diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt index f1ea941..de832f9 100644 --- a/test/cutils/CMakeLists.txt +++ b/test/cutils/CMakeLists.txt @@ -1,3 +1,5 @@ project(iSulad_LLT) add_subdirectory(utils_string) +add_subdirectory(utils_convert) +add_subdirectory(utils_array) diff --git a/test/cutils/utils_array/CMakeLists.txt b/test/cutils/utils_array/CMakeLists.txt new file mode 100644 index 0000000..677cd85 --- /dev/null +++ b/test/cutils/utils_array/CMakeLists.txt @@ -0,0 +1,27 @@ +project(iSulad_LLT) + +SET(EXE utils_array_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256/sha256.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/path.c + ${CMAKE_BINARY_DIR}/json/json_common.c + utils_array_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${GTEST_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils + ${CMAKE_BINARY_DIR}/json + ) +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,calloc -Wl,--wrap,memcpy_s") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/cutils/utils_array/utils_array_llt.cc b/test/cutils/utils_array/utils_array_llt.cc new file mode 100644 index 0000000..80072f1 --- /dev/null +++ b/test/cutils/utils_array/utils_array_llt.cc @@ -0,0 +1,235 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: utils_array llt + * Author: tanyifeng + * Create: 2019-09-24 + */ + +#include +#include +#include +#include +#include +#include "mock.h" +#include "utils_array.h" +#include "utils.h" + +extern "C" { + DECLARE_WRAPPER(calloc, void *, (size_t nmemb, size_t size)); + DEFINE_WRAPPER(calloc, void *, (size_t nmemb, size_t size), (nmemb, size)); + + DECLARE_WRAPPER(memcpy_s, errno_t, (void* dest, size_t destMax, const void* src, size_t count)); + DEFINE_WRAPPER(memcpy_s, errno_t, (void* dest, size_t destMax, const void* src, size_t count), + (dest, destMax, src, count)); +} + +TEST(utils_array, test_util_array_len) +{ + const char *array_long[] = { "abcd", "1234", "a1b", nullptr }; + + ASSERT_EQ(util_array_len(nullptr), 0); + + ASSERT_EQ(util_array_len(array_long), 3); +} + +TEST(utils_array, test_util_free_array) +{ + char **array = NULL; + + array = (char **)util_common_calloc_s(4 * sizeof(char *)); + ASSERT_NE(array, nullptr); + array[0] = util_strdup_s("test1"); + array[1] = util_strdup_s("test2"); + array[2] = util_strdup_s("test3"); + array[3] = nullptr; + + util_free_array(nullptr); + util_free_array(array); +} + +TEST(utils_array, test_util_grow_array) +{ + char **array = nullptr; + size_t capacity = 0; + int ret; + + capacity = 1; + array = (char **)util_common_calloc_s(sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, &capacity, 1, 1); + ASSERT_EQ(ret, 0); + ASSERT_NE(array, nullptr); + ASSERT_EQ(array[0], nullptr); + ASSERT_EQ(array[1], nullptr); + ASSERT_EQ(capacity, 2); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, &capacity, 1, 2); + ASSERT_EQ(ret, 0); + ASSERT_NE(array, nullptr); + ASSERT_EQ(array[0], nullptr); + ASSERT_EQ(array[1], nullptr); + ASSERT_EQ(array[2], nullptr); + ASSERT_EQ(capacity, 3); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, &capacity, 1, 4); + ASSERT_EQ(ret, 0); + ASSERT_NE(array, nullptr); + ASSERT_EQ(array[0], nullptr); + ASSERT_EQ(array[1], nullptr); + ASSERT_EQ(array[2], nullptr); + ASSERT_EQ(array[3], nullptr); + ASSERT_EQ(array[4], nullptr); + ASSERT_EQ(capacity, 5); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, &capacity, 1, 0); + ASSERT_NE(ret, 0); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, &capacity, 4, 1); + ASSERT_EQ(ret, 0); + ASSERT_NE(array, nullptr); + ASSERT_EQ(array[0], nullptr); + ASSERT_EQ(array[1], nullptr); + ASSERT_EQ(array[2], nullptr); + ASSERT_EQ(array[3], nullptr); + ASSERT_EQ(array[4], nullptr); + ASSERT_EQ(capacity, 5); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, &capacity, 0, 1); + ASSERT_EQ(ret, 0); + ASSERT_NE(array, nullptr); + ASSERT_EQ(array[0], nullptr); + ASSERT_EQ(capacity, 1); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(&array, nullptr, 1, 1); + ASSERT_NE(ret, 0); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + ret = util_grow_array(nullptr, &capacity, 1, 1); + ASSERT_NE(ret, 0); + util_free_array(array); + array = nullptr; + capacity = 0; + + capacity = 1; + array = (char **)util_common_calloc_s(capacity * sizeof(char *)); + ASSERT_NE(array, nullptr); + MOCK_SET(memcpy_s, EINVAL); + ret = util_grow_array(&array, &capacity, 1, 1); + ASSERT_NE(ret, 0); + MOCK_CLEAR(memcpy_s); + util_free_array(array); + array = nullptr; + capacity = 0; +} + +TEST(utils_array, test_util_array_append) +{ + char **array = NULL; + char **array_three = NULL; + int ret; + + ret = util_array_append(&array, "1234567890"); + ASSERT_EQ(ret, 0); + ASSERT_STREQ(array[0], "1234567890"); + ASSERT_EQ(array[1], nullptr); + util_free_array(array); + array = nullptr; + + ret = util_array_append(&array, ""); + ASSERT_EQ(ret, 0); + ASSERT_STREQ(array[0], ""); + ASSERT_EQ(array[1], nullptr); + util_free_array(array); + array = nullptr; + + ret = util_array_append(&array, nullptr); + ASSERT_NE(ret, 0); + + array_three = (char **)util_common_calloc_s(4 * sizeof(char *)); + ASSERT_NE(array_three, nullptr); + array_three[0] = util_strdup_s("test1"); + array_three[1] = util_strdup_s("test2"); + array_three[2] = util_strdup_s("test3"); + array_three[3] = nullptr; + ret = util_array_append(&array_three, "1234567890"); + ASSERT_EQ(ret, 0); + ASSERT_STREQ(array_three[0], "test1"); + ASSERT_STREQ(array_three[1], "test2"); + ASSERT_STREQ(array_three[2], "test3"); + ASSERT_STREQ(array_three[3], "1234567890"); + ASSERT_EQ(array_three[4], nullptr); + util_free_array(array_three); + array_three = nullptr; + + ret = util_array_append(nullptr, "1234567890"); + ASSERT_NE(ret, 0); + + MOCK_SET(calloc, nullptr); + ret = util_array_append(&array, ""); + ASSERT_NE(ret, 0); + MOCK_CLEAR(calloc); + util_free_array(array); + array = nullptr; + + array_three = (char **)util_common_calloc_s(4 * sizeof(char *)); + ASSERT_NE(array_three, nullptr); + array_three[0] = util_strdup_s("test1"); + array_three[1] = util_strdup_s("test2"); + array_three[2] = util_strdup_s("test3"); + array_three[3] = nullptr; + MOCK_SET(memcpy_s, EINVAL); + ret = util_array_append(&array_three, "123"); + ASSERT_NE(ret, 0); + MOCK_CLEAR(memcpy_s); + util_free_array(array_three); + array_three = nullptr; +} diff --git a/test/cutils/utils_convert/CMakeLists.txt b/test/cutils/utils_convert/CMakeLists.txt new file mode 100644 index 0000000..4fa5609 --- /dev/null +++ b/test/cutils/utils_convert/CMakeLists.txt @@ -0,0 +1,26 @@ +project(iSulad_LLT) + +SET(EXE utils_convert_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256/sha256.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/path.c + ${CMAKE_BINARY_DIR}/json/json_common.c + utils_convert_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${GTEST_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cutils + ${CMAKE_BINARY_DIR}/json + ) +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/cutils/utils_convert/utils_convert_llt.cc b/test/cutils/utils_convert/utils_convert_llt.cc new file mode 100644 index 0000000..4c76365 --- /dev/null +++ b/test/cutils/utils_convert/utils_convert_llt.cc @@ -0,0 +1,242 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: utils_convert llt + * Author: tanyifeng + * Create: 2019-07-08 + */ + +#include +#include +#include +#include +#include +#include "mock.h" +#include "utils_convert.h" + +TEST(utils_convert, test_util_safe_u16) +{ + int ret; + uint16_t converted; + ret = util_safe_u16("255", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 255); + + ret = util_safe_u16("255", NULL); + ASSERT_NE(ret, 0); + + ret = util_safe_u16("-1", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_u16("0", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 0); + + ret = util_safe_u16("1.23", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_u16("1x", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_u16(std::to_string((long long)UINT16_MAX + 1).c_str(), &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_u16("NULL", &converted); + ASSERT_NE(ret, 0); +} + +TEST(utils_convert, test_util_safe_int) +{ + int ret; + int converted; + ret = util_safe_int("123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 123456); + + ret = util_safe_int("123456", NULL); + ASSERT_NE(ret, 0); + + ret = util_safe_int("-123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, -123456); + + ret = util_safe_int("0", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 0); + + ret = util_safe_int("1.23", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_int("1x", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_int(std::to_string((long long)INT_MIN - 1).c_str(), &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_int(std::to_string((long long)INT_MAX + 1).c_str(), &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_int("NULL", &converted); + ASSERT_NE(ret, 0); +} + +TEST(utils_convert, test_util_safe_uint) +{ + int ret; + unsigned int converted; + ret = util_safe_uint("123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 123456); + + ret = util_safe_uint("123456", NULL); + ASSERT_NE(ret, 0); + + ret = util_safe_uint("-123456", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_uint("0", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 0); + + ret = util_safe_uint("1.23", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_uint("1x", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_uint(std::to_string((long long)UINT_MAX + 1).c_str(), &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_uint("NULL", &converted); + ASSERT_NE(ret, 0); +} + +TEST(utils_convert, test_util_safe_llong) +{ + int ret; + long long converted; + ret = util_safe_llong("123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 123456); + + ret = util_safe_llong("123456", NULL); + ASSERT_NE(ret, 0); + + ret = util_safe_llong("-123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, -123456); + + ret = util_safe_llong("0", &converted); + ASSERT_EQ(ret, 0); + ASSERT_EQ(converted, 0); + + ret = util_safe_llong("1.23", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_llong("1x", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_llong("-9223372036854775809", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_llong("9223372036854775808", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_llong("NULL", &converted); + ASSERT_NE(ret, 0); +} + +TEST(utils_convert, test_util_safe_strtod) +{ + int ret; + double converted; + ret = util_safe_strtod("123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_DOUBLE_EQ(converted, 123456); + + ret = util_safe_strtod("123456", NULL); + ASSERT_NE(ret, 0); + + ret = util_safe_strtod("-123456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_DOUBLE_EQ(converted, -123456); + + ret = util_safe_strtod("0", &converted); + ASSERT_EQ(ret, 0); + ASSERT_DOUBLE_EQ(converted, 0); + + ret = util_safe_strtod("123.456", &converted); + ASSERT_EQ(ret, 0); + ASSERT_DOUBLE_EQ(converted, 123.456); + + ret = util_safe_strtod("1x", &converted); + ASSERT_NE(ret, 0); + + ret = util_safe_strtod("NULL", &converted); + ASSERT_NE(ret, 0); +} + +TEST(utils_convert, test_util_str_to_bool) +{ + int ret; + bool converted; + ret = util_str_to_bool("1", &converted); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(converted); + + ret = util_str_to_bool("1", NULL); + ASSERT_NE(ret, 0); + + ret = util_str_to_bool("t", &converted); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(converted); + + ret = util_str_to_bool("T", &converted); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(converted); + + ret = util_str_to_bool("true", &converted); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(converted); + + ret = util_str_to_bool("True", &converted); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(converted); + + ret = util_str_to_bool("0", &converted); + ASSERT_EQ(ret, 0); + ASSERT_FALSE(converted); + + ret = util_str_to_bool("f", &converted); + ASSERT_EQ(ret, 0); + ASSERT_FALSE(converted); + + ret = util_str_to_bool("F", &converted); + ASSERT_EQ(ret, 0); + ASSERT_FALSE(converted); + + ret = util_str_to_bool("false", &converted); + ASSERT_EQ(ret, 0); + ASSERT_FALSE(converted); + + ret = util_str_to_bool("FALSE", &converted); + ASSERT_EQ(ret, 0); + ASSERT_FALSE(converted); + + ret = util_str_to_bool("False", &converted); + ASSERT_EQ(ret, 0); + ASSERT_FALSE(converted); + + ret = util_str_to_bool("x", &converted); + ASSERT_NE(ret, 0); + + ret = util_str_to_bool("NULL", &converted); + ASSERT_NE(ret, 0); +} diff --git a/test/cutils/utils_string/CMakeLists.txt b/test/cutils/utils_string/CMakeLists.txt index 81c2c17..37d4428 100644 --- a/test/cutils/utils_string/CMakeLists.txt +++ b/test/cutils/utils_string/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(${EXE} utils_string_llt.cc) target_include_directories(${EXE} PUBLIC + ${GTEST_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../../include ${CMAKE_CURRENT_SOURCE_DIR}/../../../src ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/sha256 diff --git a/test/cutils/utils_string/utils_string_llt.cc b/test/cutils/utils_string/utils_string_llt.cc index 0e08588..eb14a1c 100644 --- a/test/cutils/utils_string/utils_string_llt.cc +++ b/test/cutils/utils_string/utils_string_llt.cc @@ -1,5 +1,13 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2016-2019. All rights reserved. + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. * iSulad licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. * You may obtain a copy of Mulan PSL v1 at: diff --git a/test/image/CMakeLists.txt b/test/image/CMakeLists.txt new file mode 100644 index 0000000..b82d2b0 --- /dev/null +++ b/test/image/CMakeLists.txt @@ -0,0 +1,3 @@ +project(iSulad_LLT) + +add_subdirectory(oci) diff --git a/test/image/oci/CMakeLists.txt b/test/image/oci/CMakeLists.txt new file mode 100644 index 0000000..0de2f67 --- /dev/null +++ b/test/image/oci/CMakeLists.txt @@ -0,0 +1,8 @@ +project(iSulad_LLT) + +add_subdirectory(oci_config_merge) +add_subdirectory(oci_rootfs_remove) +add_subdirectory(oci_rootfs_mount) +add_subdirectory(oci_rootfs_umount) +add_subdirectory(oci_rootfs_export) +add_subdirectory(oci_rootfs_prepare) diff --git a/test/image/oci/oci_config_merge/CMakeLists.txt b/test/image/oci/oci_config_merge/CMakeLists.txt new file mode 100644 index 0000000..d8a07cb --- /dev/null +++ b/test/image/oci/oci_config_merge/CMakeLists.txt @@ -0,0 +1,58 @@ +project(iSulad_LLT) + +SET(EXE oci_config_merge_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/oci_config_merge.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_mount.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_extend.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/liblcrd.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_defs_linux.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config/lcrd_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/lcrd/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci/oci_llt_common.cc + ${CMAKE_BINARY_DIR}/json/imagetool_image.c + ${CMAKE_BINARY_DIR}/json/oci_image_spec.c + oci_config_merge_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${GTEST_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci + ) + +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,util_common_calloc_s -Wl,--wrap,merge_env") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/image/oci/oci_config_merge/imagetool_image.json b/test/image/oci/oci_config_merge/imagetool_image.json new file mode 100644 index 0000000..1a521fb --- /dev/null +++ b/test/image/oci/oci_config_merge/imagetool_image.json @@ -0,0 +1,51 @@ +{ + "id": "e4db68de4ff27c2adfea0c54bbb73a61a42f5b667c326de4d7d5b19ab71c6a3b", + "repo_tags": [ + "rnd-dockerhub.huawei.com/official/busybox:latest" + ], + "repo_digests": [ + "rnd-dockerhub.huawei.com/official/busybox@sha256:64da743694ece2ca88df34bf4c5378fdfc44a1a5b50478722e2ff98b82e4a5c9" + ], + "size": 1443304, + "created": "2019-06-15T00:19:54.402459069Z", + "Loaded": "2019-09-04T07:43:50.498364718Z", + "Spec": { + "created": "2019-06-15T00:19:54.402459069Z", + "architecture": "amd64", + "os": "linux", + "config": { + "Env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + ], + "Cmd": [ + "sh" + ] + }, + "rootfs": { + "diff_ids": [ + "sha256:6194458b07fcf01f1483d96cd6c34302ffff7f382bb151a6d023c4e80ba3050a" + ], + "type": "layers" + }, + "history": [ + { + "created": "2019-06-15T00:19:54.271494486Z", + "created_by": "/bin/sh -c #(nop) ADD file:b265aa0ea2ef7ff1f4a3e087217e75aca2c90f5c345406299664cc7969b2b28e in / " + }, + { + "created": "2019-06-15T00:19:54.402459069Z", + "created_by": "/bin/sh -c #(nop) CMD [\"sh\"]", + "empty_layer": true + } + ] + }, + "Healthcheck": { + "Test": [ + "CMD-SHELL", + "echo hello || exit 1" + ], + "Interval": 5000000000, + "Timeout": 3000000000, + "ExitOnUnhealthy": true + } +} diff --git a/test/image/oci/oci_config_merge/oci_config_merge_llt.cc b/test/image/oci/oci_config_merge/oci_config_merge_llt.cc new file mode 100644 index 0000000..86d4c00 --- /dev/null +++ b/test/image/oci/oci_config_merge/oci_config_merge_llt.cc @@ -0,0 +1,479 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_config_merge llt + * Author: wangfengtu + * Create: 2019-08-20 + */ + +#include +#include +#include +#include +#include "mock.h" +#include "oci_runtime_spec.h" +#include "oci_config_merge.h" +#include "imagetool_image.h" +#include "oci_llt_common.h" + +#define IMAGETOOL_IMAGE_FILE "image/oci/oci_config_merge/imagetool_image.json" +#define OCI_RUNTIME_SPEC_FILE "image/oci/oci_config_merge/oci_runtime_spec.json" +#define MALLOC_COUNT 5 + +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(merge_env, int, (oci_runtime_spec *oci_spec, const char **env, size_t env_len)); + DEFINE_WRAPPER(merge_env, int, (oci_runtime_spec *oci_spec, const char **env, size_t env_len), \ + (oci_spec, env, env_len)); +} + +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 NULL; + } else { + return __real_util_common_calloc_s(size); + } +} + +TEST(oci_config_merge_llt, test_oci_image_merge_config) +{ + char *imagetool_image_file = NULL; + char *oci_runtime_spec_file = NULL; + imagetool_image *tool_image = NULL; + oci_runtime_spec *oci_spec = NULL; + container_custom_config *custom_config = NULL; + char *err = NULL; + int i = 0; + + // All parameter NULL + ASSERT_NE(oci_image_merge_config(NULL, NULL, NULL), 0); + + // Parameter image_config is NULL + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + ASSERT_NE(oci_image_merge_config(NULL, oci_spec, custom_config), 0); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free(custom_config); + custom_config = NULL; + + // Parameter oci_spec is NULL + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + ASSERT_NE(oci_image_merge_config(tool_image, NULL, custom_config), 0); + + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + + // Parameter custom_spec is NULL + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + ASSERT_NE(oci_image_merge_config(tool_image, oci_spec, NULL), 0); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + + // All parameter collect + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + ASSERT_EQ(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + + // image_config's volumes not NULL + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + ASSERT_TRUE(tool_image->spec->config != NULL); + ASSERT_TRUE(tool_image->spec->config->volumes == NULL); + free(err); + err = NULL; + tool_image->spec->config->volumes = (defs_map_string_object *) util_common_calloc_s(sizeof(defs_map_string_object)); + ASSERT_TRUE(tool_image->spec->config->volumes != NULL); + tool_image->spec->config->volumes->keys = single_array_from_string("/data"); + ASSERT_TRUE(tool_image->spec->config->volumes->keys != NULL); + tool_image->spec->config->volumes->len = 1; + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + ASSERT_NE(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + + // Config merge condition 1 + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + ASSERT_TRUE(tool_image->spec->config != NULL); + + free(tool_image->spec->config->working_dir); + tool_image->spec->config->working_dir = util_strdup_s("/root"); + ASSERT_TRUE(tool_image->spec->config->working_dir != NULL); + + util_free_array(tool_image->spec->config->env); + tool_image->spec->config->env = single_array_from_string("A=a"); + ASSERT_TRUE(tool_image->spec->config->env != NULL); + tool_image->spec->config->env_len = 1; + + util_free_array(tool_image->spec->config->cmd); + tool_image->spec->config->cmd = single_array_from_string("/bin/echo"); + ASSERT_TRUE(tool_image->spec->config->cmd != NULL); + tool_image->spec->config->cmd_len = 1; + + util_free_array(tool_image->spec->config->entrypoint); + tool_image->spec->config->entrypoint = single_array_from_string("/bin/ls"); + ASSERT_TRUE(tool_image->spec->config->entrypoint != NULL); + tool_image->spec->config->entrypoint_len = 1; + + free(tool_image->spec->config->user); + tool_image->spec->config->user = util_strdup_s("mail"); + ASSERT_TRUE(tool_image->spec->config->user != NULL); + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + free(oci_spec->process->cwd); + oci_spec->process->cwd = util_strdup_s("/bin/pwd"); + ASSERT_TRUE(oci_spec->process->cwd != NULL); + + util_free_array(oci_spec->process->env); + oci_spec->process->env = single_array_from_string("B=b"); + ASSERT_TRUE(oci_spec->process->env != NULL); + oci_spec->process->env_len = 1; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + util_free_array(custom_config->cmd); + custom_config->cmd = single_array_from_string("/bin/mkdir"); + ASSERT_TRUE(custom_config->cmd != NULL); + custom_config->cmd_len = 1; + + util_free_array(custom_config->entrypoint); + custom_config->entrypoint = single_array_from_string("/bin/rmdir"); + ASSERT_TRUE(custom_config->entrypoint != NULL); + custom_config->entrypoint_len = 1; + + free(custom_config->user); + custom_config->user = util_strdup_s("daemon"); + ASSERT_TRUE(custom_config->user != NULL); + + custom_config->health_check = (defs_health_check *) util_common_calloc_s(sizeof(defs_health_check)); + ASSERT_TRUE(custom_config->health_check != NULL); + + ASSERT_EQ(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + + ASSERT_STREQ(oci_spec->process->cwd, "/root"); + + ASSERT_TRUE(oci_spec->process->env != NULL); + ASSERT_STREQ(oci_spec->process->env[0], "B=b"); + ASSERT_STREQ(oci_spec->process->env[1], "A=a"); + ASSERT_EQ(oci_spec->process->env_len, 2); + + ASSERT_TRUE(custom_config->cmd != NULL); + ASSERT_STREQ(custom_config->cmd[0], "/bin/mkdir"); + ASSERT_EQ(custom_config->cmd_len, 1); + + ASSERT_TRUE(custom_config->entrypoint != NULL); + ASSERT_STREQ(custom_config->entrypoint[0], "/bin/rmdir"); + ASSERT_EQ(custom_config->entrypoint_len, 1); + + ASSERT_STREQ(custom_config->user, "daemon"); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + + // Config merge condition 2 + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + ASSERT_TRUE(tool_image->spec->config != NULL); + + free(tool_image->spec->config->working_dir); + tool_image->spec->config->working_dir = NULL; + + util_free_array(tool_image->spec->config->env); + tool_image->spec->config->env = NULL; + tool_image->spec->config->env_len = 0; + + util_free_array(tool_image->spec->config->cmd); + tool_image->spec->config->cmd = single_array_from_string("/bin/echo"); + ASSERT_TRUE(tool_image->spec->config->cmd != NULL); + tool_image->spec->config->cmd_len = 1; + + util_free_array(tool_image->spec->config->entrypoint); + tool_image->spec->config->entrypoint = single_array_from_string("/bin/ls"); + ASSERT_TRUE(tool_image->spec->config->entrypoint != NULL); + tool_image->spec->config->entrypoint_len = 1; + + free(tool_image->spec->config->user); + tool_image->spec->config->user = util_strdup_s("mail"); + ASSERT_TRUE(tool_image->spec->config->user != NULL); + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + free(oci_spec->process->cwd); + oci_spec->process->cwd = util_strdup_s("/bin/pwd"); + ASSERT_TRUE(oci_spec->process->cwd != NULL); + + util_free_array(oci_spec->process->env); + oci_spec->process->env = single_array_from_string("B=b"); + ASSERT_TRUE(oci_spec->process->env != NULL); + oci_spec->process->env_len = 1; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + util_free_array(custom_config->cmd); + custom_config->cmd = NULL; + custom_config->cmd_len = 0; + + util_free_array(custom_config->entrypoint); + custom_config->entrypoint = NULL; + custom_config->entrypoint_len = 0; + + free(custom_config->user); + custom_config->user = NULL; + + ASSERT_EQ(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + + ASSERT_STREQ(oci_spec->process->cwd, "/bin/pwd"); + + ASSERT_TRUE(oci_spec->process->env != NULL); + ASSERT_STREQ(oci_spec->process->env[0], "B=b"); + ASSERT_EQ(oci_spec->process->env_len, 1); + + ASSERT_TRUE(custom_config->cmd != NULL); + ASSERT_STREQ(custom_config->cmd[0], "/bin/echo"); + ASSERT_EQ(custom_config->cmd_len, 1); + + ASSERT_TRUE(custom_config->entrypoint != NULL); + ASSERT_STREQ(custom_config->entrypoint[0], "/bin/ls"); + ASSERT_EQ(custom_config->entrypoint_len, 1); + + ASSERT_STREQ(custom_config->user, "mail"); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + + // Test malloc failed + for (i = 0; i < MALLOC_COUNT; i++) { + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + // Test update_health_check_from_image executed failed caused by malloc failed. + if (i == 3) { + g_malloc_match = 2; + custom_config->health_check = (defs_health_check *) util_common_calloc_s(sizeof(defs_health_check)); + ASSERT_TRUE(custom_config->health_check != NULL); + } + // Test do_duplicate_entrypoints executed failed caused by malloc failed. + if (i == 4) { + g_malloc_match = 2; + util_free_array(tool_image->spec->config->entrypoint); + tool_image->spec->config->entrypoint = single_array_from_string("/bin/ls"); + ASSERT_TRUE(tool_image->spec->config->entrypoint != NULL); + tool_image->spec->config->entrypoint_len = 1; + } + + MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail); + ASSERT_NE(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + MOCK_CLEAR(util_common_calloc_s); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + } + + // Test merge_env fail + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + MOCK_SET(merge_env, -1); + ASSERT_NE(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + MOCK_CLEAR(merge_env); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; + + // Test test_len == NULL + imagetool_image_file = json_path(IMAGETOOL_IMAGE_FILE); + ASSERT_TRUE(imagetool_image_file != NULL); + tool_image = imagetool_image_parse_file(imagetool_image_file, NULL, &err); + ASSERT_TRUE(tool_image != NULL); + ASSERT_TRUE(tool_image->spec != NULL); + free(err); + err = NULL; + + oci_runtime_spec_file = json_path(OCI_RUNTIME_SPEC_FILE); + ASSERT_TRUE(oci_runtime_spec_file != NULL); + oci_spec = oci_runtime_spec_parse_file(oci_runtime_spec_file, NULL, &err); + ASSERT_TRUE(oci_spec != NULL); + free(err); + err = NULL; + + util_free_array(tool_image->healthcheck->test); + tool_image->healthcheck->test_len = 0; + + custom_config = (container_custom_config *) util_common_calloc_s(sizeof(container_custom_config)); + ASSERT_TRUE(custom_config != NULL); + + ASSERT_NE(oci_image_merge_config(tool_image, oci_spec, custom_config), 0); + + free_oci_runtime_spec(oci_spec); + oci_spec = NULL; + free_imagetool_image(tool_image); + tool_image = NULL; + free(custom_config); + custom_config = NULL; +} diff --git a/test/image/oci/oci_config_merge/oci_runtime_spec.json b/test/image/oci/oci_config_merge/oci_runtime_spec.json new file mode 100644 index 0000000..cdc3a4e --- /dev/null +++ b/test/image/oci/oci_config_merge/oci_runtime_spec.json @@ -0,0 +1,741 @@ +{ + "ociVersion": "1.0.0-rc5-dev", + "hooks": { + + }, + "annotations": { + "log.console.file": "none", + "log.console.filerotate": "7", + "log.console.filesize": "1MB", + "rootfs.mount": "/var/lib/lcrd/mnt/rootfs", + "native.umask": "secure" + }, + "hostname": "localhost", + "mounts": [ + { + "source": "proc", + "destination": "/proc", + "options": [ + "nosuid", + "noexec", + "nodev" + ], + "type": "proc" + }, + { + "source": "tmpfs", + "destination": "/dev", + "options": [ + "nosuid", + "strictatime", + "mode=755", + "size=65536k" + ], + "type": "tmpfs" + }, + { + "source": "devpts", + "destination": "/dev/pts", + "options": [ + "nosuid", + "noexec", + "newinstance", + "ptmxmode=0666", + "mode=0620", + "gid=5" + ], + "type": "devpts" + }, + { + "source": "sysfs", + "destination": "/sys", + "options": [ + "nosuid", + "noexec", + "nodev", + "ro" + ], + "type": "sysfs" + }, + { + "source": "shm", + "destination": "/dev/shm", + "options": [ + "nosuid", + "noexec", + "nodev", + "mode=1777", + "size=67108864" + ], + "type": "tmpfs" + }, + { + "source": "cgroup", + "destination": "/sys/fs/cgroup", + "options": [ + "nosuid", + "noexec", + "nodev", + "ro" + ], + "type": "cgroup" + }, + { + "source": "mqueue", + "destination": "/dev/mqueue", + "options": [ + "nosuid", + "noexec", + "nodev" + ], + "type": "mqueue" + }, + { + "source": "/var/lib/lcrd/engines/lcr/ad6c3f33518ed7e17a6d889a1327aa386f8b869927e2540821fb02310f567310/hostname", + "destination": "/etc/hostname", + "options": [ + "rbind", + "rprivate" + ], + "type": "bind" + }, + { + "source": "/var/lib/lcrd/engines/lcr/ad6c3f33518ed7e17a6d889a1327aa386f8b869927e2540821fb02310f567310/resolv.conf", + "destination": "/etc/resolv.conf", + "options": [ + "rbind", + "rprivate" + ], + "type": "bind" + }, + { + "source": "/var/lib/lcrd/engines/lcr/ad6c3f33518ed7e17a6d889a1327aa386f8b869927e2540821fb02310f567310/hosts", + "destination": "/etc/hosts", + "options": [ + "rbind", + "rprivate" + ], + "type": "bind" + } + ], + "root": { + "path": "/var/lib/lcrd/storage/overlay/40c51530fc1b4610f7f56633ef78fc5e4ad1a6f14cad59d5a1d012ff800c4289/merged" + }, + "process": { + "args": [ + "sh" + ], + "consoleSize": { + + }, + "cwd": "/", + "env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "TERM=xterm", + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + ], + "terminal": true, + "user": { + + }, + "capabilities": { + "bounding": [ + "CAP_CHOWN", + "CAP_DAC_OVERRIDE", + "CAP_FSETID", + "CAP_FOWNER", + "CAP_MKNOD", + "CAP_NET_RAW", + "CAP_SETGID", + "CAP_SETUID", + "CAP_SETFCAP", + "CAP_SETPCAP", + "CAP_NET_BIND_SERVICE", + "CAP_SYS_CHROOT", + "CAP_KILL", + "CAP_AUDIT_WRITE" + ] + } + }, + "linux": { + "namespaces": [ + { + "type": "pid" + }, + { + "type": "network" + }, + { + "type": "ipc" + }, + { + "type": "uts" + }, + { + "type": "mount" + } + ], + "resources": { + "devices": [ + { + "type": "a", + "major": -1, + "minor": -1, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": -1, + "minor": -1, + "access": "m" + }, + { + "allow": true, + "type": "b", + "major": -1, + "minor": -1, + "access": "m" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 3, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 5, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 7, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 1, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 5, + "minor": 2, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 8, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 1, + "minor": 9, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 136, + "minor": -1, + "access": "rwm" + }, + { + "allow": true, + "type": "c", + "major": 10, + "minor": 200, + "access": "rwm" + }, + { + "type": "c", + "major": 10, + "minor": 229, + "access": "rwm" + } + ] + }, + "seccomp": { + "defaultAction": "SCMP_ACT_ERRNO", + "architectures": [ + "SCMP_ARCH_X86_64", + "SCMP_ARCH_X86", + "SCMP_ARCH_X32", + "SCMP_ARCH_AARCH64", + "SCMP_ARCH_ARM" + ], + "syscalls": [ + { + "names": [ + "accept", + "accept4", + "access", + "adjtimex", + "alarm", + "bind", + "brk", + "capget", + "capset", + "chdir", + "chmod", + "chown", + "chown32", + "clock_getres", + "clock_gettime", + "clock_nanosleep", + "close", + "connect", + "copy_file_range", + "creat", + "dup", + "dup2", + "dup3", + "epoll_create", + "epoll_create1", + "epoll_ctl", + "epoll_ctl_old", + "epoll_pwait", + "epoll_wait", + "epoll_wait_old", + "eventfd", + "eventfd2", + "execve", + "execveat", + "exit", + "exit_group", + "faccessat", + "fadvise64", + "fadvise64_64", + "fallocate", + "fanotify_mark", + "fchdir", + "fchmod", + "fchmodat", + "fchown", + "fchown32", + "fchownat", + "fcntl", + "fcntl64", + "fdatasync", + "fgetxattr", + "flistxattr", + "flock", + "fork", + "fremovexattr", + "fsetxattr", + "fstat", + "fstat64", + "fstatat64", + "fstatfs", + "fstatfs64", + "fsync", + "ftruncate", + "ftruncate64", + "futex", + "futimesat", + "getcpu", + "getcwd", + "getdents", + "getdents64", + "getegid", + "getegid32", + "geteuid", + "geteuid32", + "getgid", + "getgid32", + "getgroups", + "getgroups32", + "getitimer", + "getpeername", + "getpgid", + "getpgrp", + "getpid", + "getppid", + "getpriority", + "getrandom", + "getresgid", + "getresgid32", + "getresuid", + "getresuid32", + "getrlimit", + "get_robust_list", + "getrusage", + "getsid", + "getsockname", + "getsockopt", + "get_thread_area", + "gettid", + "gettimeofday", + "getuid", + "getuid32", + "getxattr", + "inotify_add_watch", + "inotify_init", + "inotify_init1", + "inotify_rm_watch", + "io_cancel", + "ioctl", + "io_destroy", + "io_getevents", + "ioprio_get", + "ioprio_set", + "io_setup", + "io_submit", + "ipc", + "kill", + "lchown", + "lchown32", + "lgetxattr", + "link", + "linkat", + "listen", + "listxattr", + "llistxattr", + "_llseek", + "lremovexattr", + "lseek", + "lsetxattr", + "lstat", + "lstat64", + "madvise", + "memfd_create", + "mincore", + "mkdir", + "mkdirat", + "mknod", + "mknodat", + "mlock", + "mlock2", + "mlockall", + "mmap", + "mmap2", + "mprotect", + "mq_getsetattr", + "mq_notify", + "mq_open", + "mq_timedreceive", + "mq_timedsend", + "mq_unlink", + "mremap", + "msgctl", + "msgget", + "msgrcv", + "msgsnd", + "msync", + "munlock", + "munlockall", + "munmap", + "nanosleep", + "newfstatat", + "_newselect", + "open", + "openat", + "pause", + "pipe", + "pipe2", + "poll", + "ppoll", + "prctl", + "pread64", + "preadv", + "preadv2", + "prlimit64", + "pselect6", + "pwrite64", + "pwritev", + "pwritev2", + "read", + "readahead", + "readlink", + "readlinkat", + "readv", + "recv", + "recvfrom", + "recvmmsg", + "recvmsg", + "remap_file_pages", + "removexattr", + "rename", + "renameat", + "renameat2", + "restart_syscall", + "rmdir", + "rt_sigaction", + "rt_sigpending", + "rt_sigprocmask", + "rt_sigqueueinfo", + "rt_sigreturn", + "rt_sigsuspend", + "rt_sigtimedwait", + "rt_tgsigqueueinfo", + "sched_getaffinity", + "sched_getattr", + "sched_getparam", + "sched_get_priority_max", + "sched_get_priority_min", + "sched_getscheduler", + "sched_rr_get_interval", + "sched_setaffinity", + "sched_setattr", + "sched_setparam", + "sched_setscheduler", + "sched_yield", + "seccomp", + "select", + "semctl", + "semget", + "semop", + "semtimedop", + "send", + "sendfile", + "sendfile64", + "sendmmsg", + "sendmsg", + "sendto", + "setfsgid", + "setfsgid32", + "setfsuid", + "setfsuid32", + "setgid", + "setgid32", + "setgroups", + "setgroups32", + "setitimer", + "setpgid", + "setpriority", + "setregid", + "setregid32", + "setresgid", + "setresgid32", + "setresuid", + "setresuid32", + "setreuid", + "setreuid32", + "setrlimit", + "set_robust_list", + "setsid", + "setsockopt", + "set_thread_area", + "set_tid_address", + "setuid", + "setuid32", + "setxattr", + "shmat", + "shmctl", + "shmdt", + "shmget", + "shutdown", + "sigaltstack", + "signalfd", + "signalfd4", + "sigreturn", + "socket", + "socketcall", + "socketpair", + "splice", + "stat", + "stat64", + "statfs", + "statfs64", + "statx", + "symlink", + "symlinkat", + "sync", + "sync_file_range", + "syncfs", + "sysinfo", + "tee", + "tgkill", + "time", + "timer_create", + "timer_delete", + "timerfd_create", + "timerfd_gettime", + "timerfd_settime", + "timer_getoverrun", + "timer_gettime", + "timer_settime", + "times", + "tkill", + "truncate", + "truncate64", + "ugetrlimit", + "umask", + "uname", + "unlink", + "unlinkat", + "utime", + "utimensat", + "utimes", + "vfork", + "vmsplice", + "wait4", + "waitid", + "waitpid", + "write", + "writev" + ], + "action": "SCMP_ACT_ALLOW" + }, + { + "names": [ + "ptrace" + ], + "action": "SCMP_ACT_ALLOW" + }, + { + "names": [ + "personality" + ], + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "names": [ + "personality" + ], + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "value": 8, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "names": [ + "personality" + ], + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "value": 131072, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "names": [ + "personality" + ], + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "value": 131080, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "names": [ + "personality" + ], + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "value": 4294967295, + "op": "SCMP_CMP_EQ" + } + ] + }, + { + "names": [ + "arm_fadvise64_64", + "arm_sync_file_range", + "sync_file_range2", + "breakpoint", + "cacheflush", + "set_tls" + ], + "action": "SCMP_ACT_ALLOW" + }, + { + "names": [ + "arch_prctl" + ], + "action": "SCMP_ACT_ALLOW" + }, + { + "names": [ + "modify_ldt" + ], + "action": "SCMP_ACT_ALLOW" + }, + { + "names": [ + "clone" + ], + "action": "SCMP_ACT_ALLOW", + "args": [ + { + "value": 2080505856, + "op": "SCMP_CMP_MASKED_EQ" + } + ] + }, + { + "names": [ + "chroot" + ], + "action": "SCMP_ACT_ALLOW" + } + ] + }, + "maskedPaths": [ + "/proc/acpi", + "/proc/config.gz", + "/proc/kcore", + "/proc/keys", + "/proc/latency_stats", + "/proc/timer_list", + "/proc/timer_stats", + "/proc/sched_debug", + "/proc/scsi", + "/proc/signo", + "/proc/sig_catch", + "/proc/kbox", + "/proc/oom_extend", + "/proc/fdthreshold", + "/proc/fdstat", + "/proc/fdenable", + "/proc/files_panic_enable", + "/sys/firmware", + "/proc/cpuirqstat", + "/proc/memstat", + "/proc/iomem_ext", + "/proc/livepatch" + ], + "readonlyPaths": [ + "/proc/asound", + "/proc/bus", + "/proc/fs", + "/proc/irq", + "/proc/sys", + "/proc/sysrq-trigger", + "/proc/sysrq-region-size" + ] + } +} diff --git a/test/image/oci/oci_llt_common.cc b/test/image/oci/oci_llt_common.cc new file mode 100644 index 0000000..179349a --- /dev/null +++ b/test/image/oci/oci_llt_common.cc @@ -0,0 +1,73 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_remove llt + * Author: wangfengtu + * Create: 2019-08-29 + */ + +#include +#include +#include +#include +#include "utils.h" +#include "oci_llt_common.h" + +int execvp_success(const char *file, char * const argv[]) +{ + execlp("echo", "echo"); + return -1; +} + +char **single_array_from_string(const char *value) +{ + char **arr = NULL; + int ret = 0; + + ret = util_array_append(&arr, value); + if (ret != 0) { + util_free_array(arr); + return NULL; + } + + return arr; +} + +char **conf_get_storage_opts_success() +{ + return single_array_from_string("overlay.override_kernel_check=true"); +} + +char **conf_get_registry_list_success() +{ + return single_array_from_string("docker.io"); +} + +char **conf_get_insecure_registry_list_success() +{ + return single_array_from_string("rnd-dockerhub.huawei.com"); +} + +char *json_path(const char *file) +{ + char base_path[PATH_MAX] = {0}; + char *json_file = NULL; + + if (getcwd(base_path, PATH_MAX) == NULL) { + return NULL; + } + + json_file = util_path_join(base_path, file); + if (json_file == NULL) { + return NULL; + } + + return json_file; +} diff --git a/test/image/oci/oci_llt_common.h b/test/image/oci/oci_llt_common.h new file mode 100644 index 0000000..daafcb8 --- /dev/null +++ b/test/image/oci/oci_llt_common.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_remove llt + * Author: wangfengtu + * Create: 2019-08-29 + */ + +#ifndef __OCI_LLT_COMMON_H +#define __OCI_LLT_COMMON_H + +#include +#include +#include "utils.h" +#include "oci_llt_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define DECLARE_OCI_LLT_COMMON_WRAPPER \ + extern "C" {\ + DECLARE_WRAPPER(conf_get_graph_rootpath, char *, ());\ + DEFINE_WRAPPER(conf_get_graph_rootpath, char *, (), ());\ + \ + DECLARE_WRAPPER(conf_get_graph_run_path, char *, ());\ + DEFINE_WRAPPER(conf_get_graph_run_path, char *, (), ());\ + \ + DECLARE_WRAPPER(conf_get_lcrd_storage_driver, char *, ());\ + DEFINE_WRAPPER(conf_get_lcrd_storage_driver, char *, (), ());\ + \ + DECLARE_WRAPPER_V(conf_get_storage_opts, char **, ());\ + DEFINE_WRAPPER_V(conf_get_storage_opts, char **, (), ());\ + \ + DECLARE_WRAPPER_V(conf_get_registry_list, char **, ());\ + DEFINE_WRAPPER_V(conf_get_registry_list, char **, (), ());\ + \ + DECLARE_WRAPPER_V(conf_get_insecure_registry_list, char **, ());\ + DEFINE_WRAPPER_V(conf_get_insecure_registry_list, char **, (), ());\ + \ + DECLARE_WRAPPER(conf_get_im_opt_timeout, unsigned int, ());\ + DEFINE_WRAPPER(conf_get_im_opt_timeout, unsigned int, (), ());\ + \ + DECLARE_WRAPPER_V(execvp, int, (const char *file, char * const argv[]));\ + DEFINE_WRAPPER_V(execvp, int, (const char *file, char * const argv[]), (file, argv));\ + } + +#define MOCK_SET_DEFAULT_ISULAD_KIT_OPTS \ + {\ + MOCK_SET(conf_get_graph_rootpath, util_strdup_s("/var/lib/lcrd/storage"));\ + MOCK_SET(conf_get_graph_run_path, util_strdup_s("/var/run/lcrd/storage"));\ + MOCK_SET(conf_get_lcrd_storage_driver, util_strdup_s("overlay"));\ + MOCK_SET_V(conf_get_storage_opts, conf_get_storage_opts_success);\ + MOCK_SET_V(conf_get_registry_list, conf_get_registry_list_success);\ + MOCK_SET_V(conf_get_insecure_registry_list, conf_get_insecure_registry_list_success);\ + MOCK_SET(conf_get_im_opt_timeout, 300);\ + }while(0); + +#define MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS \ + {\ + MOCK_CLEAR(conf_get_graph_rootpath);\ + MOCK_CLEAR(conf_get_graph_run_path);\ + MOCK_CLEAR(conf_get_lcrd_storage_driver);\ + MOCK_CLEAR(conf_get_storage_opts);\ + MOCK_CLEAR(conf_get_registry_list);\ + MOCK_CLEAR(conf_get_insecure_registry_list);\ + MOCK_CLEAR(conf_get_im_opt_timeout);\ + }while(0); + +char *json_path(const char *file); +int execvp_success(const char *file, char * const argv[]); +char **conf_get_storage_opts_success(); +char **conf_get_registry_list_success(); +char **conf_get_insecure_registry_list_success(); +char **single_array_from_string(const char *value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/test/image/oci/oci_rootfs_export/CMakeLists.txt b/test/image/oci/oci_rootfs_export/CMakeLists.txt new file mode 100644 index 0000000..624de79 --- /dev/null +++ b/test/image/oci/oci_rootfs_export/CMakeLists.txt @@ -0,0 +1,60 @@ +project(iSulad_LLT) + +SET(EXE oci_rootfs_export_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/oci_rootfs_export.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_mount.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_extend.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/liblcrd.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_defs_linux.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config/lcrd_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/lcrd/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/isula_imtool_interface.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/driver.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2/driver_overlay2.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci/oci_llt_common.cc + oci_rootfs_export_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2 + ) + +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,conf_get_graph_rootpath -Wl,--wrap,conf_get_graph_run_path -Wl,--wrap,conf_get_lcrd_storage_driver -Wl,--wrap,conf_get_storage_opts -Wl,--wrap,conf_get_registry_list -Wl,--wrap,conf_get_insecure_registry_list -Wl,--wrap,conf_get_im_opt_timeout -Wl,--wrap,execvp -Wl,--wrap,util_exec_cmd") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/image/oci/oci_rootfs_export/oci_rootfs_export_llt.cc b/test/image/oci/oci_rootfs_export/oci_rootfs_export_llt.cc new file mode 100644 index 0000000..88b7713 --- /dev/null +++ b/test/image/oci/oci_rootfs_export/oci_rootfs_export_llt.cc @@ -0,0 +1,180 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_export llt + * Author: wangfengtu + * Create: 2019-08-26 + */ + +#include +#include +#include +#include +#include "mock.h" +#include "utils.h" +#include "oci_rootfs_export.h" +#include "oci_llt_common.h" + +static int g_exec_cmd_count = 0; + +DECLARE_OCI_LLT_COMMON_WRAPPER + +extern "C" { + DECLARE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg)); + DEFINE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg), \ + (cb_func, args, stdin_msg, stdout_msg, stderr_msg)); +} + +static bool util_exec_cmd_fail(exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, + char **stderr_msg) +{ + g_exec_cmd_count++; + if (g_exec_cmd_count == 1) { + *stderr_msg = util_strdup_s("Device or Resource Busy"); + return false; + } + + if (g_exec_cmd_count == 2) { + *stderr_msg = NULL; + return false; + } + + return 0; +} + +TEST(oci_rootfs_export_llt, test_export_rootfs) +{ + rootfs_export_request *req = NULL; + rootfs_export_response *resp = NULL; + int i = 0; + + // Test parameter NULL part1 + ASSERT_NE(export_rootfs(NULL, NULL), 0); + + // Test parameter NULL part2 + ASSERT_NE(export_rootfs(NULL, &resp), 0); + free_rootfs_export_response(resp); + resp = NULL; + + // Test parameter NULL part3 + req = (rootfs_export_request*)util_common_calloc_s(sizeof(rootfs_export_request)); + ASSERT_TRUE(req != NULL); + req->id = util_strdup_s("3aa25673f3683f225cb4a8209347acd5a8ea9c2d4c9e02d394cbaab6933d9c4d"); + ASSERT_TRUE(req->id != NULL); + ASSERT_NE(export_rootfs(req, &resp), 0); + free_rootfs_export_request(req); + req = NULL; + free_rootfs_export_response(resp); + resp = NULL; + + // Test content of parameter NULL + req = (rootfs_export_request*)util_common_calloc_s(sizeof(rootfs_export_request)); + ASSERT_TRUE(req != NULL); + ASSERT_NE(export_rootfs(req, &resp), 0); + free_rootfs_export_request(req); + req = NULL; + free_rootfs_export_response(resp); + resp = NULL; + + // Test parameter collect + req = (rootfs_export_request*)util_common_calloc_s(sizeof(rootfs_export_request)); + ASSERT_TRUE(req != NULL); + req->file = util_strdup_s("/root/root-fs"); + ASSERT_TRUE(req->file != NULL); + req->id = util_strdup_s("3aa25673f3683f225cb4a8209347acd5a8ea9c2d4c9e02d394cbaab6933d9c4d"); + ASSERT_TRUE(req->id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + MOCK_SET_V(execvp, execvp_success); + ASSERT_EQ(export_rootfs(req, &resp), 0); + ASSERT_TRUE(resp != NULL); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + MOCK_CLEAR(execvp); + + free_rootfs_export_request(req); + req = NULL; + free_rootfs_export_response(resp); + resp = NULL; + + // Put MOCK_SET_V here to avoid warning "(style) Local variable MOCK_SET_V shadows outer variable" + MOCK_SET_V(util_exec_cmd, util_exec_cmd_fail); + // 1. Test util_exec_cmd failed with stderr_buffer != NULL + // 2. Test util_exec_cmd failed with stderr_buffer == NULL + for (i = 0; i < 3; i++) { + req = (rootfs_export_request*)util_common_calloc_s(sizeof(rootfs_export_request)); + ASSERT_TRUE(req != NULL); + req->file = util_strdup_s("/root/root-fs"); + ASSERT_TRUE(req->file != NULL); + req->id = util_strdup_s("3aa25673f3683f225cb4a8209347acd5a8ea9c2d4c9e02d394cbaab6933d9c4d"); + ASSERT_TRUE(req->id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + ASSERT_NE(export_rootfs(req, &resp), 0); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + + free_rootfs_export_request(req); + req = NULL; + free_rootfs_export_response(resp); + resp = NULL; + } + MOCK_CLEAR(util_exec_cmd); +} + +TEST(oci_rootfs_export_llt, test_free_rootfs_export_request) +{ + rootfs_export_request *req = NULL; + + // Test free NULL + free_rootfs_export_request(NULL); + + // Test free file and id NULL + req = (rootfs_export_request*)util_common_calloc_s(sizeof(rootfs_export_request)); + ASSERT_TRUE(req != NULL); + + free_rootfs_export_request(req); + req = NULL; + + // Test free all Not NULL + req = (rootfs_export_request*)util_common_calloc_s(sizeof(rootfs_export_request)); + ASSERT_TRUE(req != NULL); + req->file = util_strdup_s("/root/root-fs"); + ASSERT_TRUE(req->file != NULL); + req->id = util_strdup_s("3aa25673f3683f225cb4a8209347acd5a8ea9c2d4c9e02d394cbaab6933d9c4d"); + ASSERT_TRUE(req->id != NULL); + + free_rootfs_export_request(req); + req = NULL; +} + +TEST(oci_rootfs_export_llt, test_free_rootfs_export_response) +{ + rootfs_export_response *resp = NULL; + + // Test free NULL + free_rootfs_export_response(NULL); + + // Test free errmsg NULL + resp = (rootfs_export_response*)util_common_calloc_s(sizeof(rootfs_export_response)); + ASSERT_TRUE(resp != NULL); + + free_rootfs_export_response(resp); + resp = NULL; + + // Test free all Not NULL + resp = (rootfs_export_response*)util_common_calloc_s(sizeof(rootfs_export_response)); + ASSERT_TRUE(resp != NULL); + resp->errmsg = util_strdup_s("This is error message"); + ASSERT_TRUE(resp->errmsg != NULL); + + free_rootfs_export_response(resp); + resp = NULL; +} diff --git a/test/image/oci/oci_rootfs_mount/CMakeLists.txt b/test/image/oci/oci_rootfs_mount/CMakeLists.txt new file mode 100644 index 0000000..0f42be3 --- /dev/null +++ b/test/image/oci/oci_rootfs_mount/CMakeLists.txt @@ -0,0 +1,60 @@ +project(iSulad_LLT) + +SET(EXE oci_rootfs_mount_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/oci_rootfs_mount.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_mount.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_extend.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/liblcrd.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_defs_linux.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config/lcrd_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/lcrd/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/isula_imtool_interface.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/driver.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2/driver_overlay2.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci/oci_llt_common.cc + oci_rootfs_mount_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2 + ) + +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,conf_get_graph_rootpath -Wl,--wrap,conf_get_graph_run_path -Wl,--wrap,conf_get_lcrd_storage_driver -Wl,--wrap,conf_get_storage_opts -Wl,--wrap,conf_get_registry_list -Wl,--wrap,conf_get_insecure_registry_list -Wl,--wrap,conf_get_im_opt_timeout -Wl,--wrap,execvp -Wl,--wrap,util_exec_cmd") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/image/oci/oci_rootfs_mount/oci_rootfs_mount_llt.cc b/test/image/oci/oci_rootfs_mount/oci_rootfs_mount_llt.cc new file mode 100644 index 0000000..5c7df8c --- /dev/null +++ b/test/image/oci/oci_rootfs_mount/oci_rootfs_mount_llt.cc @@ -0,0 +1,167 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_mount llt + * Author: wangfengtu + * Create: 2019-08-26 + */ + +#include +#include +#include +#include +#include "mock.h" +#include "utils.h" +#include "oci_rootfs_mount.h" +#include "oci_llt_common.h" + +static int g_exec_cmd_count = 0; + +DECLARE_OCI_LLT_COMMON_WRAPPER + +extern "C" { + DECLARE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg)); + DEFINE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg), \ + (cb_func, args, stdin_msg, stdout_msg, stderr_msg)); +} + +static bool util_exec_cmd_fail(exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, + char **stderr_msg) +{ + g_exec_cmd_count++; + if (g_exec_cmd_count == 1) { + *stderr_msg = util_strdup_s("Device or Resource Busy"); + return false; + } + + if (g_exec_cmd_count == 2) { + *stderr_msg = NULL; + return false; + } + + if (g_exec_cmd_count == 3) { + *stdout_msg = NULL; + return true; + } + + return 0; +} + +TEST(oci_rootfs_mount_llt, test_mount_rootfs) +{ + rootfs_mount_request *req = NULL; + rootfs_mount_response *resp = NULL; + int i = 0; + + // Test parameter NULL + ASSERT_NE(mount_rootfs(NULL, NULL), 0); + ASSERT_NE(mount_rootfs(NULL, &resp), 0); + free_rootfs_mount_response(resp); + resp = NULL; + + // Test content of parameter NULL + req = (rootfs_mount_request*)util_common_calloc_s(sizeof(rootfs_mount_request)); + ASSERT_TRUE(req != NULL); + ASSERT_NE(mount_rootfs(req, &resp), 0); + free_rootfs_mount_request(req); + req = NULL; + free_rootfs_mount_response(resp); + resp = NULL; + + // Test parameter collect + req = (rootfs_mount_request*)util_common_calloc_s(sizeof(rootfs_mount_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + MOCK_SET_V(execvp, execvp_success); + ASSERT_EQ(mount_rootfs(req, &resp), 0); + ASSERT_TRUE(resp != NULL); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + MOCK_CLEAR(execvp); + + free_rootfs_mount_request(req); + req = NULL; + free_rootfs_mount_response(resp); + resp = NULL; + + // Put MOCK_SET_V here to avoid warning "(style) Local variable MOCK_SET_V shadows outer variable" + MOCK_SET_V(util_exec_cmd, util_exec_cmd_fail); + // 1. Test util_exec_cmd failed with stderr_buffer != NULL + // 2. Test util_exec_cmd failed with stderr_buffer == NULL + // 3. Test util_exec_cmd success with stdout_buffer == NULL + for (i = 0; i < 3; i++) { + req = (rootfs_mount_request*)util_common_calloc_s(sizeof(rootfs_mount_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + ASSERT_NE(mount_rootfs(req, &resp), 0); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + + free_rootfs_mount_request(req); + req = NULL; + free_rootfs_mount_response(resp); + resp = NULL; + } + MOCK_CLEAR(util_exec_cmd); +} + +TEST(oci_rootfs_mount_llt, test_free_rootfs_mount_request) +{ + rootfs_mount_request *req = NULL; + + // Test free NULL + free_rootfs_mount_request(NULL); + + // Test free name_id NULL + req = (rootfs_mount_request*)util_common_calloc_s(sizeof(rootfs_mount_request)); + ASSERT_TRUE(req != NULL); + + free_rootfs_mount_request(req); + req = NULL; + + // Test free all Not NULL + req = (rootfs_mount_request*)util_common_calloc_s(sizeof(rootfs_mount_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + free_rootfs_mount_request(req); + req = NULL; +} + +TEST(oci_rootfs_mount_llt, test_free_rootfs_mount_response) +{ + rootfs_mount_response *resp = NULL; + + // Test free NULL + free_rootfs_mount_response(NULL); + + // Test free errmsg NULL + resp = (rootfs_mount_response*)util_common_calloc_s(sizeof(rootfs_mount_response)); + ASSERT_TRUE(resp != NULL); + + free_rootfs_mount_response(resp); + resp = NULL; + + // Test free all Not NULL + resp = (rootfs_mount_response*)util_common_calloc_s(sizeof(rootfs_mount_response)); + ASSERT_TRUE(resp != NULL); + resp->errmsg = util_strdup_s("This is error message"); + ASSERT_TRUE(resp->errmsg != NULL); + + free_rootfs_mount_response(resp); + resp = NULL; +} diff --git a/test/image/oci/oci_rootfs_prepare/CMakeLists.txt b/test/image/oci/oci_rootfs_prepare/CMakeLists.txt new file mode 100644 index 0000000..dc1d053 --- /dev/null +++ b/test/image/oci/oci_rootfs_prepare/CMakeLists.txt @@ -0,0 +1,62 @@ +project(iSulad_LLT) + +SET(EXE oci_rootfs_prepare_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/oci_rootfs_prepare.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_mount.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_extend.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/liblcrd.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_defs_linux.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config/lcrd_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/lcrd/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/isula_imtool_interface.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/driver.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2/driver_overlay2.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci/oci_llt_common.cc + ${CMAKE_BINARY_DIR}/json/imagetool_prepare_response.c + ${CMAKE_BINARY_DIR}/json/oci_image_spec.c + oci_rootfs_prepare_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2 + ) + +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,conf_get_graph_rootpath -Wl,--wrap,conf_get_graph_run_path -Wl,--wrap,conf_get_lcrd_storage_driver -Wl,--wrap,conf_get_storage_opts -Wl,--wrap,conf_get_registry_list -Wl,--wrap,conf_get_insecure_registry_list -Wl,--wrap,conf_get_im_opt_timeout -Wl,--wrap,execvp -Wl,--wrap,util_exec_cmd") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/image/oci/oci_rootfs_prepare/oci_rootfs_prepare_llt.cc b/test/image/oci/oci_rootfs_prepare/oci_rootfs_prepare_llt.cc new file mode 100644 index 0000000..7eb2e89 --- /dev/null +++ b/test/image/oci/oci_rootfs_prepare/oci_rootfs_prepare_llt.cc @@ -0,0 +1,268 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_prepare llt + * Author: wangfengtu + * Create: 2019-08-26 + */ + +#include +#include +#include +#include +#include +#include "mock.h" +#include "utils.h" +#include "oci_rootfs_prepare.h" +#include "oci_llt_common.h" + +static int g_exec_cmd_count = 0; + +DECLARE_OCI_LLT_COMMON_WRAPPER + +extern "C" { + DECLARE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg)); + DEFINE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg), \ + (cb_func, args, stdin_msg, stdout_msg, stderr_msg)); +} + +static bool util_exec_cmd_fail(exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, + char **stderr_msg) +{ + g_exec_cmd_count++; + if (g_exec_cmd_count == 1) { + *stderr_msg = util_strdup_s("Device or Resource Busy"); + return false; + } + + if (g_exec_cmd_count == 2) { + *stderr_msg = NULL; + return false; + } + + if (g_exec_cmd_count == 3) { + *stdout_msg = NULL; + return true; + } + + if (g_exec_cmd_count == 4) { + *stdout_msg = util_strdup_s("invalid"); + return true; + } + + return 0; +} + +#define OCI_IMAGE_SPEC_FILE "image/oci/oci_rootfs_prepare/prepare_response.json" + +static imagetool_prepare_response *prepare_response_from_json() +{ + imagetool_prepare_response *response = NULL; + parser_error err = NULL; + char *json_file = NULL; + + json_file = json_path(OCI_IMAGE_SPEC_FILE); + if (json_file == NULL) { + return NULL; + } + + response = imagetool_prepare_response_parse_file(json_file, NULL, &err); + if (response == NULL) { + goto out; + } + +out: + free(json_file); + json_file = NULL; + + free(err); + err = NULL; + + return response; +} + +static int execvp_prepare_success(const char *file, char * const argv[]) +{ + char *json_file = NULL; + + json_file = json_path(OCI_IMAGE_SPEC_FILE); + if (json_file == NULL) { + return -1; + } + + execlp("cat", "cat", json_file, NULL); + + free(json_file); + json_file = NULL; + + return -1; +} + +TEST(oci_rootfs_prepare_llt, test_prepare_rootfs_and_get_image_conf) +{ + // In order to skip codingstyle check. Error info "Lines should very rarely be longer than 120 characters" +#define RESP_MP "/var/lib/lcrd/storage/overlay/f50fdf298b3881051cbc383e3b293619dfb53c98805937784096353302d2b213/merged" + rootfs_prepare_request *req = NULL; + rootfs_prepare_and_get_image_conf_response *resp = NULL; + int i = 0; + + // Test parameter NULL part1 + ASSERT_NE(prepare_rootfs_and_get_image_conf(NULL, NULL), 0); + ASSERT_NE(prepare_rootfs_and_get_image_conf(NULL, &resp), 0); + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + // Test parameter NULL part2 + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + ASSERT_NE(prepare_rootfs_and_get_image_conf(req, &resp), 0); + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + // Test parameter NULL part3 + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + req->image = util_strdup_s("image_name"); + ASSERT_TRUE(req->image != NULL); + ASSERT_NE(prepare_rootfs_and_get_image_conf(req, &resp), 0); + free_rootfs_prepare_request(req); + req = NULL; + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + // Test parameter NULL part4 + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + req->name = util_strdup_s("name"); + ASSERT_TRUE(req->name != NULL); + ASSERT_NE(prepare_rootfs_and_get_image_conf(req, &resp), 0); + free_rootfs_prepare_request(req); + req = NULL; + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + + // Test parameter collect + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + req->image = util_strdup_s("image_name"); + ASSERT_TRUE(req->image != NULL); + req->name = util_strdup_s("name"); + ASSERT_TRUE(req->name != NULL); + req->id = util_strdup_s("0ac632258473464fa06a317e0347834ba652a00080506f5ea10e9ef8e7db5459"); + ASSERT_TRUE(req->id != NULL); + req->storage_opts = conf_get_storage_opts_success(); + ASSERT_TRUE(req->storage_opts != NULL); + req->storage_opts_len = 1; + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + MOCK_SET_V(execvp, execvp_prepare_success); + ASSERT_EQ(prepare_rootfs_and_get_image_conf(req, &resp), 0); + ASSERT_TRUE(resp != NULL); + ASSERT_TRUE(resp->raw_response != NULL); + ASSERT_STREQ(resp->raw_response->mount_point, RESP_MP); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + MOCK_CLEAR(execvp); + + free_rootfs_prepare_request(req); + req = NULL; + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + + // Put MOCK_SET_V here to avoid warning "(style) Local variable MOCK_SET_V shadows outer variable" + MOCK_SET_V(util_exec_cmd, util_exec_cmd_fail); + // 1. Test util_exec_cmd failed with stderr_buffer != NULL + // 2. Test util_exec_cmd failed with stderr_buffer == NULL + // 3. Test util_exec_cmd success with stdout_buffer == NULL + // 4. Test util_exec_cmd success with stdout_buffer invalid + for (i = 0; i < 4; i++) { + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + req->image = util_strdup_s("image_name"); + ASSERT_TRUE(req->image != NULL); + req->name = util_strdup_s("name"); + ASSERT_TRUE(req->name != NULL); + req->id = util_strdup_s("0ac632258473464fa06a317e0347834ba652a00080506f5ea10e9ef8e7db5459"); + ASSERT_TRUE(req->id != NULL); + req->storage_opts = conf_get_storage_opts_success(); + ASSERT_TRUE(req->storage_opts != NULL); + req->storage_opts_len = 1; + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + ASSERT_NE(prepare_rootfs_and_get_image_conf(req, &resp), 0); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + + free_rootfs_prepare_request(req); + req = NULL; + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + } + MOCK_CLEAR(util_exec_cmd); +} + +TEST(oci_rootfs_prepare_llt, test_free_rootfs_prepare_request) +{ + rootfs_prepare_request *req = NULL; + + // Test free NULL + free_rootfs_prepare_request(NULL); + + // Test free content NULL + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + + free_rootfs_prepare_request(req); + req = NULL; + + // Test free all Not NULL + req = (rootfs_prepare_request*)util_common_calloc_s(sizeof(rootfs_prepare_request)); + ASSERT_TRUE(req != NULL); + req->image = util_strdup_s("image_name"); + ASSERT_TRUE(req->image != NULL); + req->name = util_strdup_s("name"); + ASSERT_TRUE(req->name != NULL); + req->id = util_strdup_s("0ac632258473464fa06a317e0347834ba652a00080506f5ea10e9ef8e7db5459"); + ASSERT_TRUE(req->id != NULL); + req->storage_opts = conf_get_storage_opts_success(); + ASSERT_TRUE(req->storage_opts != NULL); + req->storage_opts_len = 1; + + free_rootfs_prepare_request(req); + req = NULL; +} + +TEST(oci_rootfs_prepare_llt, test_free_rootfs_prepare_and_get_image_conf_response) +{ + size_t size = 0; + rootfs_prepare_and_get_image_conf_response *resp = NULL; + + // Test free NULL + free_rootfs_prepare_and_get_image_conf_response(NULL); + + // Test free errmsg NULL + size = sizeof(rootfs_prepare_and_get_image_conf_response); + resp = (rootfs_prepare_and_get_image_conf_response*)util_common_calloc_s(size); + ASSERT_TRUE(resp != NULL); + + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; + + // Test free all Not NULL + size = sizeof(rootfs_prepare_and_get_image_conf_response); + resp = (rootfs_prepare_and_get_image_conf_response*)util_common_calloc_s(size); + ASSERT_TRUE(resp != NULL); + + resp->errmsg = util_strdup_s("This is error message"); + ASSERT_TRUE(resp->errmsg != NULL); + + resp->raw_response = prepare_response_from_json(); + ASSERT_TRUE(resp->raw_response != NULL); + + free_rootfs_prepare_and_get_image_conf_response(resp); + resp = NULL; +} diff --git a/test/image/oci/oci_rootfs_prepare/prepare_response.json b/test/image/oci/oci_rootfs_prepare/prepare_response.json new file mode 100644 index 0000000..fbaa8eb --- /dev/null +++ b/test/image/oci/oci_rootfs_prepare/prepare_response.json @@ -0,0 +1 @@ +{"mount_point":"/var/lib/lcrd/storage/overlay/f50fdf298b3881051cbc383e3b293619dfb53c98805937784096353302d2b213/merged"} diff --git a/test/image/oci/oci_rootfs_remove/CMakeLists.txt b/test/image/oci/oci_rootfs_remove/CMakeLists.txt new file mode 100644 index 0000000..823e03d --- /dev/null +++ b/test/image/oci/oci_rootfs_remove/CMakeLists.txt @@ -0,0 +1,60 @@ +project(iSulad_LLT) + +SET(EXE oci_rootfs_remove_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/oci_rootfs_remove.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_mount.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_extend.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/liblcrd.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_defs_linux.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config/lcrd_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/lcrd/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/isula_imtool_interface.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/driver.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2/driver_overlay2.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci/oci_llt_common.cc + oci_rootfs_remove_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2 + ) + +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,conf_get_graph_rootpath -Wl,--wrap,conf_get_graph_run_path -Wl,--wrap,conf_get_lcrd_storage_driver -Wl,--wrap,conf_get_storage_opts -Wl,--wrap,conf_get_registry_list -Wl,--wrap,conf_get_insecure_registry_list -Wl,--wrap,conf_get_im_opt_timeout -Wl,--wrap,execvp -Wl,--wrap,util_exec_cmd") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/image/oci/oci_rootfs_remove/oci_rootfs_remove_llt.cc b/test/image/oci/oci_rootfs_remove/oci_rootfs_remove_llt.cc new file mode 100644 index 0000000..3f32494 --- /dev/null +++ b/test/image/oci/oci_rootfs_remove/oci_rootfs_remove_llt.cc @@ -0,0 +1,173 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_remove llt + * Author: wangfengtu + * Create: 2019-08-26 + */ + +#include +#include +#include +#include +#include "mock.h" +#include "utils.h" +#include "oci_rootfs_remove.h" +#include "oci_llt_common.h" + +static int g_exec_cmd_count = 0; + +DECLARE_OCI_LLT_COMMON_WRAPPER + +extern "C" { + DECLARE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg)); + DEFINE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg), \ + (cb_func, args, stdin_msg, stdout_msg, stderr_msg)); +} + +static bool util_exec_cmd_fail(exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, + char **stderr_msg) +{ + g_exec_cmd_count++; + if (g_exec_cmd_count == 1) { + *stderr_msg = util_strdup_s("Device or Resource Busy"); + return false; + } + + if (g_exec_cmd_count == 2) { + *stderr_msg = NULL; + return false; + } + + if (g_exec_cmd_count == 3) { + *stderr_msg = util_strdup_s("container not known"); + return false; + } + + return 0; +} + +TEST(oci_rootfs_remove_llt, test_remove_rootfs) +{ + rootfs_remove_request *req = NULL; + rootfs_remove_response *resp = NULL; + int i = 0; + + // Test parameter NULL + ASSERT_NE(remove_rootfs(NULL, NULL), 0); + ASSERT_NE(remove_rootfs(NULL, &resp), 0); + free_rootfs_remove_response(resp); + resp = NULL; + + // Test content of parameter NULL + req = (rootfs_remove_request*)util_common_calloc_s(sizeof(rootfs_remove_request)); + ASSERT_TRUE(req != NULL); + ASSERT_NE(remove_rootfs(req, &resp), 0); + free_rootfs_remove_request(req); + req = NULL; + free_rootfs_remove_response(resp); + resp = NULL; + + // Test parameter collect + req = (rootfs_remove_request*)util_common_calloc_s(sizeof(rootfs_remove_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + MOCK_SET_V(execvp, execvp_success); + ASSERT_EQ(remove_rootfs(req, &resp), 0); + ASSERT_TRUE(resp != NULL); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + MOCK_CLEAR(execvp); + + free_rootfs_remove_request(req); + req = NULL; + free_rootfs_remove_response(resp); + resp = NULL; + + // Put MOCK_SET_V here to avoid warning "(style) Local variable MOCK_SET_V shadows outer variable" + MOCK_SET_V(util_exec_cmd, util_exec_cmd_fail); + // 1. Test util_exec_cmd failed with stderr_buffer != NULL + // 1. Test util_exec_cmd failed with stderr_buffer == NULL + // 3. Test util_exec_cmd failed with stderr_buffer contains "container not known" + for (i = 0; i < 3; i++) { + req = (rootfs_remove_request*)util_common_calloc_s(sizeof(rootfs_remove_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + if (i == 2) { + // If stderr_buffer contains "container not known", + // remove_rootfs must return success. + ASSERT_EQ(remove_rootfs(req, &resp), 0); + } else { + ASSERT_NE(remove_rootfs(req, &resp), 0); + } + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + + free_rootfs_remove_request(req); + req = NULL; + free_rootfs_remove_response(resp); + resp = NULL; + } + MOCK_CLEAR(util_exec_cmd); +} + +TEST(oci_rootfs_remove_llt, test_free_rootfs_remove_request) +{ + rootfs_remove_request *req = NULL; + + // Test free NULL + free_rootfs_remove_request(NULL); + + // Test free name_id NULL + req = (rootfs_remove_request*)util_common_calloc_s(sizeof(rootfs_remove_request)); + ASSERT_TRUE(req != NULL); + + free_rootfs_remove_request(req); + req = NULL; + + // Test free all Not NULL + req = (rootfs_remove_request*)util_common_calloc_s(sizeof(rootfs_remove_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + free_rootfs_remove_request(req); + req = NULL; +} + +TEST(oci_rootfs_remove_llt, test_free_rootfs_remove_response) +{ + rootfs_remove_response *resp = NULL; + + // Test free NULL + free_rootfs_remove_response(NULL); + + // Test free errmsg NULL + resp = (rootfs_remove_response*)util_common_calloc_s(sizeof(rootfs_remove_response)); + ASSERT_TRUE(resp != NULL); + + free_rootfs_remove_response(resp); + resp = NULL; + + // Test free all Not NULL + resp = (rootfs_remove_response*)util_common_calloc_s(sizeof(rootfs_remove_response)); + ASSERT_TRUE(resp != NULL); + resp->errmsg = util_strdup_s("This is error message"); + ASSERT_TRUE(resp->errmsg != NULL); + + free_rootfs_remove_response(resp); + resp = NULL; +} diff --git a/test/image/oci/oci_rootfs_umount/CMakeLists.txt b/test/image/oci/oci_rootfs_umount/CMakeLists.txt new file mode 100644 index 0000000..563b528 --- /dev/null +++ b/test/image/oci/oci_rootfs_umount/CMakeLists.txt @@ -0,0 +1,60 @@ +project(iSulad_LLT) + +SET(EXE oci_rootfs_umount_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/oci_rootfs_umount.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_mount.c + ${CMAKE_BINARY_DIR}/json/host_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/specs_extend.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/liblcrd.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_defs_linux.c + ${CMAKE_BINARY_DIR}/json/defs.c + ${CMAKE_BINARY_DIR}/json/container_config_v2.c + ${CMAKE_BINARY_DIR}/json/container_config.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config/lcrd_config.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_spec.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/sysinfo.c + ${CMAKE_BINARY_DIR}/json/oci_runtime_config_linux.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/commander.c + ${CMAKE_BINARY_DIR}/json/isulad_daemon_configs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd/lcrd/arguments.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci/isula_imtool_interface.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/driver.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2/driver_overlay2.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci/oci_llt_common.cc + oci_rootfs_umount_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image/oci + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/image + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec + ${CMAKE_BINARY_DIR}/json + ${CMAKE_BINARY_DIR}/conf + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/config + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cmd + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/graphdriver/overlay2 + ) + +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,conf_get_graph_rootpath -Wl,--wrap,conf_get_graph_run_path -Wl,--wrap,conf_get_lcrd_storage_driver -Wl,--wrap,conf_get_storage_opts -Wl,--wrap,conf_get_registry_list -Wl,--wrap,conf_get_insecure_registry_list -Wl,--wrap,conf_get_im_opt_timeout -Wl,--wrap,execvp -Wl,--wrap,util_exec_cmd") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/image/oci/oci_rootfs_umount/oci_rootfs_umount_llt.cc b/test/image/oci/oci_rootfs_umount/oci_rootfs_umount_llt.cc new file mode 100644 index 0000000..bd629dc --- /dev/null +++ b/test/image/oci/oci_rootfs_umount/oci_rootfs_umount_llt.cc @@ -0,0 +1,162 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: oci_rootfs_umount llt + * Author: wangfengtu + * Create: 2019-08-26 + */ + +#include +#include +#include +#include +#include "mock.h" +#include "utils.h" +#include "oci_rootfs_umount.h" +#include "oci_llt_common.h" + +static int g_exec_cmd_count = 0; + +DECLARE_OCI_LLT_COMMON_WRAPPER + +extern "C" { + DECLARE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg)); + DEFINE_WRAPPER_V(util_exec_cmd, bool, \ + (exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, char **stderr_msg), \ + (cb_func, args, stdin_msg, stdout_msg, stderr_msg)); +} + +static bool util_exec_cmd_fail(exec_func_t cb_func, void *args, const char *stdin_msg, char **stdout_msg, + char **stderr_msg) +{ + g_exec_cmd_count++; + if (g_exec_cmd_count == 1) { + *stderr_msg = util_strdup_s("Device or Resource Busy"); + return false; + } + + if (g_exec_cmd_count == 2) { + *stderr_msg = NULL; + return false; + } + + return 0; +} + + +TEST(oci_rootfs_umount_llt, test_umount_rootfs) +{ + rootfs_umount_request *req = NULL; + rootfs_umount_response *resp = NULL; + int i = 0; + + // Test parameter NULL + ASSERT_NE(umount_rootfs(NULL, NULL), 0); + ASSERT_NE(umount_rootfs(NULL, &resp), 0); + free_rootfs_umount_response(resp); + resp = NULL; + + // Test content of parameter NULL + req = (rootfs_umount_request*)util_common_calloc_s(sizeof(rootfs_umount_request)); + ASSERT_TRUE(req != NULL); + ASSERT_NE(umount_rootfs(req, &resp), 0); + free_rootfs_umount_request(req); + req = NULL; + free_rootfs_umount_response(resp); + resp = NULL; + + // Test parameter collect + req = (rootfs_umount_request*)util_common_calloc_s(sizeof(rootfs_umount_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + MOCK_SET_V(execvp, execvp_success); + ASSERT_EQ(umount_rootfs(req, &resp), 0); + ASSERT_TRUE(resp != NULL); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + MOCK_CLEAR(execvp); + + free_rootfs_umount_request(req); + req = NULL; + free_rootfs_umount_response(resp); + resp = NULL; + + // Put MOCK_SET_V here to avoid warning "(style) Local variable MOCK_SET_V shadows outer variable" + MOCK_SET_V(util_exec_cmd, util_exec_cmd_fail); + // 1. Test util_exec_cmd failed with stderr_buffer != NULL + // 2. Test util_exec_cmd failed with stderr_buffer == NULL + for (i = 0; i < 2; i++) { + req = (rootfs_umount_request*)util_common_calloc_s(sizeof(rootfs_umount_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + MOCK_SET_DEFAULT_ISULAD_KIT_OPTS + ASSERT_NE(umount_rootfs(req, &resp), 0); + MOCK_CLEAR_DEFAULT_ISULAD_KIT_OPTS + + free_rootfs_umount_request(req); + req = NULL; + free_rootfs_umount_response(resp); + resp = NULL; + } + MOCK_CLEAR(util_exec_cmd); +} + +TEST(oci_rootfs_umount_llt, test_free_rootfs_umount_request) +{ + rootfs_umount_request *req = NULL; + + // Test free NULL + free_rootfs_umount_request(NULL); + + // Test free name_id NULL + req = (rootfs_umount_request*)util_common_calloc_s(sizeof(rootfs_umount_request)); + ASSERT_TRUE(req != NULL); + + free_rootfs_umount_request(req); + req = NULL; + + // Test free all Not NULL + req = (rootfs_umount_request*)util_common_calloc_s(sizeof(rootfs_umount_request)); + ASSERT_TRUE(req != NULL); + req->name_id = util_strdup_s("image_name"); + ASSERT_TRUE(req->name_id != NULL); + + free_rootfs_umount_request(req); + req = NULL; +} + +TEST(oci_rootfs_umount_llt, test_free_rootfs_umount_response) +{ + rootfs_umount_response *resp = NULL; + + // Test free NULL + free_rootfs_umount_response(NULL); + + // Test free errmsg NULL + resp = (rootfs_umount_response*)util_common_calloc_s(sizeof(rootfs_umount_response)); + ASSERT_TRUE(resp != NULL); + + free_rootfs_umount_response(resp); + resp = NULL; + + // Test free all Not NULL + resp = (rootfs_umount_response*)util_common_calloc_s(sizeof(rootfs_umount_response)); + ASSERT_TRUE(resp != NULL); + resp->errmsg = util_strdup_s("This is error message"); + ASSERT_TRUE(resp->errmsg != NULL); + + free_rootfs_umount_response(resp); + resp = NULL; +} diff --git a/test/include/mock.h b/test/include/mock.h index 3773ed5..caf8902 100644 --- a/test/include/mock.h +++ b/test/include/mock.h @@ -1,5 +1,13 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2016-2019. All rights reserved. + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. * iSulad licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. * You may obtain a copy of Mulan PSL v1 at: diff --git a/test/llt.sh b/test/llt.sh old mode 100755 new mode 100644 index 0647b54..848f848 --- a/test/llt.sh +++ b/test/llt.sh @@ -1,17 +1,3 @@ -####################################################################### -##- @Copyright (C) Huawei Technologies., Ltd. 2017-2019. All rights reserved. -# - iSulad licensed under the Mulan PSL v1. -# - You can use this software according to the terms and conditions of the Mulan PSL v1. -# - You may obtain a copy of Mulan PSL v1 at: -# - http://license.coscl.org.cn/MulanPSL -# - 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 v1 for more details. -##- @Description: generate cetification -##- @Author: wujing -##- @Create: 2019-04-25 -####################################################################### #! /bin/bash #set -xe @@ -114,6 +100,7 @@ function llt_empty() find ../ -name "Makefile" |xargs rm -f find ../ -name "CMakeFiles" |xargs rm -rf find ../ -name "CMakeCache.txt"|xargs rm -f + find ../ -name "CTestTestfile.cmake"|xargs rm -f rm -rf ../conf ../grpc ../json rm coverage -rf rm test_result.log -f @@ -123,28 +110,35 @@ function llt_empty() } function llt_cmake() { + ret=0 if [ x"${CMAKE_ENABLE}" = x"yes" ]; then echo ---------------------- llt cmake begin ---------------------- cd .. if [ x"${COVERAGE_ENABLE}" = x"yes" ]; then cmake ./ -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=1 -DENABLE_LLT=ON + ret=$? else cmake -DENABLE_LLT=ON ./ + ret=$? fi cd - echo ---------------------- llt cmake end ------------------------ echo fi + return $ret } function llt_compile() { + ret=0 if [ x"${COMPILE_ENABLE}" = x"yes" ]; then echo ---------------------- llt compile begin ---------------------- make -j + ret=$? echo ---------------------- llt compile end ------------------------ echo fi + return $ret } function llt_run_all_test() @@ -172,12 +166,14 @@ function llt_run_all_test() TEST_LOG=test_result.log >$TEST_LOG + ret=0 for TEST in $SPECIFY_LLT do echo $TEST $TEST $RUN_MODE if [ $? != 0 ];then echo $TEST FAILED >> $TEST_LOG + ret=1 else echo $TEST success >> $TEST_LOG fi @@ -189,6 +185,7 @@ function llt_run_all_test() echo "" echo ---------------------- llt run end -------------------------- echo + return $ret fi } @@ -237,7 +234,7 @@ function llt_coverage() fi #lcov -c ${LCOV_CMD} -o coverage/coverage.info --exclude '*_llt.c' --include '*.c' --include '*.cpp' --include '*.cc' --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph - lcov -c ${LCOV_CMD} -b $(dirname $(pwd)) --no-external --exclude '*_llt.c' -o coverage/coverage.info --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph + lcov -c ${LCOV_CMD} -b $(dirname $(pwd)) --no-external --exclude '*_llt*.cc' -o coverage/coverage.info --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph if [ $? != 0 ]; then echo "lcov generate coverage.info fail." exit 1 @@ -255,10 +252,16 @@ function llt_coverage() } llt_cmake +if [[ $? -ne 0 ]];then + exit 1 +fi llt_empty llt_compile +if [[ $? -ne 0 ]];then + exit 1 +fi llt_run_all_test +if [[ $? -ne 0 ]];then + exit 1 +fi llt_coverage - -#set +x - diff --git a/test/path/CMakeLists.txt b/test/path/CMakeLists.txt new file mode 100644 index 0000000..0934d9e --- /dev/null +++ b/test/path/CMakeLists.txt @@ -0,0 +1,26 @@ +project(iSulad_LLT) + +SET(EXE path_llt) + +add_executable(${EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/log.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/sha256/sha256.c + ${CMAKE_BINARY_DIR}/json/json_common.c + path_llt.cc) + +target_include_directories(${EXE} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../src/sha256 + ${CMAKE_BINARY_DIR}/json + ) +set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,getcwd -Wl,--wrap,readlink") +target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz) diff --git a/test/path/path_llt.cc b/test/path/path_llt.cc new file mode 100644 index 0000000..b832b88 --- /dev/null +++ b/test/path/path_llt.cc @@ -0,0 +1,372 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * iSulad licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * 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 v1 for more details. + * Description: path llt + * Author: jikui + * Create: 2019-08-22 + */ + +#include +#include +#include +#include +#include +#include "mock.h" +#include "utils.h" +#include "utils_file.h" +#include "path.h" + +#define PATH_LENGTH_TEST 100 +#define FILE_PERMISSION_TEST 0755 + +extern "C" { + DECLARE_WRAPPER_V(getcwd, char *, (char *str, size_t size)); + DEFINE_WRAPPER_V(getcwd, char *, (char *str, size_t size), (str, size)); + + DECLARE_WRAPPER_V(readlink, ssize_t, (const char *path, char *buf, size_t bufsize)); + DEFINE_WRAPPER_V(readlink, ssize_t, (const char *path, char *buf, size_t bufsize), (path, buf, bufsize)); +} + +static char *getcwd_specify(char *str, size_t size) +{ + if (str == nullptr) { + return nullptr; + } + + if (strcpy_s(str, size, "/home") != EOK) { + return nullptr; + } + return str; +} + +static int create_tmp_symbolic_link(const char *path, + const char *path_file, + const char *path_link) +{ + if (path == nullptr || path_file == nullptr || path_link == nullptr) { + return -1; + } + + if (util_mkdir_p(path, FILE_PERMISSION_TEST) != 0) { + return -1; + } + + if (symlink(path_file, path_link) != 0) { + return -1; + } + return 0; +} + +static ssize_t readlink_specify(const char *path, char *buf, size_t bufsize) +{ + const char *linkpath = "./dir/test/../file"; + size_t linkpath_length; + linkpath_length = strlen(linkpath); + + if (path == nullptr || buf == nullptr) { + return -1; + } + + if (strcpy_s(buf, bufsize, linkpath) != EOK) { + return -1; + } + + if (linkpath_length > bufsize) { + return bufsize; + } + return linkpath_length; +} + +TEST(path_llt, test_cleanpath) +{ + char *result = nullptr; + std::string str; + char realpath[PATH_MAX]; + + result = cleanpath(nullptr, realpath, sizeof(realpath)); + ASSERT_STREQ(result, nullptr); + + str = ""; + result = cleanpath(str.c_str(), realpath, sizeof(realpath)); + ASSERT_STREQ(result, nullptr); + + str = "/home/dir/../file"; + result = cleanpath(str.c_str(), realpath, sizeof(realpath)); + ASSERT_STREQ(result, "/home/file"); + + str = "/home/dir/./file"; + result = cleanpath(str.c_str(), realpath, sizeof(realpath)); + ASSERT_STREQ(result, "/home/dir/file"); + + str = "./dir/file"; + MOCK_SET_V(getcwd, getcwd_specify); + result = cleanpath(str.c_str(), realpath, sizeof(realpath)); + ASSERT_STREQ(result, "/home/dir/file"); + MOCK_CLEAR(getcwd); + + str = "/home/file"; + result = cleanpath(str.c_str(), realpath, PATH_LENGTH_TEST); + ASSERT_STREQ(result, nullptr); + + str = "/home/file"; + result = cleanpath(str.c_str(), nullptr, 0); + ASSERT_STREQ(result, nullptr); +} + +TEST(path_llt, test_specify_current_dir) +{ + ASSERT_FALSE(specify_current_dir(nullptr)); + ASSERT_TRUE(specify_current_dir("")); + ASSERT_TRUE(specify_current_dir("/home/.")); + ASSERT_TRUE(specify_current_dir(".")); + ASSERT_FALSE(specify_current_dir("/home/file")); + ASSERT_FALSE(specify_current_dir("/home/..")); + ASSERT_FALSE(specify_current_dir("/home")); + ASSERT_FALSE(specify_current_dir("home")); +} + +TEST(path_llt, test_follow_symlink_in_scope) +{ + std::string fullpath, rootpath; + char *res = nullptr; + + res = follow_symlink_in_scope(nullptr, nullptr); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + fullpath = ""; + rootpath = ""; + res = follow_symlink_in_scope(fullpath.c_str(), rootpath.c_str()); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + fullpath = "/home/dir/file"; + rootpath = "/home"; + res = follow_symlink_in_scope(fullpath.c_str(), rootpath.c_str()); + ASSERT_STREQ(res, "/home/dir/file"); + free(res); + res = nullptr; + + fullpath = "/home/dir/file"; + rootpath = "/home/dir/../file"; + res = follow_symlink_in_scope(fullpath.c_str(), rootpath.c_str()); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + fullpath = "/home/dir/file"; + rootpath = "/home/dir/../"; + res = follow_symlink_in_scope(fullpath.c_str(), rootpath.c_str()); + ASSERT_STREQ(res, "/home/dir/file"); + free(res); + res = nullptr; + + fullpath = "/tmp/just_for_llt/link"; + rootpath = "/tmp"; + const char *path = "/tmp/just_for_llt"; + const char *path_file = "./testdir/test/../file"; + const char *path_link = "/tmp/just_for_llt/link"; + ASSERT_EQ(create_tmp_symbolic_link(path, path_file, path_link), 0); + MOCK_SET_V(readlink, readlink_specify); + res = follow_symlink_in_scope(fullpath.c_str(), rootpath.c_str()); + ASSERT_STREQ(res, "/tmp/just_for_llt/dir/file"); + MOCK_CLEAR(readlink); + ASSERT_EQ(util_recursive_rmdir("/tmp/just_for_llt", 0), 0); + free(res); + res = nullptr; +} + +TEST(path_llt, test_split_dir_and_base_name) +{ + char *dir = nullptr; + char *base = nullptr; + + ASSERT_EQ(split_dir_and_base_name(nullptr, &dir, &base), -1); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + ASSERT_EQ(split_dir_and_base_name("", &dir, &base), 0); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + ASSERT_EQ(split_dir_and_base_name("/home/file", &dir, &base), 0); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + ASSERT_EQ(split_dir_and_base_name("/home/file", nullptr, nullptr), 0); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + split_dir_and_base_name("/home/file", &dir, &base); + ASSERT_STREQ(dir, "/home"); + ASSERT_STREQ(base, "file"); + free(dir); + dir = nullptr; + free(base); + base = nullptr; +} + +TEST(path_llt, test_filepath_split) +{ + char *dir = nullptr; + char *base = nullptr; + + ASSERT_EQ(filepath_split(nullptr, &dir, &base), -1); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + ASSERT_EQ(filepath_split("", &dir, &base), 0); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + ASSERT_EQ(filepath_split("/home/file", &dir, &base), 0); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + ASSERT_EQ(filepath_split("/home/file", nullptr, nullptr), 0); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + filepath_split("/home/file", &dir, &base); + ASSERT_STREQ(dir, "/home/"); + ASSERT_STREQ(base, "file"); + free(dir); + dir = nullptr; + free(base); + base = nullptr; + + filepath_split("/home/", &dir, &base); + ASSERT_STREQ(dir, "/home/"); + ASSERT_STREQ(base, ""); + free(dir); + dir = nullptr; + free(base); + base = nullptr; +} + +TEST(path_llt, test_get_resource_path) +{ + char *res = nullptr; + + res = get_resource_path(nullptr, "./test"); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + res = get_resource_path("", ""); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + res = get_resource_path("/home", "./test"); + ASSERT_STREQ(res, "/home/test"); + free(res); + res = nullptr; + + res = get_resource_path("/home/dir", "tmp/.././test"); + ASSERT_STREQ(res, "/home/dir/test"); + free(res); + res = nullptr; + + res = get_resource_path("/home/dir", ".././test"); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + res = get_resource_path("/home////dir", ".///./././test/file"); + ASSERT_STREQ(res, "/home/dir/test/file"); + free(res); + res = nullptr; +} + +TEST(path_llt, test_resolve_path) +{ + std::string rootpath, path; + char *resolvedpath = nullptr; + char *abspath = nullptr; + + ASSERT_EQ(resolve_path(nullptr, nullptr, &resolvedpath, &abspath), -1); + free(resolvedpath); + resolvedpath = nullptr; + free(abspath); + abspath = nullptr; + + rootpath = ""; + path = ""; + ASSERT_EQ(resolve_path(rootpath.c_str(), path.c_str(), &resolvedpath, &abspath), -1); + free(resolvedpath); + resolvedpath = nullptr; + free(abspath); + abspath = nullptr; + + rootpath = "/home"; + path = "/home/dir/test"; + ASSERT_EQ(resolve_path(rootpath.c_str(), path.c_str(), &resolvedpath, &abspath), 0); + free(resolvedpath); + resolvedpath = nullptr; + free(abspath); + abspath = nullptr; +} + +TEST(path_llt, test_has_trailing_path_separator) +{ + ASSERT_FALSE(has_trailing_path_separator(nullptr)); + ASSERT_FALSE(has_trailing_path_separator("")); + ASSERT_TRUE(has_trailing_path_separator("/home/")); + ASSERT_FALSE(has_trailing_path_separator("/home")); +} + +TEST(path_llt, test_preserve_trailing_dot_or_separator) +{ + std::string cleanedpath, originalpath; + char *res = nullptr; + + res = preserve_trailing_dot_or_separator(nullptr, nullptr); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + res = preserve_trailing_dot_or_separator("", ""); + ASSERT_STREQ(res, nullptr); + free(res); + res = nullptr; + + cleanedpath = "/home/test"; + originalpath = "/home/test/."; + res = preserve_trailing_dot_or_separator(cleanedpath.c_str(), originalpath.c_str()); + ASSERT_STREQ(res, "/home/test/."); + free(res); + res = nullptr; + + cleanedpath = "/home/test"; + originalpath = "/home/test/"; + res = preserve_trailing_dot_or_separator(cleanedpath.c_str(), originalpath.c_str()); + ASSERT_STREQ(res, "/home/test/"); + free(res); + res = nullptr; +} diff --git a/tools/static_check b/tools/static_check old mode 100755 new mode 100644 index d197117..fbc23ff --- a/tools/static_check +++ b/tools/static_check @@ -13,6 +13,8 @@ ##- @Create: 2019-04-25 ####################################################################### #!/bin/bash +# +# This script is the implementation portal for the iSulad project Personal level build static check. # set -euxo pipefail CURRENT_PATH=$(pwd) @@ -371,7 +373,7 @@ function cpp_check() { printf "\n" local check_rule=$(echo ${CPPCHRECK_RULE[@]} | sed -e "s/ /,/g") local start_time=$(date +%s) - result=$(cppcheck --enable=${check_rule} -j $(nproc) -i ./build ./ 2>&1 | grep -vE "^Checking|done$") + result=$(cppcheck --enable=${check_rule} -j $(nproc) -i ./build ./ 2>&1 | grep -vE "^Checking|done$|any_of algorithm instead of a raw loop") nums=$(echo "${result}" | wc -l) echo "${result}" local end_time=$(date +%s)