From 8129a31d742308828a48529c9871e10d9a4de355 Mon Sep 17 00:00:00 2001 From: Christos Nianiakas Date: Thu, 9 Jun 2016 13:37:02 +0300 Subject: [PATCH 1/2] - Checking Disqus API rate limit and stopping the test before trying to comment. This way we prevent leaving libraries/examples without comments. - Minor fix in tic-toc intialization at utils.py . - Added check in seleniumbender.py before sending email with report. If a report doesn't exists, no email is send. --- bin/seleniumbender.py | 26 ++++++++++++++------------ codebender_testing/disqus.py | 20 ++++++++++++++++++++ codebender_testing/utils.py | 5 +++++ requirements.txt | 3 ++- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/bin/seleniumbender.py b/bin/seleniumbender.py index 56f29ab..8a6981f 100755 --- a/bin/seleniumbender.py +++ b/bin/seleniumbender.py @@ -69,18 +69,20 @@ def send_mail_with_logs(self, identifier): logfile_timestamp = re.match(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-.+', logfile).group(1) report_regexp = re.compile('report_{timestamp}-{identifier}_(\d+)'.format(timestamp=logfile_timestamp, identifier=identifier)) - reportfile = sorted([filename for filename in os.listdir(reports) if filename not in self.files_to_ignore and report_regexp.match(filename)], reverse=True)[0] - changes = report_regexp.match(reportfile).group(1) - - email_date = time.strftime('%Y-%m-%d %H:%M:%S') - - command = [ - '(echo "Changes since the last time: {changes}";'.format(changes=changes), - 'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile), - 'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile), - '| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email) - ] - self.run_command(command) + reportfile = sorted([filename for filename in os.listdir(reports) if filename not in self.files_to_ignore and report_regexp.match(filename)], reverse=True) + if reportfile: + reportfile = reportfile[0] + changes = report_regexp.match(reportfile).group(1) + + email_date = time.strftime('%Y-%m-%d %H:%M:%S') + + command = [ + '(echo "Changes since the last time: {changes}";'.format(changes=changes), + 'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile), + 'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile), + '| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email) + ] + self.run_command(command) def create_command(self, test_directory, *extra_arguments): return ['tox', 'tests/' + test_directory, '--', '--url={}'.format(TARGETS[self.url])] + list(extra_arguments) diff --git a/codebender_testing/disqus.py b/codebender_testing/disqus.py index cfcb036..2abe036 100644 --- a/codebender_testing/disqus.py +++ b/codebender_testing/disqus.py @@ -2,8 +2,10 @@ # -*- coding: utf-8 -*- from codebender_testing.config import get_path +import pytest import disqusapi import simplejson +import requests import base64 import hashlib import hmac @@ -16,6 +18,7 @@ AUTHOR_URL = os.getenv('AUTHOR_URL', 'https://codebender.cc/user/codebender') DISQUS_REQUESTS_PER_HOUR = 1000 DISQUS_WAIT = (DISQUS_REQUESTS_PER_HOUR / 60) / 60 +DISQUS_MIN_REMANING_REQUESTS = 10 CHANGE_LOG = 'examples_compile_log.json' DISQUS_COMMENTS = 'disqus_comments.json' EXAMPLES_WITHOUT_LIBRARY_DB = 'examples_without_library.json' @@ -58,6 +61,10 @@ def get_disqus_sso(self, user): return "{0} {1} {2}".format(message, sig, timestamp) def update_comment(self, sketch, results, current_date, log_entry, openFailFlag, total_sketches): + check_rate_limit = self.check_rate_limit() + if not check_rate_limit: + pytest.exit('Disqus API rate limit reached') + """A comment is added to the library as soon as its first example is compiled. `library`: The library in which belongs the currently compiled example. `self.last_library`: The library in which belongs the previously compiled example. @@ -250,3 +257,16 @@ def update_post(self, post_id, message): print 'Error:', error return comment_status + + def check_rate_limit(self): + usage_check = False + try: + r = requests.get('https://disqus.com/api/3.0/applications/listUsage.json?api_secret=' + self.DISQUS_API_SECRET + '&access_token=' + self.DISQUS_ACCESS_TOKEN) + api_remaning_limit = r.headers['X-Ratelimit-Remaining'] + if api_remaning_limit > DISQUS_MIN_REMANING_REQUESTS: + usage_check = True + + except Exception as error: + print 'Error:', error + + return usage_check diff --git a/codebender_testing/utils.py b/codebender_testing/utils.py index 5d01ef0..3f3faeb 100644 --- a/codebender_testing/utils.py +++ b/codebender_testing/utils.py @@ -543,6 +543,7 @@ def open_all_libraries_and_examples(self, url, logfile): print '\nVisiting:', len(urls_to_visit), 'URLs' tic = time.time() + toc = tic for url in urls_to_visit: self.open(url) self.get_element(By.CSS_SELECTOR, '#mycontainer') @@ -578,6 +579,8 @@ def open_all_libraries_and_examples(self, url, logfile): report_creator('fetch', log_entry, log_file) + print '\nTest duration:', int(toc - tic), 'sec' + def compile_sketch(self, url, boards, iframe=False, project_view=False): """Compiles the sketch located at `url`, or an iframe within the page referred to by `url`. Raises an exception if it does not compile. @@ -643,6 +646,7 @@ def comment_compile_libraries_examples(self, sketches, library_examples_dic={}, total_sketches = len(urls_to_visit) tic = time.time() + toc = tic library_re = re.compile(r'^(.+)/library/.+$') for url in urls_to_visit: @@ -752,6 +756,7 @@ def comment_compile_libraries_examples(self, sketches, library_examples_dic={}, # Generate a report if requested. if compile_type != 'target_library' and create_report and self.run_full_compile_tests: report_creator(compile_type, log_entry, log_file) + print '\nTest duration:', int(toc - tic), 'sec' def compile_all_sketches(self, url, selector, **kwargs): diff --git a/requirements.txt b/requirements.txt index 221bb1e..9277c76 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ pyyaml pytest selenium simplejson -disqus-python \ No newline at end of file +disqus-python +requests From 55445dde82ea34e6a6cb999f7c57d11374f1089b Mon Sep 17 00:00:00 2001 From: Christos Nianiakas Date: Thu, 9 Jun 2016 15:06:45 +0300 Subject: [PATCH 2/2] Removed unecessary prints from utils.py . --- codebender_testing/utils.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/codebender_testing/utils.py b/codebender_testing/utils.py index 3f3faeb..54fecc4 100644 --- a/codebender_testing/utils.py +++ b/codebender_testing/utils.py @@ -911,9 +911,7 @@ def change_privacy(self, privacy): privateRadioButton.click() def change_name(self, name): - print name nameField = self.get_element(By.CSS_SELECTOR,'#create-sketch-modal .modal-body [id="create-sketch-name"') - print nameField nameField.clear() nameField.send_keys(name) nameField.send_keys(Keys.ENTER)