| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import ConfigParser |
| 5 | import datetime |
| 6 | import os |
| 7 | import platform |
| 8 | import shlex |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | USER_CONFIG = '~/.java_driver_tests.conf' |
| 13 | |
| 14 | def read_config(section, option, data_type='string', default=None): |
| 15 | '''Read configs as stored in the above defined USER_CONFIG.''' |
| 16 | |
| 17 | config = ConfigParser.ConfigParser() |
| 18 | config.read([os.path.expanduser(USER_CONFIG)]) |
| 19 | |
| 20 | if config.has_option(section, option): |
| 21 | if data_type == 'string': |
| 22 | to_return = config.get(section, option) |
| 23 | elif data_type == 'boolean': |
| 24 | to_return = config.getboolean(section, option) |
| 25 | return default if default else to_return |
| 26 | |
| 27 | return default if default else False |
| 28 | |
| 29 | def read_commandline(command): |
| 30 | '''Simple shell read access.''' |
| 31 | |
| 32 | return subprocess.check_output(command, shell=True) |
| 33 | |
| 34 | def execute(command): |
| 35 | '''Simple shell execute access.''' |
| 36 | |
| 37 | print 'Running command:\n\t%s\n' % command |
| 38 | subprocess.call(shlex.split(command)) |
| 39 | |
| 40 | def parse(): |
| 41 | '''Creates the argument parser for this tool.''' |
| 42 | |
| 43 | parser = argparse.ArgumentParser(description='command line tool for quick testing commands.') |
| 44 | parser.add_argument('--test', help='run a specific unit test') |
| 45 | parser.add_argument('--cassandra-version', help='run tests on a specific Cassandra version') |
| 46 | parser.add_argument('--upload', action='store_true', help='upload cobertura site to configured server') |
| 47 | parser.add_argument('--clean', action='store_true', help='runs the maven project from a clean environment') |
| 48 | parser.add_argument('--automated', action='store_true', help='ensures that runs do not get hung up by user input requests') |
| 49 | parser.add_argument('--testdocs', action='store_true', help='generates the test\'s Javadoc') |
| 50 | parser.add_argument('--samplecode', action='store_true', help='prints generated sample CQL') |
| 51 | args = parser.parse_args() |
| 52 | return args |
| 53 | |
| 54 | def check_path(): |
| 55 | '''Ensures this tool is run from the java-driver home directory.''' |
| 56 | |
| 57 | if not 'driver-core' in read_commandline('ls'): |
| 58 | sys.exit('Execute this command from your java-driver root directory.') |
| 59 | |
| 60 | |
| 61 | def maybe_setup_loopbacks(): |
| 62 | '''Currently only setting loopbacks for Mac OSX, but feel free to contribute for other setups.''' |
| 63 | |
| 64 | if platform.system() == 'Darwin' and not read_config('general', 'no_loopbacks', data_type='boolean'): |
| 65 | print 'Setting up CCM loopbacks...' |
| 66 | try: |
| 67 | loopbacks_enabled = read_commandline('ifconfig | grep "inet 127.0.1.4 netmask 0xff000000"') |
| 68 | if not loopbacks_enabled: |
| 69 | raise subprocess.CalledProcessError |
| 70 | except subprocess.CalledProcessError: |
| 71 | # For basic ccm |
| 72 | execute('sudo ifconfig lo0 alias 127.0.0.2 up') |
| 73 | execute('sudo ifconfig lo0 alias 127.0.0.3 up') |
| 74 | |
| 75 | # Additional loopbacks for the java-driver |
| 76 | execute('sudo ifconfig lo0 alias 127.0.1.1 up') |
| 77 | execute('sudo ifconfig lo0 alias 127.0.1.2 up') |
| 78 | execute('sudo ifconfig lo0 alias 127.0.1.3 up') |
| 79 | execute('sudo ifconfig lo0 alias 127.0.1.4 up') |
| 80 | execute('sudo ifconfig lo0 alias 127.0.1.5 up') |
| 81 | execute('sudo ifconfig lo0 alias 127.0.1.6 up') |
| 82 | |
| 83 | def maybe_upload_cobertura_site(): |
| 84 | ''' |
| 85 | The following must be set in the above defined USER_CONFIG: |
| 86 | [general] |
| 87 | cobertura_server = xxx |
| 88 | cobertura_directory = xxx |
| 89 | |
| 90 | If defined, the cobertura site will be rsync'd to a remote location. |
| 91 | ''' |
| 92 | |
| 93 | cobertura_server = read_config('general', 'cobertura_server') |
| 94 | cobertura_directory = read_config('general', 'cobertura_directory') |
| 95 | |
| 96 | if cobertura_server and cobertura_directory: |
| 97 | print 'rsync-ing cobertura site to %s:%s...' % ( |
| 98 | cobertura_server, |
| 99 | cobertura_directory) |
| 100 | execute('rsync -avz testing/cobertura-history %s:%s' % ( |
| 101 | cobertura_server, |
| 102 | cobertura_directory)) |
| 103 | |
| 104 | def save_cobertura_site(): |
| 105 | '''Save cobertura site folders by date''' |
| 106 | |
| 107 | execute('mkdir -p testing/cobertura-history') |
| 108 | today = datetime.date.today() |
| 109 | execute('cp -r driver-core/target/site/cobertura testing/cobertura-history/cobertura-%s' % today) |
| 110 | execute('rm -rf testing/cobertura-history/current') |
| 111 | execute('cp -r driver-core/target/site/cobertura testing/cobertura-history/current') |
| 112 | |
| 113 | def main(): |
| 114 | check_path() |
| 115 | args = parse() |
| 116 | |
| 117 | # Check if an upload is all that is required |
| 118 | if args.upload: |
| 119 | maybe_upload_cobertura_site() |
| 120 | sys.exit() |
| 121 | |
| 122 | if args.testdocs: |
| 123 | execute('mvn javadoc:test-javadoc') |
| 124 | print '\nTo view test Javadocs:' |
| 125 | print '\topen driver-core/target/site/testapidocs/index.html' |
| 126 | sys.exit() |
| 127 | |
| 128 | # Setup required ccm loopbacks |
| 129 | maybe_setup_loopbacks() |
| 130 | |
| 131 | # Start building the mvn command |
| 132 | cobertura_build_command = 'mvn' |
| 133 | |
| 134 | # Add the clean target, if asked or building the entire project |
| 135 | if args.clean or not args.test: |
| 136 | cobertura_build_command += ' clean' |
| 137 | |
| 138 | cobertura_build_command += ' versions:display-dependency-updates' # Ensures dependencies are up to date |
| 139 | cobertura_build_command += ' cobertura:cobertura' # Runs code coverage plugin |
| 140 | cobertura_build_command += ' --projects driver-core' # Runs only the main module |
| 141 | |
| 142 | if args.samplecode: |
| 143 | cobertura_build_command += ' -Pdoc' # Runs the docs "tests" and prints sample code |
| 144 | else: |
| 145 | cobertura_build_command += ' -Plong' # Runs the integration tests, not just tests |
| 146 | |
| 147 | # Use a specific Cassandra version, if asked |
| 148 | if args.cassandra_version: |
| 149 | cobertura_build_command += ' -Dcassandra.version=%s' % args.cassandra_version |
| 150 | |
| 151 | if args.test: |
| 152 | # Run against a single mvn test |
| 153 | execute('%s' |
| 154 | ' -Dmaven.test.failure.ignore=true' |
| 155 | ' -DfailIfNoTests=false' |
| 156 | ' -Dtest=%s' % (cobertura_build_command, args.test)) |
| 157 | else: |
| 158 | # Run against the entire integration suite |
| 159 | execute('%s' % cobertura_build_command) |
| 160 | |
| 161 | try: |
| 162 | if args.automated or raw_input('Save Cobertura Report? [y/N] ').lower() == 'y': |
| 163 | |
| 164 | # Save out cobertura site files |
| 165 | save_cobertura_site() |
| 166 | |
| 167 | # Perhaps move cobertura site files to a central location |
| 168 | maybe_upload_cobertura_site() |
| 169 | except KeyboardInterrupt: |
| 170 | print |
| 171 | |
| 172 | # Optionally, open coverage report when done building |
| 173 | if read_config('general', 'open_cobertura_site_after_build', 'boolean'): |
| 174 | execute('open driver-core/target/site/cobertura/index.html') |
| 175 | else: |
| 176 | print '\nTo view Cobertura report:' |
| 177 | print '\topen driver-core/target/site/cobertura/index.html' |
| 178 | |
| 179 | |
| 180 | if __name__ == '__main__': |
| 181 | main() |