media-split: Print sensible message for unlimited size

If the media is bootable, we can not split it. The limit is not used in
that case and we may overflow it with an warning message. The warning
should correctly mention what is going on instead of printing a
non-sensical message about free space on media being some huge number.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-11-08 14:39:24 +01:00
parent b7813d34ac
commit 6203541ac6
2 changed files with 39 additions and 6 deletions

View File

@ -62,7 +62,7 @@ class MediaSplitter(object):
possible minimum.
"""
def __init__(self, media_size, compose=None):
self.media_size = convert_media_size(media_size)
self.media_size = media_size
self.files = [] # to preserve order
self.file_sizes = {}
self.sticky_files = set()
@ -75,7 +75,7 @@ class MediaSplitter(object):
if old_size is not None and old_size != size:
raise ValueError("File size mismatch; file: %s; sizes: %s vs %s" % (name, old_size, size))
if size > self.media_size:
if self.media_size and size > self.media_size:
raise ValueError("File is larger than media size: %s" % name)
self.files.append(name)
@ -111,7 +111,7 @@ class MediaSplitter(object):
name = all_files.pop(0)
size = convert_file_size(self.file_sizes[name])
if not disks or disk["size"] + size > self.media_size:
if not disks or (self.media_size and disk["size"] + size > self.media_size):
disk = {"size": sticky_files_size, "files": sticky_files[:]}
disks.append(disk)
@ -119,5 +119,10 @@ class MediaSplitter(object):
disk["size"] += size
total_size_single += size
if self.compose:
self.compose.log_debug("MediaSplitter: free space on single media would be %s. Total size of single medium: %s." % (self.media_size - total_size_single, total_size_single))
if self.media_size:
self.compose.log_debug("MediaSplitter: free space on single media would be %s. "
"Total size of single medium: %s."
% (self.media_size - total_size_single, total_size_single))
else:
self.compose.log_debug("MediaSplitter: Total size of single medium: %s." % total_size_single)
return disks

View File

@ -7,6 +7,7 @@ except ImportError:
import unittest
import os
import sys
import mock
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
@ -59,6 +60,20 @@ def bl(s):
class MediaSplitterTestCase(unittest.TestCase):
def setUp(self):
self.compose = mock.Mock()
def assertFreeSpace(self, free, total):
self.assertEqual(
self.compose.mock_calls,
[mock.call.log_debug('MediaSplitter: free space on single media would be %s. '
'Total size of single medium: %s.' % (free, total))])
def assertUnlimited(self, total):
self.assertEqual(
self.compose.mock_calls,
[mock.call.log_debug('MediaSplitter: Total size of single medium: %s.' % total)])
def test_sum_size(self):
ms = media_split.MediaSplitter(bl(100))
ms.add_file('first', bl(20))
@ -87,15 +102,16 @@ class MediaSplitterTestCase(unittest.TestCase):
ms.add_file('too-big', bl(300))
def test_fit_on_one(self):
ms = media_split.MediaSplitter(bl(100))
ms = media_split.MediaSplitter(bl(100), compose=self.compose)
ms.add_file('first', bl(20))
ms.add_file('second', bl(30))
self.assertEqual(ms.split(),
[{'files': ['first', 'second'], 'size': bl(50)}])
self.assertFreeSpace(bl(50), bl(50))
def test_split_on_two_discs(self):
ms = media_split.MediaSplitter(bl(100))
ms = media_split.MediaSplitter(bl(100), compose=self.compose)
ms.add_file('first', bl(25))
ms.add_file('second', bl(40))
ms.add_file('third', bl(80))
@ -103,6 +119,7 @@ class MediaSplitterTestCase(unittest.TestCase):
self.assertEqual(ms.split(),
[{'files': ['first', 'second'], 'size': bl(65)},
{'files': ['third'], 'size': bl(80)}])
self.assertFreeSpace(bl(100 - 25 - 40 - 80), bl(25 + 40 + 80))
def test_split_with_sticky_file(self):
ms = media_split.MediaSplitter(bl(100))
@ -115,5 +132,16 @@ class MediaSplitterTestCase(unittest.TestCase):
[{'files': ['sticky', 'first', 'second'], 'size': bl(80)},
{'files': ['sticky', 'third'], 'size': bl(95)}])
def test_split_unlimited_media(self):
ms = media_split.MediaSplitter(None, compose=self.compose)
ms.add_file('first', bl(25))
ms.add_file('second', bl(40))
ms.add_file('third', bl(80))
self.assertEqual(ms.split(),
[{'files': ['first', 'second', 'third'], 'size': bl(145)}])
self.assertUnlimited(bl(25 + 40 + 80))
if __name__ == "__main__":
unittest.main()