def do_testing(self, ntry, params): """ Testing stage of testing @param ntry: number of try @type ntry: int @param params: list of testing parameters @type params: list """ try: stage = params['stages'][0] logging.debug(self.getName() + ': trying to do testing for ' + params['iname'] + ' ' + stage + ', ntry ' + str(ntry)) (ssh_key_name, ssh_key) = yamlconfig['ssh'][params['region']] logging.debug(self.getName() + ': ssh-key ' + ssh_key) con = Connection(params['instance'], 'root', ssh_key) Expect.ping_pong(con, 'uname', 'Linux') logging.info(self.getName() + ': doing testing for ' + params['iname'] + ' ' + stage) try: test_name = stage.split(':')[1] testcase = getattr(sys.modules['valid.testing_modules.' + test_name], test_name)() logging.debug(self.getName() + ': doing test ' + test_name + ' for ' + params['iname'] + ' ' + stage) test_result = testcase.test(con, params) logging.debug(self.getName() + ': ' + params['iname'] + ': test ' + test_name + ' finised with ' + str(test_result)) result = test_result except (AttributeError, TypeError, NameError, IndexError, ValueError, KeyError, boto.exception.EC2ResponseError), e: logging.error(self.getName() + ': bad test, %s %s' % (stage, e)) logging.debug(self.getName() + ':' + traceback.format_exc()) result = 'Failure' logging.info(self.getName() + ': done testing for ' + params['iname'] + ' ' + stage) params_new = params.copy() if len(params['stages']) > 1: params_new['stages'] = params['stages'][1:] mainq.put((0, 'test', params_new)) else: mainq.put((0, 'terminate', params_new)) logging.debug(self.getName() + ': done testing for ' + params['iname'] + ', result: ' + str(result)) params['result'] = {params['stages'][0]: result} con.disconnect() self.report_results(params)
def do_setup(self, ntry, params): """ Setup stage of testing @param ntry: number of try @type ntry: int @param params: list of testing parameters @type params: list """ try: logging.debug(self.getName() + ': trying to do setup for ' + ', ntry ' + str(ntry)) (ssh_key_name, ssh_key) = yamlconfig['ssh'][params['region']] logging.debug(self.getName() + ': ssh-key ' + ssh_key) for user in ['ec2-user', 'cloud-user']: # If we're able to login with one of these users allow root ssh immediately try: con = Connection(params['instance'], user, ssh_key) Expect.ping_pong(con, 'uname', 'Linux') Expect.ping_pong(con, 'sudo su -c \'cp -af /home/' + user + '/.ssh/authorized_keys /root/.ssh/authorized_keys; chown root.root /root/.ssh/authorized_keys; restorecon /root/.ssh/authorized_keys\' && echo SUCCESS', '\r\nSUCCESS\r\n') except: pass con = Connection(params['instance'], 'root', ssh_key) remote_command(con, '[ `uname` = \'Linux\' ]') logging.debug(self.getName() + ': sleeping for ' + str(settlewait) + ' sec. to make sure instance has been settled.') time.sleep(settlewait) setup_scripts = [] if 'setup' in yamlconfig: # upload and execute a setup script as root in /tmp/ logging.debug(self.getName() + ': executing global setup script: %s' % yamlconfig['setup']) local_script_path = os.path.expandvars(os.path.expanduser(yamlconfig['setup'])) setup_scripts.append(local_script_path) tf = tempfile.NamedTemporaryFile(delete=False) if 'setup' in params.keys() and params['setup']: if type(params['setup']) is list: params['setup'] = '\n'.join(map(lambda x: str(x), params['setup'])) logging.debug(self.getName() + ': executing ami-specific setup script: %s' % params['setup']) tf.write(params['setup']) setup_scripts.append(tf.name) tf.close() for script in setup_scripts: remote_script_path = '/tmp/' + os.path.basename(script) con.sftp.put(script, remote_script_path) con.sftp.chmod(remote_script_path, 0700) remote_command(con, remote_script_path) os.unlink(tf.name) con.disconnect() mainq.put((0, 'test', params.copy())) except (socket.error, paramiko.SFTPError, paramiko.SSHException, paramiko.PasswordRequiredException, paramiko.AuthenticationException, ExpectFailed) as e: logging.debug(self.getName() + ': got \'predictable\' error during instance setup, %s, ntry: %i' % (e, ntry)) logging.debug(self.getName() + ':' + traceback.format_exc()) time.sleep(10) mainq.put((ntry + 1, 'setup', params.copy())) except Exception, e: logging.error(self.getName() + ': got error during instance setup, %s %s, ntry: %i' % (type(e), e, ntry)) logging.debug(self.getName() + ':' + traceback.format_exc()) time.sleep(10) mainq.put((ntry + 1, 'setup', params.copy()))