| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import contextlib | 5 import contextlib |
| 6 import datetime | 6 import datetime |
| 7 import json | 7 import json |
| 8 | 8 |
| 9 from components import auth | 9 from components import auth |
| 10 from components import utils | 10 from components import utils |
| 11 from google.appengine.datastore import datastore_stub_util |
| 11 from google.appengine.ext import testbed | 12 from google.appengine.ext import testbed |
| 12 from testing_utils import testing | 13 from testing_utils import testing |
| 13 import mock | 14 import mock |
| 14 | 15 |
| 15 import acl | 16 import acl |
| 16 import errors | 17 import errors |
| 17 import model | 18 import model |
| 18 import service | 19 import service |
| 19 | 20 |
| 20 | 21 |
| 21 class BuildBucketServiceTest(testing.AppengineTestCase): | 22 class BuildBucketServiceTest(testing.AppengineTestCase): |
| 22 def __init__(self, *args, **kwargs): | 23 def __init__(self, *args, **kwargs): |
| 23 super(BuildBucketServiceTest, self).__init__(*args, **kwargs) | 24 super(BuildBucketServiceTest, self).__init__(*args, **kwargs) |
| 24 self.test_build = None | 25 self.test_build = None |
| 25 | 26 |
| 26 def mock_cannot(self, action): | 27 def mock_cannot(self, action): |
| 27 def can(_bucket, requested_action, _identity=None): | 28 def can(_bucket, requested_action, _identity=None): |
| 28 return action != requested_action | 29 return action != requested_action |
| 29 self.mock(acl, 'can', can) | 30 self.mock(acl, 'can', can) |
| 30 | 31 |
| 31 def setUp(self): | 32 def setUp(self): |
| 33 # Create a consistency policy that will simulate the High Replication |
| 34 # consistency model. |
| 35 self.consistency_policy = ( |
| 36 datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1.0)) |
| 37 |
| 32 super(BuildBucketServiceTest, self).setUp() | 38 super(BuildBucketServiceTest, self).setUp() |
| 33 self.service = service.BuildBucketService() | 39 self.service = service.BuildBucketService() |
| 34 self.test_build = model.Build( | 40 self.test_build = model.Build( |
| 35 bucket='chromium', | 41 bucket='chromium', |
| 36 parameters={ | 42 parameters={ |
| 37 'buildername': 'infra', | 43 'buildername': 'infra', |
| 38 'changes': [{ | 44 'changes': [{ |
| 39 'author': 'nodir@google.com', | 45 'author': 'nodir@google.com', |
| 40 'message': 'buildbucket: initial commit' | 46 'message': 'buildbucket: initial commit' |
| 41 }] | 47 }] |
| (...skipping 17 matching lines...) Expand all Loading... |
| 59 build = self.service.add( | 65 build = self.service.add( |
| 60 bucket='chromium', | 66 bucket='chromium', |
| 61 parameters=params, | 67 parameters=params, |
| 62 ) | 68 ) |
| 63 self.assertIsNotNone(build.key) | 69 self.assertIsNotNone(build.key) |
| 64 self.assertIsNotNone(build.key.id()) | 70 self.assertIsNotNone(build.key.id()) |
| 65 self.assertEqual(build.bucket, 'chromium') | 71 self.assertEqual(build.bucket, 'chromium') |
| 66 self.assertEqual(build.parameters, params) | 72 self.assertEqual(build.parameters, params) |
| 67 self.assertEqual(build.created_by, auth.get_current_identity()) | 73 self.assertEqual(build.created_by, auth.get_current_identity()) |
| 68 | 74 |
| 75 def test_add_with_client_operation_id(self): |
| 76 build = self.service.add( |
| 77 bucket='chromium', |
| 78 parameters={'buildername': 'linux_rel'}, |
| 79 client_operation_id=1, |
| 80 ) |
| 81 build2 = self.service.add( |
| 82 bucket='chromium', |
| 83 parameters={'buildername': 'linux_rel'}, |
| 84 client_operation_id=1, |
| 85 ) |
| 86 self.assertIsNotNone(build.key) |
| 87 self.assertEqual(build, build2) |
| 88 |
| 69 def test_add_with_bad_bucket_name(self): | 89 def test_add_with_bad_bucket_name(self): |
| 70 with self.assertRaises(errors.InvalidInputError): | 90 with self.assertRaises(errors.InvalidInputError): |
| 71 self.service.add(bucket='chromium as') | 91 self.service.add(bucket='chromium as') |
| 72 with self.assertRaises(errors.InvalidInputError): | 92 with self.assertRaises(errors.InvalidInputError): |
| 73 self.service.add(bucket='') | 93 self.service.add(bucket='') |
| 74 | 94 |
| 75 def test_add_with_leasing(self): | 95 def test_add_with_leasing(self): |
| 76 build = self.service.add( | 96 build = self.service.add( |
| 77 bucket='chromium', | 97 bucket='chromium', |
| 78 lease_expiration_date=utils.utcnow () + datetime.timedelta(seconds=10), | 98 lease_expiration_date=utils.utcnow () + datetime.timedelta(seconds=10), |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 self.assertEqual(build.status, model.BuildStatus.SCHEDULED) | 646 self.assertEqual(build.status, model.BuildStatus.SCHEDULED) |
| 627 self.assertIsNone(build.lease_key) | 647 self.assertIsNone(build.lease_key) |
| 628 | 648 |
| 629 def test_completed_builds_are_not_reset(self): | 649 def test_completed_builds_are_not_reset(self): |
| 630 self.test_build.status = model.BuildStatus.COMPLETED | 650 self.test_build.status = model.BuildStatus.COMPLETED |
| 631 self.test_build.result = model.BuildResult.SUCCESS | 651 self.test_build.result = model.BuildResult.SUCCESS |
| 632 self.test_build.put() | 652 self.test_build.put() |
| 633 self.service.reset_expired_builds() | 653 self.service.reset_expired_builds() |
| 634 build = self.test_build.key.get() | 654 build = self.test_build.key.get() |
| 635 self.assertEqual(build.status, model.BuildStatus.COMPLETED) | 655 self.assertEqual(build.status, model.BuildStatus.COMPLETED) |
| OLD | NEW |