2017-03-02 08:10:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-04-06 13:28:33 +00:00
|
|
|
import itertools
|
2017-03-02 08:10:27 +00:00
|
|
|
import mock
|
|
|
|
import os
|
2020-01-29 14:15:47 +00:00
|
|
|
import six
|
2020-01-22 10:02:22 +00:00
|
|
|
|
2018-04-06 13:28:33 +00:00
|
|
|
try:
|
|
|
|
import unittest2 as unittest
|
|
|
|
except ImportError:
|
|
|
|
import unittest
|
2017-03-02 08:10:27 +00:00
|
|
|
|
|
|
|
from pungi.wrappers import iso
|
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
CORRECT_OUTPUT = """dummy.iso: 31ff3e405e26ad01c63b62f6b11d30f6
|
2017-03-02 08:10:27 +00:00
|
|
|
Fragment sums: 6eb92e7bda221d7fe5f19b4d21468c9bf261d84c96d145d96c76444b9cbc
|
|
|
|
Fragment count: 20
|
|
|
|
Supported ISO: no
|
2020-01-22 10:02:22 +00:00
|
|
|
"""
|
2017-03-02 08:10:27 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
INCORRECT_OUTPUT = """This should never happen: File not found"""
|
2017-03-02 08:10:27 +00:00
|
|
|
|
2020-01-29 14:15:47 +00:00
|
|
|
# Cached to use in tests that mock os.listdir
|
|
|
|
orig_listdir = os.listdir
|
|
|
|
|
|
|
|
|
|
|
|
def fake_listdir(pattern, result=None, exc=None):
|
|
|
|
"""Create a function that mocks os.listdir. If the path contains pattern,
|
|
|
|
result will be returned or exc raised. Otherwise it's normal os.listdir
|
|
|
|
"""
|
|
|
|
# The point of this is to avoid issues on Python 2, where apparently
|
|
|
|
# isdir() is using listdir(), so the mocking is breaking it.
|
|
|
|
def worker(path):
|
|
|
|
if isinstance(path, six.string_types) and pattern in path:
|
|
|
|
if exc:
|
|
|
|
raise exc
|
|
|
|
return result
|
|
|
|
return orig_listdir(path)
|
2020-01-22 10:02:22 +00:00
|
|
|
|
2020-01-29 14:15:47 +00:00
|
|
|
return worker
|
|
|
|
|
2017-03-02 08:10:27 +00:00
|
|
|
|
|
|
|
class TestIsoUtils(unittest.TestCase):
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2017-03-02 08:10:27 +00:00
|
|
|
def test_get_implanted_md5_correct(self, mock_run):
|
|
|
|
mock_run.return_value = (0, CORRECT_OUTPUT)
|
|
|
|
logger = mock.Mock()
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(
|
|
|
|
iso.get_implanted_md5("dummy.iso", logger=logger),
|
|
|
|
"31ff3e405e26ad01c63b62f6b11d30f6",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_run.call_args_list,
|
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
["/usr/bin/checkisomd5", "--md5sumonly", "dummy.iso"],
|
|
|
|
universal_newlines=True,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2017-03-02 08:10:27 +00:00
|
|
|
self.assertEqual(logger.mock_calls, [])
|
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2017-03-02 08:10:27 +00:00
|
|
|
def test_get_implanted_md5_incorrect(self, mock_run):
|
|
|
|
mock_run.return_value = (0, INCORRECT_OUTPUT)
|
|
|
|
logger = mock.Mock()
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(iso.get_implanted_md5("dummy.iso", logger=logger), None)
|
|
|
|
self.assertEqual(
|
|
|
|
mock_run.call_args_list,
|
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
["/usr/bin/checkisomd5", "--md5sumonly", "dummy.iso"],
|
|
|
|
universal_newlines=True,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2018-03-29 14:34:52 +00:00
|
|
|
self.assertTrue(len(logger.mock_calls) > 0)
|
2017-02-20 09:34:55 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.util.run_unmount_cmd")
|
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2017-02-20 09:34:55 +00:00
|
|
|
def test_mount_iso(self, mock_run, mock_unmount):
|
2017-12-21 02:18:00 +00:00
|
|
|
# first tuple is return value for command 'which guestmount'
|
2020-02-06 07:09:32 +00:00
|
|
|
# value determines type of the mount/unmount
|
|
|
|
# command ('1' - guestmount is not available)
|
2017-12-21 02:18:00 +00:00
|
|
|
# for approach as a root, pair commands mount-umount are used
|
2020-01-22 10:02:22 +00:00
|
|
|
mock_run.side_effect = [(1, ""), (0, "")]
|
|
|
|
with iso.mount("dummy") as temp_dir:
|
2017-12-21 02:18:00 +00:00
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
|
|
|
self.assertEqual(len(mock_run.call_args_list), 2)
|
|
|
|
mount_call_str = str(mock_run.call_args_list[1])
|
|
|
|
self.assertTrue(mount_call_str.startswith("call(['mount'"))
|
|
|
|
self.assertEqual(len(mock_unmount.call_args_list), 1)
|
|
|
|
unmount_call_str = str(mock_unmount.call_args_list[0])
|
|
|
|
self.assertTrue(unmount_call_str.startswith("call(['umount'"))
|
|
|
|
self.assertFalse(os.path.isdir(temp_dir))
|
|
|
|
|
2020-01-29 14:15:47 +00:00
|
|
|
@mock.patch("pungi.util.rmtree")
|
|
|
|
@mock.patch("os.listdir", new=fake_listdir("guestfs", ["root"]))
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.util.run_unmount_cmd")
|
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2020-01-29 14:15:47 +00:00
|
|
|
def test_guestmount(self, mock_run, mock_unmount, mock_rmtree):
|
|
|
|
# first tuple is return value for command 'which guestmount'
|
2020-02-06 07:09:32 +00:00
|
|
|
# value determines type of the mount/unmount
|
|
|
|
# command ('0' - guestmount is available)
|
2020-01-29 14:15:47 +00:00
|
|
|
# for approach as a non-root, pair commands guestmount-fusermount are used
|
2020-01-22 10:02:22 +00:00
|
|
|
mock_run.side_effect = [(0, ""), (0, "")]
|
|
|
|
with iso.mount("dummy") as temp_dir:
|
2020-01-29 14:15:47 +00:00
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
|
|
|
self.assertEqual(len(mock_run.call_args_list), 2)
|
|
|
|
mount_call_str = str(mock_run.call_args_list[1])
|
|
|
|
self.assertTrue(mount_call_str.startswith("call(['guestmount'"))
|
|
|
|
self.assertEqual(len(mock_unmount.call_args_list), 1)
|
|
|
|
unmount_call_str = str(mock_unmount.call_args_list[0])
|
|
|
|
self.assertTrue(unmount_call_str.startswith("call(['fusermount'"))
|
|
|
|
self.assertFalse(os.path.isdir(temp_dir))
|
|
|
|
|
|
|
|
@mock.patch("pungi.util.rmtree")
|
|
|
|
@mock.patch("os.listdir", new=fake_listdir("guestfs", []))
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.util.run_unmount_cmd")
|
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2020-01-29 14:15:47 +00:00
|
|
|
def test_guestmount_cleans_up_cache(self, mock_run, mock_unmount, mock_rmtree):
|
|
|
|
# first tuple is return value for command 'which guestmount'
|
2020-02-06 07:09:32 +00:00
|
|
|
# value determines type of the mount/unmount
|
|
|
|
# command ('0' - guestmount is available)
|
2020-01-29 14:15:47 +00:00
|
|
|
# for approach as a non-root, pair commands guestmount-fusermount are used
|
2020-01-22 10:02:22 +00:00
|
|
|
mock_run.side_effect = [(0, ""), (0, "")]
|
|
|
|
with iso.mount("dummy") as temp_dir:
|
2020-01-29 14:15:47 +00:00
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
|
|
|
self.assertEqual(len(mock_run.call_args_list), 2)
|
|
|
|
mount_call_str = str(mock_run.call_args_list[1])
|
|
|
|
self.assertTrue(mount_call_str.startswith("call(['guestmount'"))
|
|
|
|
self.assertEqual(len(mock_unmount.call_args_list), 1)
|
|
|
|
unmount_call_str = str(mock_unmount.call_args_list[0])
|
|
|
|
self.assertTrue(unmount_call_str.startswith("call(['fusermount'"))
|
|
|
|
self.assertFalse(os.path.isdir(temp_dir))
|
|
|
|
|
|
|
|
@mock.patch("pungi.util.rmtree")
|
|
|
|
@mock.patch("os.listdir", new=fake_listdir("guestfs", OSError("No such file")))
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.util.run_unmount_cmd")
|
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
|
|
|
def test_guestmount_handles_missing_cache(
|
|
|
|
self, mock_run, mock_unmount, mock_rmtree
|
|
|
|
):
|
2017-12-21 02:18:00 +00:00
|
|
|
# first tuple is return value for command 'which guestmount'
|
2020-02-06 07:09:32 +00:00
|
|
|
# value determines type of the mount/unmount
|
|
|
|
# command ('0' - guestmount is available)
|
2017-12-21 02:18:00 +00:00
|
|
|
# for approach as a non-root, pair commands guestmount-fusermount are used
|
2020-01-22 10:02:22 +00:00
|
|
|
mock_run.side_effect = [(0, ""), (0, "")]
|
|
|
|
with iso.mount("dummy") as temp_dir:
|
2017-02-20 09:34:55 +00:00
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
2017-12-21 02:18:00 +00:00
|
|
|
self.assertEqual(len(mock_run.call_args_list), 2)
|
|
|
|
mount_call_str = str(mock_run.call_args_list[1])
|
|
|
|
self.assertTrue(mount_call_str.startswith("call(['guestmount'"))
|
2017-02-20 09:34:55 +00:00
|
|
|
self.assertEqual(len(mock_unmount.call_args_list), 1)
|
2017-12-21 02:18:00 +00:00
|
|
|
unmount_call_str = str(mock_unmount.call_args_list[0])
|
|
|
|
self.assertTrue(unmount_call_str.startswith("call(['fusermount'"))
|
2017-02-20 09:34:55 +00:00
|
|
|
self.assertFalse(os.path.isdir(temp_dir))
|
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.util.run_unmount_cmd")
|
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2017-02-20 09:34:55 +00:00
|
|
|
def test_mount_iso_always_unmounts(self, mock_run, mock_unmount):
|
2020-01-22 10:02:22 +00:00
|
|
|
mock_run.side_effect = [(1, ""), (0, "")]
|
2017-02-20 09:34:55 +00:00
|
|
|
try:
|
2020-01-22 10:02:22 +00:00
|
|
|
with iso.mount("dummy") as temp_dir:
|
2017-02-20 09:34:55 +00:00
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
|
|
|
raise RuntimeError()
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
2017-12-21 02:18:00 +00:00
|
|
|
self.assertEqual(len(mock_run.call_args_list), 2)
|
2017-02-20 09:34:55 +00:00
|
|
|
self.assertEqual(len(mock_unmount.call_args_list), 1)
|
|
|
|
self.assertFalse(os.path.isdir(temp_dir))
|
2017-05-16 08:28:55 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("pungi.util.run_unmount_cmd")
|
|
|
|
@mock.patch("pungi.wrappers.iso.run")
|
2017-05-16 08:28:55 +00:00
|
|
|
def test_mount_iso_raises_on_error(self, mock_run, mock_unmount):
|
|
|
|
log = mock.Mock()
|
2020-01-22 10:02:22 +00:00
|
|
|
mock_run.side_effect = [(1, ""), (1, "Boom")]
|
2017-05-16 08:28:55 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
2020-01-22 10:02:22 +00:00
|
|
|
with iso.mount("dummy", logger=log) as temp_dir:
|
2017-05-16 08:28:55 +00:00
|
|
|
self.assertTrue(os.path.isdir(temp_dir))
|
2017-12-21 02:18:00 +00:00
|
|
|
self.assertEqual(len(mock_run.call_args_list), 2)
|
2017-05-16 08:28:55 +00:00
|
|
|
self.assertEqual(len(mock_unmount.call_args_list), 0)
|
|
|
|
self.assertEqual(len(log.mock_calls), 1)
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCmpGraftPoints(unittest.TestCase):
|
|
|
|
def assertSorted(self, *args):
|
|
|
|
"""Tests that all permutations of arguments yield the same sorted results."""
|
|
|
|
for perm in itertools.permutations(args):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(sorted(perm, key=iso.graft_point_sort_key), list(args))
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_eq(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("pkgs/foo.rpm", "pkgs/foo.rpm")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_rpms_sorted_alphabetically(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("pkgs/bar.rpm", "pkgs/foo.rpm")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_images_sorted_alphabetically(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("aaa.img", "images/foo", "isolinux/foo")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_other_files_sorted_alphabetically(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("bar.txt", "foo.txt")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_rpms_after_images(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("foo.ins", "bar.rpm")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_other_after_images(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("EFI/anything", "zzz.txt")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_rpm_after_other(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("bbb.txt", "aaa.rpm")
|
2017-11-02 13:51:00 +00:00
|
|
|
|
|
|
|
def test_all_kinds(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertSorted("etc/file", "ppc/file", "c.txt", "d.txt", "a.rpm", "b.rpm")
|