cmtice | 6ffbb92 | 2014-05-07 10:54:00 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2014 Google Inc. All Rights Reserved |
| 4 | |
| 5 | import download_images |
| 6 | from utils import command_executer |
| 7 | from utils import logger |
| 8 | |
| 9 | import os |
| 10 | import mock |
| 11 | import unittest |
| 12 | |
| 13 | MOCK_LOGGER = logger.GetLogger(log_dir="", mock=True) |
| 14 | |
| 15 | class ImageDownloaderTestcast(unittest.TestCase): |
| 16 | |
| 17 | |
| 18 | @mock.patch.object(os, 'makedirs') |
| 19 | @mock.patch.object(os.path, 'exists') |
| 20 | def test_download_image(self, mock_path_exists, mock_mkdirs): |
| 21 | |
| 22 | # Set mock and test values. |
| 23 | mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter) |
| 24 | test_chroot = "/usr/local/home/chromeos" |
| 25 | test_build_id = "lumpy-release/R36-5814.0.0" |
| 26 | |
| 27 | downloader = download_images.ImageDownloader(logger_to_use=MOCK_LOGGER, |
| 28 | cmd_exec=mock_cmd_exec) |
| 29 | |
| 30 | # Set os.path.exists to always return False and run downloader |
| 31 | mock_path_exists.return_value = False |
| 32 | downloader._DownloadImage(test_chroot, test_build_id) |
| 33 | |
| 34 | # Verify os.path.exists was called twice, with proper arguments. |
| 35 | self.assertEqual(mock_path_exists.call_count, 2) |
| 36 | mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') |
| 37 | mock_path_exists.assert_any_call('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') |
| 38 | |
| 39 | # Verify we called os.mkdirs |
| 40 | self.assertEqual(mock_mkdirs.call_count, 1) |
| 41 | mock_mkdirs.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') |
| 42 | |
| 43 | # Verify we called ChrootRunCommand once, with proper arguments. |
| 44 | self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 1) |
| 45 | 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') |
| 46 | |
| 47 | # Reset the velues in the mocks; set os.path.exists to always return True. |
| 48 | mock_path_exists.reset_mock() |
| 49 | mock_cmd_exec.reset_mock() |
| 50 | mock_path_exists.return_value = True |
| 51 | |
| 52 | # Run downloader |
| 53 | downloader._DownloadImage(test_chroot, test_build_id) |
| 54 | |
| 55 | # Verify os.path.exists was called twice, with proper arguments. |
| 56 | self.assertEqual(mock_path_exists.call_count, 2) |
| 57 | mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') |
| 58 | mock_path_exists.assert_any_call('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0') |
| 59 | |
| 60 | # Verify we made no RunCommand or ChrootRunCommand calls (since |
| 61 | # os.path.exists returned True, there was no work do be done). |
| 62 | self.assertEqual (mock_cmd_exec.RunCommand.call_count, 0) |
| 63 | self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 0) |
| 64 | |
| 65 | |
| 66 | |
| 67 | @mock.patch.object(os.path, 'exists') |
| 68 | def test_uncompress_image(self, mock_path_exists): |
| 69 | |
| 70 | # set mock and test values. |
| 71 | mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter) |
| 72 | test_chroot = '/usr/local/home/chromeos' |
| 73 | test_build_id = 'lumpy-release/R36-5814.0.0' |
| 74 | |
| 75 | downloader = download_images.ImageDownloader(logger_to_use=MOCK_LOGGER, |
| 76 | cmd_exec=mock_cmd_exec) |
| 77 | |
| 78 | # Set os.path.exists to always return False and run uncompress. |
| 79 | mock_path_exists.return_value = False |
| 80 | downloader._UncompressImage(test_chroot, test_build_id) |
| 81 | |
| 82 | # Verify os.path.exists was called once, with correct arguments. |
| 83 | self.assertEqual (mock_path_exists.call_count, 1) |
| 84 | mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') |
| 85 | |
| 86 | # Verify ChrootRunCommand was called, with correct arguments. |
| 87 | self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 1) |
| 88 | 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') |
| 89 | |
| 90 | # Set os.path.exists to always return False and run uncompress. |
| 91 | mock_path_exists.reset_mock() |
| 92 | mock_cmd_exec.reset_mock() |
| 93 | mock_path_exists.return_value = True |
| 94 | downloader._UncompressImage(test_chroot, test_build_id) |
| 95 | |
| 96 | # Verify os.path.exists was called once, with correct arguments. |
| 97 | self.assertEqual (mock_path_exists.call_count, 1) |
| 98 | mock_path_exists.assert_called_with('/usr/local/home/chromeos/chroot/tmp/lumpy-release/R36-5814.0.0/chromiumos_test_image.bin') |
| 99 | |
| 100 | # Verify ChrootRunCommand was not called. |
| 101 | self.assertEqual (mock_cmd_exec.ChrootRunCommand.call_count, 0) |
| 102 | |
| 103 | |
| 104 | |
| 105 | def test_run(self): |
| 106 | |
| 107 | # Set test arguments |
| 108 | test_chroot = "/usr/local/home/chromeos" |
| 109 | test_build_id = "remote/lumpy/latest-dev" |
| 110 | |
| 111 | # Set values to test/check. |
| 112 | self.called_download_image = False |
| 113 | self.called_uncompress_image = False |
| 114 | self.called_get_build_id = False |
| 115 | |
| 116 | # Define fake stub functions for Run to call |
| 117 | def FakeGetBuildID(unused_root, unused_xbuddy_label): |
| 118 | self.called_get_build_id = True |
| 119 | return 'lumpy-release/R36-5814.0.0' |
| 120 | |
| 121 | def GoodDownloadImage(root, build_id): |
| 122 | self.called_download_image = True |
| 123 | return "chromiumos_test_image.bin" |
| 124 | |
| 125 | def BadDownloadImage(root, build_id): |
| 126 | self.called_download_image = True |
| 127 | return None |
| 128 | |
| 129 | def FakeUncompressImage(root, build_id): |
| 130 | self.called_uncompress_image = True |
| 131 | return 0 |
| 132 | |
| 133 | # Initialize downloader |
| 134 | downloader = download_images.ImageDownloader(logger_to_use=MOCK_LOGGER) |
| 135 | |
| 136 | # Set downloader to call fake stubs. |
| 137 | downloader._GetBuildID = FakeGetBuildID |
| 138 | downloader._UncompressImage = FakeUncompressImage |
| 139 | downloader._DownloadImage = GoodDownloadImage |
| 140 | |
| 141 | # Call Run. |
| 142 | downloader.Run(test_chroot, test_build_id) |
| 143 | |
| 144 | # Make sure it called both _DownloadImage and _UncompressImage |
| 145 | self.assertTrue (self.called_download_image) |
| 146 | self.assertTrue (self.called_uncompress_image) |
| 147 | |
| 148 | # Reset values; Now use fake stub that simulates DownloadImage failing. |
| 149 | self.called_download_image = False |
| 150 | self.called_uncompress_image = False |
| 151 | downloader._DownloadImage = BadDownloadImage |
| 152 | |
| 153 | # Call Run again. |
| 154 | downloader.Run (test_chroot, test_build_id) |
| 155 | |
| 156 | # Verify that UncompressImage was not called, since _DownloadImage "failed" |
| 157 | self.assertTrue (self.called_download_image) |
| 158 | self.assertFalse (self.called_uncompress_image) |
| 159 | |
| 160 | |
| 161 | if __name__ == '__main__': |
| 162 | unittest.main() |