[virt-tools-list] [virt-bootstrap] [PATCH 5/9] tests: Apply unit tests for --use-sandbox

Radostin Stoyanov rstoyanov1 at gmail.com
Fri Jul 28 09:21:43 UTC 2017


The following changes have been made and the tests are updated
accordingly:

- Renamed function safe_untar() to untar()
- Added use_sandbox argument to untar() and untar_layers()
- Added self.use_sandbox to FileSource and DockerSource
- untar() calls execute with virt-sandbox command if use_sandbox=True
- untar() calls execute with tar command if use_sandbox=False
- The method _unpack_test_fmt() used to gather common test pattern has
  been removed because now untar_layers() and extract_layers_in_qcow2()
  have different arguments.
---
 tests/test_docker_source.py | 128 +++++++++++++++++++++++++-------------------
 tests/test_file_source.py   |  14 +++--
 tests/test_utils.py         |  68 ++++++++++++++++-------
 3 files changed, 133 insertions(+), 77 deletions(-)

diff --git a/tests/test_docker_source.py b/tests/test_docker_source.py
index 8108e31..eec3c86 100644
--- a/tests/test_docker_source.py
+++ b/tests/test_docker_source.py
@@ -44,6 +44,7 @@ class TestDockerSource(unittest.TestCase):
         m_self = mock.Mock(spec=sources.DockerSource)
         m_self.progress = mock.Mock()
         m_self.no_cache = False
+        m_self.use_sandbox = False
         m_self.url = "docker://test"
         m_self.images_dir = "/images_path"
         m_self.insecure = True
@@ -67,6 +68,7 @@ class TestDockerSource(unittest.TestCase):
                   'fmt': 'dir',
                   'not_secure': False,
                   'no_cache': False,
+                  'use_sandbox': True,
                   'progress': mock.Mock(),
                   'username': 'username',
                   'password': 'password'}
@@ -83,6 +85,7 @@ class TestDockerSource(unittest.TestCase):
             src_instance.progress: kwargs['progress'].update_progress,
             src_instance.username: kwargs['username'],
             src_instance.password: kwargs['password'],
+            src_instance.use_sandbox: kwargs['use_sandbox'],
             src_instance.output_format: kwargs['fmt'],
             src_instance.no_cache: kwargs['no_cache'],
             src_instance.insecure: kwargs['not_secure'],
@@ -493,66 +496,69 @@ class TestDockerSource(unittest.TestCase):
     ###################################
     # Tests for: unpack()
     ###################################
-    def _unpack_test_fmt(self, output_format, patch_method=None,
-                         side_effect=None, m_self=None):
+    def test_unpack_dir_format(self):
         """
-        This method is gather common test pattern used in the following
-        two test cases.
+        Ensures that unpack() calls untar_layers() when the output format
+        is set to 'dir'.
         """
-        m_self = m_self if m_self else self._mock_docker_source()
-        m_self.output_format = output_format
         dest = 'foo'
+        m_self = self._mock_docker_source()
+        m_self.output_format = 'dir'
 
-        if patch_method:
-            with mock.patch(patch_method) as mocked:
-                if side_effect:
-                    mocked.side_effect = side_effect
-                sources.DockerSource.unpack(m_self, dest)
-
-            mocked.assert_called_once_with(m_self.layers, dest,
-                                           m_self.progress)
-        else:
+        patched_method = 'virtBootstrap.utils.untar_layers'
+        with mock.patch(patched_method) as m_untar_layers:
             sources.DockerSource.unpack(m_self, dest)
 
-        m_self.fetch_layers.assert_called_once()
-
-    def test_unpack_dir_format(self):
-        """
-        Ensures that unpack() calls untar_layers() when the output format
-        is set to 'dir'.
-        """
-        self._unpack_test_fmt('dir', 'virtBootstrap.utils.untar_layers')
+        m_untar_layers.assert_called_once_with(m_self.layers,
+                                               dest, m_self.progress,
+                                               m_self.use_sandbox)
 
     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')
+        dest = 'foo'
+        m_self = self._mock_docker_source()
+        m_self.output_format = 'qcow2'
 
-    def unpack_raise_error_test(self,
-                                output_format,
-                                patch_method,
-                                side_effect=None,
-                                msg=None):
-        """
-        This method is gather common test pattern used in the following
-        four test cases.
-        """
-        with self.assertRaises(Exception) as err:
-            self._unpack_test_fmt(output_format, patch_method,
-                                  side_effect)
-        if msg:
-            self.assertEqual(msg, str(err.exception))
+        patched_method = 'virtBootstrap.utils.extract_layers_in_qcow2'
+        with mock.patch(patched_method) as m_extract_layers_in_qcow2:
+            sources.DockerSource.unpack(m_self, dest)
+
+        m_extract_layers_in_qcow2.assert_called_once_with(m_self.layers,
+                                                          dest,
+                                                          m_self.progress)
 
     def test_unpack_raise_error_for_unknown_format(self):
         """
         Ensures that unpack() throws an Exception when called with
         invalid output format.
         """
