[virt-tools-list] [virt-bootstrap] [PATCH v4 23/26] tests: Update tests for untar()

Radostin Stoyanov rstoyanov1 at gmail.com
Thu Aug 3 13:13:21 UTC 2017


Update the tests of safe_untar() to match the behaviour of untar().

Rename 'safe_untar' to 'untar' in the test suite and add the `faster`
class variable when mocking out DockerSource and FileSource.
---
 tests/test_docker_source.py |  3 +-
 tests/test_file_source.py   | 12 ++++----
 tests/test_utils.py         | 68 +++++++++++++++++++++++++++++++++------------
 3 files changed, 59 insertions(+), 24 deletions(-)

diff --git a/tests/test_docker_source.py b/tests/test_docker_source.py
index 3865be6..d9fe32f 100644
--- a/tests/test_docker_source.py
+++ b/tests/test_docker_source.py
@@ -47,6 +47,7 @@ class TestDockerSource(unittest.TestCase):
         m_self.url = "docker://test"
         m_self.images_dir = "/images_path"
         m_self.insecure = True
+        m_self.faster = False
         m_self.username = 'user'
         m_self.password = 'password'
         m_self.layers = [
@@ -512,7 +513,7 @@ class TestDockerSource(unittest.TestCase):
                 sources.DockerSource.unpack(m_self, dest)
 
             mocked.assert_called_once_with(m_self.layers, dest,
-                                           m_self.progress)
+                                           m_self.progress, m_self.faster)
         else:
             sources.DockerSource.unpack(m_self, dest)
 
diff --git a/tests/test_file_source.py b/tests/test_file_source.py
index a55ae4e..b850a7c 100644
--- a/tests/test_file_source.py
+++ b/tests/test_file_source.py
@@ -71,21 +71,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.faster = False
         m_self.output_format = 'dir'
         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.faster)
 
     def _unpack_raise_error_test(self,
                                  output_format,
@@ -100,6 +101,7 @@ class TestFileSource(unittest.TestCase):
         m_self.progress = mock.Mock()
         m_self.path = 'foo'
         m_self.output_format = output_format
+        m_self.faster = False
         dest = 'bar'
 
         with mock.patch.multiple('os.path',
@@ -125,11 +127,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 56f3460..07bcd6e 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -115,29 +115,60 @@ 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, faster, expected_call):
         """
-        Ensures that safe_untar() calls execute with virt-sandbox
-        command to extract source files to destination folder.
-        Test for users with EUID 0 and 1000.
+        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 not faster:
+                    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, faster)
+                    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 'faster' is set to False.
+        """
+        src = 'foo'
+        dest = 'bar'
+        faster = False
+
+        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, faster, cmd)
+
+    def test_utils_untar_calls_execute_tar(self):
+        """
+        Ensures that untar() calls execute with tar command when
+        faster is set to True.
+        """
+        src = 'foo'
+        dest = 'bar'
+        faster = True
+
+        cmd = ['/bin/tar',
+               'xf', src,
+               '-C', dest,
+               '--exclude', 'dev/*']
+        self._apply_test_to_untar(src, dest, faster, cmd)
 
     ###################################
     # Tests for: bytes_to_size()
@@ -218,12 +249,13 @@ 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]
+        expected_calls = [mock.call(layer, dest_dir, False)
+                          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)
+        mocked['untar'].assert_has_calls(expected_calls)
 
     ###################################
     # Tests for: get_image_dir()
-- 
2.13.3




More information about the virt-tools-list mailing list