Chromium Code Reviews| 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 |
| 11 import unittest | 11 import unittest |
| 12 | 12 |
| 13 import test_env | 13 import test_env |
| 14 test_env.setup_test_env() | 14 test_env.setup_test_env() |
| 15 | 15 |
| 16 from google.appengine.api import datastore_errors | 16 from google.appengine.api import datastore_errors |
| 17 from google.appengine.ext import ndb | 17 from google.appengine.ext import ndb |
| 18 | 18 |
| 19 from components import auth_testing | 19 from components import auth_testing |
| 20 from components import utils | 20 from components import utils |
| 21 from test_support import test_case | 21 from test_support import test_case |
| 22 | 22 |
| 23 from server import task_pack | 23 from server import task_pack |
| 24 from server import task_request | 24 from server import task_request |
| 25 | 25 |
| 26 | 26 |
| 27 # pylint: disable=W0212 | 27 # pylint: disable=W0212 |
| 28 | 28 |
| 29 | 29 |
| 30 PINNED_PACKAGE_VERSION = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' | |
| 31 | |
| 32 | |
| 30 def _gen_request(properties=None, **kwargs): | 33 def _gen_request(properties=None, **kwargs): |
| 31 """Creates a TaskRequest.""" | 34 """Creates a TaskRequest.""" |
| 35 properties = properties or {} | |
| 36 packages = properties.pop('packages', [{ | |
| 37 'package_name': 'rm', | |
| 38 'version': PINNED_PACKAGE_VERSION, | |
| 39 }]) | |
| 32 props = { | 40 props = { |
| 33 'command': [u'command1', u'arg1'], | 41 'command': [u'command1', u'arg1'], |
| 42 'packages': [task_request.CipdPackage(**p) for p in packages], | |
| 34 'dimensions': { | 43 'dimensions': { |
| 35 u'OS': u'Windows-3.1.1', | 44 u'OS': u'Windows-3.1.1', |
| 36 u'hostname': u'localhost', | 45 u'hostname': u'localhost', |
| 37 u'pool': u'default', | 46 u'pool': u'default', |
| 38 }, | 47 }, |
| 39 'env': {u'foo': u'bar', u'joe': u'2'}, | 48 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 40 'execution_timeout_secs': 30, | 49 'execution_timeout_secs': 30, |
| 41 'grace_period_secs': 30, | 50 'grace_period_secs': 30, |
| 42 'idempotent': False, | 51 'idempotent': False, |
| 43 'io_timeout_secs': None, | 52 'io_timeout_secs': None, |
| 44 } | 53 } |
| 45 props.update(properties or {}) | 54 props.update(properties) |
| 46 now = utils.utcnow() | 55 now = utils.utcnow() |
| 47 args = { | 56 args = { |
| 48 'created_ts': now, | 57 'created_ts': now, |
| 49 'name': 'Request name', | 58 'name': 'Request name', |
| 50 'priority': 50, | 59 'priority': 50, |
| 51 'properties': task_request.TaskProperties(**props), | 60 'properties': task_request.TaskProperties(**props), |
| 52 'expiration_ts': now + datetime.timedelta(seconds=30), | 61 'expiration_ts': now + datetime.timedelta(seconds=30), |
| 53 'tags': [u'tag:1'], | 62 'tags': [u'tag:1'], |
| 54 'user': 'Jesus', | 63 'user': 'Jesus', |
| 55 } | 64 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 with self.assertRaises(ValueError): | 178 with self.assertRaises(ValueError): |
| 170 task_request.validate_request_key(ndb.Key('TaskRequest', 1)) | 179 task_request.validate_request_key(ndb.Key('TaskRequest', 1)) |
| 171 with self.assertRaises(ValueError): | 180 with self.assertRaises(ValueError): |
| 172 key = ndb.Key( | 181 key = ndb.Key( |
| 173 'TaskRequestShard', 'a' * (task_pack.DEPRECATED_SHARDING_LEVEL + 1), | 182 'TaskRequestShard', 'a' * (task_pack.DEPRECATED_SHARDING_LEVEL + 1), |
| 174 'TaskRequest', 0x100) | 183 'TaskRequest', 0x100) |
| 175 task_request.validate_request_key(key) | 184 task_request.validate_request_key(key) |
| 176 | 185 |
| 177 def test_make_request(self): | 186 def test_make_request(self): |
| 178 # Compare with test_make_request_clone(). | 187 # Compare with test_make_request_clone(). |
| 179 parent = task_request.make_request(_gen_request(), True) | 188 parent = mkreq(_gen_request()) |
| 180 # Hack: Would need to know about TaskResultSummary. | 189 # Hack: Would need to know about TaskResultSummary. |
| 181 parent_id = task_pack.pack_request_key(parent.key) + '1' | 190 parent_id = task_pack.pack_request_key(parent.key) + '1' |
| 182 r = _gen_request( | 191 r = _gen_request(properties=dict(idempotent=True), parent_task_id=parent_id) |
| 183 properties=dict(idempotent=True), parent_task_id=parent_id) | 192 request = mkreq(r) |
| 184 request = task_request.make_request(r, True) | |
| 185 expected_properties = { | 193 expected_properties = { |
| 186 'command': [u'command1', u'arg1'], | 194 'command': [u'command1', u'arg1'], |
| 187 'dimensions': { | 195 'dimensions': { |
| 188 u'OS': u'Windows-3.1.1', | 196 u'OS': u'Windows-3.1.1', |
| 189 u'hostname': u'localhost', | 197 u'hostname': u'localhost', |
| 190 u'pool': u'default', | 198 u'pool': u'default', |
| 191 }, | 199 }, |
| 192 'env': {u'foo': u'bar', u'joe': u'2'}, | 200 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 193 'extra_args': [], | 201 'extra_args': [], |
| 194 'execution_timeout_secs': 30, | 202 'execution_timeout_secs': 30, |
| 195 'grace_period_secs': 30, | 203 'grace_period_secs': 30, |
| 196 'idempotent': True, | 204 'idempotent': True, |
| 197 'inputs_ref': None, | 205 'inputs_ref': None, |
| 198 'io_timeout_secs': None, | 206 'io_timeout_secs': None, |
| 207 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], | |
| 199 } | 208 } |
| 200 expected_request = { | 209 expected_request = { |
| 201 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, | 210 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, |
| 202 'name': u'Request name', | 211 'name': u'Request name', |
| 203 'parent_task_id': unicode(parent_id), | 212 'parent_task_id': unicode(parent_id), |
| 204 'priority': 49, | 213 'priority': 49, |
| 205 'properties': expected_properties, | 214 'properties': expected_properties, |
| 206 # Intentionally hard code the hash value since it has to be deterministic. | 215 # Intentionally hard code the hash value since it has to be deterministic. |
| 207 # Other unit tests should use the calculated value. | 216 # Other unit tests should use the calculated value. |
| 208 'properties_hash': 'a406d174c469cb2ef6f56a49c3df0de0ae3db369', | 217 'properties_hash': '83b350298f05eff6072d54d2c6f031d06cc30449', |
| 209 'pubsub_topic': None, | 218 'pubsub_topic': None, |
| 210 'pubsub_userdata': None, | 219 'pubsub_userdata': None, |
| 211 'tags': [ | 220 'tags': [ |
| 212 u'OS:Windows-3.1.1', | 221 u'OS:Windows-3.1.1', |
| 213 u'hostname:localhost', | 222 u'hostname:localhost', |
| 214 u'pool:default', | 223 u'pool:default', |
| 215 u'priority:49', | 224 u'priority:49', |
| 216 u'tag:1', | 225 u'tag:1', |
| 217 u'user:Jesus', | 226 u'user:Jesus', |
| 218 ], | 227 ], |
| 219 'user': u'Jesus', | 228 'user': u'Jesus', |
| 220 } | 229 } |
| 221 actual = request.to_dict() | 230 actual = request.to_dict() |
| 222 actual.pop('created_ts') | 231 actual.pop('created_ts') |
| 223 actual.pop('expiration_ts') | 232 actual.pop('expiration_ts') |
| 224 self.assertEqual(expected_request, actual) | 233 self.assertEqual(expected_request, actual) |
| 225 self.assertEqual(30, request.expiration_secs) | 234 self.assertEqual(30, request.expiration_secs) |
| 226 | 235 |
| 227 def test_make_request_isolated(self): | 236 def test_make_request_isolated(self): |
| 228 parent = task_request.make_request( | 237 parent = mkreq(_gen_request(properties={ |
| 229 _gen_request( | 238 'command': [], |
| 230 properties={ | 239 'inputs_ref': { |
| 231 'command': [], | 240 'isolated': '0123456789012345678901234567890123456789', |
| 232 'inputs_ref': { | 241 'isolatedserver': 'http://localhost:1', |
| 233 'isolated': '0123456789012345678901234567890123456789', | 242 'namespace': 'default-gzip', |
| 234 'isolatedserver': 'http://localhost:1', | 243 }, |
| 235 'namespace': 'default-gzip', | 244 })) |
| 236 }, | |
| 237 }), | |
| 238 True) | |
| 239 # Hack: Would need to know about TaskResultSummary. | 245 # Hack: Would need to know about TaskResultSummary. |
| 240 parent_id = task_pack.pack_request_key(parent.key) + '1' | 246 parent_id = task_pack.pack_request_key(parent.key) + '1' |
| 241 request = task_request.make_request( | 247 request = mkreq(_gen_request( |
| 242 _gen_request(properties={'idempotent':True}, parent_task_id=parent_id), | 248 properties={'idempotent':True}, parent_task_id=parent_id)) |
| 243 True) | |
| 244 expected_properties = { | 249 expected_properties = { |
| 245 'command': [u'command1', u'arg1'], | 250 'command': [u'command1', u'arg1'], |
| 246 'dimensions': { | 251 'dimensions': { |
| 247 u'OS': u'Windows-3.1.1', | 252 u'OS': u'Windows-3.1.1', |
| 248 u'hostname': u'localhost', | 253 u'hostname': u'localhost', |
| 249 u'pool': u'default', | 254 u'pool': u'default', |
| 250 }, | 255 }, |
| 251 'env': {u'foo': u'bar', u'joe': u'2'}, | 256 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 252 'extra_args': [], | 257 'extra_args': [], |
| 253 'execution_timeout_secs': 30, | 258 'execution_timeout_secs': 30, |
| 254 'grace_period_secs': 30, | 259 'grace_period_secs': 30, |
| 255 'idempotent': True, | 260 'idempotent': True, |
| 256 'inputs_ref': None, | 261 'inputs_ref': None, |
| 257 'io_timeout_secs': None, | 262 'io_timeout_secs': None, |
| 263 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], | |
| 258 } | 264 } |
| 259 expected_request = { | 265 expected_request = { |
| 260 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, | 266 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, |
| 261 'name': u'Request name', | 267 'name': u'Request name', |
| 262 'parent_task_id': unicode(parent_id), | 268 'parent_task_id': unicode(parent_id), |
| 263 'priority': 49, | 269 'priority': 49, |
| 264 'properties': expected_properties, | 270 'properties': expected_properties, |
| 265 # Intentionally hard code the hash value since it has to be deterministic. | 271 # Intentionally hard code the hash value since it has to be deterministic. |
| 266 # Other unit tests should use the calculated value. | 272 # Other unit tests should use the calculated value. |
| 267 'properties_hash': 'a406d174c469cb2ef6f56a49c3df0de0ae3db369', | 273 'properties_hash': '83b350298f05eff6072d54d2c6f031d06cc30449', |
| 268 'pubsub_topic': None, | 274 'pubsub_topic': None, |
| 269 'pubsub_userdata': None, | 275 'pubsub_userdata': None, |
| 270 'tags': [ | 276 'tags': [ |
| 271 u'OS:Windows-3.1.1', | 277 u'OS:Windows-3.1.1', |
| 272 u'hostname:localhost', | 278 u'hostname:localhost', |
| 273 u'pool:default', | 279 u'pool:default', |
| 274 u'priority:49', | 280 u'priority:49', |
| 275 u'tag:1', | 281 u'tag:1', |
| 276 u'user:Jesus', | 282 u'user:Jesus', |
| 277 ], | 283 ], |
| 278 'user': u'Jesus', | 284 'user': u'Jesus', |
| 279 } | 285 } |
| 280 actual = request.to_dict() | 286 actual = request.to_dict() |
| 281 # expiration_ts - created_ts == scheduling_expiration_secs. | 287 # expiration_ts - created_ts == scheduling_expiration_secs. |
| 282 actual.pop('created_ts') | 288 actual.pop('created_ts') |
| 283 actual.pop('expiration_ts') | 289 actual.pop('expiration_ts') |
| 284 self.assertEqual(expected_request, actual) | 290 self.assertEqual(expected_request, actual) |
| 285 self.assertEqual(30, request.expiration_secs) | 291 self.assertEqual(30, request.expiration_secs) |
| 286 | 292 |
| 287 def test_make_request_parent(self): | 293 def test_make_request_parent(self): |
| 288 parent = task_request.make_request(_gen_request(), True) | 294 parent = mkreq(_gen_request()) |
| 289 # Hack: Would need to know about TaskResultSummary. | 295 # Hack: Would need to know about TaskResultSummary. |
| 290 parent_id = task_pack.pack_request_key(parent.key) + '1' | 296 parent_id = task_pack.pack_request_key(parent.key) + '1' |
| 291 child = task_request.make_request( | 297 child = mkreq(_gen_request(parent_task_id=parent_id)) |
| 292 _gen_request(parent_task_id=parent_id), True) | |
| 293 self.assertEqual(parent_id, child.parent_task_id) | 298 self.assertEqual(parent_id, child.parent_task_id) |
| 294 | 299 |
| 295 def test_make_request_invalid_parent_id(self): | 300 def test_make_request_invalid_parent_id(self): |
| 296 # Must ends with '1' or '2', not '0' | 301 # Must ends with '1' or '2', not '0' |
| 297 with self.assertRaises(ValueError): | 302 with self.assertRaises(ValueError): |
| 298 _gen_request(parent_task_id='1d69b9f088008810') | 303 _gen_request(parent_task_id='1d69b9f088008810') |
| 299 | 304 |
| 300 def test_make_request_idempotent(self): | 305 def test_make_request_idempotent(self): |
| 301 request = task_request.make_request( | 306 request = mkreq(_gen_request(properties=dict(idempotent=True))) |
| 302 _gen_request(properties=dict(idempotent=True)), True) | |
| 303 as_dict = request.to_dict() | 307 as_dict = request.to_dict() |
| 304 self.assertEqual(True, as_dict['properties']['idempotent']) | 308 self.assertEqual(True, as_dict['properties']['idempotent']) |
| 305 # Intentionally hard code the hash value since it has to be deterministic. | 309 # Intentionally hard code the hash value since it has to be deterministic. |
| 306 # Other unit tests should use the calculated value. | 310 # Other unit tests should use the calculated value. |
| 307 # Ensure the algorithm is deterministic. | 311 # Ensure the algorithm is deterministic. |
| 308 self.assertEqual( | 312 self.assertEqual( |
| 309 'a406d174c469cb2ef6f56a49c3df0de0ae3db369', as_dict['properties_hash']) | 313 '83b350298f05eff6072d54d2c6f031d06cc30449', as_dict['properties_hash']) |
| 310 | 314 |
| 311 def test_duped(self): | 315 def test_duped(self): |
| 312 # Two TestRequest with the same properties. | 316 # Two TestRequest with the same properties. |
| 313 request_1 = task_request.make_request( | 317 request_1 = mkreq(_gen_request(properties=dict(idempotent=True))) |
| 314 _gen_request(properties=dict(idempotent=True)), True) | |
| 315 now = utils.utcnow() | 318 now = utils.utcnow() |
| 316 request_2 = task_request.make_request( | 319 request_2 = mkreq(_gen_request( |
| 317 _gen_request( | 320 name='Other', |
| 318 name='Other', | 321 user='Other', |
| 319 user='Other', | 322 priority=201, |
| 320 priority=201, | 323 created_ts=now, |
| 321 created_ts=now, | 324 expiration_ts=now + datetime.timedelta(seconds=129), |
| 322 expiration_ts=now + datetime.timedelta(seconds=129), | 325 tags=['tag:2'], |
| 323 tags=['tag:2'], | 326 properties=dict(idempotent=True))) |
| 324 properties=dict(idempotent=True)), | |
| 325 True) | |
| 326 self.assertEqual( | 327 self.assertEqual( |
| 327 request_1.properties.properties_hash, | 328 request_1.properties.properties_hash, |
| 328 request_2.properties.properties_hash) | 329 request_2.properties.properties_hash) |
| 329 self.assertTrue(request_1.properties.properties_hash) | 330 self.assertTrue(request_1.properties.properties_hash) |
| 330 | 331 |
| 331 def test_different(self): | 332 def test_different(self): |
| 332 # Two TestRequest with different properties. | 333 # Two TestRequest with different properties. |
| 333 request_1 = task_request.make_request( | 334 request_1 = mkreq(_gen_request( |
| 334 _gen_request( | 335 properties=dict(execution_timeout_secs=30, idempotent=True))) |
| 335 properties=dict(execution_timeout_secs=30, idempotent=True)), True) | 336 request_2 = mkreq(_gen_request( |
| 336 request_2 = task_request.make_request( | 337 properties=dict(execution_timeout_secs=129, idempotent=True))) |
| 337 _gen_request( | |
| 338 properties=dict(execution_timeout_secs=129, idempotent=True)), True) | |
| 339 self.assertNotEqual( | 338 self.assertNotEqual( |
| 340 request_1.properties.properties_hash, | 339 request_1.properties.properties_hash, |
| 341 request_2.properties.properties_hash) | 340 request_2.properties.properties_hash) |
| 342 | 341 |
| 343 def test_bad_values(self): | 342 def test_bad_values(self): |
| 344 with self.assertRaises(AssertionError): | 343 with self.assertRaises(AssertionError): |
| 345 task_request.make_request(None, True) | 344 mkreq(None) |
| 346 with self.assertRaises(AssertionError): | 345 with self.assertRaises(AssertionError): |
| 347 task_request.make_request({}, True) | 346 mkreq({}) |
| 348 with self.assertRaises(AttributeError): | 347 with self.assertRaises(AttributeError): |
| 349 task_request.make_request(_gen_request(properties={'foo': 'bar'}), True) | 348 mkreq(_gen_request(properties={'foo': 'bar'})) |
| 350 task_request.make_request(_gen_request(), True) | 349 mkreq(_gen_request()) |
| 351 | 350 |
| 352 with self.assertRaises(datastore_errors.BadValueError): | 351 with self.assertRaises(datastore_errors.BadValueError): |
| 353 task_request.make_request( | 352 mkreq(_gen_request(properties=dict(command=[]))) |
| 354 _gen_request(properties=dict(command=[])), True) | |
| 355 with self.assertRaises(datastore_errors.BadValueError): | 353 with self.assertRaises(datastore_errors.BadValueError): |
| 356 task_request.make_request( | 354 mkreq(_gen_request(properties=dict(command={'a': 'b'}))) |
| 357 _gen_request(properties=dict(command={'a': 'b'})), True) | |
| 358 with self.assertRaises(datastore_errors.BadValueError): | 355 with self.assertRaises(datastore_errors.BadValueError): |
| 359 task_request.make_request( | 356 mkreq(_gen_request(properties=dict(command='python'))) |
| 360 _gen_request(properties=dict(command='python')), True) | 357 mkreq(_gen_request(properties=dict(command=['python']))) |
| 361 task_request.make_request( | 358 mkreq(_gen_request(properties=dict(command=[u'python']))) |
| 362 _gen_request(properties=dict(command=['python'])), True) | 359 |
| 363 task_request.make_request( | 360 with self.assertRaises(datastore_errors.BadValueError): |
| 364 _gen_request(properties=dict(command=[u'python'])), True) | 361 mkreq(_gen_request(properties=dict( |
| 362 command=[u'python'], | |
| 363 packages=[{}]))) | |
| 364 | |
| 365 with self.assertRaises(datastore_errors.BadValueError): | |
| 366 mkreq(_gen_request(properties=dict(packages=[dict(package_name='rm')]))) | |
| 367 with self.assertRaises(datastore_errors.BadValueError): | |
| 368 mkreq(_gen_request(properties=dict( | |
| 369 packages=[{'package_name': 'infra|rm', 'version': 'latest'}]))) | |
| 370 mkreq(_gen_request(properties=dict( | |
| 371 packages=[{'package_name': 'rm', 'version': 'latest'}]))) | |
| 365 | 372 |
| 366 with self.assertRaises(TypeError): | 373 with self.assertRaises(TypeError): |
| 367 task_request.make_request( | 374 mkreq(_gen_request(properties=dict(dimensions=[]))) |
| 368 _gen_request(properties=dict(dimensions=[])), True) | |
| 369 with self.assertRaises(datastore_errors.BadValueError): | 375 with self.assertRaises(datastore_errors.BadValueError): |
| 370 task_request.make_request( | 376 mkreq(_gen_request(properties=dict(dimensions={}))) |
| 371 _gen_request(properties=dict(dimensions={})), True) | |
| 372 with self.assertRaises(datastore_errors.BadValueError): | 377 with self.assertRaises(datastore_errors.BadValueError): |
| 373 task_request.make_request( | 378 mkreq(_gen_request( |
| 374 _gen_request( | 379 properties=dict(dimensions={u'id': u'b', u'a:': u'b'}))) |
| 375 properties=dict(dimensions={u'id': u'b', u'a:': u'b'})), True) | 380 mkreq(_gen_request( |
| 376 task_request.make_request( | 381 properties=dict(dimensions={u'id': u'b', u'a.': u'b'}))) |
| 377 _gen_request( | |
| 378 properties=dict(dimensions={u'id': u'b', u'a.': u'b'})), True) | |
| 379 | 382 |
| 380 with self.assertRaises(TypeError): | 383 with self.assertRaises(TypeError): |
| 381 task_request.make_request( | 384 mkreq(_gen_request(properties=dict(env=[]))) |
| 382 _gen_request(properties=dict(env=[])), True) | |
| 383 with self.assertRaises(TypeError): | 385 with self.assertRaises(TypeError): |
| 384 task_request.make_request( | 386 mkreq(_gen_request(properties=dict(env={u'a': 1}))) |
| 385 _gen_request(properties=dict(env={u'a': 1})), True) | 387 mkreq(_gen_request(properties=dict(env={}))) |
| 386 task_request.make_request(_gen_request(properties=dict(env={})), True) | |
| 387 | 388 |
| 388 with self.assertRaises(datastore_errors.BadValueError): | 389 with self.assertRaises(datastore_errors.BadValueError): |
| 389 task_request.make_request( | 390 mkreq(_gen_request(priority=task_request.MAXIMUM_PRIORITY+1)) |
| 390 _gen_request(priority=task_request.MAXIMUM_PRIORITY+1), True) | 391 mkreq(_gen_request(priority=task_request.MAXIMUM_PRIORITY)) |
| 391 task_request.make_request( | |
| 392 _gen_request(priority=task_request.MAXIMUM_PRIORITY), True) | |
| 393 | 392 |
| 394 with self.assertRaises(datastore_errors.BadValueError): | 393 with self.assertRaises(datastore_errors.BadValueError): |
| 395 task_request.make_request( | 394 mkreq(_gen_request( |
| 396 _gen_request( | 395 properties=dict(execution_timeout_secs=task_request._ONE_DAY_SECS+1))) |
| 397 properties=dict( | 396 mkreq(_gen_request( |
| 398 execution_timeout_secs=task_request._ONE_DAY_SECS+1)), | 397 properties=dict(execution_timeout_secs=task_request._ONE_DAY_SECS))) |
| 399 True) | |
| 400 task_request.make_request( | |
| 401 _gen_request( | |
| 402 properties=dict(execution_timeout_secs=task_request._ONE_DAY_SECS)), | |
| 403 True) | |
| 404 | 398 |
| 405 now = utils.utcnow() | 399 now = utils.utcnow() |
| 406 with self.assertRaises(datastore_errors.BadValueError): | 400 with self.assertRaises(datastore_errors.BadValueError): |
| 407 task_request.make_request( | 401 mkreq(_gen_request( |
| 408 _gen_request( | 402 created_ts=now, |
| 409 created_ts=now, | 403 expiration_ts=now + datetime.timedelta( |
| 410 expiration_ts= | 404 seconds=task_request._MIN_TIMEOUT_SECS-1))) |
| 411 now+datetime.timedelta( | |
| 412 seconds=task_request._MIN_TIMEOUT_SECS-1)), | |
| 413 True) | |
| 414 with self.assertRaises(datastore_errors.BadValueError): | 405 with self.assertRaises(datastore_errors.BadValueError): |
| 415 task_request.make_request( | 406 mkreq(_gen_request( |
| 416 _gen_request( | 407 created_ts=now, |
| 417 created_ts=now, | 408 expiration_ts= |
| 418 expiration_ts= | 409 now+datetime.timedelta(seconds=task_request._SEVEN_DAYS_SECS+1))) |
| 419 now+datetime.timedelta( | 410 mkreq(_gen_request( |
| 420 seconds=task_request._SEVEN_DAYS_SECS+1)), | 411 created_ts=now, |
| 421 True) | 412 expiration_ts= |
| 422 task_request.make_request( | 413 now+datetime.timedelta(seconds=task_request._MIN_TIMEOUT_SECS))) |
| 423 _gen_request( | 414 mkreq(_gen_request( |
| 424 created_ts=now, | 415 created_ts=now, |
| 425 expiration_ts= | 416 expiration_ts= |
| 426 now+datetime.timedelta( | 417 now + datetime.timedelta(seconds=task_request._SEVEN_DAYS_SECS))) |
| 427 seconds=task_request._MIN_TIMEOUT_SECS)), | |
| 428 True) | |
| 429 task_request.make_request( | |
| 430 _gen_request( | |
| 431 created_ts=now, | |
| 432 expiration_ts= | |
| 433 now+datetime.timedelta( | |
| 434 seconds=task_request._SEVEN_DAYS_SECS)), | |
| 435 True) | |
| 436 | 418 |
| 437 # Try with isolated/isolatedserver/namespace. | 419 # Try with isolated/isolatedserver/namespace. |
| 438 with self.assertRaises(datastore_errors.BadValueError): | 420 with self.assertRaises(datastore_errors.BadValueError): |
| 439 task_request.make_request( | 421 mkreq(_gen_request(properties=dict( |
| 440 _gen_request(properties=dict( | 422 command=['see', 'spot', 'run'], |
| 441 command=['see', 'spot', 'run'], | 423 inputs_ref=task_request.FilesRef()))) |
| 442 inputs_ref=task_request.FilesRef())), | |
| 443 True) | |
| 444 | 424 |
| 445 def test_make_request_clone(self): | 425 def test_make_request_clone(self): |
| 446 # Compare with test_make_request(). | 426 # Compare with test_make_request(). |
| 447 parent = task_request.make_request(_gen_request(), True) | 427 parent = mkreq(_gen_request()) |
| 448 # Hack: Would need to know about TaskResultSummary. | 428 # Hack: Would need to know about TaskResultSummary. |
| 449 parent_id = task_pack.pack_request_key(parent.key) + '1' | 429 parent_id = task_pack.pack_request_key(parent.key) + '1' |
| 450 data = _gen_request( | 430 data = _gen_request( |
| 451 properties=dict(idempotent=True), parent_task_id=parent_id) | 431 properties=dict(idempotent=True), parent_task_id=parent_id) |
| 452 request = task_request.make_request_clone( | 432 request = task_request.make_request_clone(mkreq(data)) |
| 453 task_request.make_request(data, True)) | |
| 454 # Differences from make_request() are: | 433 # Differences from make_request() are: |
| 455 # - idempotent was reset to False. | 434 # - idempotent was reset to False. |
| 456 # - parent_task_id was reset to None. | 435 # - parent_task_id was reset to None. |
| 457 expected_properties = { | 436 expected_properties = { |
| 458 'command': [u'command1', u'arg1'], | 437 'command': [u'command1', u'arg1'], |
| 459 'dimensions': { | 438 'dimensions': { |
| 460 u'OS': u'Windows-3.1.1', | 439 u'OS': u'Windows-3.1.1', |
| 461 u'hostname': u'localhost', | 440 u'hostname': u'localhost', |
| 462 u'pool': u'default', | 441 u'pool': u'default', |
| 463 }, | 442 }, |
| 464 'env': {u'foo': u'bar', u'joe': u'2'}, | 443 'env': {u'foo': u'bar', u'joe': u'2'}, |
| 465 'execution_timeout_secs': 30, | 444 'execution_timeout_secs': 30, |
| 466 'extra_args': [], | 445 'extra_args': [], |
| 467 'grace_period_secs': 30, | 446 'grace_period_secs': 30, |
| 468 'idempotent': False, | 447 'idempotent': False, |
| 469 'inputs_ref': None, | 448 'inputs_ref': None, |
| 470 'io_timeout_secs': None, | 449 'io_timeout_secs': None, |
| 450 'packages': [{'package_name': 'rm', 'version': PINNED_PACKAGE_VERSION}], | |
| 471 } | 451 } |
| 472 # Differences from make_request() are: | 452 # Differences from make_request() are: |
| 473 # - parent_task_id was reset to None. | 453 # - parent_task_id was reset to None. |
| 474 # - tag 'user:' was replaced | 454 # - tag 'user:' was replaced |
| 475 # - user was replaced. | 455 # - user was replaced. |
| 476 expected_request = { | 456 expected_request = { |
| 477 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, | 457 'authenticated': auth_testing.DEFAULT_MOCKED_IDENTITY, |
| 478 'name': u'Request name (Retry #1)', | 458 'name': u'Request name (Retry #1)', |
| 479 'parent_task_id': None, | 459 'parent_task_id': None, |
| 480 'priority': 49, | 460 'priority': 49, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 529 datetime.datetime(2012, 1, 2, 3, 4, 5, 123000), | 509 datetime.datetime(2012, 1, 2, 3, 4, 5, 123000), |
| 530 task_request.request_key_to_datetime(key)) | 510 task_request.request_key_to_datetime(key)) |
| 531 | 511 |
| 532 def test_request_id_to_key(self): | 512 def test_request_id_to_key(self): |
| 533 # Simple XOR. | 513 # Simple XOR. |
| 534 self.assertEqual( | 514 self.assertEqual( |
| 535 ndb.Key(task_request.TaskRequest, 0x7f14acec2fcfffff), | 515 ndb.Key(task_request.TaskRequest, 0x7f14acec2fcfffff), |
| 536 task_request.request_id_to_key(0xeb5313d0300000)) | 516 task_request.request_id_to_key(0xeb5313d0300000)) |
| 537 | 517 |
| 538 | 518 |
| 519 def mkreq(req): | |
|
M-A Ruel
2016/04/25 19:09:45
Put it at the top.
nodir
2016/04/25 20:27:16
Done.
| |
| 520 return task_request.make_request(req, True) | |
| 521 | |
| 539 if __name__ == '__main__': | 522 if __name__ == '__main__': |
| 540 if '-v' in sys.argv: | 523 if '-v' in sys.argv: |
| 541 unittest.TestCase.maxDiff = None | 524 unittest.TestCase.maxDiff = None |
| 542 logging.basicConfig( | 525 logging.basicConfig( |
| 543 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 526 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 544 unittest.main() | 527 unittest.main() |
| OLD | NEW |