From 2bc719a33aacba151efe39bc229d4788bfbe400f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Tue, 18 Apr 2017 16:12:04 +0200 Subject: [PATCH] util: Show choices for volid if all are too long MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we fail to generate a volume ID that fits in 32 characters, the error message should include the options that were considered. It could show that there might be a substitution that could fix the problem. Signed-off-by: Lubomír Sedlář --- pungi/util.py | 5 ++++- tests/test_util.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pungi/util.py b/pungi/util.py index 43953a55..6174bad5 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -366,6 +366,7 @@ def get_volid(compose, arch, variant=None, escape_spaces=False, disc_type=False) else: all_products = products + tried = set() for i in all_products: if not variant_uid and "%(variant)s" in i: continue @@ -384,9 +385,11 @@ def get_volid(compose, arch, variant=None, escape_spaces=False, disc_type=False) volid = _apply_substitutions(compose, volid) if len(volid) <= 32: break + tried.add(volid) if volid and len(volid) > 32: - raise ValueError("Could not create volume ID <= 32 characters") + raise ValueError("Could not create volume ID longer than 32 bytes, options are %r", + sorted(tried, key=len)) if volid and escape_spaces: volid = volid.replace(" ", r"\x20") diff --git a/tests/test_util.py b/tests/test_util.py index d6c5d0c2..dac94396 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -158,6 +158,28 @@ class TestVolumeIdGenerator(unittest.TestCase): self.assertEqual(volid, expected) + @mock.patch('pungi.compose.ComposeInfo') + def test_get_volid_too_long(self, ci): + conf = { + 'release_short': 'rel_short2', + 'release_version': '6.0', + 'release_is_layered': False, + 'image_volid_formats': [ + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', # 34 chars + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', # 33 chars + ], + 'image_volid_layered_product_formats': [], + 'volume_id_substitutions': {}, + } + variant = mock.Mock(uid='Server', type='variant') + c = compose.Compose(conf, self.tmp_dir) + + with self.assertRaises(ValueError) as ctx: + util.get_volid(c, 'x86_64', variant, escape_spaces=False, disc_type=False) + + self.assertIn('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', str(ctx.exception)) + self.assertIn('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', str(ctx.exception)) + class TestFindOldCompose(unittest.TestCase): def setUp(self):