Skip to content

Commit

Permalink
Add test to trigger out-of-diskspace error.
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonsn committed Jul 11, 2022
1 parent 021bba6 commit 1330f64
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ jobs:
- name: run container with tests
working-directory: ${{ github.workspace }}
run: |
docker run --security-opt seccomp:unconfined --rm -e DIST -v$(pwd):/build -w/build ${DIST}/tdnf-build ./ci/docker-entrypoint.sh
docker run --privileged --security-opt seccomp:unconfined --rm -e DIST -v$(pwd):/build -w/build ${DIST}/tdnf-build ./ci/docker-entrypoint.sh
2 changes: 1 addition & 1 deletion ci/Dockerfile.fedora
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN dnf -y install gcc make cmake libcurl-devel rpm-devel rpm-build \
findutils python3-pytest python3-requests python3-urllib3 \
python3-pyOpenSSL python3 python3-devel valgrind gpgme-devel \
libxml2-devel openssl-devel sqlite-devel rpm-sign which python3-pip \
shadow-utils sudo
shadow-utils sudo e2fsprogs util-linux

RUN pip3 install flake8

Expand Down
2 changes: 1 addition & 1 deletion ci/Dockerfile.photon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN tdnf install -y --enablerepo=photon-debuginfo \
python3-devel valgrind gpgme-devel glibc-debuginfo \
libxml2-devel openssl-devel zlib-devel sqlite-devel \
python3-requests python3-urllib3 python3-pyOpenSSL \
sudo shadow which
sudo shadow which e2fsprogs util-linux

# this can to be removed once latest pytest gets published in photon
RUN pip3 install pytest flake8
Expand Down
2 changes: 1 addition & 1 deletion ci/Dockerfile.photon-3.0
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN tdnf install -y --enablerepo=photon-debuginfo \
valgrind gpgme-devel glibc-debuginfo libxml2-devel \
openssl-devel zlib-devel sqlite-devel python3-requests \
python3-urllib3 python3-pyOpenSSL python3-pip \
sudo shadow which
sudo shadow which e2fsprogs util-linux

# TODO: we need to fix pytest in Ph3, after that this can be removed
RUN pip3 install pytest flake8
Expand Down
2 changes: 2 additions & 0 deletions pytests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
config.json
virtual_disks/
mount-small-cache
5 changes: 5 additions & 0 deletions pytests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/config.json" @ONLY
)

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/mount-small-cache.in"
"${CMAKE_CURRENT_SOURCE_DIR}/mount-small-cache" @ONLY
)

add_custom_target(check
COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH="${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" pytest
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
2 changes: 2 additions & 0 deletions pytests/config.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"repo_path": "@CMAKE_CURRENT_BINARY_DIR@/repo",
"test_path": "@CMAKE_SOURCE_DIR@/pytests",
"build_dir": "@CMAKE_BINARY_DIR@",
"small_cache_path": "@CMAKE_CURRENT_BINARY_DIR@/small_cache/",
"plugin_path": "@CMAKE_BINARY_DIR@/plugins/lib",
"automatic_script": "@CMAKE_SOURCE_DIR@/bin/tdnf-automatic",
"automatic_conf": "@CMAKE_SOURCE_DIR@/etc/tdnf/automatic.conf",
Expand All @@ -15,6 +16,7 @@
"requiring_package": "tdnf-test-dependson-base",
"required_package": "tdnf-test-base",
"dummy_requires_pkgname": "tdnf-test-dummy-requires",
"toolarge_pkgname": "tdnf-test-toolarge",
"valgrind_minimum_version": "3.15.0",
"valgrind_minimum_version_aarch64": "3.17.0"
}
41 changes: 41 additions & 0 deletions pytests/mount-small-cache.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

## file: mount-small-cache.sh
## brief: mounts an intentionally small directory for out-of-diskspace tests.
## author: Preston Narchetti <[email protected]>

if [ ${EUID} -ne 0 ]; then
echo "Script must be run as root." 1>&2
exit 1
fi

