|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import time |
| 10 | + |
| 11 | +SHELL='/bin/bash' |
| 12 | +SOURCE = 'codebender_cc' |
| 13 | + |
| 14 | +class Tests: |
| 15 | + def __init__(self, url, environment): |
| 16 | + self.source=SOURCE |
| 17 | + self.url = url |
| 18 | + self.environment = os.path.abspath(environment) |
| 19 | + self. email = os. getenv( 'EMAIL', '[email protected]') |
| 20 | + self.files_to_ignore = [ |
| 21 | + '.gitignore' |
| 22 | + ] |
| 23 | + |
| 24 | + def run(self, operation, libraries=None): |
| 25 | + if operation == 'common': |
| 26 | + self.common() |
| 27 | + elif operation == 'libraries': |
| 28 | + self.libraries() |
| 29 | + elif operation == 'examples': |
| 30 | + self.examples() |
| 31 | + elif operation == 'sketches': |
| 32 | + self.sketches() |
| 33 | + elif operation == 'compile': |
| 34 | + self.compile(libraries) |
| 35 | + elif operation == 'noplugin': |
| 36 | + self.noplugin() |
| 37 | + elif operation == 'walkthrough': |
| 38 | + self.walkthrough() |
| 39 | + elif operation == 'staging': |
| 40 | + self.staging() |
| 41 | + |
| 42 | + def run_command(self, command, user_agent=None): |
| 43 | + if user_agent: |
| 44 | + os.environ['SELENIUM_USER_AGENT'] = user_agent |
| 45 | + command = ' '.join(command) |
| 46 | + print('command:', command) |
| 47 | + return subprocess.call(command, shell=True, executable=SHELL) |
| 48 | + |
| 49 | + def send_mail_no_logs(self, identifier): |
| 50 | + command = ['mail', '-s', '"Selenium Tests: {identifier} Failed To Run" {email} <<< "Something went wrong with {identifier} tests. Please check the logs."'.format(identifier=identifier, email=self.email)] |
| 51 | + run_command(command) |
| 52 | + |
| 53 | + def send_mail_with_logs(self, identifier): |
| 54 | + default_tests_dir = os.path.normpath(os.path.join(os.getcwd(), '..')) |
| 55 | + root_dir = os.getenv('ROOTDIR', default_tests_dir) |
| 56 | + |
| 57 | + logs = os.path.join(root_dir, 'logs') |
| 58 | + reports = os.path.join(root_dir, 'reports') |
| 59 | + log_regexp = re.compile('.+{identifier}.+'.format(identifier=identifier)) |
| 60 | + |
| 61 | + try: |
| 62 | + logfile = sorted([filename for filename in os.listdir(logs) if filename not in files_to_ignore and log_regexp.match(filename)], reverse=True)[0] |
| 63 | + logfile_timestamp = re.match(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-.+', logfile).group(1) |
| 64 | + |
| 65 | + report_regexp = re.compile('report_{timestamp}-{identifier}_(\d+)'.format(timestamp=logfile_timestamp, identifier=identifier)) |
| 66 | + reportfile = sorted([filename for filename in os.listdir(reports) if filename not in files_to_ignore and report_regexp.match(filename)], reverse=True)[0] |
| 67 | + changes = report_regexp.match(reportfile).group(1) |
| 68 | + |
| 69 | + email_date = time.strftime('%Y-%m-%d %H:%M:%S') |
| 70 | + |
| 71 | + command = [ |
| 72 | + '(echo "Changes since the last time: {changes}";'.format(changes=changes), |
| 73 | + 'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile), |
| 74 | + 'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile), |
| 75 | + '| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email) |
| 76 | + ] |
| 77 | + run_command(command) |
| 78 | + except: |
| 79 | + pass |
| 80 | + |
| 81 | + def create_command(self, test_directory, *extra_arguments): |
| 82 | + return ['tox', 'tests/' + test_directory, '--', '--url={}'.format(TARGETS[self.url]), '--source={}'.format(SOURCE)] + list(extra_arguments) |
| 83 | + |
| 84 | + def common(self, identifier='common'): |
| 85 | + command = self.create_command('common', '--plugin') |
| 86 | + retval = self.run_command(command) |
| 87 | + if retval != 0: |
| 88 | + self.send_mail_no_logs(identifier) |
| 89 | + |
| 90 | + def libraries(self, identifier='libraries_fetch'): |
| 91 | + command = self.create_command('libraries_fetch', '-F', '--plugin') |
| 92 | + self.run_command(command) |
| 93 | + self.send_mail_with_logs(identifier) |
| 94 | + |
| 95 | + def examples(self, identifier='libraries_test'): |
| 96 | + command = self.create_command('libraries', '-F', '--plugin') |
| 97 | + self.run_command(command) |
| 98 | + self.send_mail_with_logs(identifier) |
| 99 | + |
| 100 | + def sketches(self, libraries, identifier='cb_compile_tester'): |
| 101 | + command = self.create_command('compile_tester', '-F', '--plugin') |
| 102 | + self.run_command(command) |
| 103 | + self.send_mail_with_logs(identifier) |
| 104 | + |
| 105 | + def compile(self, libraries): |
| 106 | + command = self.create_command('target_libraries', '-F', '--plugin', '--libraries={}'.format(libraries)) |
| 107 | + self.run_command(command) |
| 108 | + |
| 109 | + def noplugin(self, identifier = 'noplugin'): |
| 110 | + command = self.create_command('noplugin') |
| 111 | + retval = self.run_command(command) |
| 112 | + if retval != 0: |
| 113 | + self.send_mail_no_logs(identifier) |
| 114 | + |
| 115 | + def walkthrough(self, identifier='walkthrough'): |
| 116 | + command = self.create_command('walkthrough', '--plugin') |
| 117 | + retvals = [] |
| 118 | + user_agents = [ |
| 119 | + 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium', |
| 120 | + 'Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium', |
| 121 | + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium' |
| 122 | + ] |
| 123 | + for user_agent in user_agents: |
| 124 | + retval = self.run_command(command, user_agent=user_agent) |
| 125 | + retvals.append(retval) |
| 126 | + retval = max(retvals) |
| 127 | + if retval != 0: |
| 128 | + self.send_mail_no_logs(identifier) |
| 129 | + |
| 130 | + def staging(self): |
| 131 | + command = self.create_command('compile_tester', '-F', '--plugin') |
| 132 | + self.run_command(command) |
| 133 | + |
| 134 | +OPERATIONS = { |
| 135 | + 'common':'\tTest site common functionality', |
| 136 | + 'libraries': 'Visit all libraries and their examples', |
| 137 | + 'examples': 'Compile all examples', |
| 138 | + 'sketches': 'Compile sketch of cb_compile_tester user', |
| 139 | + 'compile': '\tCompile specific examples', |
| 140 | + 'noplugin': 'Run tests without app/plugin installed', |
| 141 | + 'walkthrough': 'Run tests for walkthrough', |
| 142 | + 'staging': '\tRun tests for staging only' |
| 143 | +} |
| 144 | + |
| 145 | +TARGETS = { |
| 146 | + 'live': 'https://codebender.cc', |
| 147 | + 'staging': 'https://staging.codebender.cc', |
| 148 | + 'local': 'http://dev.codebender.cc' |
| 149 | +} |
| 150 | + |
| 151 | +def main(): |
| 152 | + available_operations = ['{operation}\t{description}'.format(operation=x, description=OPERATIONS[x]) for x in sorted(OPERATIONS.keys())] |
| 153 | + available_targets = ['{target}\t{url}'.format(target=x, url=TARGETS[x]) for x in sorted(TARGETS.keys())] |
| 154 | + |
| 155 | + parser = argparse.ArgumentParser(description="seleniumbender - A command line tool for codebender's Selenium tests", |
| 156 | + epilog='Happy testing :)', |
| 157 | + formatter_class=argparse.RawTextHelpFormatter) |
| 158 | + parser.add_argument('operation', |
| 159 | + help='Operation to execute.\nAvailable operations:\n\t{operations}'.format(operations='\n\t'.join(available_operations))) |
| 160 | + parser.add_argument('--target', |
| 161 | + default='live', |
| 162 | + help='Target site for the tests.\nAvailable targets (default: live):\n\t{targets}'.format(targets='\n\t'.join(available_targets))) |
| 163 | + parser.add_argument('--config', |
| 164 | + default='env_vars.sh', |
| 165 | + help='Configuration file to load (default: config.cfg).') |
| 166 | + parser.add_argument('--libraries', |
| 167 | + default=None, |
| 168 | + help='Libraries to test (comma separated machine names) when using option: target') |
| 169 | + parser.add_argument('--saucelabs', |
| 170 | + action='store_true', |
| 171 | + default=False, |
| 172 | + help='Use saucelabs as the Selenium server') |
| 173 | + |
| 174 | + # Parse arguments |
| 175 | + args = parser.parse_args() |
| 176 | + operation = args.operation |
| 177 | + if operation not in OPERATIONS: |
| 178 | + print('Unsupported operation!\n') |
| 179 | + parser.print_help() |
| 180 | + sys.exit() |
| 181 | + |
| 182 | + target = args.target |
| 183 | + if target not in TARGETS: |
| 184 | + print('Unsupported target!\n') |
| 185 | + parser.print_help() |
| 186 | + sys.exit() |
| 187 | + |
| 188 | + config = args.config |
| 189 | + if not os.path.exists(config): |
| 190 | + print('Config file:', config, 'does not exist') |
| 191 | + sys.exit() |
| 192 | + |
| 193 | + libraries = args.libraries |
| 194 | + if operation == 'target' and not libraries: |
| 195 | + print('No target libraries specified!\n') |
| 196 | + parser.print_help() |
| 197 | + sys.exit() |
| 198 | + |
| 199 | + # Read environment variables file |
| 200 | + output = subprocess.check_output('source {}; env'.format(config), shell=True, executable=SHELL) |
| 201 | + env_vars = dict((line.split('=', 1) for line in output.splitlines())) |
| 202 | + os.environ.update(env_vars) |
| 203 | + |
| 204 | + # Run tests |
| 205 | + tests = Tests(target, config) |
| 206 | + tests.run(operation, libraries=libraries) |
| 207 | + |
| 208 | +if __name__ == '__main__': |
| 209 | + main() |
0 commit comments