From 97ce4a6effd8daa80ed5eb0971f3aeec3f62dc5a Mon Sep 17 00:00:00 2001
From: Ruby Loo <rloo@yahoo-inc.com>
Date: Wed, 25 Nov 2015 15:59:07 +0000
Subject: [PATCH] Default for root_helper config

This sets the default for root_helper config to the value used
by ironic: 'sudo ironic-rootwrap /etc/ironic/rootwrap.conf'.

The logic was changed a bit so as not to pass root_helper or
run_as_root arguments to processutils.execute() unless they
had been specified or needed to be passed.

Change-Id: I8cf8c07102d0a8bd4fff14afc8c2f9db6beeb949
Fixes-Bug: #1519870
---
 ironic_lib/tests/test_utils.py | 20 +++++++++++---------
 ironic_lib/utils.py            | 12 +++++++-----
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/ironic_lib/tests/test_utils.py b/ironic_lib/tests/test_utils.py
index fe9e2aaf..b16a2f04 100644
--- a/ironic_lib/tests/test_utils.py
+++ b/ironic_lib/tests/test_utils.py
@@ -146,8 +146,7 @@ grep foo
                                                           execute_mock):
         utils.execute('foo', use_standard_locale=True)
         execute_mock.assert_called_once_with('foo',
-                                             env_variables={'LC_ALL': 'C'},
-                                             run_as_root=False)
+                                             env_variables={'LC_ALL': 'C'})
 
     @mock.patch.object(processutils, 'execute')
     def test_execute_use_standard_locale_with_env_variables(self,
@@ -156,16 +155,14 @@ grep foo
                       env_variables={'foo': 'bar'})
         execute_mock.assert_called_once_with('foo',
                                              env_variables={'LC_ALL': 'C',
-                                                            'foo': 'bar'},
-                                             run_as_root=False)
+                                                            'foo': 'bar'})
 
     @mock.patch.object(processutils, 'execute')
     def test_execute_not_use_standard_locale(self, execute_mock):
         utils.execute('foo', use_standard_locale=False,
                       env_variables={'foo': 'bar'})
         execute_mock.assert_called_once_with('foo',
-                                             env_variables={'foo': 'bar'},
-                                             run_as_root=False)
+                                             env_variables={'foo': 'bar'})
 
     def test_execute_without_root_helper(self):
         CONF.set_override('root_helper', None, group='ironic_lib')
@@ -180,11 +177,16 @@ grep foo
             execute_mock.assert_called_once_with('foo', run_as_root=False)
 
     def test_execute_with_root_helper(self):
-        CONF.set_override('root_helper', 'sudo', group='ironic_lib')
+        with mock.patch.object(processutils, 'execute') as execute_mock:
+            utils.execute('foo', run_as_root=False)
+            execute_mock.assert_called_once_with('foo', run_as_root=False)
+
+    def test_execute_with_root_helper_run_as_root(self):
         with mock.patch.object(processutils, 'execute') as execute_mock:
             utils.execute('foo', run_as_root=True)
-            execute_mock.assert_called_once_with('foo', run_as_root=True,
-                                                 root_helper='sudo')
+            execute_mock.assert_called_once_with(
+                'foo', run_as_root=True,
+                root_helper=CONF.ironic_lib.root_helper)
 
 
 class MkfsTestCase(test_base.BaseTestCase):
diff --git a/ironic_lib/utils.py b/ironic_lib/utils.py
index a09a8173..3b03e299 100644
--- a/ironic_lib/utils.py
+++ b/ironic_lib/utils.py
@@ -32,7 +32,7 @@ from ironic_lib import exception
 
 utils_opts = [
     cfg.StrOpt('root_helper',
-               default=None,
+               default='sudo ironic-rootwrap /etc/ironic/rootwrap.conf',
                help='Command that is prefixed to commands that are run as '
                     'root. If not specified, no commands are run as root.'),
 ]
@@ -62,10 +62,12 @@ def execute(*cmd, **kwargs):
         kwargs['env_variables'] = env
 
     # If root_helper config is not specified, no commands are run as root.
-    if not CONF.ironic_lib.root_helper:
-        kwargs['run_as_root'] = False
-    else:
-        kwargs['root_helper'] = CONF.ironic_lib.root_helper
+    run_as_root = kwargs.get('run_as_root', False)
+    if run_as_root:
+        if not CONF.ironic_lib.root_helper:
+            kwargs['run_as_root'] = False
+        else:
+            kwargs['root_helper'] = CONF.ironic_lib.root_helper
 
     result = processutils.execute(*cmd, **kwargs)
     LOG.debug('Execution completed, command line is "%s"',