| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The LUCI Authors. All rights reserved. | 2 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed by the Apache v2.0 license that can be | 3 # Use of this source code is governed by the Apache v2.0 license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import datetime | 6 import datetime |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import random | 9 import random |
| 10 import sys | 10 import sys |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 return task_request.make_request(req, True) | 34 return task_request.make_request(req, True) |
| 35 | 35 |
| 36 | 36 |
| 37 def _gen_request(properties=None, **kwargs): | 37 def _gen_request(properties=None, **kwargs): |
| 38 """Creates a TaskRequest.""" | 38 """Creates a TaskRequest.""" |
| 39 properties = properties or {} | 39 properties = properties or {} |
| 40 packages = properties.pop('packages', [{ | 40 packages = properties.pop('packages', [{ |
| 41 'package_name': 'rm', | 41 'package_name': 'rm', |
| 42 'version': PINNED_PACKAGE_VERSION, | 42 'version': PINNED_PACKAGE_VERSION, |
| 43 }]) | 43 }]) |
| 44 inputs_ref = properties.pop('inputs_ref', { |
| 45 'isolatedserver': 'https://isolateserver.appspot.com', |
| 46 'namespace': 'default-gzip', |
| 47 }) |
| 44 props = { | 48 props = { |
| 45 'command': [u'command1', u'arg1'], | 49 'command': [u'command1', u'arg1'], |
| 46 'packages': [task_request.CipdPackage(**p) for p in packages], | 50 'packages': [task_request.CipdPackage(**p) for p in packages], |
| 47 'dimensions': { | 51 'dimensions': { |
| 48 u'OS': u'Windows-3.1.1', | 52 u'OS': u'Windows-3.1.1', |
| 49 u'hostname': u'localhost', | 53 u'hostname': u'localhost', |
| 50 u'pool': u'default', | 54 u'pool': u'default', |
| 51 }, | 55 }, |
| 52 'env': {u'foo': u'bar', u'joe': u'2'}, | 56 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 53 'execution_timeout_secs': 30, | 57 'execution_timeout_secs': 30, |
| 54 'grace_period_secs': 30, | 58 'grace_period_secs': 30, |
| 55 'idempotent': False, | 59 'idempotent': False, |
| 60 'inputs_ref': inputs_ref, |
| 56 'io_timeout_secs': None, | 61 'io_timeout_secs': None, |
| 57 } | 62 } |
| 58 props.update(properties) | 63 props.update(properties) |
| 59 now = utils.utcnow() | 64 now = utils.utcnow() |
| 60 args = { | 65 args = { |
| 61 'created_ts': now, | 66 'created_ts': now, |
| 62 'name': 'Request name', | 67 'name': 'Request name', |
| 63 'priority': 50, | 68 'priority': 50, |
| 64 'properties': task_request.TaskProperties(**props), | 69 'properties': task_request.TaskProperties(**props), |
| 65 'expiration_ts': now + datetime.timedelta(seconds=30), | 70 'expiration_ts': now + datetime.timedelta(seconds=30), |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 'dimensions': { | 204 'dimensions': { |
| 200 u'OS': u'Windows-3.1.1', | 205 u'OS': u'Windows-3.1.1', |
| 201 u'hostname': u'localhost', | 206 u'hostname': u'localhost', |
| 202 u'pool': u'default', | 207 u'pool': u'default', |
| 203 }, | 208 }, |
| 204 'env': {u'foo': u'bar', u'joe': u'2'}, | 209 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 205 'extra_args': [], | 210 'extra_args': [], |
| 206 'execution_timeout_secs': 30, | 211 'execution_timeout_secs': 30, |
| 207 'grace_period_secs': 30, | 212 'grace_period_secs': 30, |
| 208 'idempotent': True, | 213 'idempotent': True, |
| 209 'inputs_ref': None, | 214 'inputs_ref': { |
| 215 'isolated': None, |
| 216 'isolatedserver': 'https://isolateserver.appspot.com', |
| 217 'namespace': 'default-gzip', |
| 218 }, |
| 210 'io_timeout_secs': None, | 219 'io_timeout_secs': None, |
| 211 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], | 220 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], |
| 212 } | 221 } |
| 213 expected_request = { | 222 expected_request = { |
| 214 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, | 223 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, |
| 215 'name': u'Request name', | 224 'name': u'Request name', |
| 216 'parent_task_id': unicode(parent_id), | 225 'parent_task_id': unicode(parent_id), |
| 217 'priority': 49, | 226 'priority': 49, |
| 218 'properties': expected_properties, | 227 'properties': expected_properties, |
| 219 # Intentionally hard code the hash value since it has to be deterministic. | 228 # Intentionally hard code the hash value since it has to be deterministic. |
| 220 # Other unit tests should use the calculated value. | 229 # Other unit tests should use the calculated value. |
| 221 'properties_hash': '83b350298f05eff6072d54d2c6f031d06cc30449', | 230 'properties_hash': 'c3e067b4e232be5478e7147bb1f0506477444014', |
| 222 'pubsub_topic': None, | 231 'pubsub_topic': None, |
| 223 'pubsub_userdata': None, | 232 'pubsub_userdata': None, |
| 224 'tags': [ | 233 'tags': [ |
| 225 u'OS:Windows-3.1.1', | 234 u'OS:Windows-3.1.1', |
| 226 u'hostname:localhost', | 235 u'hostname:localhost', |
| 227 u'pool:default', | 236 u'pool:default', |
| 228 u'priority:49', | 237 u'priority:49', |
| 229 u'tag:1', | 238 u'tag:1', |
| 230 u'user:Jesus', | 239 u'user:Jesus', |
| 231 ], | 240 ], |
| (...skipping 23 matching lines...) Expand all Loading... |
| 255 'dimensions': { | 264 'dimensions': { |
| 256 u'OS': u'Windows-3.1.1', | 265 u'OS': u'Windows-3.1.1', |
| 257 u'hostname': u'localhost', | 266 u'hostname': u'localhost', |
| 258 u'pool': u'default', | 267 u'pool': u'default', |
| 259 }, | 268 }, |
| 260 'env': {u'foo': u'bar', u'joe': u'2'}, | 269 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 261 'extra_args': [], | 270 'extra_args': [], |
| 262 'execution_timeout_secs': 30, | 271 'execution_timeout_secs': 30, |
| 263 'grace_period_secs': 30, | 272 'grace_period_secs': 30, |
| 264 'idempotent': True, | 273 'idempotent': True, |
| 265 'inputs_ref': None, | 274 'inputs_ref': { |
| 275 'isolated': None, |
| 276 'isolatedserver': 'https://isolateserver.appspot.com', |
| 277 'namespace': 'default-gzip', |
| 278 }, |
| 266 'io_timeout_secs': None, | 279 'io_timeout_secs': None, |
| 267 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], | 280 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], |
| 268 } | 281 } |
| 269 expected_request = { | 282 expected_request = { |
| 270 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, | 283 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, |
| 271 'name': u'Request name', | 284 'name': u'Request name', |
| 272 'parent_task_id': unicode(parent_id), | 285 'parent_task_id': unicode(parent_id), |
| 273 'priority': 49, | 286 'priority': 49, |
| 274 'properties': expected_properties, | 287 'properties': expected_properties, |
| 275 # Intentionally hard code the hash value since it has to be deterministic. | 288 # Intentionally hard code the hash value since it has to be deterministic. |
| 276 # Other unit tests should use the calculated value. | 289 # Other unit tests should use the calculated value. |
| 277 'properties_hash': '83b350298f05eff6072d54d2c6f031d06cc30449', | 290 'properties_hash': 'c3e067b4e232be5478e7147bb1f0506477444014', |
| 278 'pubsub_topic': None, | 291 'pubsub_topic': None, |
| 279 'pubsub_userdata': None, | 292 'pubsub_userdata': None, |
| 280 'tags': [ | 293 'tags': [ |
| 281 u'OS:Windows-3.1.1', | 294 u'OS:Windows-3.1.1', |
| 282 u'hostname:localhost', | 295 u'hostname:localhost', |
| 283 u'pool:default', | 296 u'pool:default', |
| 284 u'priority:49', | 297 u'priority:49', |
| 285 u'tag:1', | 298 u'tag:1', |
| 286 u'user:Jesus', | 299 u'user:Jesus', |
| 287 ], | 300 ], |
| (...skipping 19 matching lines...) Expand all Loading... |
| 307 _gen_request(parent_task_id='1d69b9f088008810') | 320 _gen_request(parent_task_id='1d69b9f088008810') |
| 308 | 321 |
| 309 def test_make_request_idempotent(self): | 322 def test_make_request_idempotent(self): |
| 310 request = mkreq(_gen_request(properties=dict(idempotent=True))) | 323 request = mkreq(_gen_request(properties=dict(idempotent=True))) |
| 311 as_dict = request.to_dict() | 324 as_dict = request.to_dict() |
| 312 self.assertEqual(True, as_dict['properties']['idempotent']) | 325 self.assertEqual(True, as_dict['properties']['idempotent']) |
| 313 # Intentionally hard code the hash value since it has to be deterministic. | 326 # Intentionally hard code the hash value since it has to be deterministic. |
| 314 # Other unit tests should use the calculated value. | 327 # Other unit tests should use the calculated value. |
| 315 # Ensure the algorithm is deterministic. | 328 # Ensure the algorithm is deterministic. |
| 316 self.assertEqual( | 329 self.assertEqual( |
| 317 '83b350298f05eff6072d54d2c6f031d06cc30449', as_dict['properties_hash']) | 330 'c3e067b4e232be5478e7147bb1f0506477444014', as_dict['properties_hash']) |
| 318 | 331 |
| 319 def test_duped(self): | 332 def test_duped(self): |
| 320 # Two TestRequest with the same properties. | 333 # Two TestRequest with the same properties. |
| 321 request_1 = mkreq(_gen_request(properties=dict(idempotent=True))) | 334 request_1 = mkreq(_gen_request(properties=dict(idempotent=True))) |
| 322 now = utils.utcnow() | 335 now = utils.utcnow() |
| 323 request_2 = mkreq(_gen_request( | 336 request_2 = mkreq(_gen_request( |
| 324 name='Other', | 337 name='Other', |
| 325 user='Other', | 338 user='Other', |
| 326 priority=201, | 339 priority=201, |
| 327 created_ts=now, | 340 created_ts=now, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 created_ts=now, | 435 created_ts=now, |
| 423 expiration_ts= | 436 expiration_ts= |
| 424 now+datetime.timedelta(seconds=task_request._MIN_TIMEOUT_SECS))) | 437 now+datetime.timedelta(seconds=task_request._MIN_TIMEOUT_SECS))) |
| 425 mkreq(_gen_request( | 438 mkreq(_gen_request( |
| 426 created_ts=now, | 439 created_ts=now, |
| 427 expiration_ts= | 440 expiration_ts= |
| 428 now + datetime.timedelta(seconds=task_request._SEVEN_DAYS_SECS))) | 441 now + datetime.timedelta(seconds=task_request._SEVEN_DAYS_SECS))) |
| 429 | 442 |
| 430 # Try with isolated/isolatedserver/namespace. | 443 # Try with isolated/isolatedserver/namespace. |
| 431 with self.assertRaises(datastore_errors.BadValueError): | 444 with self.assertRaises(datastore_errors.BadValueError): |
| 445 # Both command and inputs_ref.isolated. |
| 432 mkreq(_gen_request(properties=dict( | 446 mkreq(_gen_request(properties=dict( |
| 433 command=['see', 'spot', 'run'], | 447 command=['see', 'spot', 'run'], |
| 434 inputs_ref=task_request.FilesRef()))) | 448 inputs_ref=task_request.FilesRef( |
| 449 isolated='deadbeef', |
| 450 isolatedserver='http://localhost:1', |
| 451 namespace='default-gzip')))) |
| 452 with self.assertRaises(datastore_errors.BadValueError): |
| 453 # inputs_ref without server/namespace. |
| 454 mkreq(_gen_request(properties=dict(inputs_ref=task_request.FilesRef()))) |
| 455 mkreq(_gen_request(properties=dict( |
| 456 command=[], |
| 457 inputs_ref=task_request.FilesRef( |
| 458 isolated='deadbeefdeadbeefdeadbeefdeadbeefdeadbeef', |
| 459 isolatedserver='http://localhost:1', |
| 460 namespace='default-gzip')))) |
| 435 | 461 |
| 436 def test_make_request_clone(self): | 462 def test_make_request_clone(self): |
| 437 # Compare with test_make_request(). | 463 # Compare with test_make_request(). |
| 438 parent = mkreq(_gen_request()) | 464 parent = mkreq(_gen_request()) |
| 439 # Hack: Would need to know about TaskResultSummary. | 465 # Hack: Would need to know about TaskResultSummary. |
| 440 parent_id = task_pack.pack_request_key(parent.key) + '1' | 466 parent_id = task_pack.pack_request_key(parent.key) + '1' |
| 441 data = _gen_request( | 467 data = _gen_request( |
| 442 properties=dict(idempotent=True), parent_task_id=parent_id) | 468 properties=dict(idempotent=True), parent_task_id=parent_id) |
| 443 request = task_request.make_request_clone(mkreq(data)) | 469 request = task_request.make_request_clone(mkreq(data)) |
| 444 # Differences from make_request() are: | 470 # Differences from make_request() are: |
| 445 # - idempotent was reset to False. | 471 # - idempotent was reset to False. |
| 446 # - parent_task_id was reset to None. | 472 # - parent_task_id was reset to None. |
| 447 expected_properties = { | 473 expected_properties = { |
| 448 'command': [u'command1', u'arg1'], | 474 'command': [u'command1', u'arg1'], |
| 449 'dimensions': { | 475 'dimensions': { |
| 450 u'OS': u'Windows-3.1.1', | 476 u'OS': u'Windows-3.1.1', |
| 451 u'hostname': u'localhost', | 477 u'hostname': u'localhost', |
| 452 u'pool': u'default', | 478 u'pool': u'default', |
| 453 }, | 479 }, |
| 454 'env': {u'foo': u'bar', u'joe': u'2'}, | 480 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 455 'execution_timeout_secs': 30, | 481 'execution_timeout_secs': 30, |
| 456 'extra_args': [], | 482 'extra_args': [], |
| 457 'grace_period_secs': 30, | 483 'grace_period_secs': 30, |
| 458 'idempotent': False, | 484 'idempotent': False, |
| 459 'inputs_ref': None, | 485 'inputs_ref': { |
| 486 'isolated': None, |
| 487 'isolatedserver': 'https://isolateserver.appspot.com', |
| 488 'namespace': 'default-gzip', |
| 489 }, |
| 460 'io_timeout_secs': None, | 490 'io_timeout_secs': None, |
| 461 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], | 491 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], |
| 462 } | 492 } |
| 463 # Differences from make_request() are: | 493 # Differences from make_request() are: |
| 464 # - parent_task_id was reset to None. | 494 # - parent_task_id was reset to None. |
| 465 # - tag 'user:' was replaced | 495 # - tag 'user:' was replaced |
| 466 # - user was replaced. | 496 # - user was replaced. |
| 467 expected_request = { | 497 expected_request = { |
| 468 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, | 498 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, |
| 469 'name': u'Request name (Retry #1)', | 499 'name': u'Request name (Retry #1)', |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 ndb.Key(task_request.TaskRequest, 0x7f14acec2fcfffff), | 556 ndb.Key(task_request.TaskRequest, 0x7f14acec2fcfffff), |
| 527 task_request.request_id_to_key(0xeb5313d0300000)) | 557 task_request.request_id_to_key(0xeb5313d0300000)) |
| 528 | 558 |
| 529 | 559 |
| 530 if __name__ == '__main__': | 560 if __name__ == '__main__': |
| 531 if '-v' in sys.argv: | 561 if '-v' in sys.argv: |
| 532 unittest.TestCase.maxDiff = None | 562 unittest.TestCase.maxDiff = None |
| 533 logging.basicConfig( | 563 logging.basicConfig( |
| 534 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 564 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 535 unittest.main() | 565 unittest.main() |
| OLD | NEW |