[virt-tools-list] [virt-bootstrap] [PATCH 9/9] tests: Match changes when setting root_password

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


- bootstrap() does not call set_root_password().
- self.root_password has been added to DockerSource and FileSource.
- FileSource.unpack() calls set_root_password_in_rootfs() and
  set_root_password_in_image() for format 'dir' and 'qcow2'
  respectively.
- DockerSOurce.unpack calls set_root_password() when self.root_password
  is not None.
---
 tests/test_docker_source.py  | 18 ++++++++++++++++
 tests/test_file_source.py    | 50 ++++++++++++++++++++++++++++++++++++++++++++
 tests/test_virt_bootstrap.py | 22 -------------------
 3 files changed, 68 insertions(+), 22 deletions(-)

diff --git a/tests/test_docker_source.py b/tests/test_docker_source.py
index eec3c86..8940f7f 100644
--- a/tests/test_docker_source.py
+++ b/tests/test_docker_source.py
@@ -45,6 +45,7 @@ class TestDockerSource(unittest.TestCase):
         m_self.progress = mock.Mock()
         m_self.no_cache = False
         m_self.use_sandbox = False
+        m_self.root_password = None
         m_self.url = "docker://test"
         m_self.images_dir = "/images_path"
         m_self.insecure = True
@@ -623,3 +624,20 @@ class TestDockerSource(unittest.TestCase):
                 sources.DockerSource.unpack(m_self, 'foo')
 
         m_rmtree.assert_called_once_with(m_self.images_dir)
+
+    def test_unpack_calls_set_root_password(self):
+        """
+        Ensures that unpack() calls set_root_password() when
+        self.root_password is not None.
+        """
+        dest = 'foo'
+        m_self = self._mock_docker_source()
+        m_self.output_format = 'dir'
+        m_self.root_password = 'secret'
+
+        with mock.patch('virtBootstrap.utils.untar_layers'):
+            with mock.patch('virtBootstrap.utils.set_root_password') as mocked:
+                sources.DockerSource.unpack(m_self, dest)
+
+        mocked.assert_called_once_with(m_self.output_format,
+                                       dest, m_self.root_password)
diff --git a/tests/test_file_source.py b/tests/test_file_source.py
index d78c4fa..7b22689 100644
--- a/tests/test_file_source.py
+++ b/tests/test_file_source.py
@@ -42,6 +42,7 @@ class TestFileSource(unittest.TestCase):
         kwargs = {'uri': mock.Mock(),
                   'fmt': 'dir',
                   'use_sandbox': True,
+                  'root_password': 'secret',
                   'progress': mock.Mock()}
 
         src_instance = sources.FileSource(**kwargs)
@@ -50,6 +51,7 @@ class TestFileSource(unittest.TestCase):
             src_instance.path: kwargs['uri'].path,
             src_instance.output_format: kwargs['fmt'],
             src_instance.use_sandbox: kwargs['use_sandbox'],
+            src_instance.root_password: kwargs['root_password'],
             src_instance.progress: kwargs['progress'].update_progress
         }
         for value in test_values:
@@ -81,6 +83,7 @@ class TestFileSource(unittest.TestCase):
         m_self.path = 'foo'
         m_self.output_format = 'dir'
         m_self.use_sandbox = False
+        m_self.root_password = None
         dest = 'bar'
 
         with mock.patch('os.path.isfile') as m_isfile:
@@ -90,6 +93,52 @@ class TestFileSource(unittest.TestCase):
 
         m_untar.assert_called_once_with(m_self.path, dest, m_self.use_sandbox)
 
+    def test_unpack_calls_set_root_password_in_rootfs(self):
+        """
+        Ensures that unpack() calls set_root_password_in_rootfs()
+        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
+        m_self.root_password = 'secret'
+        dest = 'bar'
+
+        patched_method = 'virtBootstrap.utils.set_root_password_in_rootfs'
+        with mock.patch('os.path.isfile') as m_isfile:
+            m_isfile.return_value = True
+            with mock.patch('virtBootstrap.utils.untar'):
+                with mock.patch(patched_method) as mocked:
+                    sources.FileSource.unpack(m_self, dest)
+
+        mocked.assert_called_once_with(dest, m_self.root_password)
+
+    def test_unpack_calls_set_root_password_in_image(self):
+        """
+        Ensures that unpack() calls set_root_password_in_image()
+        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 = 'qcow2'
+        m_self.use_sandbox = False
+        m_self.root_password = 'secret'
+        dest = 'bar'
+
+        patched_method = 'virtBootstrap.utils.set_root_password_in_image'
+        with mock.patch.multiple('os.path', realpath=mock.DEFAULT,
+                                 isfile=mock.DEFAULT) as m_path:
+            m_path['isfile'].return_value = True
+            m_path['realpath'].return_value = dest
+            with mock.patch('virtBootstrap.utils.create_qcow2'):
+                with mock.patch(patched_method) as mocked:
+                    sources.FileSource.unpack(m_self, dest)
+
+        mocked.assert_called_once_with(dest, m_self.root_password)
+
     def test_unpack_to_qcow2(self):
         """
         Ensures that unpack() calls create_qcow2() when the output
@@ -99,6 +148,7 @@ class TestFileSource(unittest.TestCase):
         m_self.progress = mock.Mock()
         m_self.path = 'foo'
         m_self.output_format = 'qcow2'
+        m_self.root_password = None
         dest = 'bar'
         qcow2_file_path = 'foobar'
 
diff --git a/tests/test_virt_bootstrap.py b/tests/test_virt_bootstrap.py
index ff744f7..6f0ea33 100644
--- a/tests/test_virt_bootstrap.py
+++ b/tests/test_virt_bootstrap.py
@@ -390,28 +390,6 @@ class TestVirtBootstrap(unittest.TestCase):
             for kwarg in params:
                 self.assertEqual(called_with_kwargs[kwarg], params[kwarg])
 
-    def test_if_bootstrap_calls_set_root_password(self):
-        """
-        Ensures that bootstrap() calls set_root_password() when the argument
-        root_password is specified.
-        """
-        src, fmt, dest, root_password = 'foo', 'fmt', 'bar', 'root_password'
-        with mock.patch.multiple(virt_bootstrap,
-                                 get_source=mock.DEFAULT,
-                                 os=mock.DEFAULT,
-                                 utils=mock.DEFAULT,
-                                 sys=mock.DEFAULT) as mocked:
-            mocked['os'].path.exists.return_value = True
-            mocked['os'].path.isdir.return_value = True
-            mocked['os'].access.return_value = True
-
-            virt_bootstrap.bootstrap(src, dest,
-                                     fmt=fmt,
-                                     root_password=root_password)
-
-            mocked['utils'].set_root_password.assert_called_once_with(
-                fmt, dest, root_password)
-
     def test_if_bootstrap_calls_set_mapping_uid_gid(self):
         """
         Ensures that bootstrap() calls mapping_uid_gid() when the argument
-- 
2.9.4




More information about the virt-tools-list mailing list