-        msg = 'Unknown format:foo'
-        self.unpack_raise_error_test('foo', None, None, msg)
+        m_self = self._mock_docker_source()
+        m_self.output_format = 'foo'
+        msg = 'Unknown format:%s' % m_self.output_format
+
+        with self.assertRaises(Exception) as err:
+            sources.DockerSource.unpack(m_self, 'foo')
+
+        self.assertEqual(msg, str(err.exception))
+
+    def unpack_raise_error_test(self, output_format, patched_method,
+                                side_effect, msg):
+        """
+        This method is gather common test pattern used in the following
+        two test cases.
+        """
+        m_self = self._mock_docker_source()
+        m_self.output_format = output_format
+
+        with self.assertRaises(Exception) as err:
+            with mock.patch(patched_method) as mocked:
+                mocked.side_effect = side_effect
+                sources.DockerSource.unpack(m_self, 'foo')
+
+        self.assertEqual(msg, str(err.exception))
 
     def test_unpack_raise_error_if_untar_fail(self):
         """
@@ -574,22 +580,35 @@ class TestDockerSource(unittest.TestCase):
         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):
+    def test_unpack_dir_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('virtBootstrap.sources.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
+        m_self.output_format = 'dir'
+
+        with mock.patch('virtBootstrap.sources.shutil.rmtree') as m_shutil:
+            with mock.patch('virtBootstrap.utils.untar_layers'):
+                sources.DockerSource.unpack(m_self, 'foo')
+
+        m_shutil.assert_called_once_with(m_self.images_dir)
+
+    def test_unpack_qcow2_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.
+        """
+        m_self = self._mock_docker_source()
+        m_self.no_cache = True
+        m_self.output_format = 'qcow2'
+
+        with mock.patch('virtBootstrap.sources.shutil.rmtree') as m_shutil:
+            with mock.patch('virtBootstrap.utils.extract_layers_in_qcow2'):
+                sources.DockerSource.unpack(m_self, 'foo')
+
+        m_shutil.assert_called_once_with(m_self.images_dir)
 
     def test_unpack_no_cache_clean_up_on_failure(self):
         """
@@ -601,5 +620,6 @@ class TestDockerSource(unittest.TestCase):
         m_self.no_cache = True
         with self.assertRaises(Exception):
             with mock.patch('shutil.rmtree') as m_rmtree:
-                self._unpack_test_fmt('foo', None, m_self=m_self)
+                sources.DockerSource.unpack(m_self, 'foo')
+
         m_rmtree.assert_called_once_with(m_self.images_dir)
diff --git a/tests/test_file_source.py b/tests/test_file_source.py
index 6e89aa2..d78c4fa 100644
--- a/tests/test_file_source.py
+++ b/tests/test_file_source.py
@@ -41,6 +41,7 @@ class TestFileSource(unittest.TestCase):
         """
         kwargs = {'uri': mock.Mock(),
                   'fmt': 'dir',
+                  'use_sandbox': True,
                   'progress': mock.Mock()}
 
         src_instance = sources.FileSource(**kwargs)
@@ -48,6 +49,7 @@ class TestFileSource(unittest.TestCase):
         test_values = {
             src_instance.path: kwargs['uri'].path,
             src_instance.output_format: kwargs['fmt'],
+            src_instance.use_sandbox: kwargs['use_sandbox'],
             src_instance.progress: kwargs['progress'].update_progress
         }
         for value in test_values:
@@ -71,21 +73,22 @@ class TestFileSource(unittest.TestCase):
 
     def test_unpack_to_dir(self):
         """
-        Ensures that unpack() calls safe_untar() when the output format
+        Ensures that unpack() calls untar() when the output format
         is set to 'dir'.
         """
         m_self = mock.Mock(spec=sources.FileSource)
         m_self.progress = mock.Mock()
         m_self.path = 'foo'
         m_self.output_format = 'dir'
+        m_self.use_sandbox = False
         dest = 'bar'
 
         with mock.patch('os.path.isfile') as m_isfile:
             m_isfile.return_value = True
-            with mock.patch('virtBootstrap.utils.safe_untar') as m_untar:
+            with mock.patch('virtBootstrap.utils.untar') as m_untar:
                 sources.FileSource.unpack(m_self, dest)
 
