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
4 # found in the LICENSE file.
8 from admin_servlets
import ResetCommitServlet
9 from commit_tracker
import CommitTracker
10 from object_store_creator
import ObjectStoreCreator
11 from servlet
import Request
14 _COMMIT_HISTORY_DATA
= (
15 '1234556789abcdef1234556789abcdef12345567',
16 'f00f00f00f00f00f00f00f00f00f00f00f00f00f',
17 '1010101010101010101010101010101010101010',
18 'abcdefabcdefabcdefabcdefabcdefabcdefabcd',
19 '4242424242424242424242424242424242424242',
23 class _ResetCommitDelegate(ResetCommitServlet
.Delegate
):
24 def __init__(self
, commit_tracker
):
25 self
._commit
_tracker
= commit_tracker
27 def CreateCommitTracker(self
):
28 return self
._commit
_tracker
31 class AdminServletsTest(unittest
.TestCase
):
33 object_store_creator
= ObjectStoreCreator(start_empty
=True)
34 self
._commit
_tracker
= CommitTracker(object_store_creator
)
35 for id in _COMMIT_HISTORY_DATA
:
36 self
._commit
_tracker
.Set('master', id).Get()
38 def _ResetCommit(self
, commit_name
, commit_id
):
39 return ResetCommitServlet(
40 Request
.ForTest('%s/%s' % (commit_name
, commit_id
)),
41 _ResetCommitDelegate(self
._commit
_tracker
)).Get()
43 def _AssertBadRequest(self
, commit_name
, commit_id
):
44 response
= self
._ResetCommit
(commit_name
, commit_id
)
45 self
.assertEqual(response
.status
, 400,
46 'Should have failed to reset to commit %s to %s.' %
47 (commit_name
, commit_id
))
49 def _AssertOk(self
, commit_name
, commit_id
):
50 response
= self
._ResetCommit
(commit_name
, commit_id
)
51 self
.assertEqual(response
.status
, 200,
52 'Failed to reset commit %s to %s.' % (commit_name
, commit_id
))
54 def testResetCommitServlet(self
):
55 # Make sure all the valid commits can be used for reset.
56 for id in _COMMIT_HISTORY_DATA
:
57 self
._AssertOk
('master', id)
59 # Non-existent commit should fail to update
60 self
._AssertBadRequest
('master',
61 'b000000000000000000000000000000000000000')
63 # Commit 'master' should still point to the last valid entry
64 self
.assertEqual(self
._commit
_tracker
.Get('master').Get(),
65 _COMMIT_HISTORY_DATA
[-1])
67 # Reset to a valid commit but older
68 self
._AssertOk
('master', _COMMIT_HISTORY_DATA
[0])
70 # Commit 'master' should point to the first history entry
71 self
.assertEqual(self
._commit
_tracker
.Get('master').Get(),
72 _COMMIT_HISTORY_DATA
[0])
74 # Add a new entry to the history and validate that it can be used for reset.
75 _NEW_ENTRY
= '9999999999999999999999999999999999999999'
76 self
._commit
_tracker
.Set('master', _NEW_ENTRY
).Get()
77 self
._AssertOk
('master', _NEW_ENTRY
)
79 # Add a bunch (> 50) of entries to ensure that _NEW_ENTRY has been flushed
81 for i
in xrange(0, 20):
82 for id in _COMMIT_HISTORY_DATA
:
83 self
._commit
_tracker
.Set('master', id).Get()
85 # Verify that _NEW_ENTRY is no longer valid for reset.
86 self
._AssertBadRequest
('master', _NEW_ENTRY
)
88 if __name__
== '__main__':