[virt-tools-list] [virt-bootstrap] [PATCH v4 21/26] Use Build_QCOW2_Image to set root password

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


Update the unit tests to match these changes.

The function set_root_password_in_image() is now implemented in the
class Build_QCOW2_Image and the function set_root_password() is not
required anymore.
---
 src/virtBootstrap/sources/docker_source.py |  4 +-
 src/virtBootstrap/sources/file_source.py   |  4 +-
 src/virtBootstrap/utils.py                 | 24 ------------
 src/virtBootstrap/virt_bootstrap.py        | 14 ++++---
 tests/test_utils.py                        | 62 ------------------------------
 tests/test_virt_bootstrap.py               |  8 ++--
 6 files changed, 18 insertions(+), 98 deletions(-)

diff --git a/src/virtBootstrap/sources/docker_source.py b/src/virtBootstrap/sources/docker_source.py
index 45e6c1d..207d166 100644
--- a/src/virtBootstrap/sources/docker_source.py
+++ b/src/virtBootstrap/sources/docker_source.py
@@ -65,6 +65,7 @@ class DockerSource(object):
         self.password = kwargs.get('password', None)
         self.uid_map = kwargs.get('uid_map', None)
         self.gid_map = kwargs.get('gid_map', None)
+        self.root_password = kwargs.get('root_password', None)
         self.output_format = kwargs.get('fmt', utils.DEFAULT_OUTPUT_FORMAT)
         self.insecure = kwargs.get('not_secure', False)
         self.no_cache = kwargs.get('no_cache', False)
@@ -279,7 +280,8 @@ class DockerSource(object):
                     dest=dest,
                     progress=self.progress,
                     uid_map=self.uid_map,
-                    gid_map=self.gid_map
+                    gid_map=self.gid_map,
+                    root_password=self.root_password
                 )
             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 70ce8b8..e10cee3 100644
--- a/src/virtBootstrap/sources/file_source.py
+++ b/src/virtBootstrap/sources/file_source.py
@@ -49,6 +49,7 @@ class FileSource(object):
         self.output_format = kwargs.get('fmt', utils.DEFAULT_OUTPUT_FORMAT)
         self.uid_map = kwargs.get('uid_map', None)
         self.gid_map = kwargs.get('gid_map', None)
+        self.root_password = kwargs.get('root_password', None)
         self.progress = kwargs['progress'].update_progress
 
     def unpack(self, dest):
@@ -74,7 +75,8 @@ class FileSource(object):
                 dest=dest,
                 progress=self.progress,
                 uid_map=self.uid_map,
-                gid_map=self.gid_map
+                gid_map=self.gid_map,
+                root_password=self.root_password
             )
         else:
             raise Exception("Unknown format:" + self.output_format)
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index 8f56d7a..1731b2a 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -32,7 +32,6 @@ import sys
 import tarfile
 import tempfile
 import logging
-import re
 
 import guestfs
 import passlib.hosts
@@ -512,29 +511,6 @@ def set_root_password_in_rootfs(rootfs, password):
         os.chmod(shadow_file, shadow_file_permissions)
 
 
