2015-11-20 12:14:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-25 11:39:02 +00:00
|
|
|
try:
|
|
|
|
import unittest2 as unittest
|
|
|
|
except ImportError:
|
|
|
|
import unittest
|
2024-01-26 07:38:32 +00:00
|
|
|
try:
|
|
|
|
from unittest import mock
|
|
|
|
except ImportError:
|
|
|
|
import mock
|
2015-11-20 12:14:24 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
|
2017-07-12 12:52:40 +00:00
|
|
|
from pungi.phases.image_checksum import ImageChecksumPhase, dump_checksums
|
2016-02-22 14:58:34 +00:00
|
|
|
from tests.helpers import DummyCompose, PungiTestCase
|
2015-11-20 12:14:24 +00:00
|
|
|
|
|
|
|
|
2016-02-11 14:15:36 +00:00
|
|
|
class TestImageChecksumPhase(PungiTestCase):
|
2016-09-14 08:18:01 +00:00
|
|
|
def test_phase_is_never_skipped(self):
|
|
|
|
compose = DummyCompose(self.topdir, {})
|
|
|
|
phase = ImageChecksumPhase(compose)
|
|
|
|
self.assertFalse(phase.skip())
|
|
|
|
|
2015-11-20 12:14:24 +00:00
|
|
|
def test_config_skip_individual_with_multiple_algorithms(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
compose = DummyCompose(
|
|
|
|
self.topdir,
|
|
|
|
{"media_checksums": ["md5", "sha1"], "media_checksum_one_file": True},
|
|
|
|
)
|
2015-11-20 12:14:24 +00:00
|
|
|
phase = ImageChecksumPhase(compose)
|
2016-08-22 14:08:25 +00:00
|
|
|
with self.assertRaises(ValueError) as ctx:
|
2015-11-20 12:14:24 +00:00
|
|
|
phase.validate()
|
2016-08-22 14:08:25 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertIn("media_checksum_one_file", str(ctx.exception))
|
2015-11-20 12:14:24 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("os.path.exists")
|
|
|
|
@mock.patch("kobo.shortcuts.compute_file_checksums")
|
|
|
|
@mock.patch("pungi.phases.image_checksum.dump_checksums")
|
2017-07-12 12:52:40 +00:00
|
|
|
def test_checksum_one_file(self, dump_checksums, cc, exists):
|
2020-01-22 10:02:22 +00:00
|
|
|
compose = DummyCompose(
|
|
|
|
self.topdir,
|
|
|
|
{"media_checksums": ["sha256"], "media_checksum_one_file": True},
|
|
|
|
)
|
2015-11-20 12:14:24 +00:00
|
|
|
|
|
|
|
phase = ImageChecksumPhase(compose)
|
|
|
|
|
|
|
|
exists.return_value = True
|
2020-01-22 10:02:22 +00:00
|
|
|
cc.return_value = {"sha256": "cafebabe"}
|
2015-11-20 12:14:24 +00:00
|
|
|
|
|
|
|
phase.run()
|
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
dump_checksums.assert_called_once_with(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/CHECKSUM",
|
|
|
|
set([("image.iso", 123, "sha256", "cafebabe")]),
|
|
|
|
)
|
|
|
|
cc.assert_called_once_with(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso", ["sha256"]
|
|
|
|
)
|
|
|
|
compose.image.add_checksum.assert_called_once_with(None, "sha256", "cafebabe")
|
2015-11-20 12:14:24 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
@mock.patch("os.path.exists")
|
|
|
|
@mock.patch("kobo.shortcuts.compute_file_checksums")
|
|
|
|
@mock.patch("pungi.phases.image_checksum.dump_checksums")
|
2017-07-12 12:52:40 +00:00
|
|
|
def test_checksum_save_individuals(self, dump_checksums, cc, exists):
|
2020-01-22 10:02:22 +00:00
|
|
|
compose = DummyCompose(self.topdir, {"media_checksums": ["md5", "sha256"]})
|
2015-11-20 12:14:24 +00:00
|
|
|
|
|
|
|
phase = ImageChecksumPhase(compose)
|
|
|
|
|
|
|
|
exists.return_value = True
|
2020-01-22 10:02:22 +00:00
|
|
|
cc.return_value = {"md5": "cafebabe", "sha256": "deadbeef"}
|
2015-11-20 12:14:24 +00:00
|
|
|
|
|
|
|
phase.run()
|
|
|
|
|
2017-01-23 09:54:54 +00:00
|
|
|
dump_checksums.assert_has_calls(
|
2020-01-22 10:02:22 +00:00
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso.MD5SUM",
|
|
|
|
set([("image.iso", 123, "md5", "cafebabe")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso.SHA256SUM",
|
|
|
|
set([("image.iso", 123, "sha256", "deadbeef")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/MD5SUM",
|
|
|
|
set([("image.iso", 123, "md5", "cafebabe")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/SHA256SUM",
|
|
|
|
set([("image.iso", 123, "sha256", "deadbeef")]),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
any_order=True,
|
|
|
|
)
|
|
|
|
cc.assert_called_once_with(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso", ["md5", "sha256"]
|
|
|
|
)
|
|
|
|
compose.image.add_checksum.assert_has_calls(
|
|
|
|
[mock.call(None, "sha256", "deadbeef"), mock.call(None, "md5", "cafebabe")],
|
|
|
|
any_order=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch("os.path.exists")
|
|
|
|
@mock.patch("kobo.shortcuts.compute_file_checksums")
|
|
|
|
@mock.patch("pungi.phases.image_checksum.dump_checksums")
|
2017-07-12 12:52:40 +00:00
|
|
|
def test_checksum_one_file_custom_name(self, dump_checksums, cc, exists):
|
2020-01-22 10:02:22 +00:00
|
|
|
compose = DummyCompose(
|
|
|
|
self.topdir,
|
|
|
|
{
|
|
|
|
"media_checksums": ["sha256"],
|
|
|
|
"media_checksum_one_file": True,
|
2020-02-06 07:09:32 +00:00
|
|
|
"media_checksum_base_filename": "%(release_short)s-%(variant)s-%(version)s-%(date)s%(type_suffix)s.%(respin)s_%(label)s-%(dirname)s", # noqa: E501
|
2020-01-22 10:02:22 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
compose.compose_label = "Alpha-1.0"
|
2015-12-03 08:03:50 +00:00
|
|
|
|
|
|
|
phase = ImageChecksumPhase(compose)
|
|
|
|
|
|
|
|
exists.return_value = True
|
2020-01-22 10:02:22 +00:00
|
|
|
cc.return_value = {"sha256": "cafebabe"}
|
2015-12-03 08:03:50 +00:00
|
|
|
|
|
|
|
phase.run()
|
|
|
|
|
2017-07-12 12:52:40 +00:00
|
|
|
dump_checksums.assert_called_once_with(
|
2020-01-22 10:02:22 +00:00
|
|
|
self.topdir
|
2020-02-06 07:09:32 +00:00
|
|
|
+ "/compose/Client/i386/iso/test-Client-1.0-20151203.t.0_Alpha-1.0-iso-CHECKSUM", # noqa: E501
|
2020-01-22 10:02:22 +00:00
|
|
|
set([("image.iso", 123, "sha256", "cafebabe")]),
|
|
|
|
)
|
|
|
|
cc.assert_called_once_with(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso", ["sha256"]
|
|
|
|
)
|
|
|
|
compose.image.add_checksum.assert_called_once_with(None, "sha256", "cafebabe")
|
|
|
|
|
|
|
|
@mock.patch("os.path.exists")
|
|
|
|
@mock.patch("kobo.shortcuts.compute_file_checksums")
|
|
|
|
@mock.patch("pungi.phases.image_checksum.dump_checksums")
|
2017-07-12 12:52:40 +00:00
|
|
|
def test_checksum_save_individuals_custom_name(self, dump_checksums, cc, exists):
|
2020-01-22 10:02:22 +00:00
|
|
|
compose = DummyCompose(
|
|
|
|
self.topdir,
|
|
|
|
{
|
|
|
|
"media_checksums": ["md5", "sha256"],
|
2020-02-06 07:09:32 +00:00
|
|
|
"media_checksum_base_filename": "%(release_short)s-%(variant)s-%(version)s-%(date)s%(type_suffix)s.%(respin)s", # noqa: E501
|
2020-01-22 10:02:22 +00:00
|
|
|
},
|
|
|
|
)
|
2015-12-03 08:03:50 +00:00
|
|
|
|
|
|
|
phase = ImageChecksumPhase(compose)
|
|
|
|
|
|
|
|
exists.return_value = True
|
2020-01-22 10:02:22 +00:00
|
|
|
cc.return_value = {"md5": "cafebabe", "sha256": "deadbeef"}
|
2015-12-03 08:03:50 +00:00
|
|
|
|
|
|
|
phase.run()
|
|
|
|
|
2017-01-23 09:54:54 +00:00
|
|
|
dump_checksums.assert_has_calls(
|
2020-01-22 10:02:22 +00:00
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso.MD5SUM",
|
|
|
|
set([("image.iso", 123, "md5", "cafebabe")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso.SHA256SUM",
|
|
|
|
set([("image.iso", 123, "sha256", "deadbeef")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir
|
|
|
|
+ "/compose/Client/i386/iso/test-Client-1.0-20151203.t.0-MD5SUM",
|
|
|
|
set([("image.iso", 123, "md5", "cafebabe")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir
|
|
|
|
+ "/compose/Client/i386/iso/test-Client-1.0-20151203.t.0-SHA256SUM",
|
|
|
|
set([("image.iso", 123, "sha256", "deadbeef")]),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
any_order=True,
|
|
|
|
)
|
|
|
|
cc.assert_called_once_with(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso", ["md5", "sha256"]
|
|
|
|
)
|
|
|
|
compose.image.add_checksum.assert_has_calls(
|
|
|
|
[mock.call(None, "sha256", "deadbeef"), mock.call(None, "md5", "cafebabe")],
|
|
|
|
any_order=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch("os.path.exists")
|
|
|
|
@mock.patch("kobo.shortcuts.compute_file_checksums")
|
|
|
|
@mock.patch("pungi.phases.image_checksum.dump_checksums")
|
|
|
|
def test_checksum_save_individuals_custom_name_str_format(
|
|
|
|
self, dump_checksums, cc, exists
|
|
|
|
):
|
|
|
|
compose = DummyCompose(
|
|
|
|
self.topdir,
|
|
|
|
{
|
|
|
|
"media_checksums": ["md5", "sha256"],
|
2020-02-06 07:09:32 +00:00
|
|
|
"media_checksum_base_filename": "{release_short}-{variant}-{version}-{date}{type_suffix}.{respin}", # noqa: E501
|
2020-01-22 10:02:22 +00:00
|
|
|
},
|
|
|
|
)
|
2016-06-27 13:15:01 +00:00
|
|
|
|
|
|
|
phase = ImageChecksumPhase(compose)
|
|
|
|
|
|
|
|
exists.return_value = True
|
2020-01-22 10:02:22 +00:00
|
|
|
cc.return_value = {"md5": "cafebabe", "sha256": "deadbeef"}
|
2016-06-27 13:15:01 +00:00
|
|
|
|
|
|
|
phase.run()
|
|
|
|
|
2017-01-23 09:54:54 +00:00
|
|
|
dump_checksums.assert_has_calls(
|
2020-01-22 10:02:22 +00:00
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso.MD5SUM",
|
|
|
|
set([("image.iso", 123, "md5", "cafebabe")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso.SHA256SUM",
|
|
|
|
set([("image.iso", 123, "sha256", "deadbeef")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir
|
|
|
|
+ "/compose/Client/i386/iso/test-Client-1.0-20151203.t.0-MD5SUM",
|
|
|
|
set([("image.iso", 123, "md5", "cafebabe")]),
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
self.topdir
|
|
|
|
+ "/compose/Client/i386/iso/test-Client-1.0-20151203.t.0-SHA256SUM",
|
|
|
|
set([("image.iso", 123, "sha256", "deadbeef")]),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
any_order=True,
|
|
|
|
)
|
|
|
|
cc.assert_called_once_with(
|
|
|
|
self.topdir + "/compose/Client/i386/iso/image.iso", ["md5", "sha256"]
|
|
|
|
)
|
|
|
|
compose.image.add_checksum.assert_has_calls(
|
|
|
|
[mock.call(None, "sha256", "deadbeef"), mock.call(None, "md5", "cafebabe")],
|
|
|
|
any_order=True,
|
|
|
|
)
|
2015-11-20 12:14:24 +00:00
|
|
|
|
2017-01-23 09:54:54 +00:00
|
|
|
|
2017-07-12 12:52:40 +00:00
|
|
|
class TestDumpChecksums(unittest.TestCase):
|
2015-11-20 12:14:24 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.tmp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.tmp_dir)
|
|
|
|
|
|
|
|
def test_dump_checksums(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
dump_checksums(
|
|
|
|
os.path.join(self.tmp_dir, "CHECKSUM"),
|
|
|
|
[
|
|
|
|
("file2.iso", 456, "md5", "cafebabe"),
|
|
|
|
("file1.iso", 123, "md5", "abcdef"),
|
|
|
|
],
|
|
|
|
)
|
2015-11-20 12:14:24 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
with open(os.path.join(self.tmp_dir, "CHECKSUM"), "r") as f:
|
|
|
|
data = f.read().rstrip().split("\n")
|
2015-11-20 12:14:24 +00:00
|
|
|
expected = [
|
2020-01-22 10:02:22 +00:00
|
|
|
"# file1.iso: 123 bytes",
|
|
|
|
"MD5 (file1.iso) = abcdef",
|
|
|
|
"# file2.iso: 456 bytes",
|
|
|
|
"MD5 (file2.iso) = cafebabe",
|
2015-11-20 12:14:24 +00:00
|
|
|
]
|
2017-07-12 12:52:40 +00:00
|
|
|
self.assertEqual(expected, data)
|