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 | |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 30 | from acloud.internal.lib import driver_test_lib |
| 31 | from acloud.internal.lib import utils |
| 32 | |
Kevin Cheng | 53aa5a5 | 2018-12-03 01:33:55 -0800 | [diff] [blame^] | 33 | # Tkinter may not be supported so mock it out. |
| 34 | try: |
| 35 | import Tkinter |
| 36 | except ImportError: |
| 37 | Tkinter = mock.Mock() |
| 38 | |
Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 39 | class FakeTkinter(object): |
| 40 | """Fake implementation of Tkinter.Tk()""" |
| 41 | |
| 42 | def __init__(self, width=None, height=None): |
| 43 | self.width = width |
| 44 | self.height = height |
| 45 | |
| 46 | # pylint: disable=invalid-name |
| 47 | def winfo_screenheight(self): |
| 48 | """Return the screen height.""" |
| 49 | return self.height |
| 50 | |
| 51 | # pylint: disable=invalid-name |
| 52 | def winfo_screenwidth(self): |
| 53 | """Return the screen width.""" |
| 54 | return self.width |
| 55 | |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 56 | |
| 57 | class UtilsTest(driver_test_lib.BaseDriverTest): |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 58 | """Test Utils.""" |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 59 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 60 | def TestTempDirSuccess(self): |
| 61 | """Test create a temp dir.""" |
| 62 | self.Patch(os, "chmod") |
| 63 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 64 | self.Patch(shutil, "rmtree") |
| 65 | with utils.TempDir(): |
| 66 | pass |
| 67 | # Verify. |
| 68 | tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member |
| 69 | shutil.rmtree.assert_called_with("/tmp/tempdir") # pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 70 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 71 | def TestTempDirExceptionRaised(self): |
| 72 | """Test create a temp dir and exception is raised within with-clause.""" |
| 73 | self.Patch(os, "chmod") |
| 74 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 75 | self.Patch(shutil, "rmtree") |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 76 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 77 | class ExpectedException(Exception): |
| 78 | """Expected exception.""" |
| 79 | pass |
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 | def _Call(): |
| 82 | with utils.TempDir(): |
| 83 | raise ExpectedException("Expected exception.") |
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 | # Verify. ExpectedException should be raised. |
| 86 | self.assertRaises(ExpectedException, _Call) |
| 87 | tempfile.mkdtemp.assert_called_once() # pylint: disable=no-member |
| 88 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 89 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 90 | def testTempDirWhenDeleteTempDirNoLongerExist(self): # pylint: disable=invalid-name |
| 91 | """Test create a temp dir and dir no longer exists during deletion.""" |
| 92 | self.Patch(os, "chmod") |
| 93 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 94 | expected_error = EnvironmentError() |
| 95 | expected_error.errno = errno.ENOENT |
| 96 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 97 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 98 | def _Call(): |
| 99 | with utils.TempDir(): |
| 100 | pass |
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 | # Verify no exception should be raised when rmtree raises |
| 103 | # EnvironmentError with errno.ENOENT, i.e. |
| 104 | # directory no longer exists. |
| 105 | _Call() |
| 106 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 107 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 108 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 109 | def testTempDirWhenDeleteEncounterError(self): |
| 110 | """Test create a temp dir and encoutered error during deletion.""" |
| 111 | self.Patch(os, "chmod") |
| 112 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 113 | expected_error = OSError("Expected OS Error") |
| 114 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 115 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 116 | def _Call(): |
| 117 | with utils.TempDir(): |
| 118 | pass |
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 | # Verify OSError should be raised. |
| 121 | self.assertRaises(OSError, _Call) |
| 122 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 123 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | 26e4dc1 | 2018-03-04 19:01:59 -0800 | [diff] [blame] | 124 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 125 | def testTempDirOrininalErrorRaised(self): |
| 126 | """Test original error is raised even if tmp dir deletion failed.""" |
| 127 | self.Patch(os, "chmod") |
| 128 | self.Patch(tempfile, "mkdtemp", return_value="/tmp/tempdir") |
| 129 | expected_error = OSError("Expected OS Error") |
| 130 | self.Patch(shutil, "rmtree", side_effect=expected_error) |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 131 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 132 | class ExpectedException(Exception): |
| 133 | """Expected exception.""" |
| 134 | pass |
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 | def _Call(): |
| 137 | with utils.TempDir(): |
| 138 | raise ExpectedException("Expected Exception") |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 139 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 140 | # Verify. |
| 141 | # ExpectedException should be raised, and OSError |
| 142 | # should not be raised. |
| 143 | self.assertRaises(ExpectedException, _Call) |
| 144 | tempfile.mkdtemp.assert_called_once() #pylint: disable=no-member |
| 145 | shutil.rmtree.assert_called_with("/tmp/tempdir") #pylint: disable=no-member |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 146 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 147 | def testCreateSshKeyPairKeyAlreadyExists(self): #pylint: disable=invalid-name |
| 148 | """Test when the key pair already exists.""" |
| 149 | public_key = "/fake/public_key" |
| 150 | private_key = "/fake/private_key" |
cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 151 | self.Patch(os.path, "exists", side_effect=[True, True]) |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 152 | self.Patch(subprocess, "check_call") |
cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 153 | self.Patch(os, "makedirs", return_value=True) |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 154 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| 155 | self.assertEqual(subprocess.check_call.call_count, 0) #pylint: disable=no-member |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 156 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 157 | def testCreateSshKeyPairKeyAreCreated(self): |
| 158 | """Test when the key pair created.""" |
| 159 | public_key = "/fake/public_key" |
| 160 | private_key = "/fake/private_key" |
| 161 | self.Patch(os.path, "exists", return_value=False) |
cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 162 | self.Patch(os, "makedirs", return_value=True) |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 163 | self.Patch(subprocess, "check_call") |
| 164 | self.Patch(os, "rename") |
| 165 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
| 166 | self.assertEqual(subprocess.check_call.call_count, 1) #pylint: disable=no-member |
| 167 | subprocess.check_call.assert_called_with( #pylint: disable=no-member |
| 168 | utils.SSH_KEYGEN_CMD + |
| 169 | ["-C", getpass.getuser(), "-f", private_key], |
| 170 | stdout=mock.ANY, |
| 171 | stderr=mock.ANY) |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 172 | |
cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 173 | def testCreatePublicKeyAreCreated(self): |
| 174 | """Test when the PublicKey created.""" |
| 175 | public_key = "/fake/public_key" |
| 176 | private_key = "/fake/private_key" |
| 177 | self.Patch(os.path, "exists", side_effect=[False, True, True]) |
| 178 | self.Patch(os, "makedirs", return_value=True) |
| 179 | mock_open = mock.mock_open(read_data=public_key) |
cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 180 | self.Patch(subprocess, "check_output") |
| 181 | self.Patch(os, "rename") |
Kevin Cheng | da4f07a | 2018-06-26 10:25:05 -0700 | [diff] [blame] | 182 | with mock.patch("__builtin__.open", mock_open): |
| 183 | utils.CreateSshKeyPairIfNotExist(private_key, public_key) |
cylan | 4f73c1f | 2018-07-19 16:40:31 +0800 | [diff] [blame] | 184 | self.assertEqual(subprocess.check_output.call_count, 1) #pylint: disable=no-member |
| 185 | subprocess.check_output.assert_called_with( #pylint: disable=no-member |
| 186 | utils.SSH_KEYGEN_PUB_CMD +["-f", private_key]) |
| 187 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 188 | def TestRetryOnException(self): |
| 189 | """Test Retry.""" |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 190 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 191 | def _IsValueError(exc): |
| 192 | return isinstance(exc, ValueError) |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 193 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 194 | num_retry = 5 |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 195 | |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 196 | @utils.RetryOnException(_IsValueError, num_retry) |
| 197 | def _RaiseAndRetry(sentinel): |
| 198 | sentinel.alert() |
| 199 | raise ValueError("Fake error.") |
| 200 | |
| 201 | sentinel = mock.MagicMock() |
| 202 | self.assertRaises(ValueError, _RaiseAndRetry, sentinel) |
| 203 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| 204 | |
| 205 | def testRetryExceptionType(self): |
| 206 | """Test RetryExceptionType function.""" |
| 207 | |
| 208 | def _RaiseAndRetry(sentinel): |
| 209 | sentinel.alert() |
| 210 | raise ValueError("Fake error.") |
| 211 | |
| 212 | num_retry = 5 |
| 213 | sentinel = mock.MagicMock() |
| 214 | self.assertRaises( |
| 215 | ValueError, |
| 216 | utils.RetryExceptionType, (KeyError, ValueError), |
| 217 | num_retry, |
| 218 | _RaiseAndRetry, |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 219 | 0, # sleep_multiplier |
| 220 | 1, # retry_backoff_factor |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 221 | sentinel=sentinel) |
| 222 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
| 223 | |
| 224 | def testRetry(self): |
| 225 | """Test Retry.""" |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 226 | mock_sleep = self.Patch(time, "sleep") |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 227 | |
| 228 | def _RaiseAndRetry(sentinel): |
| 229 | sentinel.alert() |
| 230 | raise ValueError("Fake error.") |
| 231 | |
| 232 | num_retry = 5 |
| 233 | sentinel = mock.MagicMock() |
| 234 | self.assertRaises( |
| 235 | ValueError, |
| 236 | utils.RetryExceptionType, (ValueError, KeyError), |
| 237 | num_retry, |
| 238 | _RaiseAndRetry, |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 239 | 1, # sleep_multiplier |
| 240 | 2, # retry_backoff_factor |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 241 | sentinel=sentinel) |
| 242 | |
| 243 | self.assertEqual(1 + num_retry, sentinel.alert.call_count) |
Kevin Cheng | d25feee | 2018-05-24 10:15:20 -0700 | [diff] [blame] | 244 | mock_sleep.assert_has_calls( |
cylan | 0d77ae1 | 2018-05-18 08:36:48 +0000 | [diff] [blame] | 245 | [ |
| 246 | mock.call(1), |
| 247 | mock.call(2), |
| 248 | mock.call(4), |
| 249 | mock.call(8), |
| 250 | mock.call(16) |
| 251 | ]) |
Fang Deng | f24be08 | 2018-02-10 10:09:55 -0800 | [diff] [blame] | 252 | |
Kevin Cheng | eb85e86 | 2018-10-09 15:35:13 -0700 | [diff] [blame] | 253 | @mock.patch("__builtin__.raw_input") |
| 254 | def testGetAnswerFromList(self, mock_raw_input): |
| 255 | """Test GetAnswerFromList.""" |
| 256 | answer_list = ["image1.zip", "image2.zip", "image3.zip"] |
| 257 | mock_raw_input.return_value = 0 |
| 258 | with self.assertRaises(SystemExit): |
| 259 | utils.GetAnswerFromList(answer_list) |
| 260 | mock_raw_input.side_effect = [1, 2, 3, 1] |
| 261 | self.assertEqual(utils.GetAnswerFromList(answer_list), |
| 262 | ["image1.zip"]) |
| 263 | self.assertEqual(utils.GetAnswerFromList(answer_list), |
| 264 | ["image2.zip"]) |
| 265 | self.assertEqual(utils.GetAnswerFromList(answer_list), |
| 266 | ["image3.zip"]) |
| 267 | self.assertEqual(utils.GetAnswerFromList(answer_list, |
| 268 | enable_choose_all=True), |
| 269 | answer_list) |
| 270 | |
Kevin Cheng | 53aa5a5 | 2018-12-03 01:33:55 -0800 | [diff] [blame^] | 271 | @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] | 272 | @mock.patch.object(Tkinter, "Tk") |
| 273 | def testCalculateVNCScreenRatio(self, mock_tk): |
Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 274 | """Test Calculating the scale ratio of VNC display.""" |
Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 275 | # Get scale-down ratio if screen height is smaller than AVD height. |
Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 276 | mock_tk.return_value = FakeTkinter(height=800, width=1200) |
Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 277 | avd_h = 1920 |
| 278 | avd_w = 1080 |
| 279 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 0.4) |
| 280 | |
| 281 | # Get scale-down ratio if screen width is smaller than AVD width. |
Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 282 | mock_tk.return_value = FakeTkinter(height=800, width=1200) |
Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 283 | avd_h = 900 |
| 284 | avd_w = 1920 |
| 285 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 0.6) |
| 286 | |
| 287 | # Scale ratio = 1 if screen is larger than AVD. |
Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 288 | mock_tk.return_value = FakeTkinter(height=1080, width=1920) |
Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 289 | avd_h = 800 |
| 290 | avd_w = 1280 |
| 291 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 1) |
| 292 | |
| 293 | # Get the scale if ratio of width is smaller than the |
| 294 | # ratio of height. |
Kevin Cheng | 358fb3e | 2018-11-13 14:05:54 -0800 | [diff] [blame] | 295 | mock_tk.return_value = FakeTkinter(height=1200, width=800) |
Sam Chiu | 7a477f5 | 2018-10-22 11:20:36 +0800 | [diff] [blame] | 296 | avd_h = 1920 |
| 297 | avd_w = 1080 |
| 298 | self.assertEqual(utils.CalculateVNCScreenRatio(avd_w, avd_h), 0.6) |
| 299 | |
herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 300 | # pylint: disable=protected-access |
| 301 | def testCheckUserInGroups(self): |
| 302 | """Test CheckUserInGroups.""" |
| 303 | self.Patch(os, "getgroups", return_value=[1, 2, 3]) |
| 304 | gr1 = mock.MagicMock() |
| 305 | gr1.gr_name = "fake_gr_1" |
| 306 | gr2 = mock.MagicMock() |
| 307 | gr2.gr_name = "fake_gr_2" |
| 308 | gr3 = mock.MagicMock() |
| 309 | gr3.gr_name = "fake_gr_3" |
| 310 | self.Patch(grp, "getgrgid", side_effect=[gr1, gr2, gr3]) |
| 311 | |
| 312 | # User in all required groups should return true. |
| 313 | self.assertTrue( |
| 314 | utils.CheckUserInGroups( |
| 315 | ["fake_gr_1", "fake_gr_2"])) |
| 316 | |
| 317 | # User not in all required groups should return False. |
| 318 | self.Patch(grp, "getgrgid", side_effect=[gr1, gr2, gr3]) |
| 319 | self.assertFalse( |
| 320 | utils.CheckUserInGroups( |
| 321 | ["fake_gr_1", "fake_gr_4"])) |
| 322 | |
| 323 | @mock.patch.object(utils, "CheckUserInGroups") |
| 324 | def testAddUserGroupsToCmd(self, mock_user_group): |
| 325 | """Test AddUserGroupsToCmd.""" |
| 326 | command = "test_command" |
| 327 | groups = ["group1", "group2"] |
| 328 | # Don't add user group in command |
| 329 | mock_user_group.return_value = True |
| 330 | expected_value = "test_command" |
| 331 | self.assertEqual(expected_value, utils.AddUserGroupsToCmd(command, |
| 332 | groups)) |
| 333 | |
| 334 | # Add user group in command |
| 335 | mock_user_group.return_value = False |
| 336 | expected_value = "sg group1 <<EOF\nsg group2\ntest_command\nEOF" |
| 337 | self.assertEqual(expected_value, utils.AddUserGroupsToCmd(command, |
| 338 | groups)) |
| 339 | |
Fang Deng | 69498c3 | 2017-03-02 14:29:30 -0800 | [diff] [blame] | 340 | |
| 341 | if __name__ == "__main__": |
| 342 | unittest.main() |