Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 - The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 16 | """Tests for acloud.internal.lib.utils.""" |
| 17 | |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 18 | import errno |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 19 | import getpass |
| 20 | import os |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 21 | import shutil |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 22 | import subprocess |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 23 | import tempfile |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 24 | import time |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 25 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 26 | import unittest |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 27 | import mock |
| 28 | |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 29 | from acloud.internal.lib import driver_test_lib |
| 30 | from acloud.internal.lib import utils |
| 31 | |
| 32 | |
| 33 | class UtilsTest(driver_test_lib.BaseDriverTest): |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 34 | """Test Utils.""" |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 35 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 36 | def TestTempDirSuccess(self): |
| 37 | """Test create a temp dir.""" |
| 38 | self.Patch(os, "chmod") |
| 39 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 40 | self.Patch(shutil, "rmtree") |
| 41 | with utils.TempDir(): |
| 42 | pass |
| 43 | # Verify. |
| 44 | tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member |
| 45 | shutil.rmtree.assert_called_with("/tmp/tempdir") # pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 46 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 47 | def TestTempDirExceptionRaised(self): |
| 48 | """Test create a temp dir and exception is raised within with-clause.""" |
| 49 | self.Patch(os, "chmod") |
| 50 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 51 | self.Patch(shutil, "rmtree") |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 52 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 53 | class ExpectedException(Exception): |
| 54 | """Expected exception.""" |
| 55 | pass |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 56 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 57 | def _Call(): |
| 58 | with utils.TempDir(): |
| 59 | raise ExpectedException("Expected exception.") |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 60 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 61 | # Verify. ExpectedException should be raised. |
| 62 | self.assertRaises(ExpectedException, _Call) |
| 63 | tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member |
| 64 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 65 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 66 | def testTempDirWhenDeleteTempDirNoLongerExist(self): # pylint: disable=invalid-name |
| 67 | """Test create a temp dir and dir no longer exists during deletion.""" |
| 68 | self.Patch(os, "chmod") |
| 69 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 70 | expected_error = EnvironmentError() |
| 71 | expected_error.errno = errno.ENOENT |
| 72 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 73 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 74 | def _Call(): |
| 75 | with utils.TempDir(): |
| 76 | pass |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 77 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 78 | # Verify no exception should be raised when rmtree raises |
| 79 | # EnvironmentError with errno.ENOENT, i.e. |
| 80 | # directory no longer exists. |
| 81 | _Call() |
| 82 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 83 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 84 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 85 | def testTempDirWhenDeleteEncounterError(self): |
| 86 | """Test create a temp dir and encoutered error during deletion.""" |
| 87 | self.Patch(os, "chmod") |
| 88 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 89 | expected_error = OSError("Expected OS Error") |
| 90 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 91 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 92 | def _Call(): |
| 93 | with utils.TempDir(): |
| 94 | pass |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 95 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 96 | # Verify OSError should be raised. |
| 97 | self.assertRaises(OSError, _Call) |
| 98 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 99 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 100 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 101 | def testTempDirOrininalErrorRaised(self): |
| 102 | """Test original error is raised even if tmp dir deletion failed.""" |
| 103 | self.Patch(os, "chmod") |
| 104 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 105 | expected_error = OSError("Expected OS Error") |
| 106 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 107 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 108 | class ExpectedException(Exception): |
| 109 | """Expected exception.""" |
| 110 | pass |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 111 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 112 | def _Call(): |
| 113 | with utils.TempDir(): |
| 114 | raise ExpectedException("Expected Exception") |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 115 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 116 | # Verify. |
| 117 | # ExpectedException should be raised, and OSError |
| 118 | # should not be raised. |
| 119 | self.assertRaises(ExpectedException, _Call) |
| 120 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 121 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 122 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 123 | def testCreateSshKeyPairKeyAlreadyExists(self): #pylint: disable=invalid-name |
| 124 | """Test when the key pair already exists.""" |
| 125 | public_key = "/fake/public_key" |
| 126 | private_key = "/fake/private_key" |
| 127 | self.Patch( |
| 128 | os.path, "exists", side_effect=lambda path: path == public_key) |
| 129 | self.Patch(subprocess, "check_call") |
| 130 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| 131 | self.assertEqual(subprocess.check_call.call_count, 0) #pylint: disable=no-member |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 132 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 133 | def testCreateSshKeyPairKeyAreCreated(self): |
| 134 | """Test when the key pair created.""" |
| 135 | public_key = "/fake/public_key" |
| 136 | private_key = "/fake/private_key" |
| 137 | self.Patch(os.path, "exists", return_value=False) |
| 138 | self.Patch(subprocess, "check_call") |
| 139 | self.Patch(os, "rename") |
| 140 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| 141 | self.assertEqual(subprocess.check_call.call_count, 1) #pylint: disable=no-member |
| 142 | subprocess.check_call.assert_called_with( #pylint: disable=no-member |
| 143 | utils.SSH_KEYGEN_CMD + |
| 144 | ["-C", getpass.getuser(), "-f", private_key], |
| 145 | stdout=mock.ANY, |
| 146 | stderr=mock.ANY) |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 147 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 148 | def TestRetryOnException(self): |
| 149 | """Test Retry.""" |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 150 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 151 | def _IsValueError(exc): |
| 152 | return isinstance(exc, ValueError) |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 153 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 154 | num_retry = 5 |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 155 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 156 | @utils.RetryOnException(_IsValueError, num_retry) |
| 157 | def _RaiseAndRetry(sentinel): |
| 158 | sentinel.alert() |
| 159 | raise ValueError("Fake error.") |
| 160 | |
| 161 | sentinel = mock.MagicMock() |
| 162 | self.assertRaises(ValueError, _RaiseAndRetry, sentinel) |
| 163 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| 164 | |
| 165 | def testRetryExceptionType(self): |
| 166 | """Test RetryExceptionType function.""" |
| 167 | |
| 168 | def _RaiseAndRetry(sentinel): |
| 169 | sentinel.alert() |
| 170 | raise ValueError("Fake error.") |
| 171 | |
| 172 | num_retry = 5 |
| 173 | sentinel = mock.MagicMock() |
| 174 | self.assertRaises( |
| 175 | ValueError, |
| 176 | utils.RetryExceptionType, (KeyError, ValueError), |
| 177 | num_retry, |
| 178 | _RaiseAndRetry, |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 179 | 0, # sleep_multiplier |
| 180 | 1, # retry_backoff_factor |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 181 | sentinel=sentinel) |
| 182 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| 183 | |
| 184 | def testRetry(self): |
| 185 | """Test Retry.""" |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 186 | mock_sleep = self.Patch(time, "sleep") |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 187 | |
| 188 | def _RaiseAndRetry(sentinel): |
| 189 | sentinel.alert() |
| 190 | raise ValueError("Fake error.") |
| 191 | |
| 192 | num_retry = 5 |
| 193 | sentinel = mock.MagicMock() |
| 194 | self.assertRaises( |
| 195 | ValueError, |
| 196 | utils.RetryExceptionType, (ValueError, KeyError), |
| 197 | num_retry, |
| 198 | _RaiseAndRetry, |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 199 | 1, # sleep_multiplier |
| 200 | 2, # retry_backoff_factor |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 201 | sentinel=sentinel) |
| 202 | |
| 203 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 204 | mock_sleep.assert_has_calls( |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 205 | [ |
| 206 | mock.call(1), |
| 207 | mock.call(2), |
| 208 | mock.call(4), |
| 209 | mock.call(8), |
| 210 | mock.call(16) |
| 211 | ]) |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 212 | |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 213 | |
| 214 | if __name__ == "__main__": |
| 215 | unittest.main() |