Update acloud_kernel and goldfish_compute_client + others to be pylint compliant.
- According to 4 spaces, the code modified base on the rule.
- Added pylint disable comments to avoid pylint error check.
Bug: None
Test: run pylint tools base on the file pylintrc CL 688459
pylint public/acloud_kernel/acloud_kernel.py
pylint public/acloud_kernel/kernel_swapper_test.py
pylint public/acloud_kernel/kernel_swapper.py
pylint internal/lib/goldfish_compute_client.py
pylint internal/lib/goldfish_compute_client_test.py
pylint internal/lib/driver_test_lib.py
pylint internal/lib/utils.py
pylint internal/lib/utils_test.py
Change-Id: Iab1d9aaed5d52f598feede9ab399af87b0fcdc81
diff --git a/internal/lib/utils_test.py b/internal/lib/utils_test.py
index a450933..a5e845c 100644
--- a/internal/lib/utils_test.py
+++ b/internal/lib/utils_test.py
@@ -13,7 +13,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-
"""Tests for acloud.internal.lib.utils."""
import errno
@@ -24,163 +23,190 @@
import tempfile
import time
+import unittest
import mock
-import unittest
from acloud.internal.lib import driver_test_lib
from acloud.internal.lib import utils
class UtilsTest(driver_test_lib.BaseDriverTest):
+ """Test Utils."""
- def testTempDir_Success(self):
- """Test create a temp dir."""
- self.Patch(os, "chmod")
- self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
- self.Patch(shutil, "rmtree")
- with utils.TempDir():
- pass
- # Verify.
- tempfile.mkdtemp.assert_called_once()
- shutil.rmtree.assert_called_with("/tmp/tempdir")
+ def TestTempDirSuccess(self):
+ """Test create a temp dir."""
+ self.Patch(os, "chmod")
+ self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
+ self.Patch(shutil, "rmtree")
+ with utils.TempDir():
+ pass
+ # Verify.
+ tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member
+ shutil.rmtree.assert_called_with("/tmp/tempdir") # pylint: disable=no-member
- def testTempDir_ExceptionRaised(self):
- """Test create a temp dir and exception is raised within with-clause."""
- self.Patch(os, "chmod")
- self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
- self.Patch(shutil, "rmtree")
+ def TestTempDirExceptionRaised(self):
+ """Test create a temp dir and exception is raised within with-clause."""
+ self.Patch(os, "chmod")
+ self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
+ self.Patch(shutil, "rmtree")
- class ExpectedException(Exception):
- pass
+ class ExpectedException(Exception):
+ """Expected exception."""
+ pass
- def _Call():
- with utils.TempDir():
- raise ExpectedException("Expected exception.")
- # Verify. ExpectedException should be raised.
- self.assertRaises(ExpectedException, _Call)
- tempfile.mkdtemp.assert_called_once()
- shutil.rmtree.assert_called_with("/tmp/tempdir")
+ def _Call():
+ with utils.TempDir():
+ raise ExpectedException("Expected exception.")
- def testTempDir_WhenDeleteTempDirNoLongerExist(self):
- """Test create a temp dir and dir no longer exists during deletion."""
- self.Patch(os, "chmod")
- self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
- expected_error = EnvironmentError()
- expected_error.errno = errno.ENOENT
- self.Patch(shutil, "rmtree", side_effect=expected_error)
- def _Call():
- with utils.TempDir():
- pass
- # Verify no exception should be raised when rmtree raises
- # EnvironmentError with errno.ENOENT, i.e.
- # directory no longer exists.
- _Call()
- tempfile.mkdtemp.assert_called_once()
- shutil.rmtree.assert_called_with("/tmp/tempdir")
+ # Verify. ExpectedException should be raised.
+ self.assertRaises(ExpectedException, _Call)
+ tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member
+ shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member
- def testTempDir_WhenDeleteEncounterError(self):
- """Test create a temp dir and encoutered error during deletion."""
- self.Patch(os, "chmod")
- self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
- expected_error = OSError("Expected OS Error")
- self.Patch(shutil, "rmtree", side_effect=expected_error)
- def _Call():
- with utils.TempDir():
- pass
+ def testTempDirWhenDeleteTempDirNoLongerExist(self): # pylint: disable=invalid-name
+ """Test create a temp dir and dir no longer exists during deletion."""
+ self.Patch(os, "chmod")
+ self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
+ expected_error = EnvironmentError()
+ expected_error.errno = errno.ENOENT
+ self.Patch(shutil, "rmtree", side_effect=expected_error)
- # Verify OSError should be raised.
- self.assertRaises(OSError, _Call)
- tempfile.mkdtemp.assert_called_once()
- shutil.rmtree.assert_called_with("/tmp/tempdir")
+ def _Call():
+ with utils.TempDir():
+ pass
- def testTempDir_OrininalErrorRaised(self):
- """Test original error is raised even if tmp dir deletion failed."""
- self.Patch(os, "chmod")
- self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
- expected_error = OSError("Expected OS Error")
- self.Patch(shutil, "rmtree", side_effect=expected_error)
+ # Verify no exception should be raised when rmtree raises
+ # EnvironmentError with errno.ENOENT, i.e.
+ # directory no longer exists.
+ _Call()
+ tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member
+ shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member
- class ExpectedException(Exception):
- pass
+ def testTempDirWhenDeleteEncounterError(self):
+ """Test create a temp dir and encoutered error during deletion."""
+ self.Patch(os, "chmod")
+ self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
+ expected_error = OSError("Expected OS Error")
+ self.Patch(shutil, "rmtree", side_effect=expected_error)
- def _Call():
- with utils.TempDir():
- raise ExpectedException("Expected Exception")
+ def _Call():
+ with utils.TempDir():
+ pass
- # Verify.
- # ExpectedException should be raised, and OSError
- # should not be raised.
- self.assertRaises(ExpectedException, _Call)
- tempfile.mkdtemp.assert_called_once()
- shutil.rmtree.assert_called_with("/tmp/tempdir")
+ # Verify OSError should be raised.
+ self.assertRaises(OSError, _Call)
+ tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member
+ shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member
- def testCreateSshKeyPair_KeyAlreadyExists(self):
- """Test when the key pair already exists."""
- public_key = "/fake/public_key"
- private_key = "/fake/private_key"
- self.Patch(os.path, "exists", side_effect=lambda path: path == public_key)
- self.Patch(subprocess, "check_call")
- utils.CreateSshKeyPairIfNotExist(private_key, public_key)
- self.assertEqual(subprocess.check_call.call_count, 0)
+ def testTempDirOrininalErrorRaised(self):
+ """Test original error is raised even if tmp dir deletion failed."""
+ self.Patch(os, "chmod")
+ self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir")
+ expected_error = OSError("Expected OS Error")
+ self.Patch(shutil, "rmtree", side_effect=expected_error)
- def testCreateSshKeyPair_KeyAreCreated(self):
- """Test when the key pair created."""
- public_key = "/fake/public_key"
- private_key = "/fake/private_key"
- self.Patch(os.path, "exists", return_value=False)
- self.Patch(subprocess, "check_call")
- self.Patch(os, "rename")
- utils.CreateSshKeyPairIfNotExist(private_key, public_key)
- self.assertEqual(subprocess.check_call.call_count, 1)
- subprocess.check_call.assert_called_with(
- utils.SSH_KEYGEN_CMD + ["-C", getpass.getuser(), "-f", private_key],
- stdout=mock.ANY, stderr=mock.ANY)
+ class ExpectedException(Exception):
+ """Expected exception."""
+ pass
- def testRetryOnException(self):
- def _IsValueError(exc):
- return isinstance(exc, ValueError)
- num_retry = 5
+ def _Call():
+ with utils.TempDir():
+ raise ExpectedException("Expected Exception")
- @utils.RetryOnException(_IsValueError, num_retry)
- def _RaiseAndRetry(sentinel):
- sentinel.alert()
- raise ValueError("Fake error.")
+ # Verify.
+ # ExpectedException should be raised, and OSError
+ # should not be raised.
+ self.assertRaises(ExpectedException, _Call)
+ tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member
+ shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member
- sentinel = mock.MagicMock()
- self.assertRaises(ValueError, _RaiseAndRetry, sentinel)
- self.assertEqual(1 + num_retry, sentinel.alert.call_count)
+ def testCreateSshKeyPairKeyAlreadyExists(self): #pylint: disable=invalid-name
+ """Test when the key pair already exists."""
+ public_key = "/fake/public_key"
+ private_key = "/fake/private_key"
+ self.Patch(
+ os.path, "exists", side_effect=lambda path: path == public_key)
+ self.Patch(subprocess, "check_call")
+ utils.CreateSshKeyPairIfNotExist(private_key, public_key)
+ self.assertEqual(subprocess.check_call.call_count, 0) #pylint: disable=no-member
- def testRetryExceptionType(self):
- """Test RetryExceptionType function."""
- def _RaiseAndRetry(sentinel):
- sentinel.alert()
- raise ValueError("Fake error.")
+ def testCreateSshKeyPairKeyAreCreated(self):
+ """Test when the key pair created."""
+ public_key = "/fake/public_key"
+ private_key = "/fake/private_key"
+ self.Patch(os.path, "exists", return_value=False)
+ self.Patch(subprocess, "check_call")
+ self.Patch(os, "rename")
+ utils.CreateSshKeyPairIfNotExist(private_key, public_key)
+ self.assertEqual(subprocess.check_call.call_count, 1) #pylint: disable=no-member
+ subprocess.check_call.assert_called_with( #pylint: disable=no-member
+ utils.SSH_KEYGEN_CMD +
+ ["-C", getpass.getuser(), "-f", private_key],
+ stdout=mock.ANY,
+ stderr=mock.ANY)
- num_retry = 5
- sentinel = mock.MagicMock()
- self.assertRaises(ValueError, utils.RetryExceptionType,
- (KeyError, ValueError), num_retry, _RaiseAndRetry,
- sentinel=sentinel)
- self.assertEqual(1 + num_retry, sentinel.alert.call_count)
+ def TestRetryOnException(self):
+ """Test Retry."""
- def testRetry(self):
- """Test Retry."""
- self.Patch(time, "sleep")
- def _RaiseAndRetry(sentinel):
- sentinel.alert()
- raise ValueError("Fake error.")
+ def _IsValueError(exc):
+ return isinstance(exc, ValueError)
- num_retry = 5
- sentinel = mock.MagicMock()
- self.assertRaises(ValueError, utils.RetryExceptionType,
- (ValueError, KeyError), num_retry, _RaiseAndRetry,
- sleep_multiplier=1,
- retry_backoff_factor=2,
- sentinel=sentinel)
+ num_retry = 5
- self.assertEqual(1 + num_retry, sentinel.alert.call_count)
- time.sleep.assert_has_calls(
- [mock.call(1), mock.call(2), mock.call(4), mock.call(8), mock.call(16)])
+ @utils.RetryOnException(_IsValueError, num_retry)
+ def _RaiseAndRetry(sentinel):
+ sentinel.alert()
+ raise ValueError("Fake error.")
+
+ sentinel = mock.MagicMock()
+ self.assertRaises(ValueError, _RaiseAndRetry, sentinel)
+ self.assertEqual(1 + num_retry, sentinel.alert.call_count)
+
+ def testRetryExceptionType(self):
+ """Test RetryExceptionType function."""
+
+ def _RaiseAndRetry(sentinel):
+ sentinel.alert()
+ raise ValueError("Fake error.")
+
+ num_retry = 5
+ sentinel = mock.MagicMock()
+ self.assertRaises(
+ ValueError,
+ utils.RetryExceptionType, (KeyError, ValueError),
+ num_retry,
+ _RaiseAndRetry,
+ sentinel=sentinel)
+ self.assertEqual(1 + num_retry, sentinel.alert.call_count)
+
+ def testRetry(self):
+ """Test Retry."""
+ self.Patch(time, "sleep")
+
+ def _RaiseAndRetry(sentinel):
+ sentinel.alert()
+ raise ValueError("Fake error.")
+
+ num_retry = 5
+ sentinel = mock.MagicMock()
+ self.assertRaises(
+ ValueError,
+ utils.RetryExceptionType, (ValueError, KeyError),
+ num_retry,
+ _RaiseAndRetry,
+ sleep_multiplier=1,
+ retry_backoff_factor=2,
+ sentinel=sentinel)
+
+ self.assertEqual(1 + num_retry, sentinel.alert.call_count)
+ time.sleep.assert_has_calls( #pylint: disable=no-member
+ [
+ mock.call(1),
+ mock.call(2),
+ mock.call(4),
+ mock.call(8),
+ mock.call(16)
+ ])
if __name__ == "__main__":