tests: Add tests for image-minimizer

This commit is contained in:
Brian C. Lane 2021-12-21 13:18:23 -08:00
parent ab9068c2d0
commit cc90406a58
4 changed files with 58 additions and 1 deletions

View File

@ -47,7 +47,7 @@ check:
test:
@echo "*** Running tests ***"
PYTHONPATH=$(PYTHONPATH):./src/ $(PYTHON) -X dev -m pytest -v --cov-branch \
--cov=pylorax ./tests/pylorax/
--cov=pylorax ./tests/pylorax/ ./tests/image-minimizer/
coverage3 report -m
[ -f "/usr/bin/coveralls" ] && [ -n "$(COVERALLS_REPO_TOKEN)" ] && coveralls || echo

View File

@ -0,0 +1,11 @@
# test script for image-minimizer
drop /etc/pki/rpm-gpg/*
keep /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-11-primary
keep /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-12-primary
drop /usr/share/zoneinfo/*
keep /usr/share/zoneinfo/America
keep /usr/share/zoneinfo/US
keep /usr/share/zoneinfo/UTC
droprpm fedora-*
keeprpm fedora-release
keeprpm fedora-gpg-keys

View File

@ -0,0 +1 @@
../../src/bin/image-minimizer

View File

@ -0,0 +1,45 @@
import os
from subprocess import check_call, CalledProcessError
import tempfile
import unittest
from minimizer import ImageMinimizer
class BuildStampTestCase(unittest.TestCase):
def test_minimizer_ok(self):
with tempfile.TemporaryDirectory(prefix="minimize.test.") as rootdir:
check_call(["dnf", "--installroot", rootdir, "install", "-y", \
"filesystem"])
im = ImageMinimizer("./tests/image-minimizer/im-script.txt", rootdir, False, False)
im.filter()
# /etc/pki/rpm-gpg/ should only have 2 files
self.assertEqual(sorted(os.listdir(f"{rootdir}/etc/pki/rpm-gpg/")), ["RPM-GPG-KEY-fedora-11-primary", "RPM-GPG-KEY-fedora-12-primary"])
# zoneinfo should have 2 directories and a file
self.assertEqual(sorted(os.listdir(f"{rootdir}/usr/share/zoneinfo/")), ["America", "US", "UTC"])
check_call(["rpm", "--root", rootdir, "-q", "fedora-release", "fedora-gpg-keys"])
with self.assertRaises(CalledProcessError):
check_call(["rpm", "--root", rootdir, "-q", "fedora-repos"])
def test_minimizer_empty(self):
## No packages in tree (this is ok, nothing to remove)
with tempfile.TemporaryDirectory(prefix="minimize.test.") as rootdir:
im = ImageMinimizer("./tests/image-minimizer/im-script.txt", rootdir, False, False)
im.filter()
def test_minimizer_missing_script(self):
## No minimizer script
with tempfile.TemporaryDirectory(prefix="minimize.test.") as rootdir:
im = ImageMinimizer("./tests/image-minimizer/missing.txt", rootdir, False, False)
with self.assertRaises(FileNotFoundError):
im.filter()
def test_minimizer_missing_root(self):
## Missing directory
im = ImageMinimizer("./tests/image-minimizer/im-script.txt", "/tmp/minimizer.root", False, False)
with self.assertRaises(FileNotFoundError):
im.filter()