diff --git a/CI/test_cases/container_cases/cp.bash b/CI/test_cases/container_cases/cp.bash new file mode 100644 index 0000000000000000000000000000000000000000..a59a7f8e89398132e0d1de798441a62f29d57140 --- /dev/null +++ b/CI/test_cases/container_cases/cp.bash @@ -0,0 +1,292 @@ +#!/bin/bash +# +# attributes: isulad basic container hook +# concurrent: NA +# spend time: 25 + +####################################################################### +##- @Copyright (C) Huawei Technologies., Ltd. 2020. All rights reserved. +# - iSulad licensed under the Mulan PSL v2. +# - You can use this software according to the terms and conditions of the Mulan PSL v2. +# - You may obtain a copy of Mulan PSL v2 at: +# - http://license.coscl.org.cn/MulanPSL2 +# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# - PURPOSE. +# - See the Mulan PSL v2 for more details. +##- @Description:CI +##- @Author: lifeng +##- @Create: 2020-06-03 +####################################################################### +declare -r curr_path=$(dirname $(readlink -f "$0")) +source ../helpers.bash + +cpfiles=/tmp/subcmdcp + +test_cp_file_from_container() +{ + local ret=0 + containername=$1 + # cp from container + dstfile=$cpfiles/passwd + rm -rf $dstfile + cd $cpfiles + + isula cp nonexists:etc . 2>&1 | grep "No such container" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check noexists output" && ((ret++)) + + isula cp nonexists:etc $containername:$cpfiles 2>&1 | grep "copying between containers is not supported" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check copying between containers output" && ((ret++)) + + isula cp $containername:etc/passwd . + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + if [ ! -f $dstfile ];then + msg_err "${FUNCNAME[0]}:${LINENO} - failed to check dstfile" && ((ret++)) + fi + + rm -rf $dstfile + + dstfile=$cpfiles/passwd_renamed + rm -rf $dstfile + isula cp $containername:../etc/passwd passwd_renamed + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + if [ ! -f $dstfile ];then + msg_err "${FUNCNAME[0]}:${LINENO} - failed to check dstfile" && ((ret++)) + fi + rm -rf $dstfile + + isula cp $containername:/etc/../etc/passwd/ $cpfiles 2>&1 | grep "Not a directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp $containername:/etc/nonexists $cpfiles 2>&1 | grep "No such file or directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + dstfile=$cpfiles/etc + rm -rf $dstfile + touch $dstfile + isula cp $containername:/etc $dstfile 2>&1 | grep "cannot copy directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + rm -rf $dstfile + + isula cp $containername:/etc/passwd $cpfiles/nonexists/ 2>&1 | grep "no such directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + return ${ret} +} + +test_cp_dir_from_container() +{ + local ret=0 + containername=$1 + # cp from container + dstfile=$cpfiles/etc + rm -rf $dstfile + cd $cpfiles + isula cp $containername:etc . + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + if [ ! -d $dstfile ];then + msg_err "${FUNCNAME[0]}:${LINENO} - failed to check dstfile" && ((ret++)) + fi + rm -rf $dstfile + + dstfile=$cpfiles/etc_renamed + rm -rf $dstfile + isula cp $containername:../etc etc_renamed + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + if [ ! -d $dstfile ];then + msg_err "${FUNCNAME[0]}:${LINENO} - failed to check dstfile" && ((ret++)) + fi + rm -rf $dstfile + + dstfile=$cpfiles/etcfiles + rm -rf $dstfile + mkdir -p $dstfile + isula cp $containername:/etc/. etcfiles + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + if [ ! -f $dstfile/passwd ];then + msg_err "${FUNCNAME[0]}:${LINENO} - failed to check dstfile" && ((ret++)) + fi + rm -rf $dstfile + + return ${ret} +} + +test_cp_file_to_container() +{ + local ret=0 + containername=$1 + # cp from container + dstfile=$cpfiles/passwd + cd /etc + + isula cp passwd nonexists:$dstfile 2>&1 | grep "No such container" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp passwd $containername:./$cpfiles + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername /bin/sh -c "ls $dstfile" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + dstfile=$cpfiles/passwd_renamed + isula cp ../../../etc/passwd $containername:../$dstfile + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername /bin/sh -c "ls $dstfile" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp ./passwd $containername:/etc/passwd/ 2>&1 | grep "no such directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp /etc/../etc/passwd $containername:/etc/passwd/nonexists 2>&1 | grep "extraction point is not a directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp /etc/passwd $containername:$cpfiles/nonexists/ 2>&1 | grep "no such directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp /etc/passwd $containername:$cpfiles/nonexists/nonexists 2>&1 | grep "No such file or directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp /etc/nonexists $containername:$cpfiles 2>&1 | grep "No such file or directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + rm -rf $dstfile + + dstfile=$cpfiles/etc + isula exec $containername /bin/sh -c "rm -rf $dstfile; touch $dstfile" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp /etc $containername:$dstfile 2>&1 | grep "cannot copy directory" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + return ${ret} +} + +test_cp_dir_to_container() +{ + local ret=0 + containername=$1 + # cp from container + dstfile=$cpfiles/etc + cd / + isula cp .././etc $containername:$cpfiles + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername /bin/sh -c "ls $dstfile" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + dstfile=$cpfiles/etc_renamed + isula cp ./etc $containername:$dstfile + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername /bin/sh -c "ls $dstfile" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + dstfile=$cpfiles/etcfiles + cd /etc + isula exec $containername /bin/sh -c "rm -rf $dstfile; mkdir -p $dstfile" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp . $containername:$dstfile + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername /bin/sh -c "ls $dstfile/passwd" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + return ${ret} +} + +test_cp_symlink_to_container() +{ + local ret=0 + containername=$1 + cd /tmp + rm -rf l1 + ln -s ../..$cpfiles/linkto l1 + isula cp l1 $containername:$cpfiles + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername ls -al $cpfiles | grep "l1.*../..$cpfiles/linkto" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp l1 $containername:$cpfiles/l1 + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername ls -al $cpfiles | grep "linkto.*../..$cpfiles/linkto" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + rm -rf l1 + + isula exec $containername /bin/sh -c "cd $cpfiles; rm -rf t1 t2 target; ln -s ./t1 t2; ln -s target t1" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp /etc/passwd $containername:$cpfiles/t2 + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula exec $containername /bin/sh -c "cat $cpfiles/target | grep root" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + return ${ret} +} + +test_cp_symlink_from_container() +{ + local ret=0 + containername=$1 + cd $cpfiles + rm -rf l1 l2 + isula exec $containername /bin/sh -c "cd $cpfiles; rm -rf l1 l2 target; touch target; ln -s target l1; ln -s l1 l2" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + isula cp $containername:$cpfiles/l1 . + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + ls -al . | grep "l1.*target" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do copy" && ((ret++)) + + return ${ret} +} + +function cp_test_t() +{ + local ret=0 + local image="busybox" + local test="container cp test => (${FUNCNAME[@]})" + + msg_info "${test} starting..." + + isula pull ${image} + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to pull image: ${image}" && return ${FAILURE} + + isula images | grep busybox + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - missing list image: ${image}" && ((ret++)) + + containername=test_cmd_cp + isula run -n $containername -itd $image + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to run container: ${image}" && ((ret++)) + + rm -rf $cpfiles + mkdir -p $cpfiles + isula exec $containername mkdir -p $cpfiles + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to exec container: ${containername}" && ((ret++)) + + test_cp_file_from_container $containername || ((ret++)) + test_cp_dir_from_container $containername || ((ret++)) + test_cp_file_to_container $containername || ((ret++)) + test_cp_symlink_to_container $containername || ((ret++)) + test_cp_symlink_from_container $containername || ((ret++)) + + isula rm -f $containername + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to rm container: ${containername}" && ((ret++)) + + rm -rf $cpfiles + echo "test end" + return ${ret} +} + +declare -i ans=0 + +cp_test_t || ((ans++)) + +show_result ${ans} "${curr_path}/${0}" diff --git a/CI/test_cases/container_cases/rename.bash b/CI/test_cases/container_cases/rename.bash new file mode 100644 index 0000000000000000000000000000000000000000..0347cd7b732a200f56e12f25ba50156ab4e37e0d --- /dev/null +++ b/CI/test_cases/container_cases/rename.bash @@ -0,0 +1,90 @@ +#!/bin/bash +# +# attributes: isulad basic container hook +# concurrent: NA +# spend time: 5 + +####################################################################### +##- @Copyright (C) Huawei Technologies., Ltd. 2020. All rights reserved. +# - iSulad licensed under the Mulan PSL v2. +# - You can use this software according to the terms and conditions of the Mulan PSL v2. +# - You may obtain a copy of Mulan PSL v2 at: +# - http://license.coscl.org.cn/MulanPSL2 +# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# - PURPOSE. +# - See the Mulan PSL v2 for more details. +##- @Description:CI +##- @Author: lifeng +##- @Create: 2020-06-03 +####################################################################### + +declare -r curr_path=$(dirname $(readlink -f "$0")) +source ../helpers.bash + +function test_rename_spec() +{ + local ret=0 + local image="busybox" + local test="container rename test => (${FUNCNAME[@]})" + old_name=old_name + rename_log=/tmp/rename.log + + msg_info "${test} starting..." + + isula pull ${image} + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to pull image: ${image}" && return ${FAILURE} + + isula images | grep busybox + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - missing list image: ${image}" && ((ret++)) + + CONT=`isula run -n $old_name -itd ${image}` + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to run container with image: ${image}" && ((ret++)) + + isula rename > $rename_log 2>&1 + [[ $? -ne 1 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check rename container exit code: ${image}" && ((ret++)) + + cat $rename_log | grep "requires 2 arguments" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + isula rename $old_name $old_name > $rename_log 2>&1 + [[ $? -ne 1 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check rename same container exit code: ${image}" && ((ret++)) + + cat $rename_log | grep "Renaming a container with the same name as its current name" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + isula rename no_exist no_exist1 > $rename_log 2>&1 + [[ $? -ne 1 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check rename same container exit code: ${image}" && ((ret++)) + + cat $rename_log | grep "No such container" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + isula rename $old_name 1 > $rename_log 2>&1 + [[ $? -ne 1 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check rename same container exit code: ${image}" && ((ret++)) + + cat $rename_log | grep "Invalid container new name" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + isula rename $old_name 123@ > $rename_log 2>&1 + [[ $? -ne 1 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check rename same container exit code: ${image}" && ((ret++)) + + cat $rename_log | grep "Invalid container new name" + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + isula rename $old_name new_name > $rename_log 2>&1 + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + isula rm -f new_name + [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to check error output with image: ${image}" && ((ret++)) + + rm -f $rename_log + + msg_info "${test} finished with return ${ret}..." + return ${ret} +} + +declare -i ans=0 + +test_rename_spec || ((ans++)) + +show_result ${ans} "${curr_path}/${0}" \ No newline at end of file diff --git a/src/daemon/modules/image/oci/oci_export.c b/src/daemon/modules/image/oci/oci_export.c index ec68bfa879b949d9c4e1ea9d8955cf9b60b6c6b0..8bf27f4d4a0d527fc77ae275bc10001b75b81bd4 100644 --- a/src/daemon/modules/image/oci/oci_export.c +++ b/src/daemon/modules/image/oci/oci_export.c @@ -50,7 +50,7 @@ out: free(errmsg); errmsg = NULL; - ret2 = storage_rootfs_umount(id); + ret2 = storage_rootfs_umount(id, false); if (ret2 != 0) { ret = ret2; ERROR("umount container %s failed", id); diff --git a/src/daemon/modules/image/oci/oci_image.c b/src/daemon/modules/image/oci/oci_image.c index 5ed1961dd83c1a539c9f845f859430a9f1c30d2e..fe058f7233262f0b986ef3c3539f267fdd645e95 100644 --- a/src/daemon/modules/image/oci/oci_image.c +++ b/src/daemon/modules/image/oci/oci_image.c @@ -223,7 +223,7 @@ int oci_umount_rf(const im_umount_request *request) return -1; } - return storage_rootfs_umount(request->name_id); + return storage_rootfs_umount(request->name_id, request->force); } int oci_rmi(const im_rmi_request *request) diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c index fa5967d3cb2c7a09975c2775a0f88aafbe1c1412..86b094ff87777d4e9b4079ae46115e913504e549 100644 --- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c +++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c @@ -2140,7 +2140,7 @@ static int check_grow_base_device_fs(struct device_set *devset, image_devmapper_ base_dev_size = get_base_device_size(devset); if (devset->base_fs_size < base_dev_size) { - ERROR("devmapper: Base fs size:%lu cannot be smaller than %lu",devset->base_fs_size, base_dev_size); + ERROR("devmapper: Base fs size:%lu cannot be smaller than %lu", devset->base_fs_size, base_dev_size); return -1; } diff --git a/src/daemon/modules/image/oci/storage/storage.c b/src/daemon/modules/image/oci/storage/storage.c index adb3d39ef126d29b3fa23211684bf2269d50096a..3c042e439697877b8ca8d6878d9f97cdee0c6491 100644 --- a/src/daemon/modules/image/oci/storage/storage.c +++ b/src/daemon/modules/image/oci/storage/storage.c @@ -1177,7 +1177,7 @@ out: return mount_point; } -int storage_rootfs_umount(const char *container_id) +int storage_rootfs_umount(const char *container_id, bool force) { int ret = 0; storage_rootfs *rootfs_info = NULL; @@ -1195,7 +1195,7 @@ int storage_rootfs_umount(const char *container_id) goto out; } - if (layer_store_umount(rootfs_info->layer, true) != 0) { + if (layer_store_umount(rootfs_info->layer, force) != 0) { ERROR("Failed to umount layer %s", rootfs_info->layer); ret = -1; goto out; diff --git a/src/daemon/modules/image/oci/storage/storage.h b/src/daemon/modules/image/oci/storage/storage.h index fcc067331773cd454f8187bea18cb1df11fc00bd..ff114cc488dd7f10633c05c7dc6c6734488c89cb 100644 --- a/src/daemon/modules/image/oci/storage/storage.h +++ b/src/daemon/modules/image/oci/storage/storage.h @@ -167,7 +167,7 @@ int storage_rootfs_fs_usgae(const char *container_id, imagetool_fs_info *fs_info char *storage_rootfs_mount(const char *container_id); -int storage_rootfs_umount(const char *container_id); +int storage_rootfs_umount(const char *container_id, bool force); #ifdef __cplusplus }