| 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 |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 20 | import grp |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 21 | import os |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 22 | import shutil |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 23 | import subprocess |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 24 | import tempfile |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 25 | import time |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 26 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 27 | import unittest |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 28 | import mock |
| 29 | |
| Sam Chiu | 7de3b23 | 2018-12-06 19:45:52 +0800 | [diff] [blame] | 30 | from acloud import errors |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 31 | from acloud.internal.lib import driver_test_lib |
| 32 | from acloud.internal.lib import utils |
| 33 | |
| herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 34 | |
| Kevin Cheng | 53aa5a5 | 2018-12-03 01:33:55 -0800 | [diff] [blame] | 35 | # Tkinter may not be supported so mock it out. |
| 36 | try: |
| 37 | import Tkinter |
| 38 | except ImportError: |
| 39 | Tkinter = mock.Mock() |
| 40 | |
| herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 41 | |
| Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 42 | class FakeTkinter(object): |
| 43 | """Fake implementation of Tkinter.Tk()""" |
| 44 | |
| 45 | def __init__(self, width=None, height=None): |
| 46 | self.width = width |
| 47 | self.height = height |
| 48 | |
| 49 | # pylint: disable=invalid-name |
| 50 | def winfo_screenheight(self): |
| 51 | """Return the screen height.""" |
| 52 | return self.height |
| 53 | |
| 54 | # pylint: disable=invalid-name |
| 55 | def winfo_screenwidth(self): |
| 56 | """Return the screen width.""" |
| 57 | return self.width |
| 58 | |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 59 | |
| chojoyce | efafc02 | 2018-11-08 17:22:16 +0800 | [diff] [blame] | 60 | # pylint: disable=too-many-public-methods |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 61 | class UtilsTest(driver_test_lib.BaseDriverTest): |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 62 | """Test Utils.""" |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 63 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 64 | def TestTempDirSuccess(self): |
| 65 | """Test create a temp dir.""" |
| 66 | self.Patch(os, "chmod") |
| 67 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 68 | self.Patch(shutil, "rmtree") |
| 69 | with utils.TempDir(): |
| 70 | pass |
| 71 | # Verify. |
| 72 | tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member |
| 73 | shutil.rmtree.assert_called_with("/tmp/tempdir") # pylint: disable=no-member |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 74 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 75 | def TestTempDirExceptionRaised(self): |
| 76 | """Test create a temp dir and exception is raised within with-clause.""" |
| 77 | self.Patch(os, "chmod") |
| 78 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 79 | self.Patch(shutil, "rmtree") |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 80 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 81 | class ExpectedException(Exception): |
| 82 | """Expected exception.""" |
| 83 | pass |
| 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 _Call(): |
| 86 | with utils.TempDir(): |
| 87 | raise ExpectedException("Expected exception.") |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 88 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 89 | # Verify. ExpectedException should be raised. |
| 90 | self.assertRaises(ExpectedException, _Call) |
| 91 | tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member |
| 92 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 93 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 94 | def testTempDirWhenDeleteTempDirNoLongerExist(self): # pylint: disable=invalid-name |
| 95 | """Test create a temp dir and dir no longer exists during deletion.""" |
| 96 | self.Patch(os, "chmod") |
| 97 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 98 | expected_error = EnvironmentError() |
| 99 | expected_error.errno = errno.ENOENT |
| 100 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 101 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 102 | def _Call(): |
| 103 | with utils.TempDir(): |
| 104 | pass |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 105 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 106 | # Verify no exception should be raised when rmtree raises |
| 107 | # EnvironmentError with errno.ENOENT, i.e. |
| 108 | # directory no longer exists. |
| 109 | _Call() |
| 110 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 111 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 112 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 113 | def testTempDirWhenDeleteEncounterError(self): |
| 114 | """Test create a temp dir and encoutered error during deletion.""" |
| 115 | self.Patch(os, "chmod") |
| 116 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 117 | expected_error = OSError("Expected OS Error") |
| 118 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 119 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 120 | def _Call(): |
| 121 | with utils.TempDir(): |
| 122 | pass |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 123 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 124 | # Verify OSError should be raised. |
| 125 | self.assertRaises(OSError, _Call) |
| 126 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 127 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
| Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 128 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 129 | def testTempDirOrininalErrorRaised(self): |
| 130 | """Test original error is raised even if tmp dir deletion failed.""" |
| 131 | self.Patch(os, "chmod") |
| 132 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 133 | expected_error = OSError("Expected OS Error") |
| 134 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 135 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 136 | class ExpectedException(Exception): |
| 137 | """Expected exception.""" |
| 138 | pass |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 139 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 140 | def _Call(): |
| 141 | with utils.TempDir(): |
| 142 | raise ExpectedException("Expected Exception") |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 143 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 144 | # Verify. |
| 145 | # ExpectedException should be raised, and OSError |
| 146 | # should not be raised. |
| 147 | self.assertRaises(ExpectedException, _Call) |
| 148 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 149 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
| 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 testCreateSshKeyPairKeyAlreadyExists(self): #pylint: disable=invalid-name |
| 152 | """Test when the key pair already exists.""" |
| 153 | public_key = "/fake/public_key" |
| 154 | private_key = "/fake/private_key" |
| cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 155 | self.Patch(os.path, "exists", side_effect=[True, True]) |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 156 | self.Patch(subprocess, "check_call") |
| cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 157 | self.Patch(os, "makedirs", return_value=True) |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 158 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| 159 | self.assertEqual(subprocess.check_call.call_count, 0) #pylint: disable=no-member |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 160 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 161 | def testCreateSshKeyPairKeyAreCreated(self): |
| 162 | """Test when the key pair created.""" |
| 163 | public_key = "/fake/public_key" |
| 164 | private_key = "/fake/private_key" |
| 165 | self.Patch(os.path, "exists", return_value=False) |
| cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 166 | self.Patch(os, "makedirs", return_value=True) |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 167 | self.Patch(subprocess, "check_call") |
| 168 | self.Patch(os, "rename") |
| 169 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| 170 | self.assertEqual(subprocess.check_call.call_count, 1) #pylint: disable=no-member |
| 171 | subprocess.check_call.assert_called_with( #pylint: disable=no-member |
| 172 | utils.SSH_KEYGEN_CMD + |
| 173 | ["-C", getpass.getuser(), "-f", private_key], |
| 174 | stdout=mock.ANY, |
| 175 | stderr=mock.ANY) |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 176 | |
| cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 177 | def testCreatePublicKeyAreCreated(self): |
| 178 | """Test when the PublicKey created.""" |
| 179 | public_key = "/fake/public_key" |
| 180 | private_key = "/fake/private_key" |
| 181 | self.Patch(os.path, "exists", side_effect=[False, True, True]) |
| 182 | self.Patch(os, "makedirs", return_value=True) |
| 183 | mock_open = mock.mock_open(read_data=public_key) |
| cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 184 | self.Patch(subprocess, "check_output") |
| 185 | self.Patch(os, "rename") |
| Kevin Cheng | da4f07a | 2018-06-26 10:25:05 -0700 | [diff] [blame] | 186 | with mock.patch("__builtin__.open", mock_open): |
| 187 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 188 | self.assertEqual(subprocess.check_output.call_count, 1) #pylint: disable=no-member |
| 189 | subprocess.check_output.assert_called_with( #pylint: disable=no-member |
| 190 | utils.SSH_KEYGEN_PUB_CMD +["-f", private_key]) |
| 191 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 192 | def TestRetryOnException(self): |
| 193 | """Test Retry.""" |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 194 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 195 | def _IsValueError(exc): |
| 196 | return isinstance(exc, ValueError) |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 197 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 198 | num_retry = 5 |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 199 | |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 200 | @utils.RetryOnException(_IsValueError, num_retry) |
| 201 | def _RaiseAndRetry(sentinel): |
| 202 | sentinel.alert() |
| 203 | raise ValueError("Fake error.") |
| 204 | |
| 205 | sentinel = mock.MagicMock() |
| 206 | self.assertRaises(ValueError, _RaiseAndRetry, sentinel) |
| 207 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| 208 | |
| 209 | def testRetryExceptionType(self): |
| 210 | """Test RetryExceptionType function.""" |
| 211 | |
| 212 | def _RaiseAndRetry(sentinel): |
| 213 | sentinel.alert() |
| 214 | raise ValueError("Fake error.") |
| 215 | |
| 216 | num_retry = 5 |
| 217 | sentinel = mock.MagicMock() |
| 218 | self.assertRaises( |
| 219 | ValueError, |
| 220 | utils.RetryExceptionType, (KeyError, ValueError), |
| 221 | num_retry, |
| 222 | _RaiseAndRetry, |
| Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 223 | 0, # sleep_multiplier |
| 224 | 1, # retry_backoff_factor |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 225 | sentinel=sentinel) |
| 226 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| 227 | |
| 228 | def testRetry(self): |
| 229 | """Test Retry.""" |
| Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 230 | mock_sleep = self.Patch(time, "sleep") |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 231 | |
| 232 | def _RaiseAndRetry(sentinel): |
| 233 | sentinel.alert() |
| 234 | raise ValueError("Fake error.") |
| 235 | |
| 236 | num_retry = 5 |
| 237 | sentinel = mock.MagicMock() |
| 238 | self.assertRaises( |
| 239 | ValueError, |
| 240 | utils.RetryExceptionType, (ValueError, KeyError), |
| 241 | num_retry, |
| 242 | _RaiseAndRetry, |
| Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 243 | 1, # sleep_multiplier |
| 244 | 2, # retry_backoff_factor |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 245 | sentinel=sentinel) |
| 246 | |
| 247 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 248 | mock_sleep.assert_has_calls( |
| cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 249 | [ |
| 250 | mock.call(1), |
| 251 | mock.call(2), |
| 252 | mock.call(4), |
| 253 | mock.call(8), |
| 254 | mock.call(16) |
| 255 | ]) |
| Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 256 | |
| Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 257 | @mock.patch("__builtin__.raw_input") |
| 258 | def testGetAnswerFromList(self, mock_raw_input): |
| 259 | """Test GetAnswerFromList.""" |
| 260 | answer_list = ["image1.zip", "image2.zip", "image3.zip"] |
| 261 | mock_raw_input.return_value = 0 |
| 262 | with self.assertRaises(SystemExit): |
| 263 | utils.GetAnswerFromList(answer_list) |
| Sam Chiu | 7169134 | 2019-03-27 17:30:12 +0800 | [diff] [blame] | 264 | mock_raw_input.side_effect = [1, 2, 3, 4] |
| Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 265 | self.assertEqual(utils.GetAnswerFromList(answer_list), |
| 266 | ["image1.zip"]) |
| 267 | self.assertEqual(utils.GetAnswerFromList(answer_list), |
| 268 | ["image2.zip"]) |
| 269 | self.assertEqual(utils.GetAnswerFromList(answer_list), |
| 270 | ["image3.zip"]) |
| 271 | self.assertEqual(utils.GetAnswerFromList(answer_list, |
| 272 | enable_choose_all=True), |
| 273 | answer_list) |
| 274 | |
| Kevin Cheng | 53aa5a5 | 2018-12-03 01:33:55 -0800 | [diff] [blame] | 275 | @unittest.skipIf(isinstance(Tkinter, mock.Mock), "Tkinter mocked out, test case not needed.") |
| Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 276 | @mock.patch.object(Tkinter, "Tk") |
| 277 | def testCalculateVNCScreenRatio(self, mock_tk): |
| Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 278 | """Test Calculating the scale ratio of VNC display.""" |
| Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 279 | # Get scale-down ratio if screen height is smaller than AVD height. |
| Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 280 | mock_tk.return_value = FakeTkinter(height=800, width=1200) |
| Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 281 | avd_h = 1920 |
| 282 | avd_w = 1080 |
| 283 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 0.4) |
| 284 | |
| 285 | # Get scale-down ratio if screen width is smaller than AVD width. |
| Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 286 | mock_tk.return_value = FakeTkinter(height=800, width=1200) |
| Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 287 | avd_h = 900 |
| 288 | avd_w = 1920 |
| 289 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 0.6) |
| 290 | |
| 291 | # Scale ratio = 1 if screen is larger than AVD. |
| Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 292 | mock_tk.return_value = FakeTkinter(height=1080, width=1920) |
| Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 293 | avd_h = 800 |
| 294 | avd_w = 1280 |
| 295 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 1) |
| 296 | |
| 297 | # Get the scale if ratio of width is smaller than the |
| 298 | # ratio of height. |
| Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 299 | mock_tk.return_value = FakeTkinter(height=1200, width=800) |
| Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 300 | avd_h = 1920 |
| 301 | avd_w = 1080 |
| 302 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 0.6) |
| 303 | |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 304 | # pylint: disable=protected-access |
| 305 | def testCheckUserInGroups(self): |
| 306 | """Test CheckUserInGroups.""" |
| 307 | self.Patch(os, "getgroups", return_value=[1, 2, 3]) |
| 308 | gr1 = mock.MagicMock() |
| 309 | gr1.gr_name = "fake_gr_1" |
| 310 | gr2 = mock.MagicMock() |
| 311 | gr2.gr_name = "fake_gr_2" |
| 312 | gr3 = mock.MagicMock() |
| 313 | gr3.gr_name = "fake_gr_3" |
| 314 | self.Patch(grp, "getgrgid", side_effect=[gr1, gr2, gr3]) |
| 315 | |
| 316 | # User in all required groups should return true. |
| 317 | self.assertTrue( |
| 318 | utils.CheckUserInGroups( |
| 319 | ["fake_gr_1", "fake_gr_2"])) |
| 320 | |
| 321 | # User not in all required groups should return False. |
| 322 | self.Patch(grp, "getgrgid", side_effect=[gr1, gr2, gr3]) |
| 323 | self.assertFalse( |
| 324 | utils.CheckUserInGroups( |
| 325 | ["fake_gr_1", "fake_gr_4"])) |
| 326 | |
| 327 | @mock.patch.object(utils, "CheckUserInGroups") |
| 328 | def testAddUserGroupsToCmd(self, mock_user_group): |
| 329 | """Test AddUserGroupsToCmd.""" |
| 330 | command = "test_command" |
| 331 | groups = ["group1", "group2"] |
| 332 | # Don't add user group in command |
| 333 | mock_user_group.return_value = True |
| 334 | expected_value = "test_command" |
| 335 | self.assertEqual(expected_value, utils.AddUserGroupsToCmd(command, |
| 336 | groups)) |
| 337 | |
| 338 | # Add user group in command |
| 339 | mock_user_group.return_value = False |
| 340 | expected_value = "sg group1 <<EOF\nsg group2\ntest_command\nEOF" |
| 341 | self.assertEqual(expected_value, utils.AddUserGroupsToCmd(command, |
| 342 | groups)) |
| 343 | |
| Kevin Cheng | ce6cfb0 | 2018-12-04 13:21:31 -0800 | [diff] [blame] | 344 | @staticmethod |
| 345 | def testScpPullFileSuccess(): |
| 346 | """Test scp pull file successfully.""" |
| 347 | subprocess.check_call = mock.MagicMock() |
| 348 | utils.ScpPullFile("/tmp/test", "/tmp/test_1.log", "192.168.0.1") |
| 349 | subprocess.check_call.assert_called_with(utils.SCP_CMD + [ |
| 350 | "192.168.0.1:/tmp/test", "/tmp/test_1.log"]) |
| 351 | |
| 352 | @staticmethod |
| 353 | def testScpPullFileWithUserNameSuccess(): |
| 354 | """Test scp pull file successfully.""" |
| 355 | subprocess.check_call = mock.MagicMock() |
| 356 | utils.ScpPullFile("/tmp/test", "/tmp/test_1.log", "192.168.0.1", |
| 357 | user_name="abc") |
| 358 | subprocess.check_call.assert_called_with(utils.SCP_CMD + [ |
| 359 | "abc@192.168.0.1:/tmp/test", "/tmp/test_1.log"]) |
| 360 | |
| 361 | # pylint: disable=invalid-name |
| 362 | @staticmethod |
| 363 | def testScpPullFileWithUserNameWithRsaKeySuccess(): |
| 364 | """Test scp pull file successfully.""" |
| 365 | subprocess.check_call = mock.MagicMock() |
| 366 | utils.ScpPullFile("/tmp/test", "/tmp/test_1.log", "192.168.0.1", |
| 367 | user_name="abc", rsa_key_file="/tmp/my_key") |
| 368 | subprocess.check_call.assert_called_with(utils.SCP_CMD + [ |
| 369 | "-i", "/tmp/my_key", "abc@192.168.0.1:/tmp/test", |
| 370 | "/tmp/test_1.log"]) |
| 371 | |
| 372 | def testScpPullFileScpFailure(self): |
| 373 | """Test scp pull file failure.""" |
| 374 | subprocess.check_call = mock.MagicMock( |
| 375 | side_effect=subprocess.CalledProcessError(123, "fake", |
| 376 | "fake error")) |
| 377 | self.assertRaises( |
| 378 | errors.DeviceConnectionError, |
| 379 | utils.ScpPullFile, "/tmp/test", "/tmp/test_1.log", "192.168.0.1") |
| 380 | |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 381 | |
| chojoyce | efafc02 | 2018-11-08 17:22:16 +0800 | [diff] [blame] | 382 | def testTimeoutException(self): |
| 383 | """Test TimeoutException.""" |
| 384 | @utils.TimeoutException(1, "should time out") |
| 385 | def functionThatWillTimeOut(): |
| 386 | """Test decorator of @utils.TimeoutException should timeout.""" |
| 387 | time.sleep(5) |
| 388 | |
| 389 | self.assertRaises(errors.FunctionTimeoutError, |
| 390 | functionThatWillTimeOut) |
| 391 | |
| 392 | |
| 393 | def testTimeoutExceptionNoTimeout(self): |
| 394 | """Test No TimeoutException.""" |
| 395 | @utils.TimeoutException(5, "shouldn't time out") |
| 396 | def functionThatShouldNotTimeout(): |
| 397 | """Test decorator of @utils.TimeoutException shouldn't timeout.""" |
| 398 | return None |
| 399 | try: |
| 400 | functionThatShouldNotTimeout() |
| 401 | except errors.FunctionTimeoutError: |
| 402 | self.fail("shouldn't timeout") |
| 403 | |
| cylan | 4b4bbd0 | 2019-01-16 14:49:17 +0800 | [diff] [blame] | 404 | def testAutoConnectCreateSSHTunnelFail(self): |
| cylan | d370db2 | 2019-07-17 16:04:00 +0800 | [diff] [blame^] | 405 | """Test auto connect.""" |
| cylan | 4b4bbd0 | 2019-01-16 14:49:17 +0800 | [diff] [blame] | 406 | fake_ip_addr = "1.1.1.1" |
| 407 | fake_rsa_key_file = "/tmp/rsa_file" |
| 408 | fake_target_vnc_port = 8888 |
| 409 | target_adb_port = 9999 |
| 410 | ssh_user = "fake_user" |
| 411 | call_side_effect = subprocess.CalledProcessError(123, "fake", |
| 412 | "fake error") |
| 413 | result = utils.ForwardedPorts(vnc_port=None, adb_port=None) |
| 414 | self.Patch(subprocess, "check_call", side_effect=call_side_effect) |
| 415 | self.assertEqual(result, utils.AutoConnect(fake_ip_addr, |
| 416 | fake_rsa_key_file, |
| 417 | fake_target_vnc_port, |
| 418 | target_adb_port, |
| 419 | ssh_user)) |
| 420 | |
| cylan | d370db2 | 2019-07-17 16:04:00 +0800 | [diff] [blame^] | 421 | # pylint: disable=protected-access,no-member |
| 422 | def testExtraArgsSSHTunnel(self): |
| 423 | """Tesg extra args will be the same with expanded args.""" |
| 424 | fake_ip_addr = "1.1.1.1" |
| 425 | fake_rsa_key_file = "/tmp/rsa_file" |
| 426 | fake_target_vnc_port = 8888 |
| 427 | target_adb_port = 9999 |
| 428 | ssh_user = "fake_user" |
| 429 | fake_port = 12345 |
| 430 | self.Patch(utils, "PickFreePort", return_value=fake_port) |
| 431 | self.Patch(utils, "_ExecuteCommand") |
| 432 | self.Patch(subprocess, "check_call", return_value=True) |
| 433 | extra_args_ssh_tunnel = "-o command='shell %s %h' -o command1='ls -la'" |
| 434 | utils.AutoConnect(ip_addr=fake_ip_addr, |
| 435 | rsa_key_file=fake_rsa_key_file, |
| 436 | target_vnc_port=fake_target_vnc_port, |
| 437 | target_adb_port=target_adb_port, |
| 438 | ssh_user=ssh_user, |
| 439 | client_adb_port=fake_port, |
| 440 | extra_args_ssh_tunnel=extra_args_ssh_tunnel) |
| 441 | args_list = ["-i", "/tmp/rsa_file", |
| 442 | "-o", "UserKnownHostsFile=/dev/null", |
| 443 | "-o", "StrictHostKeyChecking=no", |
| 444 | "-L", "12345:127.0.0.1:8888", |
| 445 | "-L", "12345:127.0.0.1:9999", |
| 446 | "-N", "-f", "-l", "fake_user", "1.1.1.1", |
| 447 | "-o", "command=shell %s %h", |
| 448 | "-o", "command1=ls -la"] |
| 449 | first_call_args = utils._ExecuteCommand.call_args_list[0][0] |
| 450 | self.assertEqual(first_call_args[1], args_list) |
| 451 | |
| chojoyce | efafc02 | 2018-11-08 17:22:16 +0800 | [diff] [blame] | 452 | |
| Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 453 | if __name__ == "__main__": |
| 454 | unittest.main() |