-        m_untar.assert_called_once_with(m_self.path, dest)
+        m_untar.assert_called_once_with(m_self.path, dest, m_self.use_sandbox)
 
     def test_unpack_to_qcow2(self):
         """
@@ -123,6 +126,7 @@ class TestFileSource(unittest.TestCase):
         m_self.progress = mock.Mock()
         m_self.path = 'foo'
         m_self.output_format = output_format
+        m_self.use_sandbox = False
         dest = 'bar'
 
         with mock.patch.multiple('os.path',
@@ -148,11 +152,11 @@ class TestFileSource(unittest.TestCase):
 
     def test_unpack_raise_error_if_untar_fail(self):
         """
-        Ensures that unpack() throws an Exception when safe_untar()
+        Ensures that unpack() throws an Exception when untar()
         fails.
         """
         msg = 'Caught untar failure'
-        patch_method = 'virtBootstrap.utils.safe_untar'
+        patch_method = 'virtBootstrap.utils.untar'
         self._unpack_raise_error_test(output_format='dir',
                                       side_effect=Exception(msg),
                                       patch_method=patch_method,
diff --git a/tests/test_utils.py b/tests/test_utils.py
index aa9bdc2..dacfbd1 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -115,29 +115,59 @@ class TestUtils(unittest.TestCase):
                 utils.execute(['foo'])
 
     ###################################
-    # Tests for: safe_untar()
+    # Tests for: untar()
     ###################################
-    def test_utils_safe_untar_calls_execute(self):
+    def _apply_test_to_untar(self, src, dest, use_sandbox, expected_call):
         """
-        Ensures that safe_untar() calls execute with virt-sandbox
-        command to extract source files to destination folder.
+        This method contains common test pattern used in the next two
+        test cases.
         Test for users with EUID 0 and 1000.
         """
         with mock.patch('virtBootstrap.utils.os.geteuid') as m_geteuid:
             for uid in [0, 1000]:
+                # Set UID
                 m_geteuid.return_value = uid
                 reload(utils)
+
+                # If using virt-sandbox insert the LIBVIRT_CONN value
+                # in the expected call, after UID has been set.
+                if use_sandbox:
+                    expected_call[2] = utils.LIBVIRT_CONN
+
                 with mock.patch('virtBootstrap.utils.execute') as m_execute:
-                    src, dest = 'foo', 'bar'
-                    utils.safe_untar('foo', 'bar')
-                    cmd = ['virt-sandbox',
-                           '-c', utils.LIBVIRT_CONN,
-                           '-m', 'host-bind:/mnt=' + dest,
-                           '--',
-                           '/bin/tar', 'xf', src,
-                           '-C', '/mnt',
-                           '--exclude', 'dev/*']
-                    m_execute.assert_called_once_with(cmd)
+                    utils.untar(src, dest, use_sandbox)
+                    m_execute.assert_called_once_with(expected_call)
+
+    def test_utils_untar_calls_execute_virt_sandbox(self):
+        """
+        Ensures that untar() calls execute with virt-sandbox
+        command when 'use_sandbox' is set to True.
+        """
+        src = 'foo'
+        dest = 'bar'
+        use_sandbox = True
+
+        cmd = ['virt-sandbox',
+               '-c', 'utils.LIBVIRT_CONN',
+               '-m', 'host-bind:/mnt=' + dest,
+               '--',
+               '/bin/tar', 'xf', src,
+               '-C', '/mnt',
+               '--exclude', 'dev/*']
+        self._apply_test_to_untar(src, dest, use_sandbox, cmd)
+
+    def test_utils_untar_calls_execute_tar(self):
+        """
+        Ensures that untar() calls execute with tar command.
+        """
+        src = 'foo'
+        dest = 'bar'
+        use_sandbox = False
+
+        cmd = ['/bin/tar', 'xf', src,
+               '-C', dest,
+               '--exclude', 'dev/*']
+        self._apply_test_to_untar(src, dest, use_sandbox, cmd)
 
     ###################################
     # Tests for: bytes_to_size()
@@ -216,12 +246,14 @@ class TestUtils(unittest.TestCase):
         layers = ['l1', 'l2', 'l3']
         layers_list = [['', '', layer] for layer in layers]
         dest_dir = '/foo'
-        expected_calls = [mock.call(layer, dest_dir) for layer in layers]
+        use_sandbox = True
+        expected_calls = [mock.call(layer, dest_dir, use_sandbox)
+                          for layer in layers]
         with mock.patch.multiple(utils,
-                                 safe_untar=mock.DEFAULT,
+                                 untar=mock.DEFAULT,
                                  log_layer_extract=mock.DEFAULT) as mocked:
-            utils.untar_layers(layers_list, dest_dir, mock.Mock())
-        mocked['safe_untar'].assert_has_calls(expected_calls)
+            utils.untar_layers(layers_list, dest_dir, mock.Mock(), use_sandbox)
+        mocked['untar'].assert_has_calls(expected_calls)
 
     ###################################
     # Tests for: create_qcow2()
-- 
2.9.4




More information about the virt-tools-list mailing list