def global_setup_script(params): """ Custom Setup stage of testing @param params: testing parameters @type params: dict """ hostname = params['hostname'] script = None try: script = params['global_setup_script'] logger.debug('%s %s got global setup script: %s', brief(params), hostname, script) except KeyError as err: logger.debug('%s no global setup script', brief(params)) if script is None: # nothing to do return params script = os.path.expandvars(os.path.expanduser(script)) remote_script = '/tmp/' + basename(script) script_timeout = params.get('global_setup_script_timeout', DEFAULT_GLOBAL_SETUP_SCRIPT_TIMEOUT) con = get_connection(params) logger.debug('%s: got connection', hostname) con.sftp.put(script, remote_script) logger.debug('%s sftp succeeded %s -> %s', hostname, script, remote_script) con.sftp.chmod(remote_script, 0700) logger.debug('%s chmod succeeded 0700 %s', hostname, remote_script) Expect.ping_pong(con, '%s && echo SUCCESS' % remote_script, '\r\nSUCCESS\r\n', timeout=script_timeout) logger.debug('%s set up script finished %s', hostname, remote_script) return params
def global_setup_script(params): """ Custom Setup stage of testing @param params: testing parameters @type params: dict """ hostname = params['hostname'] script = None try: script = params['global_setup_script'] logger.debug('%s %s got global setup script: %s', brief(params), hostname, script) except KeyError as err: logger.debug('%s no global setup script', brief(params)) if script is None: # nothing to do return params script = os.path.expandvars(os.path.expanduser(script)) remote_script = '/tmp/' + os.path.basename(script) script_timeout = params.get('global_setup_script_timeout', DEFAULT_GLOBAL_SETUP_SCRIPT_TIMEOUT) con = get_connection(params) logger.debug('%s: got connection', hostname) con.sftp.put(script, remote_script) logger.debug('%s sftp succeeded %s -> %s', hostname, script, remote_script) con.sftp.chmod(remote_script, 0700) logger.debug('%s chmod succeeded 0700 %s', hostname, remote_script) Expect.ping_pong(con, '%s && echo SUCCESS' % remote_script, '\r\nSUCCESS\r\n', timeout=script_timeout) logger.debug('%s set up script finished %s', hostname, remote_script) return params
def test_execute(params): """ Testing stage: perform all tests required in params @param params: testing parameters @type params: dict @return: list of test results """ assert 'test' in params, 'test field missing in %s' % brief(params) assert 'name' in params['test'], 'test name field missing in %s' % brief(params) assert 'stage' in params['test'], 'test stage field missing in %s' % brief(params) test_name = params['test']['name'] test_stage = params['test']['stage'] params['test']['exception'] = None params['test']['log'] = [] params['test']['result'] = RESULT_PASSED hostname = params['hostname'] logger.debug('trying %s %s %s', hostname, test_stage, test_name) try: test_cls = TEST_CLASSES[test_name] stage = TEST_STAGES[test_stage] assert test_name in stage, 'could not locate test %s in stage %s' % (test_name, test_stage) except KeyError as err: params['test_result'] = 'error: missing test/stage: %s/%s' % (test_name, test_stage) raise TestingError('missing test: %' % test_name) # asserts connection; tries reconnecting # FIXME: data race condition somewhere resets the ssh user # can't locate atm --- enforcing root /me ashamed params = params.copy() params['ssh']['user'] = '******' con = get_connection(params) params['test']['start_time'] = time.time() # perform the testing try: test_obj = test_cls() test_obj.test(con, params) logger.debug('%s %s %s succeeded', hostname, test_stage, test_name) except AssertionError as err: # not caught in the test case but means the test failed params['test']['result'] = RESULT_FAILED params['test']['exception'] = traceback.format_exc() except SkipException as err: # risen by a test case, means this test is skipped params['test']['result'] = RESULT_SKIP params['test']['exception'] = traceback.format_exc() else: # no assertion errors detected --- check all cmd logs test_cmd_results = [cmd['result'] for cmd in test_obj.log if 'result' in cmd] test_result = RESULT_FAILED in test_cmd_results and RESULT_FAILED or RESULT_PASSED test_result = RESULT_ERROR in test_cmd_results and RESULT_ERROR or test_result params['test']['result'] = test_result finally: params['test']['log'] = test_obj.log params['test']['end_time'] = time.time() return params
def test_execute(params): """ Testing stage: perform all tests required in params @param params: testing parameters @type params: dict @return: list of test results """ assert 'test' in params, 'test field missing in %s' % brief(params) assert 'name' in params['test'], 'test name field missing in %s' % brief( params) assert 'stage' in params['test'], 'test stage field missing in %s' % brief( params) test_name = params['test']['name'] test_stage = params['test']['stage'] params['test']['exception'] = None params['test']['log'] = [] params['test']['result'] = RESULT_PASSED hostname = params['hostname'] logger.debug('trying %s %s %s', hostname, test_stage, test_name) try: test_cls = TEST_CLASSES[test_name] stage = TEST_STAGES[test_stage] assert test_name in stage, 'could not locate test %s in stage %s' % ( test_name, test_stage) except KeyError as err: params['test_result'] = 'error: missing test/stage: %s/%s' % ( test_name, test_stage) raise TestingError('missing test: %' % test_name) # asserts connection; tries reconnecting con = get_connection(params) # perform the testing try: test_obj = test_cls() test_obj.test(con, params) logger.debug('%s %s %s succeeded', hostname, test_stage, test_name) except AssertionError as err: # not caught in the test case but means the test failed params['test']['result'] = RESULT_FAILED params['test']['exception'] = traceback.format_exc() else: # no assertion errors detected --- check all cmd logs test_cmd_results = [ cmd['result'] for cmd in test_obj.log if 'result' in cmd ] test_result = RESULT_FAILED in test_cmd_results and RESULT_FAILED or RESULT_PASSED test_result = RESULT_ERROR in test_cmd_results and RESULT_ERROR or test_result params['test']['result'] = test_result finally: params['test']['log'] = test_obj.log return params
def allow_root_login(params): '''allow root ssh login''' _, host, user, ssh_key = connection_cache_key(params) if user == 'root': # user root --- nothing to do logger.debug('user already root for %s', brief(params)) return params from textwrap import dedent command = dedent(r''' sudo cp -af /home/%s/.ssh/authorized_keys /root/.ssh/authorized_keys && \ sudo chown root.root /root/.ssh/authorized_keys && \ sudo restorecon -Rv /root/.ssh && \ echo SUCCESS ''' % user) # Exceptions cause retries, save for ExpectFailed with connection_ctx(host, user, ssh_key) as con: save_bash_history(con) try: Expect.ping_pong(con, command, '(?s).*\r\nSUCCESS\r\n.*') except ExpectFailed as err: # retry raise EAgain(err) # update user to root params['ssh']['user'] = '******' return params
def allow_root_login(params): '''allow root ssh login''' _, host, user, ssh_key = connection_cache_key(params) if user == 'root': # user root --- nothing to do logger.debug('user already root for %s', brief(params)) return params from textwrap import dedent # FIXME: copying the skel items explicitly so paramiko minds the prompt # rhel/fedora atomic issue: http://ask.projectatomic.io/en/question/141/root-home-missing-usual-skel-items/ command = dedent(r''' sudo cp -af /home/%s/.ssh/authorized_keys /root/.ssh/authorized_keys && \ sudo chown root.root /root/.ssh/authorized_keys && \ sudo restorecon -Rv /root/.ssh && \ sudo cp -f /etc/skel/.bash* ~root/ && \ echo SUCCESS ''' % user) # Exceptions cause retries, save for ExpectFailed with connection_ctx(host, user, ssh_key) as con: save_bash_history(con) try: Expect.ping_pong(con, command, '(?s).*\r\nSUCCESS\r\n.*') except ExpectFailed as err: # retry raise EAgain(err) # update user to root params['ssh']['user'] = '******' return params
def terminate_instance(params): """ Terminate stage of testing @param params: testing parameters @type params: dict """ hostname = params['hostname'] if 'keepalive' in params and params['keepalive']: logger.info('will not terminate %s (keepalive requested)', hostname) return try: drop_connection(params) except ConnectionCacheError as err: logger.debug('not dropping any connection to %s; not in cache', brief(params)) driver = cloud.get_driver(params['cloud'], logger, CLOUD_DRIVER_MAXWAIT) logger.debug('trying to terminate %s', hostname) driver.terminate(params) logger.info('terminated %s', hostname) return params