-def set_root_password_in_image(image, password):
-    """
-    Set password on the root user within image
-    """
-    password_hash = passlib.hosts.linux_context.hash(password)
-    execute(['virt-edit',
-             '-a', image, '/etc/shadow',
-             '-e', 's,^root:.*?:,root:%s:,' % re.escape(password_hash)])
-
-
-def set_root_password(fmt, dest, root_password):
-    """
-    Set root password
-    """
-    if fmt == "dir":
-        set_root_password_in_rootfs(dest, root_password)
-    elif fmt == "qcow2":
-        layers = [layer for layer in os.listdir(dest)
-                  if layer.startswith('layer-')]
-        set_root_password_in_image(os.path.join(dest, max(layers)),
-                                   root_password)
-
-
 def write_progress(prog):
     """
     Write progress output to console
diff --git a/src/virtBootstrap/virt_bootstrap.py b/src/virtBootstrap/virt_bootstrap.py
index 99aca24..cbd9f0c 100755
--- a/src/virtBootstrap/virt_bootstrap.py
+++ b/src/virtBootstrap/virt_bootstrap.py
@@ -128,15 +128,17 @@ def bootstrap(uri, dest,
            gid_map=gid_map,
            not_secure=not_secure,
            no_cache=no_cache,
+           root_password=root_password,
            progress=prog).unpack(dest)
 
-    if root_password is not None:
-        logger.info("Setting password of the root account")
-        utils.set_root_password(fmt, dest, root_password)
+    if fmt == "dir":
+        if root_password is not None:
+            logger.info("Setting password of the root account")
+            utils.set_root_password_in_rootfs(dest, root_password)
 
-    if fmt == "dir" and uid_map or gid_map:
-        logger.info("Mapping UID/GID")
-        utils.mapping_uid_gid(dest, uid_map, gid_map)
+        if uid_map or gid_map:
+            logger.info("Mapping UID/GID")
+            utils.mapping_uid_gid(dest, uid_map, gid_map)
 
 
 def set_logging_conf(loglevel=None):
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 7ce2ba4..56f3460 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -496,68 +496,6 @@ class TestUtils(unittest.TestCase):
                                                % hashed_password)
 
     ###################################
-    # Tests for: set_root_password_in_image()
-    ###################################
-    @mock.patch('virtBootstrap.utils.execute')
-    def test_utils_set_root_password_in_image(self, m_execute):
-        """
-        Ensures that set_root_password_in_image() calls virt-edit
-        with correct arguments.
-        """
-        image, password = 'foo', 'password'
-        password_hash = ('$6$rounds=656000$PaQ/H4c/k8Ix9YOM$'
-                         'cyD47r9PtAE2LhnkpdbVzsiQbM0/h2S/1Bv'
-                         'u/sXqUtCg.3Ijp7TQy/8tEVstxMy5k5v4mh'
-                         'CGFqnVv7S6wd.Ah/')
-
-        expected_call = [
-            'virt-edit',
-            '-a', image, '/etc/shadow',
-            '-e', 's,^root:.*?:,root:%s:,' % utils.re.escape(password_hash)]
-
-        hash_function = 'virtBootstrap.utils.passlib.hosts.linux_context.hash'
-        with mock.patch(hash_function) as m_hash:
-            m_hash.return_value = password_hash
-            utils.set_root_password_in_image(image, password)
-
-        m_execute.assert_called_once_with(expected_call)
-
-    ###################################
-    # Tests for: set_root_password()
-    ###################################
-    @mock.patch('virtBootstrap.utils.set_root_password_in_rootfs')
-    def test_utils_set_root_password_dir(self, m_set_root_password_in_rootfs):
-        """
-        Ensures that set_root_password() calls set_root_password_in_rootfs()
-        when the format is set to "dir".
-        """
-        fmt, dest, root_password = 'dir', 'dest', 'root_password'
-        utils.set_root_password(fmt, dest, root_password)
-
-        m_set_root_password_in_rootfs.assert_called_once_with(
-            dest, root_password
-        )
-
-    @mock.patch('virtBootstrap.utils.set_root_password_in_image')
-    def test_utils_set_root_password_qcow2(self, m_set_root_password_in_image):
-        """
-        Ensures that set_root_password() calls set_root_password_in_image()
-        when the format is set to "qcow2" with the path to the last
-        extracted layer.
-        """
-        fmt, dest, root_password = 'qcow2', 'dest', 'root_password'
-        layers = ['layer-0.qcow2', 'layer-1.qcow2']
-
-        with mock.patch('os.listdir') as m_listdir:
-            m_listdir.return_value = layers
-            utils.set_root_password(fmt, dest, root_password)
-
-        m_set_root_password_in_image.assert_called_once_with(
-            utils.os.path.join(dest, max(layers)),
-            root_password
-        )
-
-    ###################################
     # Tests for: write_progress()
     ###################################
     def test_utils_write_progress_fill_terminal_width(self):
diff --git a/tests/test_virt_bootstrap.py b/tests/test_virt_bootstrap.py
index c0def7e..6105d0a 100644
--- a/tests/test_virt_bootstrap.py
+++ b/tests/test_virt_bootstrap.py
@@ -242,9 +242,9 @@ class TestVirtBootstrap(unittest.TestCase):
     def test_if_bootstrap_calls_set_root_password(self):
         """
         Ensures that bootstrap() calls set_root_password() when the argument
-        root_password is specified.
+        root_password is specified and fmt='dir'.
         """
-        src, fmt, dest, root_password = 'foo', 'fmt', 'bar', 'root_password'
+        src, fmt, dest, root_password = 'foo', 'dir', 'bar', 'root_password'
         with mock.patch.multiple(virt_bootstrap,
                                  get_source=mock.DEFAULT,
                                  os=mock.DEFAULT,
@@ -258,8 +258,8 @@ class TestVirtBootstrap(unittest.TestCase):
                                      fmt=fmt,
                                      root_password=root_password)
 
-            mocked['utils'].set_root_password.assert_called_once_with(
-                fmt, dest, root_password)
+            (mocked['utils'].set_root_password_in_rootfs
+             .assert_called_once_with(dest, root_password))
 
     def test_if_bootstrap_calls_set_mapping_uid_gid(self):
         """
-- 
2.13.3




More information about the virt-tools-list mailing list