[virt-tools-list] [virt-bootstrap] [PATCH v5 04/13] Use libguestfs python's binding
Radostin Stoyanov
rstoyanov1 at gmail.com
Fri Aug 4 14:30:40 UTC 2017
These changes aim to remove the functions create_qcow2() and
extract_layers_in_qcow2() as well as the unit tests for them
and enable the use of libguestfs python's binding.
---
src/virtBootstrap/sources/docker_source.py | 6 +-
src/virtBootstrap/sources/file_source.py | 13 ++--
src/virtBootstrap/utils.py | 62 -------------------
tests/test_build_qcow2_image.py | 2 +-
tests/test_docker_source.py | 38 +++---------
tests/test_file_source.py | 35 -----------
tests/test_utils.py | 99 ------------------------------
7 files changed, 20 insertions(+), 235 deletions(-)
diff --git a/src/virtBootstrap/sources/docker_source.py b/src/virtBootstrap/sources/docker_source.py
index 63865d8..9d7c187 100644
--- a/src/virtBootstrap/sources/docker_source.py
+++ b/src/virtBootstrap/sources/docker_source.py
@@ -267,7 +267,11 @@ class DockerSource(object):
elif self.output_format == 'qcow2':
self.progress("Extracting container layers into qcow2 images",
value=50, logger=logger)
- utils.extract_layers_in_qcow2(self.layers, dest, self.progress)
+ utils.Build_QCOW2_Image(
+ tar_files=self.tar_files,
+ dest=dest,
+ progress=self.progress
+ )
else:
raise Exception("Unknown format:" + self.output_format)
diff --git a/src/virtBootstrap/sources/file_source.py b/src/virtBootstrap/sources/file_source.py
index c02f735..3bfccf2 100644
--- a/src/virtBootstrap/sources/file_source.py
+++ b/src/virtBootstrap/sources/file_source.py
@@ -63,14 +63,11 @@ class FileSource(object):
utils.safe_untar(self.path, dest)
elif self.output_format == 'qcow2':
- # Remove the old path
- file_name = os.path.basename(self.path)
- qcow2_file = os.path.realpath('{}/{}.qcow2'.format(dest,
- file_name))
-
- self.progress("Extracting files into qcow2 image", value=0,
- logger=logger)
- utils.create_qcow2(self.path, qcow2_file)
+ utils.Build_QCOW2_Image(
+ tar_files=[self.path],
+ dest=dest,
+ progress=self.progress
+ )
else:
raise Exception("Unknown format:" + self.output_format)
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index 2899022..a84c93f 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -42,7 +42,6 @@ logger = logging.getLogger(__name__)
DEFAULT_OUTPUT_FORMAT = 'dir'
# Default virtual size of qcow2 image
-DEF_QCOW2_SIZE = '5G'
DEF_BASE_IMAGE_SIZE = 5 * 1024 * 1024 * 1024
if os.geteuid() == 0:
@@ -302,67 +301,6 @@ def get_mime_type(path):
)
-def create_qcow2(tar_file, layer_file, backing_file=None, size=DEF_QCOW2_SIZE):
- """
- Create qcow2 image from tarball.
- """
- qemu_img_cmd = ["qemu-img", "create", "-f", "qcow2", layer_file, size]
-
- if not backing_file:
- logger.info("Creating base qcow2 image")
- execute(qemu_img_cmd)
-
- logger.info("Formatting qcow2 image")
- execute(['virt-format',
- '--format=qcow2',
- '--partition=none',
- '--filesystem=ext3',
- '-a', layer_file])
- else:
- # Add backing chain
- qemu_img_cmd.insert(2, "-b")
- qemu_img_cmd.insert(3, backing_file)
-
- logger.info("Creating qcow2 image with backing chain")
- execute(qemu_img_cmd)
-
- # Extract tarball using "tar-in" command from libguestfs
- tar_in_cmd = ["guestfish",
- "-a", layer_file,
- '-m', '/dev/sda',
- 'tar-in', tar_file, "/"]
-
- # Check if tarball is compressed
- compression = get_compression_type(tar_file)
- if compression is not None:
- tar_in_cmd.append('compress:' + compression)
-
- # Execute virt-tar-in command
- execute(tar_in_cmd)
-
-
-def extract_layers_in_qcow2(layers_list, dest_dir, progress):
- """
- Extract docker layers in qcow2 images with backing chains.
- """
- qcow2_backing_file = None
-
- nlayers = len(layers_list)
- for index, layer in enumerate(layers_list):
- log_layer_extract(layer, index + 1, nlayers, progress)
- tar_file = layer[2]
-
- # Name format for the qcow2 image
- qcow2_layer_file = "{}/layer-{}.qcow2".format(dest_dir, index)
- # Create the image layer
- create_qcow2(tar_file, qcow2_layer_file, qcow2_backing_file)
- # Keep the file path for the next layer
- qcow2_backing_file = qcow2_layer_file
-
- # Update progress value
- progress(value=(float(index + 1) / nlayers * 50) + 50)
-
-
def get_image_dir(no_cache=False):
"""
Get the directory where image layers are stored.
diff --git a/tests/test_build_qcow2_image.py b/tests/test_build_qcow2_image.py
index 3ace5a3..782761e 100644
--- a/tests/test_build_qcow2_image.py
+++ b/tests/test_build_qcow2_image.py
@@ -220,7 +220,7 @@ class TestBuild_Image(unittest.TestCase):
images = []
for archive in TAR_FILES:
dest = os.path.join(IMAGES_DIR, archive.split('.')[0])
- images.append(os.path.join(dest, "%s.qcow2" % archive))
+ images.append(os.path.join(dest, "layer-0.qcow2"))
uri = os.path.join(TAR_DIR, archive)
virt_bootstrap.bootstrap(
uri=uri,
diff --git a/tests/test_docker_source.py b/tests/test_docker_source.py
index 4859e1b..3865be6 100644
--- a/tests/test_docker_source.py
+++ b/tests/test_docker_source.py
@@ -525,14 +525,6 @@ class TestDockerSource(unittest.TestCase):
"""
self._unpack_test_fmt('dir', 'virtBootstrap.utils.untar_layers')
- def test_unpack_qcow2_format(self):
- """
- Ensures that unpack() calls extract_layers_in_qcow2() when the
- output format is set to 'qcow2'.
- """
- self._unpack_test_fmt('qcow2',
- 'virtBootstrap.utils.extract_layers_in_qcow2')
-
def unpack_raise_error_test(self,
output_format,
patch_method,
@@ -566,32 +558,20 @@ class TestDockerSource(unittest.TestCase):
patch_method = 'virtBootstrap.utils.untar_layers'
self.unpack_raise_error_test('dir', patch_method, side_effect, msg)
- def test_unpack_raise_error_if_extract_in_qcow2_fail(self):
- """
- Ensures that unpack() throws an Exception when
- extract_layers_in_qcow2() fails.
- """
- msg = 'Caught extract_layers_in_qcow2 failure'
- side_effect = Exception(msg)
- patch_method = 'virtBootstrap.utils.extract_layers_in_qcow2'
- self.unpack_raise_error_test('qcow2', patch_method, side_effect, msg)
-
def test_unpack_no_cache_clean_up(self):
"""
Ensures that unpack() removes the folder which stores tar archives
of image layers when no_cache is set to True.
"""
- output_formats = ['dir', 'qcow2']
- patch_methods = [
- 'virtBootstrap.utils.untar_layers',
- 'virtBootstrap.utils.extract_layers_in_qcow2'
- ]
- for fmt, patch_mthd in zip(output_formats, patch_methods):
- m_self = self._mock_docker_source()
- m_self.no_cache = True
- with mock.patch('shutil.rmtree') as m_shutil:
- self._unpack_test_fmt(fmt, patch_mthd, m_self=m_self)
- m_shutil.assert_called_once_with(m_self.images_dir)
+ m_self = self._mock_docker_source()
+ m_self.no_cache = True
+ with mock.patch('shutil.rmtree') as m_shutil:
+ self._unpack_test_fmt(
+ 'dir',
+ 'virtBootstrap.utils.untar_layers',
+ m_self=m_self
+ )
+ m_shutil.assert_called_once_with(m_self.images_dir)
def test_unpack_no_cache_clean_up_on_failure(self):
"""
diff --git a/tests/test_file_source.py b/tests/test_file_source.py
index 6e89aa2..a55ae4e 100644
--- a/tests/test_file_source.py
+++ b/tests/test_file_source.py
@@ -87,29 +87,6 @@ class TestFileSource(unittest.TestCase):
m_untar.assert_called_once_with(m_self.path, dest)
- def test_unpack_to_qcow2(self):
- """
- Ensures that unpack() calls create_qcow2() when the output
- format is set to 'qcow2'.
- """
- m_self = mock.Mock(spec=sources.FileSource)
- m_self.progress = mock.Mock()
- m_self.path = 'foo'
- m_self.output_format = 'qcow2'
- dest = 'bar'
- qcow2_file_path = 'foobar'
-
- with mock.patch.multiple('os.path',
- isfile=mock.DEFAULT,
- realpath=mock.DEFAULT) as mocked:
-
- mocked['isfile'].return_value = True
- mocked['realpath'].return_value = qcow2_file_path
- with mock.patch('virtBootstrap.utils.create_qcow2') as m_qcow2:
- sources.FileSource.unpack(m_self, dest)
-
- m_qcow2.assert_called_once_with(m_self.path, qcow2_file_path)
-
def _unpack_raise_error_test(self,
output_format,
side_effect=None,
@@ -157,15 +134,3 @@ class TestFileSource(unittest.TestCase):
side_effect=Exception(msg),
patch_method=patch_method,
msg=msg)
-
- def test_unpack_raise_error_if_extract_in_qcow2_fail(self):
- """
- Ensures that unpack() throws an Exception when create_qcow2()
- fails.
- """
- msg = 'Caught extract_layers_in_qcow2 failure'
- patch_method = 'virtBootstrap.utils.create_qcow2'
- self._unpack_raise_error_test(output_format='qcow2',
- side_effect=Exception(msg),
- patch_method=patch_method,
- msg=msg)
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 0b6ccc0..e45a2c9 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -226,105 +226,6 @@ class TestUtils(unittest.TestCase):
mocked['safe_untar'].assert_has_calls(expected_calls)
###################################
- # Tests for: create_qcow2()
- ###################################
- def _apply_test_to_create_qcow2(self, expected_calls, *args):
- """
- This method contains common test pattern used in the next two
- test cases.
- """
- with mock.patch.multiple(utils,
- execute=mock.DEFAULT,
- logger=mock.DEFAULT,
- get_mime_type=mock.DEFAULT) as mocked:
- mocked['get_mime_type'].return_value = 'application/x-gzip'
- utils.create_qcow2(*args)
- mocked['execute'].assert_has_calls(expected_calls)
-
- def test_utils_create_qcow2_base_layer(self):
- """
- Ensures that create_qcow2() creates base layer when
- backing_file = None.
- """
- tar_file = 'foo'
- layer_file = 'bar'
- size = '5G'
- backing_file = None
-
- expected_calls = [
- mock.call(["qemu-img", "create", "-f", "qcow2", layer_file, size]),
-
- mock.call(['virt-format',
- '--format=qcow2',
- '--partition=none',
- '--filesystem=ext3',
- '-a', layer_file]),
-
- mock.call(['guestfish',
- '-a', layer_file,
- '-m', '/dev/sda',
- 'tar-in', tar_file, '/', 'compress:gzip'])
- ]
-
- self._apply_test_to_create_qcow2(expected_calls, tar_file, layer_file,
- backing_file, size)
-
- def test_utils_create_qcow2_layer_with_backing_chain(self):
- """
- Ensures that create_qcow2() creates new layer with backing chains
- when backing_file is specified.
- """
- tar_file = 'foo'
- layer_file = 'bar'
- backing_file = 'base'
- size = '5G'
-
- expected_calls = [
- mock.call(['qemu-img', 'create',
- '-b', backing_file,
- '-f', 'qcow2',
- layer_file, size]),
-
- mock.call(['guestfish',
- '-a', layer_file,
- '-m', '/dev/sda',
- 'tar-in', tar_file, '/', 'compress:gzip'])
- ]
-
- self._apply_test_to_create_qcow2(expected_calls, tar_file, layer_file,
- backing_file, size)
-
- ###################################
- # Tests for: extract_layers_in_qcow2()
- ###################################
- def test_utils_if_all_layers_extracted_in_order_in_qcow2(self):
- """
- Ensures that extract_layers_in_qcow2() iterates through all
- layers in order.
- """
- layers = ['l1', 'l2', 'l3']
- layers_list = [['', '', layer] for layer in layers]
- dest_dir = '/foo'
-
- # Generate expected calls
- expected_calls = []
- qcow2_backing_file = None
- for index, layer in enumerate(layers):
- qcow2_layer_file = dest_dir + "/layer-%s.qcow2" % index
- expected_calls.append(
- mock.call(layer, qcow2_layer_file, qcow2_backing_file))
- qcow2_backing_file = qcow2_layer_file
-
- # Mocking out and execute
- with mock.patch.multiple(utils,
- create_qcow2=mock.DEFAULT,
- log_layer_extract=mock.DEFAULT) as mocked:
- utils.extract_layers_in_qcow2(layers_list, dest_dir, mock.Mock())
-
- # Check actual calls
- mocked['create_qcow2'].assert_has_calls(expected_calls)
-
- ###################################
# Tests for: get_image_dir()
###################################
def test_utils_getimage_dir(self):
--
2.13.4
More information about the virt-tools-list
mailing list