Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: appengine/cr-buildbucket/test/service_test.py

Issue 1082303002: buildbucket: put_batch endpoint (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 ) 85 )
80 self.assertTrue(build.is_leased) 86 self.assertTrue(build.is_leased)
81 self.assertGreater(build.lease_expiration_date, utils.utcnow()) 87 self.assertGreater(build.lease_expiration_date, utils.utcnow())
82 self.assertIsNotNone(build.lease_key) 88 self.assertIsNotNone(build.lease_key)
83 89
84 def test_add_with_auth_error(self): 90 def test_add_with_auth_error(self):
85 self.mock_cannot(acl.Action.ADD_BUILD) 91 self.mock_cannot(acl.Action.ADD_BUILD)
86 with self.assertRaises(auth.AuthorizationError): 92 with self.assertRaises(auth.AuthorizationError):
87 self.service.add(self.test_build.bucket) 93 self.service.add(self.test_build.bucket)
88 94
95 ################################# ADD_BATCH ##################################
96
97 def test_add_batch(self):
98 builds = self.service.add_batch([{'bucket': 'chromium'}, {'bucket': 'v8'}])
99 self.assertEqual(len(builds), 2)
100
101 self.assertIsNotNone(builds[0].key)
102 self.assertIsNotNone(builds[0].key.id())
103 self.assertEqual(builds[0].bucket, 'chromium')
104
105 self.assertIsNotNone(builds[1].key)
106 self.assertIsNotNone(builds[1].key.id())
107 self.assertEqual(builds[1].bucket, 'v8')
108
109 def test_add_batch_too_many(self):
110 build_reqs = [{'bucket': 'chromium'}] * 30
111 with self.assertRaises(errors.InvalidInputError):
112 self.service.add_batch(build_reqs)
113
114 def test_add_batch_with_bad_build(self):
115 with self.assertRaises(errors.InvalidInputError):
116 self.service.add_batch([{'bucket': 'chromium'}, {'bucket': 2}])
117 self.assertFalse(model.Build.query().fetch())
118
89 #################################### GET ##################################### 119 #################################### GET #####################################
90 120
91 def test_get(self): 121 def test_get(self):
92 self.test_build.put() 122 self.test_build.put()
93 build = self.service.get(self.test_build.key.id()) 123 build = self.service.get(self.test_build.key.id())
94 self.assertEqual(build, self.test_build) 124 self.assertEqual(build, self.test_build)
95 125
96 def test_get_nonexistent_build(self): 126 def test_get_nonexistent_build(self):
97 self.assertIsNone(self.service.get(42)) 127 self.assertIsNone(self.service.get(42))
98 128
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 self.assertEqual(build.status, model.BuildStatus.SCHEDULED) 656 self.assertEqual(build.status, model.BuildStatus.SCHEDULED)
627 self.assertIsNone(build.lease_key) 657 self.assertIsNone(build.lease_key)
628 658
629 def test_completed_builds_are_not_reset(self): 659 def test_completed_builds_are_not_reset(self):
630 self.test_build.status = model.BuildStatus.COMPLETED 660 self.test_build.status = model.BuildStatus.COMPLETED
631 self.test_build.result = model.BuildResult.SUCCESS 661 self.test_build.result = model.BuildResult.SUCCESS
632 self.test_build.put() 662 self.test_build.put()
633 self.service.reset_expired_builds() 663 self.service.reset_expired_builds()
634 build = self.test_build.key.get() 664 build = self.test_build.key.get()
635 self.assertEqual(build.status, model.BuildStatus.COMPLETED) 665 self.assertEqual(build.status, model.BuildStatus.COMPLETED)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698