## must match the path set in config.json.in.
small_cache_path="@CMAKE_CURRENT_BINARY_DIR@/small_cache/"
quota_size="1M"

## used to check return code for each command.
function check_err {
rc=$?
if [ $rc -ne 0 ]; then
echo $1
exit $rc
fi
}

echo "Creating mount point."
rm -rf ./virtual_disks
mkdir -p ./virtual_disks
rm -rf ${small_cache_path}
mkdir -p ${small_cache_path}
check_err "Failed to mkdir ${small_cache_path}."

echo "Creating tmpfs."
touch ./virtual_disks/fs.ext4
dd if=/dev/zero of=./virtual_disks/fs.ext4 bs=${quota_size} count=1
check_err "Failed to make exf4 tmpfs: dd"
mkfs.ext4 ./virtual_disks/fs.ext4
check_err "Failed to make exf4 tmpfs: mkfs.ext4"

echo "Mounting tmpfs at ${small_cache_path}."
mount -o loop,rw,usrquota,grpquota ./virtual_disks/fs.ext4 ${small_cache_path}
check_err "Failed to mount exf4 tmpfs."
32 changes: 32 additions & 0 deletions pytests/repo/tdnf-test-toolarge.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# tdnf-test-toolarge spec file
#
Summary: creates a fake random file that is too large for cache dir.
Name: tdnf-test-toolarge
Version: 1.0.1
Release: 1
Vendor: VMware, Inc.
Distribution: Photon
License: VMware
Url: http://www.vmware.com
Group: Applications/tdnftest

%description
Part of tdnf test spec. Creates a large file to intentionally trigger an
out of disk space error.

%prep

%build

%install
mkdir -p %_topdir/%buildroot/lib/toolarge/
touch %_topdir/%buildroot/lib/toolarge/largedata.txt
dd if=/dev/urandom of=%_topdir/%buildroot/lib/toolarge/largedata.txt bs=1M count=1

%files
/lib/toolarge/largedata.txt

%changelog
- Initial build. First version

43 changes: 43 additions & 0 deletions pytests/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ def disable_cache(utils):
utils.run(['sed', '-i', '/keepcache/d', tdnf_config])


def switch_cache_path(utils, new_path):
tdnf_config = os.path.join(utils.config['repo_path'], 'tdnf.conf')
utils.run(['sed', '-i', '/cachedir/d', tdnf_config])
utils.run(['sed', '-i', '$ a cachedir={}'.format(new_path), tdnf_config])


def clean_small_cache(utils):
utils.run(['rm', '-rf', utils.config['small_cache_path']])


def try_mount_small_cache():
import subprocess
mount_script = subprocess.Popen(
'./mount-small-cache',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd="."
)
out, err = mount_script.communicate()
return mount_script.returncode


def check_package_in_cache(utils, pkgname):
cache_dir = utils.tdnf_config.get('main', 'cachedir')
ret = utils.run(['find', cache_dir, '-name', pkgname + '*.rpm'])
Expand Down Expand Up @@ -167,3 +190,23 @@ def test_download_vs_cache_size_multiple_packages(utils):
cached_rpm_bytes = sum(utils.get_cached_package_sizes(cache_dir).values())

assert(utils.floats_approx_equal(down_bytes, cached_rpm_bytes))


@pytest.mark.skipif(try_mount_small_cache() != 0, reason="Failed to mount small cache directory.")
def test_cache_directory_out_of_disk_space(utils):
small_cache_path = utils.config['small_cache_path']
switch_cache_path(utils, small_cache_path)
enable_cache(utils)
clean_small_cache(utils)

run_args = ['tdnf', 'install', '-y', '--nogpgcheck']
pkg_list = [utils.config["toolarge_pkgname"]]
for pkgname in pkg_list:
utils.erase_package(pkgname)
run_args.append(pkgname)
ret = utils.run(run_args)

switch_cache_path(utils, utils.tdnf_config.get('main', 'cachedir'))
clean_cache(utils)
clean_small_cache(utils)
assert(ret['retval'] == 1036)

0 comments on commit 1330f64

Please sign in to comment.