def assert_checklist_for_tags(self, tags, requestid=None): num_checks = 0 checks = [] # Gather reference checklists from the code for tag in tags: # While the tag name is 'search-backend', the checklist type # is truncated to 'search'. if tag == 'search-backend': tag = 'search' if tag not in checklist_reminders: continue plain_list = checklist_reminders[tag] num_checks += len(plain_list) checks += [(tag, check) for check in plain_list] cleanup_tag = '%s-cleanup' % tag cleanup_list = checklist_reminders[cleanup_tag] num_checks += len(cleanup_list) checks += [(cleanup_tag, check) for check in cleanup_list] reqid = self.make_request_with_tags(tags, requestid) checklists = self.get_checklists(reqid) T.assert_equal(num_checks, len(checklists)) for check in checks: T.assert_in((reqid, check[0], check[1]), checklists) return reqid
def test_checklist_single_search_tag(self): with fake_checklist_request(): # insert fake data from FakeDataMixin fake_pushid = 2 self.insert_pushes() self.insert_requests() test1_request = self.get_requests_by_user('testuser1')[0] self.insert_pushcontent(test1_request['id'], fake_pushid) # insert fake checklist data checklist_queries = [ db.push_checklist.insert({ 'request': test1_request['id'], 'type': 'search', 'target': 'prod' }), db.push_checklist.insert({ 'request': test1_request['id'], 'type': 'search-cleanup', 'target': 'post-verify-prod' }), ] db.execute_transaction_cb(checklist_queries, on_db_return) uri = "/checklist?id=%d" % fake_pushid response = self.fetch(uri) T.assert_equal(response.error, None) T.assert_not_in("No checklist items for this push", response.body) T.assert_not_in("multiple requests", response.body) T.assert_in("for testuser1", response.body) T.assert_in("After Certifying - Do In Prod", response.body)
def test_branch_name_with_slash(self): fake_request = """{ "id": 10, "user": "******", "repo": "testuser", "state": "requested", "branch": "testuser/testbranch", "revision": "00000", "description": "description for 'testuser/testbranch'", "title": "funny title", "reviewid": 102 }""" with nested( mock.patch.object(SummaryForBranchServlet, "get_current_user", return_value="testuser"), mock.patch.object(SummaryForBranchServlet, "async_api_call", side_effect=self.mocked_api_call), mock.patch.object(self, "api_response", return_value="[%s]" % fake_request), ): self.fetch( "/summaryforbranch?userbranch=testuser/testuser/testbranch") response = self.wait() T.assert_equal(response.error, None) T.assert_in("'testuser/testbranch'", response.body) T.assert_in("102", response.body)
def assert_request_buttons(self, tree, button_classes, button_text): found_buttons = [] for button in tree.iter('button'): T.assert_in(button.attrib['class'], button_classes) T.assert_in(button.text, button_text) found_buttons.append(button) T.assert_equal(len(button_classes), len(found_buttons))
def test_checklist_duplicate(self): with fake_checklist_request(): # insert fake data from FakeDataMixin fake_pushid = 2 self.insert_pushes() self.insert_requests() test1_request = self.get_requests_by_user('testuser1')[0] test2_request = self.get_requests_by_user('testuser2')[0] self.insert_pushcontent(test1_request['id'], fake_pushid) self.insert_pushcontent(test2_request['id'], fake_pushid) # insert fake checklist data checklist_queries = [] for req in (test1_request, test2_request): checklist_queries.append(db.push_checklist.insert({ 'request': req['id'], 'type': 'search', 'target': 'prod' })) checklist_queries.append(db.push_checklist.insert({ 'request': req['id'], 'type': 'search-cleanup', 'target': 'post-verify-prod' })) db.execute_transaction_cb(checklist_queries, on_db_return) uri = "/checklist?id=%d" % fake_pushid response = self.fetch(uri) T.assert_equal(response.error, None) T.assert_not_in("No checklist items for this push", response.body) T.assert_not_equal(re.search("for testuser\d,testuser\d", response.body), None) T.assert_in("After Certifying - Do In Prod", response.body)
def test_get_repository_uri_basic(self): T.MockedSettings["git"] = self.fake_settings with mock.patch.dict(Settings, T.MockedSettings): T.assert_equal(core.git.GitQueue._get_repository_uri("main_repository"), "git://example/main_repository") T.assert_equal(core.git.GitQueue._get_repository_uri("second_repository"), "git://example/dev_directory/second_repository")
def test_hoods_checklists(self): with fake_checklist_request(): # insert fake data from FakeDataMixin fake_pushid = 2 self.insert_pushes() self.insert_requests() req = self.get_requests_by_user('testuser1')[0] self.insert_pushcontent(req['id'], fake_pushid) # insert fake checklist data checklist_queries = [] checklist_items = ( {'request': req['id'], 'type': 'hoods', 'target': 'stage'}, {'request': req['id'], 'type': 'hoods', 'target': 'post-stage'}, {'request': req['id'], 'type': 'hoods', 'target': 'prod'}, {'request': req['id'], 'type': 'hoods-cleanup', 'target': 'post-verify-stage'}, ) for checklist_item in checklist_items: checklist_queries.append(db.push_checklist.insert(checklist_item)) db.execute_transaction_cb(checklist_queries, on_db_return) uri = "/checklist?id=%d" % fake_pushid response = self.fetch(uri) T.assert_equal(response.error, None) T.assert_not_in("No checklist items for this push", response.body) T.assert_in("Notify testuser1 to deploy Geoservices to stage", response.body) T.assert_in("Notify testuser1 to deploy Geoservices to prod", response.body) T.assert_in("Ask Search to force index distribution on stage for testuser1", response.body)
def test_checklist_duplicate(self): with fake_checklist_request(): # insert fake data from FakeDataMixin fake_pushid = 2 self.insert_pushes() self.insert_requests() test1_request = self.get_requests_by_user('testuser1')[0] test2_request = self.get_requests_by_user('testuser2')[0] self.insert_pushcontent(test1_request['id'], fake_pushid) self.insert_pushcontent(test2_request['id'], fake_pushid) # insert fake checklist data checklist_queries = [] for req in (test1_request, test2_request): checklist_queries.append( db.push_checklist.insert({ 'request': req['id'], 'type': 'search', 'target': 'prod' })) checklist_queries.append( db.push_checklist.insert({ 'request': req['id'], 'type': 'search-cleanup', 'target': 'post-verify-prod' })) db.execute_transaction_cb(checklist_queries, on_db_return) uri = "/checklist?id=%d" % fake_pushid response = self.fetch(uri) T.assert_equal(response.error, None) T.assert_not_in("No checklist items for this push", response.body) T.assert_not_equal( re.search("for testuser\d,testuser\d", response.body), None) T.assert_in("After Certifying - Do In Prod", response.body)
def test_process_queue_duplicate(self): with nested( mock.patch("%s.core.git.GitQueue.update_request_failure" % __name__), mock.patch("%s.core.git.GitQueue.update_request_successful" % __name__), # This will fail, stop logging errors mock.patch("%s.core.git.logging.error" % __name__), mock.patch( "%s.core.git.GitQueue._get_request_with_sha" % __name__, return_value = {'id': 10, 'state': 'requested'} ), self.mocked_update_request(self.fake_request, self.fake_request) ): # GitQueue._get_request_with_sha returning a value means # we have a duplicated request. This should trigger a # failure T.assert_equal(core.git.GitQueue.update_request_failure.call_count, 1) T.assert_equal(core.git.GitQueue.update_request_successful.call_count, 0) # Match the error message for duplicate revision. error_msg # should be the last item of the first call object's *args list # (from mock library). T.assert_in( "another request with the same revision sha", core.git.GitQueue.update_request_failure.call_args_list[0][0][-1] )
def test_get_api_page(self): T.MockedSettings['api_app'] = {'port': 8043, 'servername': 'push.test.com'} with mock.patch.dict(Settings, T.MockedSettings): T.assert_equal( RequestHandler.get_api_page("pushes"), "http://push.test.com:8043/api/pushes" )
def test_process_queue_duplicate(self): with nested( mock.patch("%s.core.git.GitQueue.update_request_failure" % __name__), mock.patch("%s.core.git.GitQueue.update_request_successful" % __name__), # This will fail, stop logging errors mock.patch("%s.core.git.logging.error" % __name__), mock.patch("%s.core.git.GitQueue._get_request_with_sha" % __name__, return_value={ 'id': 10, 'state': 'requested' }), self.mocked_update_request(self.fake_request, self.fake_request)): # GitQueue._get_request_with_sha returning a value means # we have a duplicated request. This should trigger a # failure T.assert_equal(core.git.GitQueue.update_request_failure.call_count, 1) T.assert_equal( core.git.GitQueue.update_request_successful.call_count, 0) # Match the error message for duplicate revision. error_msg # should be the last item of the first call object's *args list # (from mock library). T.assert_in( "another request with the same revision sha", core.git.GitQueue.update_request_failure.call_args_list[0][0] [-1])
def test_removerequest(self): results = [] def on_db_return(success, db_results): assert success results.extend(db_results.fetchall()) with nested( mock.patch.dict(db.Settings, T.MockedSettings), mock.patch.object( RemoveRequestServlet, "get_current_user", return_value="testuser" ) ): results = [] db.execute_cb(db.push_pushcontents.select(), on_db_return) num_results_before = len(results) uri = "/removerequest?request=1&push=1" response = self.fetch(uri) T.assert_equal(response.error, None) results = [] db.execute_cb(db.push_pushcontents.select(), on_db_return) num_results_after = len(results) T.assert_equal(num_results_after, num_results_before - 1, "Request removal failed.")
def test_search_and_hoods(self): tag = ['hoods'] orig_reqid = self.assert_checklist_for_tags(tag) tags = ['search-backend', 'hoods'] new_reqid = self.assert_checklist_for_tags(tags, orig_reqid) T.assert_equal(orig_reqid, new_reqid)
def test_pushplans_and_search(self): tag = ['pushplans'] orig_reqid = self.assert_checklist_for_tags(tag) tags = ['pushplans', 'search-backend'] new_reqid = self.assert_checklist_for_tags(tags, orig_reqid) T.assert_equal(orig_reqid, new_reqid)
def test_create_request_bookmarklet(self): with self.page(CreateRequestBookmarkletHandler) as response: # We'll get a javascript as the body, just check some # variable names/strings that we know is there in the # script. T.assert_equal(response.error, None) T.assert_in("ticketNumberToURL", response.body) T.assert_in("codeReview", response.body)
def test_pushplans_and_hoods(self): tag = ['hoods'] orig_reqid = self.assert_checklist_for_tags(tag) tags = ['pushplans', 'hoods'] new_reqid = self.assert_checklist_for_tags(tags, orig_reqid) T.assert_equal(orig_reqid, new_reqid)
def test_update_request_successful(self): with nested( mock.patch("%s.core.git.MailQueue.enqueue_user_email" % __name__), mock.patch("%s.core.git.webhook_req" % __name__)): core.git.GitQueue.update_request_successful(self.fake_request) T.assert_equal(core.git.MailQueue.enqueue_user_email.call_count, 1) T.assert_equal(core.git.webhook_req.call_count, 3)
def test_escape(self): T.assert_equal( [k for k in self.d if self.ed[k] != self.escaped[k]], [], "EscapedDict values doesn't match with pre-computed valued", ) T.assert_in("&", self.ed["amp"]) T.assert_not_in(">", self.ed["gt"])
def test_update_request_failure(self): with nested( mock.patch("%s.core.git.MailQueue.enqueue_user_email" % __name__), mock.patch("%s.core.git.webhook_req" % __name__), mock.patch("%s.core.git.logging.error" % __name__), ): core.git.GitQueue.update_request_failure(self.fake_request, "fake failure") T.assert_equal(core.git.MailQueue.enqueue_user_email.call_count, 1)
def test_update_request_successful(self): with nested( mock.patch("%s.core.git.MailQueue.enqueue_user_email" % __name__), mock.patch("%s.core.git.webhook_req" % __name__) ): core.git.GitQueue.update_request_successful(self.fake_request) T.assert_equal(core.git.MailQueue.enqueue_user_email.call_count, 1) T.assert_equal(core.git.webhook_req.call_count, 3)
def test_get_repository_uri_with_auth(self): T.MockedSettings["git"] = self.fake_settings T.MockedSettings["git"]["auth"] = "myuser:mypass" with mock.patch.dict(Settings, T.MockedSettings): T.assert_equal(core.git.GitQueue._get_repository_uri("main_repository"), "git://*****:*****@example/main_repository") T.assert_equal(core.git.GitQueue._get_repository_uri("second_repository"), "git://*****:*****@example/dev_directory/second_repository")
def test_include_new_push(self): tree = self.render_pushes_page() found_form = [] for form in tree.iter('form'): if form.attrib['id'] == 'push-info-form': found_form.append(form) T.assert_equal(len(found_form), 1)
def test_kill_processes(self): with contextlib.nested( self.mock_method('%s.pid.os.kill' % __name__, None, None), self.mock_method('%s.pid.is_process_alive' % __name__, False, None) ): # this should run fine, all processes are dead pids = [1, 2, 3, 4, 5] pid.kill_processes(pids) T.assert_equal(pids, [])
def test_get_repository_uri_basic(self): T.MockedSettings["git"] = self.fake_settings with mock.patch.dict(Settings, T.MockedSettings): T.assert_equal( core.git.GitQueue._get_repository_uri("main_repository"), "git://example/main_repository") T.assert_equal( core.git.GitQueue._get_repository_uri("second_repository"), "git://example/dev_directory/second_repository")
def test_include_request_info(self): tree = self.render_module_request_with_users(self.basic_request,'testuser', 'testuser', **self.basic_kwargs) found_ul = [] for ul in tree.iter('ul'): if ul.attrib['class'] == 'request-info-inline': found_ul.append(ul) T.assert_equal(1, len(found_ul))
def test_kill_processes(self): with contextlib.nested( self.mock_method('%s.pid.os.kill' % __name__, None, None), self.mock_method('%s.pid.is_process_alive' % __name__, False, None)): # this should run fine, all processes are dead pids = [1, 2, 3, 4, 5] pid.kill_processes(pids) T.assert_equal(pids, [])
def test_pushes(self): pushes, last_push = self.api_call("pushes") T.assert_length(pushes, 2) T.assert_equal(last_push, 2) pushes, last_push = self.api_call("pushes?rpp=1") T.assert_length(pushes, 1) pushes, last_push = self.api_call("pushes?before=%d" % time.time()) T.assert_length(pushes, 2)
def test_get_repository_uri_with_auth(self): T.MockedSettings["git"] = self.fake_settings T.MockedSettings["git"]["auth"] = "myuser:mypass" with mock.patch.dict(Settings, T.MockedSettings): T.assert_equal( core.git.GitQueue._get_repository_uri("main_repository"), "git://*****:*****@example/main_repository") T.assert_equal( core.git.GitQueue._get_repository_uri("second_repository"), "git://*****:*****@example/dev_directory/second_repository")
def test_push_buildbot_revision_link(self): with self.request_fake_pushdata() as fakepush: pushdata, response = fakepush T.assert_equal(response.error, None) requests = pushdata[1] all_requests = requests["all"] first_request = all_requests[0] buildbot_link = "http://%s/rev/%s" % (Settings["buildbot"]["servername"], first_request["revision"]) T.assert_equal(self.find_buildbot_link(response, buildbot_link), True)
def test_xmppqueue_on_db_complete(self, xmppq, *_): self.call_on_db_complete() no_watcher_call_args = xmppq.call_args_list[0][0] T.assert_equal(['testuser'], no_watcher_call_args[0]) T.assert_in('for testuser', no_watcher_call_args[1]) watched_call_args = xmppq.call_args_list[1][0] T.assert_equal(['testuser', 'testuser1', 'testuser2'], watched_call_args[0]) T.assert_in('for testuser (testuser1,testuser2)', watched_call_args[1])
def test_update_duplicate_request_discarded(self): duplicate_req = copy.deepcopy(self.fake_request) duplicate_req['state'] = "discarded" with nested( mock.patch("%s.core.git.GitQueue.update_request_failure" % __name__), mock.patch("%s.core.git.GitQueue.update_request_successful" % __name__), self.mocked_update_request(self.fake_request, duplicate_req) ): T.assert_equal(core.git.GitQueue.update_request_failure.call_count, 0) T.assert_equal(core.git.GitQueue.update_request_successful.call_count, 1)
def assert_button_link(self, button, tree, text=None, num=1): classname = '%s-request' % button.lower() if text is None: text = button.capitalize() buttons = [] for button in tree.iter('button'): if button.attrib['class'] == classname: T.assert_equal(text, button.text) buttons.append(button) T.assert_equal(num, len(buttons))
def test_send_mail(self): with mock.patch.object(core.mail.MailQueue, "smtp") as mocked_smtp: with mock.patch.object(core.mail.MailQueue, "message_queue"): recipient = "*****@*****.**" message = "test message" subject = "test subject" from_email = "*****@*****.**" core.mail.MailQueue._send_email(recipient, message, subject, from_email) T.assert_equal(mocked_smtp.sendmail.called, True)
def test_cross_val_score(): # Generate some toy data. rng = np.random.RandomState(0) U = rng.rand(50, 3) V = rng.rand(3, 20) X = np.dot(U, V) cv = ShuffleSplit(n_iter=10) mf = ExplicitMF(n_components=3, max_iter=10, alpha=1e-3, random_state=0, verbose=0) scores = cross_val_score(mf, X, cv) assert_equal(len(scores), cv.n_iter)
def test_request_push_buttons_as_random_user(self): kwargs = dict(self.basic_kwargs) kwargs['push_buttons'] = True tree = self.render_module_request_with_users(self.basic_request, 'testuser', 'notuser', **kwargs) buttons_found = [] for button in tree.iter('button'): T.assert_not_in(button.attrib['class'], self.pushmaster_button_classes + self.push_button_classes) buttons_found.append(button) T.assert_equal(0, len(buttons_found))
def test_push_buildbot_revision_link(self): with self.request_fake_pushdata() as fakepush: pushdata, response = fakepush T.assert_equal(response.error, None) requests = pushdata[1] all_requests = requests['all'] first_request = all_requests[0] buildbot_link = "http://%s/rev/%s" % ( Settings['buildbot']['servername'], first_request['revision']) T.assert_equal(self.find_buildbot_link(response, buildbot_link), True)
def test_include_push_button_bar(self): tree = self.render_etree( self.push_page, push_info=self.basic_push, **self.basic_kwargs) found_ul = [] for ul in tree.iter('ul'): if 'id' in ul.attrib and ul.attrib['id'] == 'action-buttons': found_ul.append(ul) T.assert_equal(1, len(found_ul))
def test_cross_val_score_multi(): # Generate some toy data. rng = np.random.RandomState(0) U = rng.rand(50, 3) V = rng.rand(3, 20) X = np.dot(U, V) X = (X > X.mean()).astype(np.int32) cv = ShuffleSplit(n_iter=10) mf = ImplicitMF(n_components=3, max_iter=10, alpha=1e-3, random_state=0) scores = cross_val_score(mf, X, cv, metric=["precision", "recall"]) assert_equal(scores.shape, (cv.n_iter, 2))
def test_update_duplicate_request_discarded(self): duplicate_req = copy.deepcopy(self.fake_request) duplicate_req['state'] = "discarded" with nested( mock.patch("%s.core.git.GitQueue.update_request_failure" % __name__), mock.patch("%s.core.git.GitQueue.update_request_successful" % __name__), self.mocked_update_request(self.fake_request, duplicate_req)): T.assert_equal(core.git.GitQueue.update_request_failure.call_count, 0) T.assert_equal( core.git.GitQueue.update_request_successful.call_count, 1)
def test_pushes_order(self): self.insert_pushes() pushes, _ = self.api_call("pushes") T.assert_length(pushes, 6) lastpush = None for push in pushes: if lastpush is not None: if push['state'] == 'accepting': T.assert_equal('accepting', lastpush['state']) elif lastpush['state'] != 'accepting': T.assert_gte(push['modified'], lastpush['modified']) lastpush = push
def test_include_push_buttons(self): kwargs = dict(self.basic_kwargs) kwargs['push_buttons'] = True tree = self.render_module_request_with_users(self.basic_request, 'testuser', 'testuser', **kwargs) found_span = [] for span in tree.iter('span'): T.assert_not_equal('edit-request-buttons', span.attrib['class']) if span.attrib['class'] == 'push-request-buttons': found_span.append(span) T.assert_equal(1, len(found_span))