diff options
Diffstat (limited to 'crosperf/download_images_unittest.py')
-rwxr-xr-x | crosperf/download_images_unittest.py | 78 |
1 files changed, 44 insertions, 34 deletions
diff --git a/crosperf/download_images_unittest.py b/crosperf/download_images_unittest.py index 9ca40a81..237369b7 100755 --- a/crosperf/download_images_unittest.py +++ b/crosperf/download_images_unittest.py @@ -12,10 +12,10 @@ from cros_utils import logger import test_flag -MOCK_LOGGER = logger.GetLogger(log_dir="", mock=True) +MOCK_LOGGER = logger.GetLogger(log_dir='', mock=True) -class ImageDownloaderTestcast(unittest.TestCase): +class ImageDownloaderTestcast(unittest.TestCase): @mock.patch.object(os, 'makedirs') @mock.patch.object(os.path, 'exists') @@ -23,9 +23,9 @@ class ImageDownloaderTestcast(unittest.TestCase): # Set mock and test values. mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter) - test_chroot = "/usr/local/home/chromeos" - test_build_id = "lumpy-release/R36-5814.0.0" - image_path = ("gs://chromeos-image-archive/%s/chromiumos_test_image.tar.xz" + test_chroot = '/usr/local/home/chromeos' + test_build_id = 'lumpy-release/R36-5814.0.0' + image_path = ('gs://chromeos-image-archive/%s/chromiumos_test_image.tar.xz' % test_build_id) downloader = download_images.ImageDownloader(logger_to_use=MOCK_LOGGER, @@ -38,16 +38,23 @@ class ImageDownloaderTestcast(unittest.TestCase): # Verify os.path.exists was called twice, with proper arguments. self.assertEqual(mock_path_exists.call_count, 2) - mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') - mock_path_exists.assert_any_call('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') + mock_path_exists.assert_called_with( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') + mock_path_exists.assert_any_call( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') # Verify we called os.mkdirs self.assertEqual(mock_mkdirs.call_count, 1) - mock_mkdirs.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') + mock_mkdirs.assert_called_with( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') # Verify we called ChrootRunCommand once, with proper arguments. - self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 1) - mock_cmd_exec.ChrootRunCommand.assert_called_with('/usr/local/home/chromeos', 'gsutil cp gs://chromeos-image-archive/lumpy-release/R36-5814.0.0/chromiumos_test_image.tar.xz /tmp/lumpy-release/R36-5814.0.0') + self.assertEqual(mock_cmd_exec.ChrootRunCommand.call_count, 1) + mock_cmd_exec.ChrootRunCommand.assert_called_with( + '/usr/local/home/chromeos', + 'gsutil cp ' + 'gs://chromeos-image-archive/lumpy-release/R36-5814.0.0/chromiumos_test_image.tar.xz' + ' /tmp/lumpy-release/R36-5814.0.0') # Reset the velues in the mocks; set os.path.exists to always return True. mock_path_exists.reset_mock() @@ -55,19 +62,19 @@ class ImageDownloaderTestcast(unittest.TestCase): mock_path_exists.return_value = True # Run downloader - downloader._DownloadImage(test_chroot, test_build_id,image_path) + downloader._DownloadImage(test_chroot, test_build_id, image_path) # Verify os.path.exists was called twice, with proper arguments. self.assertEqual(mock_path_exists.call_count, 2) - mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') - mock_path_exists.assert_any_call('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') + mock_path_exists.assert_called_with( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') + mock_path_exists.assert_any_call( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') # Verify we made no RunCommand or ChrootRunCommand calls (since # os.path.exists returned True, there was no work do be done). - self.assertEqual (mock_cmd_exec.RunCommand.call_count, 0) - self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 0) - - + self.assertEqual(mock_cmd_exec.RunCommand.call_count, 0) + self.assertEqual(mock_cmd_exec.ChrootRunCommand.call_count, 0) @mock.patch.object(os.path, 'exists') def test_uncompress_image(self, mock_path_exists): @@ -85,12 +92,16 @@ class ImageDownloaderTestcast(unittest.TestCase): downloader._UncompressImage(test_chroot, test_build_id) # Verify os.path.exists was called once, with correct arguments. - self.assertEqual (mock_path_exists.call_count, 1) - mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') + self.assertEqual(mock_path_exists.call_count, 1) + mock_path_exists.assert_called_with( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') # Verify ChrootRunCommand was called, with correct arguments. - self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 1) - mock_cmd_exec.ChrootRunCommand.assert_called_with('/usr/local/home/chromeos', 'cd /tmp/lumpy-release/R36-5814.0.0 ;unxz chromiumos_test_image.tar.xz; tar -xvf chromiumos_test_image.tar') + self.assertEqual(mock_cmd_exec.ChrootRunCommand.call_count, 1) + mock_cmd_exec.ChrootRunCommand.assert_called_with( + '/usr/local/home/chromeos', + 'cd /tmp/lumpy-release/R36-5814.0.0 ;unxz ' + 'chromiumos_test_image.tar.xz; tar -xvf chromiumos_test_image.tar') # Set os.path.exists to always return False and run uncompress. mock_path_exists.reset_mock() @@ -99,19 +110,18 @@ class ImageDownloaderTestcast(unittest.TestCase): downloader._UncompressImage(test_chroot, test_build_id) # Verify os.path.exists was called once, with correct arguments. - self.assertEqual (mock_path_exists.call_count, 1) - mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') + self.assertEqual(mock_path_exists.call_count, 1) + mock_path_exists.assert_called_with( + '/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') # Verify ChrootRunCommand was not called. - self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 0) - - + self.assertEqual(mock_cmd_exec.ChrootRunCommand.call_count, 0) def test_run(self): # Set test arguments - test_chroot = "/usr/local/home/chromeos" - test_build_id = "remote/lumpy/latest-dev" + test_chroot = '/usr/local/home/chromeos' + test_build_id = 'remote/lumpy/latest-dev' # Set values to test/check. self.called_download_image = False @@ -125,7 +135,7 @@ class ImageDownloaderTestcast(unittest.TestCase): def GoodDownloadImage(root, build_id, image_path): self.called_download_image = True - return "chromiumos_test_image.bin" + return 'chromiumos_test_image.bin' def BadDownloadImage(root, build_id, image_path): self.called_download_image = True @@ -147,8 +157,8 @@ class ImageDownloaderTestcast(unittest.TestCase): downloader.Run(test_chroot, test_build_id) # Make sure it called both _DownloadImage and _UncompressImage - self.assertTrue (self.called_download_image) - self.assertTrue (self.called_uncompress_image) + self.assertTrue(self.called_download_image) + self.assertTrue(self.called_uncompress_image) # Reset values; Now use fake stub that simulates DownloadImage failing. self.called_download_image = False @@ -156,11 +166,11 @@ class ImageDownloaderTestcast(unittest.TestCase): downloader._DownloadImage = BadDownloadImage # Call Run again. - downloader.Run (test_chroot, test_build_id) + downloader.Run(test_chroot, test_build_id) # Verify that UncompressImage was not called, since _DownloadImage "failed" - self.assertTrue (self.called_download_image) - self.assertFalse (self.called_uncompress_image) + self.assertTrue(self.called_download_image) + self.assertFalse(self.called_uncompress_image) if __name__ == '__main__': |