File gcei-handle-failed-open.patch of Package google-compute-engine-init.3436

From b4833ce8f55827ba4f1da51547f437e0f024064f Mon Sep 17 00:00:00 2001
From: Robert Schweikert <rjschwei@suse.com>
Date: Wed, 19 Oct 2016 15:51:07 -0400
Subject: [PATCH] - Handle exception if we cannot write the sudoers file

---
 google_compute_engine/accounts/accounts_utils.py    | 14 ++++++++++----
 .../accounts/tests/accounts_utils_test.py           | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/google_compute_engine/accounts/accounts_utils.py b/google_compute_engine/accounts/accounts_utils.py
index 4096086..068128a 100644
--- a/google_compute_engine/accounts/accounts_utils.py
+++ b/google_compute_engine/accounts/accounts_utils.py
@@ -76,10 +76,16 @@ class AccountsUtils(object):
         self.logger.warning('Could not create the sudoers group. %s.', str(e))
 
     if not os.path.exists(self.google_sudoers_file):
-      with open(self.google_sudoers_file, 'w') as group:
-        message = '%{0} ALL=(ALL:ALL) NOPASSWD:ALL'.format(
-            self.google_sudoers_group)
-        group.write(message)
+      try:
+        with open(self.google_sudoers_file, 'w') as group:
+          message = '%{0} ALL=(ALL:ALL) NOPASSWD:ALL'.format(
+              self.google_sudoers_group)
+          group.write(message)
+      except IOError as e:
+        self.logger.error(
+            'Could not write sudoers file. %s. %s' % (
+                self.google_sudoers_file, str(e)))
+        return
 
     file_utils.SetPermissions(
         self.google_sudoers_file, mode=0o440, uid=0, gid=0)
diff --git a/google_compute_engine/accounts/tests/accounts_utils_test.py b/google_compute_engine/accounts/tests/accounts_utils_test.py
index c10d963..773eaa4 100644
--- a/google_compute_engine/accounts/tests/accounts_utils_test.py
+++ b/google_compute_engine/accounts/tests/accounts_utils_test.py
@@ -148,6 +148,27 @@ class AccountsUtilsTest(unittest.TestCase):
     ]
     self.assertEqual(mocks.mock_calls, expected_calls)
 
+  @mock.patch('google_compute_engine.accounts.accounts_utils.open')
+  @mock.patch('google_compute_engine.accounts.accounts_utils.os.path.exists')
+  def testCreateSudoersGroupWriteError(self, mock_exists, mock_open):
+    mocks = mock.Mock()
+    mocks.attach_mock(mock_exists, 'exists')
+    mocks.attach_mock(mock_open, 'open')
+    mocks.attach_mock(self.mock_utils._GetGroup, 'group')
+    mocks.attach_mock(self.mock_logger, 'logger')
+    self.mock_utils._GetGroup.return_value = True
+    mock_exists.return_value = False
+    mock_open.side_effect = IOError()
+    accounts_utils.AccountsUtils._CreateSudoersGroup(self.mock_utils)
+
+    expected_calls = [
+        mock.call.group(self.sudoers_group),
+        mock.call.exists(self.sudoers_file),
+        mock.call.open(self.sudoers_file, 'w'),
+        mock.call.logger.error('Could not write sudoers file. /sudoers/file. '),
+    ]
+    self.assertEqual(mocks.mock_calls, expected_calls)
+
   @mock.patch('google_compute_engine.accounts.accounts_utils.pwd')
   def testGetUser(self, mock_pwd):
     mock_pwd.getpwnam.return_value = 'Test'
-- 
2.6.6

openSUSE Build Service is sponsored by