def sign_nuget(ctx, path, certfile, certpass): if certfile and certpass: print("Signing {}\n".format(path)) else: print("Not signing: {}\n".format(path)) return cmd = "nuget sign {file} -CertificatePath {certfile} -CertificatePassword {certpass} -Timestamper {timestamp_server} -NonInteractive".format( file=path, certfile=certfile, certpass=certpass, timestamp_server=timestamp_server) ## is harder to debug, but suppress stdin/stdout/stderr echo (hide = true) so we ## don't print the password try: ctx.run(cmd, hide=True) except UnexpectedExit as e: error = "(UE) Failed to sign nuget package {file}\n".format(file=path) print(error) e.result.command = "redacted" raise UnexpectedExit(e.result, e.reason) except Failure as e: error = "(F) Failed to sign nuget package {file}\n".format(file=path) print(error) e.result.command = "redacted" raise Failure(e.result, e.reason) except Exit as e: error = "(E) Failed to sign nuget package {file}\n".format(file=path) print(error) raise
def scm_init(ctx): """Init git repo (if required) and configure git flow. Raises: Failure: .gitignore does not exist Returns: None """ if not Path('.gitignore').is_file(): raise Failure('.gitignore does not exist') new_repo = not Path('.git').is_dir() if new_repo: uri_remote = '[email protected]:{}/{}.git'.format(ctx.scm.repo_owner, ctx.scm.repo_slug) ctx.run('git init') ctx.run('git add .') ctx.run('git commit -m "Initialize repo"') ctx.run('git remote add origin {}'.format(uri_remote)) ctx.run('git tag -a "v_0.0.0" -m "cookiecutter ref"') ctx.run('git flow init -d') ctx.run('git flow config set versiontagprefix {}'.format( ctx.scm.version_tag_prefix)) if new_repo: ctx.run('git push -u origin master') ctx.run('git push -u origin develop') ctx.run('git push --tags')
def sign_binary(ctx, path, certfile, certpass): if certfile and certpass: print("Signing {}\n".format(path)) else: print("Not signing: {}\n".format(path)) return cmd = "signtool sign /f {certfile} /p {certpass} /t {timestamp_server} {file}".format( certfile=certfile, certpass=certpass, timestamp_server=timestamp_server, file=path) ## is harder to debug, but suppress stdin/stdout/stderr echo (hide = true) so we ## don't print the password try: ctx.run(cmd, hide=True) except UnexpectedExit as e: error = "(UE) Failed to sign file {file}\n".format(file=path) print(error) e.result.command = "redacted" raise UnexpectedExit(e.result, e.reason) except Failure as e: error = "(F) Failed to sign file {file}\n".format(file=path) print(error) e.result.command = "redacted" raise Failure(e.result, e.reason) except Exit as e: error = "(E) Failed to sign file {file}\n".format(file=path) print(error) raise
def test(c, cover=False, verbose=True, slow=True, eval=True, match=None, mark=None, debug=False, forked=False, fail_fast=False, jit=True): "Run tests" if not jit: os.environ['NUMBA_DISABLE_JIT'] = '1' import pytest args = ['tests'] if cover: args.append('--cov=lenskit') if verbose: args.append('--verbose') if fail_fast: args.append('-x') if not slow: args.append('-m') args.append('not slow') elif not eval: args.append('-m') args.append('not eval') if match: args.append('-k') args.append(match) if mark: args.append('-m') args.append(mark) if debug: args.append('--log-cli-level=DEBUG') if forked: args.append('--forked') rc = pytest.main(args) if rc: raise Failure(Result(exited=rc), 'tests failed')
def glide(c, version='v0.13.2'): """Download glide.""" print('Downloading glide') # find glide version, copied from glide installation script os_name = platform.system().lower() if os_name == 'linux': arch = 'linux-amd64' elif os_name == 'darwin': arch = 'darwin-amd64' else: raise Failure(os_name, reason='Only supported on OSx and Linux') print('Glide arch: {arch}'.format(arch=arch)) # avoid redownloading file if exists if os.path.isfile('{cwd}/glide'.format(cwd=c.cwd)): print('Glide exists') return c.run( 'curl -sSLo glide.tar.gz https://github.com/Masterminds/glide/releases/download/{version}/glide-{version}-{arch}.tar.gz' .format(version=version, arch=arch), hide='stdout') c.run('tar -zxf ./glide.tar.gz', hide='stdout') c.run('mv ./{arch}/glide ./glide'.format(arch=arch), hide='stdout') c.run('rm -rf ./{arch} ./glide.tar.gz'.format(arch=arch), hide='stdout') print('Glide downloaded')
def test_api(c, product, backend=''): if product == 'recursor': with c.cd('regression-tests.api'): c.run(f'PDNSRECURSOR=/opt/pdns-recursor/sbin/pdns_recursor ./runtests recursor {backend}') elif product == 'auth': with c.cd('regression-tests.api'): c.run(f'PDNSSERVER=/opt/pdns-auth/sbin/pdns_server PDNSUTIL=/opt/pdns-auth/bin/pdnsutil SDIG=/opt/pdns-auth/bin/sdig MYSQL_HOST="127.0.0.1" PGHOST="127.0.0.1" PGPORT="5432" ./runtests authoritative {backend}') else: raise Failure('unknown product')
def _fail(msg=''): """Fail a task, logging a message to stderr raises a special Failure Exception from invoke. """ if msg: print(msg, file=sys.stderr) # raising a Failure allows us to avoid a traceback # we only care about exited, but stdout, stderr, pty are required args raise Failure(Result(stdout='', stderr='', pty=False, exited=1))
def run_output_check(self, cmd, timeout=None, ignore_status=False, stdout_ok_regexp=None, stdout_err_regexp=None, stderr_ok_regexp=None, stderr_err_regexp=None, connect_timeout=300): """ Run a cmd on the remote host, check output to determine success. :param cmd: The cmd line string. :param timeout: Time limit in seconds before attempting to kill the running process. The run() function will take a few seconds longer than 'timeout' to complete if it has to kill the process. :param ignore_status: Do not raise an exception, no matter what the exit code of the cmd is. :param stdout_ok_regexp: Regular expression that should be in stdout if the cmd was successul. :param stdout_err_regexp: Regular expression that should be in stdout if the cmd failed. :param stderr_ok_regexp: regexp that should be in stderr if the cmd was successul. :param stderr_err_regexp: Regexp that should be in stderr if the cmd failed. :param connect_timeout: Connection timeout that will be passed to run. :raises: OutputCheckError under the following conditions: - The exit code of the cmd execution was not 0. - If stderr_err_regexp is found in stderr, - If stdout_err_regexp is found in stdout, - If stderr_ok_regexp is not found in stderr. - If stdout_ok_regexp is not found in stdout, """ # We ignore the status, because we will handle it at the end. result = self.run(cmd=cmd, timeout=timeout, ignore_status=True, connect_timeout=connect_timeout) # Look for the patterns, in order for (regexp, stream) in ((stderr_err_regexp, result.stderr), (stdout_err_regexp, result.stdout)): if regexp and stream: err_re = re.compile(regexp) if err_re.search(stream): e_msg = ('%s failed, found error pattern: "%s"' % (cmd, regexp)) raise OutputCheckError(e_msg, result) for (regexp, stream) in ((stderr_ok_regexp, result.stderr), (stdout_ok_regexp, result.stdout)): if regexp and stream: ok_re = re.compile(regexp) if ok_re.search(stream): if ok_re.search(stream): return if not ignore_status and result.exit_status > 0: raise Failure(result)
class RunnerTestCase(unittest.TestCase): @mock.patch('frigg_settings.build_settings') def test_runner_init(self, mock_build_settings): """ Test init of the runner class """ runner = Runner(failfast=True, verbose=True) self.assertTrue(runner.fail_fast) self.assertTrue(runner.verbose) self.assertEqual(runner.directory, os.getcwd()) mock_build_settings.assert_called_once_with(runner.directory, runner_wrapper) @mock.patch('os.path.exists', side_effect=lambda *args, **kwargs: False) @mock.patch('frigg_settings.build_settings') def test_runner_init_cwd_not_found(self, mock_build_settings, mock_exists): """ Test init of the runner class """ self.assertRaises(SystemExit, Runner, True, True, '/tmp/doesnotexcist') @mock.patch('frigg_settings.build_settings', side_effect=RuntimeError) def test_no_tasks(self, mock_build_settings): """ No tasks, system exit. """ self.assertRaises(SystemExit, Runner, False, False) @mock.patch('frigg_settings.build_settings', side_effect=TypeError) def test_invalid_frigg_file_format(self, mock_build_settings): """ Test invalid yaml format in frigg file """ self.assertRaises(SystemExit, Runner, False, False) @mock.patch(OPEN_MODULE, side_effect=lambda *args, **kwargs: FileIO('coverage_report')) @mock.patch('frigg_settings.build_settings', side_effect=lambda *args, **kwargs: {}) @mock.patch('os.path.exists', side_effect=lambda *args, **kwargs: True) @mock.patch('frigg_coverage.parse_coverage', side_effect=lambda *args, **kwargs: 10) def test_coverage_success(self, mock_parse_coverage, mock_exists, mock_build_settings, mock_open): """ Test coverage result print """ runner = Runner() runner.config['coverage'] = { 'path': 'coverage.xml', 'parser': 'python' } runner.coverage() mock_parse_coverage.assert_called_with( 'coverage_report', runner.config['coverage']['parser']) @mock.patch('frigg_settings.build_settings', side_effect=lambda *args, **kwargs: {}) @mock.patch('os.path.exists', side_effect=lambda *args, **kwargs: True) def test_coverage_no_config(self, mock_exists, mock_build_settings): """ Make sure no exception raises when the coverage config not exist """ runner = Runner() if 'coverage' in runner.config.keys(): del (runner.config['coverage']) runner.coverage() @mock.patch('sys.exit') @mock.patch('frigg_settings.build_settings', side_effect=lambda *args, **kwargs: {}) @mock.patch('os.path.exists', side_effect=lambda *args, **kwargs: True) def test_coverage_invalid_config(self, mock_exists, mock_build_settings, mock_exit): """ Test coverage function with invalid config """ runner = Runner() runner.config['coverage'] = True runner.coverage() mock_exit.assert_called_once_with(1) @mock.patch('invoke.run') @mock.patch('frigg_settings.build_settings') def test_run_command(self, mock_build_settings, mock_run): """ Test function for running commands """ runner = Runner(verbose=True, path='/tmp') runner.run_task('echo "Hello"') mock_run.assert_called_once_with('cd %s && echo "Hello"' % runner.directory, hide=False, encoding='utf8', pty=True) @mock.patch('invoke.run') @mock.patch('frigg_settings.build_settings', side_effect=lambda *args, **kwargs: {'verbose_tasks': ['echo "Hello"']}) def test_run_command_verbose_task(self, mock_build_settings, mock_run): """ Test function for running commands """ runner = Runner(failfast=False, verbose=False, path='/tmp') runner.run_task('echo "Hello"') mock_run.assert_called_once_with('cd %s && echo "Hello"' % runner.directory, hide=False, encoding='utf8', pty=True) @mock.patch('frigg_settings.build_settings') @mock.patch('invoke.run', side_effect=Failure('Custom result')) def test_run_command_failure(self, mock_run, mock_build_settings): """ Test function for command exec when the invoke return a Failure object """ runner = Runner(path='/tmp') function_time, result = runner.run_task('echo "Hello"') mock_run.assert_called_once_with('cd %s && echo "Hello"' % runner.directory, hide=True, encoding='utf8', pty=True) self.assertEqual(result, 'Custom result') self.assertIsNotNone(function_time) @mock.patch('frigg_settings.build_settings') @mock.patch('invoke.run', side_effect=lambda *args, **kwargs: sys.exit(1)) def test_run_command_exit(self, mock_run, mock_build_settings): """ Test function for command exec when invoke exits """ runner = Runner(path='/tmp') function_time, result = runner.run_task('echo "Hello"') mock_run.assert_called_once_with('cd %s && echo "Hello"' % runner.directory, hide=True, encoding='utf8', pty=True) self.assertIsNone(result) @mock.patch('frigg_runner.runner.Runner.coverage') @mock.patch('frigg_settings.build_settings') def test_handle_result(self, mock_build_settings, mock_coverage): """ Test sysexit when the build is done. """ runner = Runner(setup=True) res1 = Result('', '', True, None) res1.task = 'tox' res1.time = 1 res2 = Result('', '', False, None) res2.task = 'flake8' res2.time = 2 res3 = Result('', '', True, None) res3.task = 'bower install' res3.time = 3 res4 = Result('', '', False, None) res4.task = 'exit 1' res4.time = 4 try: runner.handle_results([res1, res2], [res3, res4]) except SystemExit as sys_exit: self.assertEqual(sys_exit.code, 1) self.assertTrue(mock_coverage.called) try: runner.handle_results([res2], [res3, res4]) except SystemExit as sys_exit: self.assertEqual(sys_exit.code, 0) self.assertRaises(SystemExit, runner.handle_results, [res1, res2], [res3, res4]) @mock.patch('frigg_runner.runner.Runner.handle_results') @mock.patch('frigg_settings.build_settings') @mock.patch('frigg_runner.runner.Runner.run_task', lambda *args, **kwargs: RUN_TASK_RESULT) def test_run(self, mock_build_settings, mock_handle_results): """ Test the run function """ runner = Runner() runner.config = {'tasks': ['flake8', 'tox']} runner.run() self.assertTrue(mock_handle_results.called) @mock.patch('frigg_runner.runner.Runner.handle_results') @mock.patch('frigg_settings.build_settings') @mock.patch('frigg_runner.runner.Runner.run_task', lambda *args, **kwargs: RUN_TASK_RESULT) def test_run_verbose(self, mock_build_settings, mock_handle_results): """ Test the run function """ runner = Runner(verbose=True) runner.config = {'tasks': ['flake8', 'tox']} runner.run() self.assertTrue(mock_handle_results.called) @skip('This test has never worked, just silently failed.' 'Because failfast makes the app exit.') @mock.patch('frigg_runner.runner.Runner.handle_results') @mock.patch('frigg_settings.build_settings') @mock.patch('frigg_runner.runner.Runner.run_task', lambda *args, **kwargs: RUN_TASK_RESULT) def test_run_fail_fast(self, mock_build_settings, mock_handle_results): """ Test the run function """ runner = Runner(failfast=True) runner.config = {'tasks': ['flake8', 'tox']} self.assertRaises(SystemExit, runner.run) self.assertTrue(mock_handle_results.called) @mock.patch('frigg_runner.runner.Runner.handle_results') @mock.patch('frigg_runner.runner.Runner.run_task', lambda *args, **kwargs: RUN_TASK_RESULT) def test_run_setup_tasks(self, mock_handle_results): """ Test the run function with setup tasks """ runner = Runner(setup=True) runner.config = {'tasks': [], 'setup_tasks': ['bower install']} runner.run() self.assertTrue(mock_handle_results.called)