| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 '''Unittests for update.py. | 6 '''Unittests for update.py. |
| 7 | 7 |
| 8 They set up a temporary directory that is used to mock a bucket, the directory | 8 They set up a temporary directory that is used to mock a bucket, the directory |
| 9 containing the configuration files and the android sdk directory. | 9 containing the configuration files and the android sdk directory. |
| 10 | 10 |
| 11 Tests run the script with various inputs and check the status of the filesystem | 11 Tests run the script with various inputs and check the status of the filesystem |
| 12 ''' | 12 ''' |
| 13 | 13 |
| 14 import contextlib |
| 15 import logging |
| 16 import os |
| 14 import shutil | 17 import shutil |
| 18 import sys |
| 15 import tempfile | 19 import tempfile |
| 16 import unittest | 20 import unittest |
| 17 import os | |
| 18 import sys | |
| 19 import zipfile | 21 import zipfile |
| 20 import contextlib | |
| 21 | 22 |
| 22 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) | 23 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 23 from play_services import update | 24 from play_services import update |
| 25 import devil_chromium # pylint: disable=import-error,unused-import |
| 26 from devil.utils import cmd_helper |
| 24 | 27 |
| 25 | 28 |
| 26 class TestFunctions(unittest.TestCase): | 29 class TestFunctions(unittest.TestCase): |
| 27 DEFAULT_CONFIG_VERSION = 42 | 30 DEFAULT_CONFIG_VERSION = '1.2.3' |
| 28 DEFAULT_LICENSE = 'Default License' | 31 DEFAULT_LICENSE = 'Default License' |
| 29 DEFAULT_ZIP_SHA1 = 'zip0and0filling0to0forty0chars0000000000' | 32 DEFAULT_ZIP_SHA1 = 'zip0and0filling0to0forty0chars0000000000' |
| 30 | 33 |
| 31 def __init__(self, *args, **kwargs): | 34 def __init__(self, *args, **kwargs): |
| 32 super(TestFunctions, self).__init__(*args, **kwargs) | 35 super(TestFunctions, self).__init__(*args, **kwargs) |
| 33 self.paths = None # Initialized in SetUpWorkdir | 36 self.paths = None # Initialized in SetUpWorkdir |
| 34 self.workdir = None # Initialized in setUp | 37 self.workdir = None # Initialized in setUp |
| 35 | 38 |
| 36 #override | 39 #override |
| 37 def setUp(self): | 40 def setUp(self): |
| 38 self.workdir = tempfile.mkdtemp() | 41 self.workdir = tempfile.mkdtemp() |
| 39 | 42 |
| 40 #override | 43 #override |
| 41 def tearDown(self): | 44 def tearDown(self): |
| 42 shutil.rmtree(self.workdir) | 45 shutil.rmtree(self.workdir) |
| 43 self.workdir = None | 46 self.workdir = None |
| 44 | 47 |
| 45 def testUpload(self): | 48 def testUpload(self): |
| 46 version = 1337 | 49 version = '2.3.4' |
| 47 self.SetUpWorkdir( | 50 self.SetUpWorkdir( |
| 48 xml_version=version, | |
| 49 gms_lib=True, | 51 gms_lib=True, |
| 52 config_version=version, |
| 50 source_prop=True) | 53 source_prop=True) |
| 51 | 54 |
| 52 status = update.main([ | 55 status = update.main([ |
| 53 'upload', | 56 'upload', |
| 54 '--dry-run', | 57 '--dry-run', |
| 55 '--skip-git', | 58 '--skip-git', |
| 56 '--bucket', self.paths.bucket, | 59 '--bucket', self.paths.bucket, |
| 57 '--config', self.paths.config_file, | 60 '--config', self.paths.config_file, |
| 58 '--sdk-root', self.paths.sdk_root | 61 '--sdk-root', self.paths.gms.sdk_root |
| 59 ]) | 62 ]) |
| 60 self.assertEqual(status, 0, 'the command should have succeeded.') | 63 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 61 | 64 |
| 62 # bucket should contain license, name = license.sha1 | 65 # bucket should contain license, name = content from LICENSE.sha1 |
| 63 self.assertTrue(os.path.isfile(self.paths.config_license_sha1)) | 66 self.assertTrue(os.path.isfile(self.paths.config_license_sha1)) |
| 64 license_sha1 = _GetFileContent(self.paths.config_license_sha1) | 67 license_sha1 = _GetFileContent(self.paths.config_license_sha1) |
| 65 bucket_license = os.path.join(self.paths.bucket, str(version), | 68 bucket_license = os.path.join(self.paths.bucket, version, license_sha1) |
| 66 license_sha1) | |
| 67 self.assertTrue(os.path.isfile(bucket_license)) | 69 self.assertTrue(os.path.isfile(bucket_license)) |
| 68 self.assertEqual(_GetFileContent(bucket_license), self.DEFAULT_LICENSE) | 70 self.assertEqual(_GetFileContent(bucket_license), self.DEFAULT_LICENSE) |
| 69 | 71 |
| 70 # bucket should contain zip, name = zip.sha1 | 72 # bucket should contain zip, name = content from zip.sha1 |
| 71 self.assertTrue(os.path.isfile(self.paths.config_zip_sha1)) | 73 self.assertTrue(os.path.isfile(self.paths.config_zip_sha1)) |
| 72 bucket_zip = os.path.join(self.paths.bucket, str(version), | 74 bucket_zip = os.path.join(self.paths.bucket, str(version), |
| 73 _GetFileContent(self.paths.config_zip_sha1)) | 75 _GetFileContent(self.paths.config_zip_sha1)) |
| 74 self.assertTrue(os.path.isfile(bucket_zip)) | 76 self.assertTrue(os.path.isfile(bucket_zip)) |
| 75 | 77 |
| 76 # unzip, should contain expected files | 78 # unzip, should contain expected files |
| 77 with zipfile.ZipFile(bucket_zip, "r") as bucket_zip_file: | 79 with zipfile.ZipFile(bucket_zip, "r") as bucket_zip_file: |
| 78 self.assertEqual(bucket_zip_file.namelist(), | 80 self.assertEqual(bucket_zip_file.namelist(), |
| 79 ['dummy_file', 'res/values/version.xml']) | 81 ['com/google/android/gms/client/2.3.4/client-2.3.4.aar']) |
| 80 | |
| 81 def testUploadAlreadyLatestVersion(self): | |
| 82 self.SetUpWorkdir( | |
| 83 xml_version=self.DEFAULT_CONFIG_VERSION, | |
| 84 gms_lib=True, | |
| 85 source_prop=True) | |
| 86 | |
| 87 status = update.main([ | |
| 88 'upload', | |
| 89 '--dry-run', | |
| 90 '--skip-git', | |
| 91 '--bucket', self.paths.bucket, | |
| 92 '--config', self.paths.config_file, | |
| 93 '--sdk-root', self.paths.sdk_root, | |
| 94 ]) | |
| 95 self.assertEqual(status, 0, 'the command should have succeeded.') | |
| 96 | |
| 97 # bucket should be empty | |
| 98 self.assertFalse(os.listdir(self.paths.bucket)) | |
| 99 self.assertFalse(os.path.isfile(self.paths.config_license_sha1)) | |
| 100 self.assertFalse(os.path.isfile(self.paths.config_zip_sha1)) | |
| 101 | 82 |
| 102 def testDownload(self): | 83 def testDownload(self): |
| 103 self.SetUpWorkdir(populate_bucket=True) | 84 self.SetUpWorkdir(populate_bucket=True) |
| 104 | 85 |
| 105 with _MockedInput('y'): | 86 with _MockedInput('y'): |
| 106 status = update.main([ | 87 status = update.main([ |
| 107 'download', | 88 'download', |
| 108 '--dry-run', | 89 '--dry-run', |
| 109 '--bucket', self.paths.bucket, | 90 '--bucket', self.paths.bucket, |
| 110 '--config', self.paths.config_file, | 91 '--config', self.paths.config_file, |
| 111 '--sdk-root', self.paths.sdk_root, | 92 '--sdk-root', self.paths.gms.sdk_root, |
| 112 ]) | 93 ]) |
| 113 | 94 |
| 114 self.assertEqual(status, 0, 'the command should have succeeded.') | 95 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 115 | 96 |
| 116 # sdk_root should contain zip contents, zip sha1, license | 97 # sdk_root should contain zip contents, zip sha1, license |
| 117 self.assertTrue(os.path.isfile(os.path.join(self.paths.gms_lib, | 98 self.assertTrue(os.path.isfile(self.paths.gms.client_paths[0])) |
| 118 'dummy_file'))) | 99 self.assertTrue(os.path.isfile(self.paths.gms.lib_zip_sha1)) |
| 119 self.assertTrue(os.path.isfile(self.paths.gms_root_sha1)) | 100 self.assertTrue(os.path.isfile(self.paths.gms.license)) |
| 120 self.assertTrue(os.path.isfile(self.paths.gms_root_license)) | 101 self.assertEquals(_GetFileContent(self.paths.gms.license), |
| 121 self.assertEquals(_GetFileContent(self.paths.gms_root_license), | |
| 122 self.DEFAULT_LICENSE) | 102 self.DEFAULT_LICENSE) |
| 123 | 103 |
| 124 def testDownloadBot(self): | 104 def testDownloadBot(self): |
| 125 self.SetUpWorkdir(populate_bucket=True, bot_env=True) | 105 self.SetUpWorkdir(populate_bucket=True, bot_env=True) |
| 126 | 106 |
| 127 # No need to type 'y' on bots | 107 # No need to type 'y' on bots |
| 128 status = update.main([ | 108 status = update.main([ |
| 129 'download', | 109 'download', |
| 130 '--dry-run', | 110 '--dry-run', |
| 131 '--bucket', self.paths.bucket, | 111 '--bucket', self.paths.bucket, |
| 132 '--config', self.paths.config_file, | 112 '--config', self.paths.config_file, |
| 133 '--sdk-root', self.paths.sdk_root, | 113 '--sdk-root', self.paths.gms.sdk_root, |
| 134 ]) | 114 ]) |
| 135 | 115 |
| 136 self.assertEqual(status, 0, 'the command should have succeeded.') | 116 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 137 | 117 |
| 138 # sdk_root should contain zip contents, zip sha1, license | 118 # sdk_root should contain zip contents, zip sha1, license |
| 139 self.assertTrue(os.path.isfile(os.path.join(self.paths.gms_lib, | 119 self.assertTrue(os.path.isfile(self.paths.gms.client_paths[0])) |
| 140 'dummy_file'))) | 120 self.assertTrue(os.path.isfile(self.paths.gms.lib_zip_sha1)) |
| 141 self.assertTrue(os.path.isfile(self.paths.gms_root_sha1)) | 121 self.assertTrue(os.path.isfile(self.paths.gms.license)) |
| 142 self.assertTrue(os.path.isfile(self.paths.gms_root_license)) | 122 self.assertEquals(_GetFileContent(self.paths.gms.license), |
| 143 self.assertEquals(_GetFileContent(self.paths.gms_root_license), | |
| 144 self.DEFAULT_LICENSE) | 123 self.DEFAULT_LICENSE) |
| 145 | 124 |
| 146 def testDownloadAlreadyUpToDate(self): | 125 def testDownloadAlreadyUpToDate(self): |
| 147 self.SetUpWorkdir( | 126 self.SetUpWorkdir( |
| 148 populate_bucket=True, | 127 populate_bucket=True, |
| 149 existing_zip_sha1=self.DEFAULT_ZIP_SHA1) | 128 existing_zip_sha1=self.DEFAULT_ZIP_SHA1) |
| 150 | 129 |
| 151 status = update.main([ | 130 status = update.main([ |
| 152 'download', | 131 'download', |
| 153 '--dry-run', | 132 '--dry-run', |
| 154 '--bucket', self.paths.bucket, | 133 '--bucket', self.paths.bucket, |
| 155 '--config', self.paths.config_file, | 134 '--config', self.paths.config_file, |
| 156 '--sdk-root', self.paths.sdk_root, | 135 '--sdk-root', self.paths.gms.sdk_root, |
| 157 ]) | 136 ]) |
| 158 | 137 |
| 159 self.assertEqual(status, 0, 'the command should have succeeded.') | 138 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 160 | 139 |
| 161 # there should not be new files downloaded to sdk_root | 140 # there should not be new files downloaded to sdk_root |
| 162 self.assertFalse(os.path.isfile(os.path.join(self.paths.gms_lib, | 141 self.assertFalse(os.path.isfile(os.path.join(self.paths.gms.client_paths[0], |
| 163 'dummy_file'))) | 142 'dummy_file'))) |
| 164 self.assertFalse(os.path.isfile(self.paths.gms_root_license)) | 143 self.assertFalse(os.path.isfile(self.paths.gms.license)) |
| 165 | 144 |
| 166 def testDownloadAcceptedLicense(self): | 145 def testDownloadAcceptedLicense(self): |
| 167 self.SetUpWorkdir( | 146 self.SetUpWorkdir( |
| 168 populate_bucket=True, | 147 populate_bucket=True, |
| 169 existing_license=self.DEFAULT_LICENSE) | 148 existing_license=self.DEFAULT_LICENSE) |
| 170 | 149 |
| 171 # License already accepted, no need to type | 150 # License already accepted, no need to type |
| 172 status = update.main([ | 151 status = update.main([ |
| 173 'download', | 152 'download', |
| 174 '--dry-run', | 153 '--dry-run', |
| 175 '--bucket', self.paths.bucket, | 154 '--bucket', self.paths.bucket, |
| 176 '--config', self.paths.config_file, | 155 '--config', self.paths.config_file, |
| 177 '--sdk-root', self.paths.sdk_root, | 156 '--sdk-root', self.paths.gms.sdk_root, |
| 178 ]) | 157 ]) |
| 179 | 158 |
| 180 self.assertEqual(status, 0, 'the command should have succeeded.') | 159 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 181 | 160 |
| 182 # sdk_root should contain zip contents, zip sha1, license | 161 # sdk_root should contain zip contents, zip sha1, license |
| 183 self.assertTrue(os.path.isfile(os.path.join(self.paths.gms_lib, | 162 self.assertTrue(os.path.isfile(self.paths.gms.client_paths[0])) |
| 184 'dummy_file'))) | 163 self.assertTrue(os.path.isfile(self.paths.gms.lib_zip_sha1)) |
| 185 self.assertTrue(os.path.isfile(self.paths.gms_root_sha1)) | 164 self.assertTrue(os.path.isfile(self.paths.gms.license)) |
| 186 self.assertTrue(os.path.isfile(self.paths.gms_root_license)) | 165 self.assertEquals(_GetFileContent(self.paths.gms.license), |
| 187 self.assertEquals(_GetFileContent(self.paths.gms_root_license), | |
| 188 self.DEFAULT_LICENSE) | 166 self.DEFAULT_LICENSE) |
| 189 | 167 |
| 190 def testDownloadNewLicense(self): | 168 def testDownloadNewLicense(self): |
| 191 self.SetUpWorkdir( | 169 self.SetUpWorkdir( |
| 192 populate_bucket=True, | 170 populate_bucket=True, |
| 193 existing_license='Old license') | 171 existing_license='Old license') |
| 194 | 172 |
| 195 with _MockedInput('y'): | 173 with _MockedInput('y'): |
| 196 status = update.main([ | 174 status = update.main([ |
| 197 'download', | 175 'download', |
| 198 '--dry-run', | 176 '--dry-run', |
| 199 '--bucket', self.paths.bucket, | 177 '--bucket', self.paths.bucket, |
| 200 '--config', self.paths.config_file, | 178 '--config', self.paths.config_file, |
| 201 '--sdk-root', self.paths.sdk_root, | 179 '--sdk-root', self.paths.gms.sdk_root, |
| 202 ]) | 180 ]) |
| 203 | 181 |
| 204 self.assertEqual(status, 0, 'the command should have succeeded.') | 182 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 205 | 183 |
| 206 # sdk_root should contain zip contents, zip sha1, NEW license | 184 # sdk_root should contain zip contents, zip sha1, NEW license |
| 207 self.assertTrue(os.path.isfile(os.path.join(self.paths.gms_lib, | 185 self.assertTrue(os.path.isfile(self.paths.gms.client_paths[0])) |
| 208 'dummy_file'))) | 186 self.assertTrue(os.path.isfile(self.paths.gms.lib_zip_sha1)) |
| 209 self.assertTrue(os.path.isfile(self.paths.gms_root_sha1)) | 187 self.assertTrue(os.path.isfile(self.paths.gms.license)) |
| 210 self.assertTrue(os.path.isfile(self.paths.gms_root_license)) | 188 self.assertEquals(_GetFileContent(self.paths.gms.license), |
| 211 self.assertEquals(_GetFileContent(self.paths.gms_root_license), | |
| 212 self.DEFAULT_LICENSE) | 189 self.DEFAULT_LICENSE) |
| 213 | 190 |
| 214 def testDownloadRefusedLicense(self): | 191 def testDownloadRefusedLicense(self): |
| 215 self.SetUpWorkdir( | 192 self.SetUpWorkdir( |
| 216 populate_bucket=True, | 193 populate_bucket=True, |
| 217 existing_license='Old license') | 194 existing_license='Old license') |
| 218 | 195 |
| 219 with _MockedInput('n'): | 196 with _MockedInput('n'): |
| 220 status = update.main([ | 197 status = update.main([ |
| 221 'download', | 198 'download', |
| 222 '--dry-run', | 199 '--dry-run', |
| 223 '--bucket', self.paths.bucket, | 200 '--bucket', self.paths.bucket, |
| 224 '--config', self.paths.config_file, | 201 '--config', self.paths.config_file, |
| 225 '--sdk-root', self.paths.sdk_root, | 202 '--sdk-root', self.paths.gms.sdk_root, |
| 226 ]) | 203 ]) |
| 227 | 204 |
| 228 self.assertEqual(status, 0, 'the command should have succeeded.') | 205 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 229 | 206 |
| 230 # there should not be new files downloaded to sdk_root | 207 # there should not be new files downloaded to sdk_root |
| 231 self.assertFalse(os.path.isfile(os.path.join(self.paths.gms_lib, | 208 self.assertFalse(os.path.isfile(os.path.join(self.paths.gms.client_paths[0], |
| 232 'dummy_file'))) | 209 'dummy_file'))) |
| 233 self.assertEquals(_GetFileContent(self.paths.gms_root_license), | 210 self.assertEquals(_GetFileContent(self.paths.gms.license), |
| 234 'Old license') | 211 'Old license') |
| 235 | 212 |
| 236 def testDownloadNoAndroidSDK(self): | 213 def testDownloadNoAndroidSDK(self): |
| 237 self.SetUpWorkdir( | 214 self.SetUpWorkdir( |
| 238 populate_bucket=True, | 215 populate_bucket=True, |
| 239 existing_license='Old license') | 216 existing_license='Old license') |
| 240 | 217 |
| 241 non_existing_sdk_root = os.path.join(self.workdir, 'non_existing_sdk_root') | 218 non_existing_sdk_root = os.path.join(self.workdir, 'non_existing_sdk_root') |
| 242 # Should not run, no typing needed | 219 # Should not run, no typing needed |
| 243 status = update.main([ | 220 status = update.main([ |
| 244 'download', | 221 'download', |
| 245 '--dry-run', | 222 '--dry-run', |
| 246 '--bucket', self.paths.bucket, | 223 '--bucket', self.paths.bucket, |
| 247 '--config', self.paths.config_file, | 224 '--config', self.paths.config_file, |
| 248 '--sdk-root', non_existing_sdk_root, | 225 '--sdk-root', non_existing_sdk_root, |
| 249 ]) | 226 ]) |
| 250 | 227 |
| 251 self.assertEqual(status, 0, 'the command should have succeeded.') | 228 self.assertEqual(status, 0, 'the command should have succeeded.') |
| 252 self.assertFalse(os.path.isdir(non_existing_sdk_root)) | 229 self.assertFalse(os.path.isdir(non_existing_sdk_root)) |
| 253 | 230 |
| 254 def SetUpWorkdir(self, | 231 def SetUpWorkdir(self, |
| 255 bot_env=False, | 232 bot_env=False, |
| 256 config_version=DEFAULT_CONFIG_VERSION, | 233 config_version=DEFAULT_CONFIG_VERSION, |
| 257 existing_license=None, | 234 existing_license=None, |
| 258 existing_zip_sha1=None, | 235 existing_zip_sha1=None, |
| 259 gms_lib=False, | 236 gms_lib=False, |
| 260 populate_bucket=False, | 237 populate_bucket=False, |
| 261 source_prop=None, | 238 source_prop=None): |
| 262 xml_version=None): | |
| 263 '''Prepares workdir by putting it in the specified state | 239 '''Prepares workdir by putting it in the specified state |
| 264 | 240 |
| 265 Args: | 241 Args: |
| 266 - general | 242 - general |
| 267 bot_env: sets or unsets CHROME_HEADLESS | 243 bot_env: sets or unsets CHROME_HEADLESS |
| 268 | 244 |
| 269 - bucket | 245 - bucket |
| 270 populate_bucket: boolean. Populate the bucket with a zip and license | 246 populate_bucket: boolean. Populate the bucket with a zip and license |
| 271 file. The sha1s will be copied to the config directory | 247 file. The sha1s will be copied to the config directory |
| 272 | 248 |
| 273 - config | 249 - config |
| 274 config_version: number. Version of the current SDK. Defaults to | 250 config_version: number. Version of the current SDK. Defaults to |
| 275 `self.DEFAULT_CONFIG_VERSION` | 251 `self.DEFAULT_CONFIG_VERSION` |
| 276 | 252 |
| 277 - sdk_root | 253 - sdk_root |
| 278 existing_license: string. Create a LICENSE file setting the specified | 254 existing_license: string. Create a LICENSE file setting the specified |
| 279 text as content of the currently accepted license. | 255 text as content of the currently accepted license. |
| 280 existing_zip_sha1: string. Create a sha1 file setting the specified | 256 existing_zip_sha1: string. Create a sha1 file setting the specified |
| 281 hash as hash of the SDK supposed to be installed | 257 hash as hash of the SDK supposed to be installed |
| 282 gms_lib: boolean. Create a dummy file in the location of the play | 258 gms_lib: boolean. Create a dummy file in the location of the play |
| 283 services SDK. | 259 services SDK. |
| 284 source_prop: boolean. Create a source.properties file that contains | 260 source_prop: boolean. Create a source.properties file that contains |
| 285 the license to upload. | 261 the license to upload. |
| 286 xml_version: number. Create a version.xml file with the specified | |
| 287 version that is used when uploading | |
| 288 ''' | 262 ''' |
| 289 self.paths = Paths(self.workdir) | 263 client_name = 'client' |
| 264 self.paths = Paths(self.workdir, config_version, [client_name]) |
| 290 | 265 |
| 291 # Create the main directories | 266 # Create the main directories |
| 292 _MakeDirs(self.paths.sdk_root) | 267 _MakeDirs(self.paths.gms.sdk_root) |
| 293 _MakeDirs(self.paths.config_dir) | 268 _MakeDirs(self.paths.config_dir) |
| 294 _MakeDirs(self.paths.bucket) | 269 _MakeDirs(self.paths.bucket) |
| 295 | 270 |
| 296 # is not configured via argument. | 271 # is not configured via argument. |
| 297 update.SHA1_DIRECTORY = self.paths.config_dir | 272 update.SHA1_DIRECTORY = self.paths.config_dir |
| 298 | 273 |
| 299 os.environ['CHROME_HEADLESS'] = '1' if bot_env else '' | 274 os.environ['CHROME_HEADLESS'] = '1' if bot_env else '' |
| 300 | 275 |
| 301 if config_version: | 276 if config_version: |
| 302 _MakeDirs(os.path.dirname(self.paths.config_file)) | 277 _MakeDirs(os.path.dirname(self.paths.config_file)) |
| 303 with open(self.paths.config_file, 'w') as stream: | 278 with open(self.paths.config_file, 'w') as stream: |
| 304 stream.write(('{"version_number":%d,' | 279 stream.write(('{"clients": ["%s"],' |
| 305 '"version_xml_path": "res/values/version.xml"}' | 280 '"version_number": "%s"}' |
| 306 '\n') % config_version) | 281 '\n') % (client_name, config_version)) |
| 307 | 282 |
| 308 if existing_license: | 283 if existing_license: |
| 309 _MakeDirs(self.paths.gms_root) | 284 _MakeDirs(self.paths.gms.package) |
| 310 with open(self.paths.gms_root_license, 'w') as stream: | 285 with open(self.paths.gms.license, 'w') as stream: |
| 311 stream.write(existing_license) | 286 stream.write(existing_license) |
| 312 | 287 |
| 313 if existing_zip_sha1: | 288 if existing_zip_sha1: |
| 314 _MakeDirs(self.paths.gms_root) | 289 _MakeDirs(self.paths.gms.package) |
| 315 with open(self.paths.gms_root_sha1, 'w') as stream: | 290 with open(self.paths.gms.lib_zip_sha1, 'w') as stream: |
| 316 stream.write(existing_zip_sha1) | 291 stream.write(existing_zip_sha1) |
| 317 | 292 |
| 318 if gms_lib: | 293 if gms_lib: |
| 319 _MakeDirs(self.paths.gms_lib) | 294 _MakeDirs(os.path.dirname(self.paths.gms.client_paths[0])) |
| 320 with open(os.path.join(self.paths.gms_lib, 'dummy_file'), 'w') as stream: | 295 with open(self.paths.gms.client_paths[0], 'w') as stream: |
| 321 stream.write('foo\n') | 296 stream.write('foo\n') |
| 322 | 297 |
| 323 if source_prop: | 298 if source_prop: |
| 324 _MakeDirs(os.path.dirname(self.paths.source_prop)) | 299 _MakeDirs(os.path.dirname(self.paths.gms.source_prop)) |
| 325 with open(self.paths.source_prop, 'w') as stream: | 300 with open(self.paths.gms.source_prop, 'w') as stream: |
| 326 stream.write('Foo=Bar\n' | 301 stream.write('Foo=Bar\n' |
| 327 'Pkg.License=%s\n' | 302 'Pkg.License=%s\n' |
| 328 'Baz=Fizz\n' % self.DEFAULT_LICENSE) | 303 'Baz=Fizz\n' % self.DEFAULT_LICENSE) |
| 329 | 304 |
| 330 if populate_bucket: | 305 if populate_bucket: |
| 331 _MakeDirs(self.paths.config_dir) | 306 _MakeDirs(self.paths.config_dir) |
| 332 bucket_dir = os.path.join(self.paths.bucket, str(config_version)) | 307 bucket_dir = os.path.join(self.paths.bucket, str(config_version)) |
| 333 _MakeDirs(bucket_dir) | 308 _MakeDirs(bucket_dir) |
| 334 | 309 |
| 335 # TODO(dgn) should we use real sha1s? comparison with the real sha1 is | 310 # TODO(dgn) should we use real sha1s? comparison with the real sha1 is |
| 336 # done but does not do anything other than displaying a message. | 311 # done but does not do anything other than displaying a message. |
| 337 config_license_sha1 = 'license0and0filling0to0forty0chars000000' | 312 config_license_sha1 = 'license0and0filling0to0forty0chars000000' |
| 338 with open(self.paths.config_license_sha1, 'w') as stream: | 313 with open(self.paths.config_license_sha1, 'w') as stream: |
| 339 stream.write(config_license_sha1) | 314 stream.write(config_license_sha1) |
| 340 | 315 |
| 341 with open(os.path.join(bucket_dir, config_license_sha1), 'w') as stream: | 316 with open(os.path.join(bucket_dir, config_license_sha1), 'w') as stream: |
| 342 stream.write(self.DEFAULT_LICENSE) | 317 stream.write(self.DEFAULT_LICENSE) |
| 343 | 318 |
| 344 config_zip_sha1 = self.DEFAULT_ZIP_SHA1 | 319 config_zip_sha1 = self.DEFAULT_ZIP_SHA1 |
| 345 with open(self.paths.config_zip_sha1, 'w') as stream: | 320 with open(self.paths.config_zip_sha1, 'w') as stream: |
| 346 stream.write(config_zip_sha1) | 321 stream.write(config_zip_sha1) |
| 347 | 322 |
| 348 pre_zip_lib = os.path.join(self.workdir, 'pre_zip_lib') | 323 pre_zip_client = os.path.join( |
| 324 self.workdir, |
| 325 'pre_zip_lib', |
| 326 os.path.relpath(self.paths.gms.client_paths[0], |
| 327 self.paths.gms.package)) |
| 328 pre_zip_lib = os.path.dirname(pre_zip_client) |
| 349 post_zip_lib = os.path.join(bucket_dir, config_zip_sha1) | 329 post_zip_lib = os.path.join(bucket_dir, config_zip_sha1) |
| 330 print(pre_zip_lib, post_zip_lib) |
| 350 _MakeDirs(pre_zip_lib) | 331 _MakeDirs(pre_zip_lib) |
| 351 with open(os.path.join(pre_zip_lib, 'dummy_file'), 'w') as stream: | 332 with open(pre_zip_client, 'w') as stream: |
| 352 stream.write('foo\n') | 333 stream.write('foo\n') |
| 353 shutil.make_archive(post_zip_lib, 'zip', pre_zip_lib) | |
| 354 # make_archive appends .zip | |
| 355 shutil.move(post_zip_lib + '.zip', post_zip_lib) | |
| 356 | 334 |
| 357 if xml_version: | 335 # pylint: disable=protected-access |
| 358 _MakeDirs(os.path.dirname(self.paths.xml_version)) | 336 update._ZipLibrary(post_zip_lib, [pre_zip_client], os.path.join( |
| 359 with open(self.paths.xml_version, 'w') as stream: | 337 self.workdir, 'pre_zip_lib')) |
| 360 stream.write( | 338 |
| 361 '<?xml version="1.0" encoding="utf-8"?>\n' | 339 if logging.getLogger().isEnabledFor(logging.DEBUG): |
| 362 '<resources>\n' | 340 self._PrintWorkdir() |
| 363 ' <integer name="google_play_services_version">%d</integer>\n' | 341 |
| 364 '</resources>\n' % xml_version) | 342 def _PrintWorkdir(self): |
| 343 cmd_helper.Call(['tree', self.workdir]) |
| 365 | 344 |
| 366 | 345 |
| 367 class Paths(object): | 346 class Paths(object): |
| 368 '''Declaration of the paths commonly manipulated in the tests.''' | 347 '''Declaration of the paths commonly manipulated in the tests.''' |
| 369 | 348 |
| 370 def __init__(self, workdir): | 349 def __init__(self, workdir, version, clients): |
| 371 self.bucket = os.path.join(workdir, 'bucket') | 350 self.bucket = os.path.join(workdir, 'bucket') |
| 372 | 351 |
| 373 self.config_dir = os.path.join(workdir, 'config') | 352 self.config_dir = os.path.join(workdir, 'config') |
| 374 self.config_file = os.path.join(self.config_dir, 'config.json') | 353 self.config_file = os.path.join(self.config_dir, 'config.json') |
| 375 self.config_license_sha1 = os.path.join(self.config_dir, 'LICENSE.sha1') | 354 self.config_license_sha1 = os.path.join(self.config_dir, 'LICENSE.sha1') |
| 376 self.config_zip_sha1 = os.path.join( | 355 self.config_zip_sha1 = os.path.join( |
| 377 self.config_dir, | 356 self.config_dir, |
| 378 'google_play_services_library.zip.sha1') | 357 'google_play_services_library.zip.sha1') |
| 379 | 358 self.gms = update.PlayServicesPaths(os.path.join(workdir, 'sdk_root'), |
| 380 self.sdk_root = os.path.join(workdir, 'sdk_root') | 359 version, clients) |
| 381 self.gms_root = os.path.join(self.sdk_root, 'extras', 'google', | |
| 382 'google_play_services') | |
| 383 self.gms_root_sha1 = os.path.join(self.gms_root, | |
| 384 'google_play_services_library.zip.sha1') | |
| 385 self.gms_root_license = os.path.join(self.gms_root, 'LICENSE') | |
| 386 self.source_prop = os.path.join(self.gms_root, 'source.properties') | |
| 387 self.gms_lib = os.path.join(self.gms_root, 'libproject', | |
| 388 'google-play-services_lib') | |
| 389 self.xml_version = os.path.join(self.gms_lib, 'res', 'values', | |
| 390 'version.xml') | |
| 391 | 360 |
| 392 | 361 |
| 393 def _GetFileContent(file_path): | 362 def _GetFileContent(file_path): |
| 394 with open(file_path, 'r') as stream: | 363 with open(file_path, 'r') as stream: |
| 395 return stream.read() | 364 return stream.read() |
| 396 | 365 |
| 397 | 366 |
| 398 def _MakeDirs(path): | 367 def _MakeDirs(path): |
| 399 '''Avoids having to do the error handling everywhere.''' | 368 '''Avoids having to do the error handling everywhere.''' |
| 400 if not os.path.exists(path): | 369 if not os.path.exists(path): |
| 401 os.makedirs(path) | 370 os.makedirs(path) |
| 402 | 371 |
| 403 | 372 |
| 404 @contextlib.contextmanager | 373 @contextlib.contextmanager |
| 405 def _MockedInput(typed_string): | 374 def _MockedInput(typed_string): |
| 406 '''Makes raw_input return |typed_string| while inside the context.''' | 375 '''Makes raw_input return |typed_string| while inside the context.''' |
| 407 try: | 376 try: |
| 408 original_raw_input = __builtins__.raw_input | 377 original_raw_input = __builtins__.raw_input |
| 409 __builtins__.raw_input = lambda _: typed_string | 378 __builtins__.raw_input = lambda _: typed_string |
| 410 yield | 379 yield |
| 411 finally: | 380 finally: |
| 412 __builtins__.raw_input = original_raw_input | 381 __builtins__.raw_input = original_raw_input |
| 413 | 382 |
| 414 | 383 |
| 415 if __name__ == '__main__': | 384 if __name__ == '__main__': |
| 416 unittest.main() | 385 # For debugging tests, just run the file with a specific test name. |
| 386 if len(sys.argv) > 1: |
| 387 logging.basicConfig(level=logging.DEBUG) |
| 388 suite = unittest.TestSuite() |
| 389 suite.addTest(TestFunctions(sys.argv[1])) |
| 390 unittest.TextTestRunner().run(suite) |
| 391 else: |
| 392 unittest.main() |
| OLD | NEW |