Example #1
0
    def prepare(self):
        """ Phone preparation: install apps etc

        pipeline:
            install lightning
            install apks
            clean log
        """
        # apps cleanup
        for apk in self.cleanup_apps:
            self.adb_execution("adb -s {device_id} uninstall {app}".format(device_id=self.source, app=apk))

        # install lightning
        self.lightning_apk_fname = resource.get_opener(self.lightning_apk_path).get_filename
        logger.info('Installing lightning apk...')
        self.adb_execution(
            "adb -s {device_id} install -r -d -t {apk}".format(device_id=self.source, apk=self.lightning_apk_fname)
        )

        # install apks
        for apk in self.test_apps:
            apk_fname = resource.get_opener(apk).get_filename
            self.adb_execution("adb -s {device_id} install -r -d -t {apk}".format(device_id=self.source, apk=apk_fname))

        # clean logcat
        self.adb_execution("adb -s {device_id} logcat -c".format(device_id=self.source))
Example #2
0
    def patch_config(self, config):
        """
        download remote resources, replace links with local filenames
        add result file section
        :param dict config: pandora config
        """
        # get expvar parameters
        if config.get("monitoring"):
            if config["monitoring"].get("expvar"):
                self.expvar = config["monitoring"]["expvar"].get("enabled")
                if config["monitoring"]["expvar"].get("port"):
                    self.expvar_port = config["monitoring"]["expvar"].get(
                        "port")
                else:
                    self.expvar_port = self.DEFAULT_EXPVAR_PORT
        # or set if expvar not exists
        else:
            config["monitoring"] = {
                "expvar": {
                    "enabled": True,
                }
            }
            self.expvar = True
            self.expvar_port = self.DEFAULT_EXPVAR_PORT

        # FIXME refactor pandora plx
        for pool in config['pools']:
            if pool.get('ammo', {}).get('file', ''):
                self.ammofile = pool['ammo']['file']
                opener = resource_manager.get_opener(self.ammofile)
                if isinstance(opener, HttpOpener):
                    pool['ammo']['file'] = opener.download_file(
                        True, try_ungzip=True)
                else:
                    pool['ammo']['file'] = opener.get_filename

            if pool.get('ammo', {}).get('source', {}).get('path', ''):
                self.ammofile = pool['ammo']['source']['path']
                opener = resource_manager.get_opener(self.ammofile)
                if isinstance(opener, HttpOpener):
                    pool['ammo']['source']['path'] = opener.download_file(
                        True, try_ungzip=True)
                else:
                    pool['ammo']['source']['path'] = opener.get_filename

            if not pool.get('result') or 'phout' not in pool.get(
                    'result', {}).get('type', ''):
                logger.warning(
                    'Seems like pandora result file not specified... adding defaults'
                )
                pool['result'] = dict(
                    destination=self.DEFAULT_REPORT_FILE,
                    type='phout',
                )
        return config
Example #3
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 try:
                     request = line.split('"')[1]
                     method, uri, proto = request.split()
                     http_ver = proto.split('/')[1]
                     if method == "GET":
                         yield (
                             HttpAmmo(
                                 uri,
                                 headers=self.headers,
                                 http_ver=http_ver, ).to_s(), None)
                     else:
                         self.warn(
                             "Skipped line: %s (unsupported method)" % line)
                 except (ValueError, IndexError) as e:
                     self.warn("Skipped line: %s (%s)" % (line, e))
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #4
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 if line.startswith('['):
                     self.headers.update(
                         _parse_header(line.strip('\r\n[]\t ')))
                 elif len(line.rstrip('\r\n')):
                     fields = line.split()
                     uri = fields[0]
                     if len(fields) > 1:
                         marker = fields[1]
                     else:
                         marker = None
                     yield (
                         HttpAmmo(
                             uri,
                             headers=[
                                 ': '.join(header)
                                 for header in self.headers.items()
                             ],
                             http_ver=self.http_ver, ).to_s(), marker)
             if info.status.ammo_count == 0:
                 self.log.error("No ammo in uri-style file")
                 raise AmmoFileError("No ammo! Cover me!")
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #5
0
    def prepare_test(self):
        try:
            console = self.core.get_plugin_of_type(ConsolePlugin)
        except KeyError as ex:
            logger.debug("Console not found: %s", ex)
            console = None

        if console:
            widget = PandoraInfoWidget(self)
            console.add_info_widget(widget)
            self.core.job.aggregator.add_result_listener(widget)

        for dep in self.dependencies:
            url = dep['url']
            filepath = dep['filepath']
            try:
                opener = resource_manager.get_opener(url, force_download=True)
            except resource_manager.RequestException:
                logger.exception('')
                raise RuntimeError(
                    'Failed to download jmeter dependency from %s' % url)
            else:
                # Подразумевается, что зависимости будут скачиваться с удаленного ресурса по http.
                if not os.path.exists(
                        os.path.abspath(os.path.dirname(filepath))):
                    os.makedirs(os.path.abspath(os.path.dirname(filepath)))
                if isinstance(opener, HttpOpener):
                    downloaded_file = opener.download_file(use_cache=True)
                    shutil.move(downloaded_file, filepath)
                self.dependencies_paths.append(filepath)
Example #6
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener() as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 try:
                     request = line.split('"')[1]
                     method, uri, proto = request.split()
                     http_ver = proto.split('/')[1]
                     if method == "GET":
                         yield (
                             HttpAmmo(
                                 uri,
                                 headers=self.headers,
                                 http_ver=http_ver, ).to_s(), None)
                     else:
                         self.warn(
                             "Skipped line: %s (unsupported method)" % line)
                 except (ValueError, IndexError) as e:
                     self.warn("Skipped line: %s (%s)" % (line, e))
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #7
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener() as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 if line.startswith('['):
                     self.headers.update(
                         _parse_header(line.strip('\r\n[]\t ')))
                 elif len(line.rstrip('\r\n')):
                     fields = line.split()
                     uri = fields[0]
                     if len(fields) > 1:
                         marker = fields[1]
                     else:
                         marker = None
                     yield (
                         HttpAmmo(
                             uri,
                             headers=[
                                 ': '.join(header)
                                 for header in self.headers.items()
                             ],
                             http_ver=self.http_ver, ).to_s(), marker)
             if info.status.ammo_count == 0:
                 self.log.error("No ammo in uri-style file")
                 raise AmmoFileError("No ammo! Cover me!")
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #8
0
 def __get_stpd_filename(self):
     ''' Choose the name for stepped data file '''
     if self.use_caching:
         sep = "|"
         hasher = hashlib.md5()
         hashed_str = "cache version 6" + sep + \
             ';'.join(self.load_profile.schedule) + sep + str(self.loop_limit)
         hashed_str += sep + str(self.ammo_limit) + sep + ';'.join(
             self.load_profile.schedule) + sep + str(self.autocases)
         hashed_str += sep + ";".join(self.uris) + sep + ";".join(
             self.headers) + sep + self.http_ver + sep + b';'.join(
                 self.chosen_cases).decode('utf8')
         hashed_str += sep + str(self.enum_ammo) + sep + str(self.ammo_type)
         if self.load_profile.is_instances():
             hashed_str += sep + str(self.instances)
         if self.ammo_file:
             opener = resource.get_opener(self.ammo_file)
             hashed_str += sep + opener.hash
         else:
             if not self.uris:
                 raise RuntimeError("Neither ammofile nor uris specified")
             hashed_str += sep + \
                 ';'.join(self.uris) + sep + ';'.join(self.headers)
         self.log.debug("stpd-hash source: %s", hashed_str)
         hasher.update(hashed_str.encode('utf8'))
         if not os.path.exists(self.cache_dir):
             os.makedirs(self.cache_dir)
         stpd = self.cache_dir + '/' + \
             os.path.basename(self.ammo_file) + \
             "_" + hasher.hexdigest() + ".stpd"
     else:
         stpd = os.path.realpath("ammo.stpd")
     self.log.debug("Generated cache file name: %s", stpd)
     return stpd
Example #9
0
 def __get_stpd_filename(self):
     ''' Choose the name for stepped data file '''
     if self.use_caching:
         sep = "|"
         hasher = hashlib.md5()
         hashed_str = "cache version 6" + sep + \
             ';'.join(self.load_profile.schedule) + sep + str(self.loop_limit)
         hashed_str += sep + str(self.ammo_limit) + sep + ';'.join(
             self.load_profile.schedule) + sep + str(self.autocases)
         hashed_str += sep + ";".join(self.uris) + sep + ";".join(
             self.headers) + sep + self.http_ver + sep + ";".join(
                 self.chosen_cases)
         hashed_str += sep + str(self.enum_ammo) + sep + str(self.ammo_type)
         if self.load_profile.is_instances():
             hashed_str += sep + str(self.instances)
         if self.ammo_file:
             opener = resource.get_opener(self.ammo_file)
             hashed_str += sep + opener.hash
         else:
             if not self.uris:
                 raise RuntimeError("Neither ammofile nor uris specified")
             hashed_str += sep + \
                 ';'.join(self.uris) + sep + ';'.join(self.headers)
         self.log.debug("stpd-hash source: %s", hashed_str)
         hasher.update(hashed_str.encode('utf8'))
         if not os.path.exists(self.cache_dir):
             os.makedirs(self.cache_dir)
         stpd = self.cache_dir + '/' + \
             os.path.basename(self.ammo_file) + \
             "_" + hasher.hexdigest() + ".stpd"
     else:
         stpd = os.path.realpath("ammo.stpd")
     self.log.debug("Generated cache file name: %s", stpd)
     return stpd
Example #10
0
    def __iter__(self):
        def read_chunk_header(ammo_file):
            chunk_header = b''
            while chunk_header == b'':
                line = ammo_file.readline()
                if line.startswith(b'['):
                    self.headers.update(_parse_header(line.strip(b'\r\n[]\t ')))
                elif line == b'':
                    return line
                else:
                    chunk_header = line.strip(b'\r\n')
            return chunk_header

        opener = resource.get_opener(self.filename)
        with opener(self.use_cache) as ammo_file:
            info.status.af_size = opener.data_length
            # if we got StopIteration here, the file is empty
            chunk_header = read_chunk_header(ammo_file)
            while chunk_header:
                if chunk_header != b'':
                    try:
                        fields = chunk_header.split()
                        chunk_size = int(fields[0])
                        uri = fields[1]
                        marker = fields[2] if len(fields) > 2 else None
                        if chunk_size == 0:
                            missile = b""
                        else:
                            missile = ammo_file.read(chunk_size)
                        if len(missile) < chunk_size:
                            raise AmmoFileError(
                                "Unexpected end of file: read %s bytes instead of %s"
                                % (len(missile), chunk_size))
                        yield (
                            HttpAmmo(
                                uri=uri,
                                headers=[
                                    ': '.join(header)
                                    for header in self.headers.items()
                                ],
                                method='POST',
                                body=missile,
                                http_ver=self.http_ver, ).to_s(), marker)
                    except (IndexError, ValueError) as e:
                        raise AmmoFileError(
                            "Error while reading ammo file. Position: %s, header: '%s', original exception: %s"
                            % (ammo_file.tell(), chunk_header, e))
                chunk_header = read_chunk_header(ammo_file)
                if chunk_header == b'':
                    self.log.debug(
                        'Reached the end of ammo file. Starting over.')
                    ammo_file.seek(0)
                    try:
                        info.status.inc_loop_count()
                    except LoopCountLimit:
                        break
                    chunk_header = read_chunk_header(ammo_file)
                info.status.af_position = ammo_file.tell()
Example #11
0
    def __iter__(self):
        def read_chunk_header(ammo_file):
            chunk_header = ''
            while chunk_header == '':
                line = ammo_file.readline()
                if line.startswith('['):
                    self.headers.update(_parse_header(line.strip('\r\n[]\t ')))
                elif line == '':
                    return line
                else:
                    chunk_header = line.strip('\r\n')
            return chunk_header

        opener = resource.get_opener(self.filename)
        with opener(self.use_cache) as ammo_file:
            info.status.af_size = opener.data_length
            # if we got StopIteration here, the file is empty
            chunk_header = read_chunk_header(ammo_file)
            while chunk_header:
                if chunk_header != '':
                    try:
                        fields = chunk_header.split()
                        chunk_size = int(fields[0])
                        uri = fields[1]
                        marker = fields[2] if len(fields) > 2 else None
                        if chunk_size == 0:
                            missile = ""
                        else:
                            missile = ammo_file.read(chunk_size)
                        if len(missile) < chunk_size:
                            raise AmmoFileError(
                                "Unexpected end of file: read %s bytes instead of %s"
                                % (len(missile), chunk_size))
                        yield (
                            HttpAmmo(
                                uri=uri,
                                headers=[
                                    ': '.join(header)
                                    for header in self.headers.items()
                                ],
                                method='POST',
                                body=missile,
                                http_ver=self.http_ver, ).to_s(), marker)
                    except (IndexError, ValueError) as e:
                        raise AmmoFileError(
                            "Error while reading ammo file. Position: %s, header: '%s', original exception: %s"
                            % (ammo_file.tell(), chunk_header, e))
                chunk_header = read_chunk_header(ammo_file)
                if chunk_header == '':
                    self.log.debug(
                        'Reached the end of ammo file. Starting over.')
                    ammo_file.seek(0)
                    info.status.inc_loop_count()
                    chunk_header = read_chunk_header(ammo_file)
                info.status.af_position = ammo_file.tell()
Example #12
0
 def get_resource(self, resource, dst, permissions=0o644):
     opener = resource_manager.get_opener(resource)
     if isinstance(opener, HttpOpener):
         tmp_path = opener.download_file(True, try_ungzip=True)
         shutil.copy(tmp_path, dst)
         logger.info('Successfully moved resource %s', dst)
     else:
         dst = opener.get_filename
     os.chmod(dst, permissions)
     logger.info('Permissions on %s have changed %d', dst, permissions)
     return dst
Example #13
0
 def get_ammo_generator(self):
     """
     return ammo generator
     """
     af_readers = {
         'phantom': missile.AmmoFileReader,
         'slowlog': missile.SlowLogReader,
         'line': missile.LineReader,
         'uri': missile.UriReader,
         'uripost': missile.UriPostReader,
         'access': missile.AccessLogReader,
         'caseline': missile.CaseLineReader,
     }
     if self.uris and self.ammo_file:
         raise StepperConfigurationError(
             'Both uris and ammo file specified. You must specify only one of them'
         )
     elif self.uris:
         ammo_gen = missile.UriStyleGenerator(self.uris,
                                              self.headers,
                                              http_ver=self.http_ver)
     elif self.ammo_file:
         if self.ammo_type in af_readers:
             if self.ammo_type == 'phantom':
                 opener = resource.get_opener(self.ammo_file)
                 with opener(self.use_cache) as ammo:
                     try:
                         ammo_str = next(ammo).decode('utf-8')
                         if not ammo_str[0].isdigit():
                             self.ammo_type = 'uri'
                             self.log.info(
                                 "Setting ammo_type 'uri' because ammo is not started with digit and you did not specify ammo format"
                             )
                         else:
                             self.log.info(
                                 "Default ammo type ('phantom') used, use 'phantom.ammo_type' option to override it"
                             )
                     except StopIteration:
                         self.log.exception(
                             "Couldn't read first line of ammo file")
                         raise AmmoFileError(
                             "Couldn't read first line of ammo file")
         else:
             raise NotImplementedError(
                 'No such ammo type implemented: "%s"' % self.ammo_type)
         ammo_gen = af_readers[self.ammo_type](self.ammo_file,
                                               headers=self.headers,
                                               http_ver=self.http_ver,
                                               use_cache=self.use_cache)
     else:
         raise StepperConfigurationError(
             'Ammo not found. Specify uris or ammo file')
     self.log.info("Using %s ammo reader" % type(ammo_gen).__name__)
     return ammo_gen
Example #14
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener() as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 yield (line.rstrip('\r\n'), None)
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #15
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 yield (line.rstrip('\r\n'), None)
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #16
0
    def prepare_test(self):
        for dep in self.jmeter_dependencies:
            url = dep['url']
            filename = dep['filename']
            try:
                opener = resource.get_opener(url, force_download=True)
            except requests.RequestException:
                logger.exception('')
                raise RuntimeError('Failed to download jmeter dependency from %s' % url)
            else:
                filepath = self.core.artifacts_dir + '/' + filename
                with open(filepath, 'w'):
                    pass
                # Подразумевается, что зависимости будут скачиваться с удаленного ресурса.
                # Если надо поддержать что-то еще, например локальные файлы, welcome
                if isinstance(opener, HttpOpener):
                    downloaded_file = opener.download_file(use_cache=True, try_ungzip=True)
                    shutil.move(downloaded_file, filepath)
                self.jmeter_dependencies_paths.append(filepath)
        self.args = [
            self.jmeter_path, "-n", "-t", self.jmx, '-j', self.jmeter_log,
            '-J', 'JMETER_HOME=%s' % self.core.artifacts_dir,
            '-J', 'jmeter.save.saveservice.default_delimiter=\\t',
            '-J', 'jmeter.save.saveservice.connect_time=true',
            '-J', 'sampleresult.timestamp.start=true',
            '-J', 'jmeter.save.saveservice.autoflush=true'
        ]

        if self.properties is not None:
            filepath = self.core.artifacts_dir + '/user.properties'
            with open(filepath, 'w') as user_props_file:
                for key, value in self.properties.items():
                    user_props_file.write(key + "=" + str(value) + '\n')
            self.args.append('-p')
            self.args.append(filepath)
            self.jmeter_dependencies_paths.append(filepath)


        self.args += shlex.split(self.user_args)

        if self.affinity:
            self.core.__setup_affinity(self.affinity, args=self.args)

        try:
            console = self.core.get_plugin_of_type(ConsolePlugin)
        except Exception as ex:
            logger.debug("Console not found: %s", ex)
            console = None

        if console:
            widget = JMeterInfoWidget(self)
            console.add_info_widget(widget)
            self.core.job.aggregator.add_result_listener(widget)
Example #17
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 yield (line.rstrip(b'\r\n'), None) if isinstance(
                     line, bytes) else (line.rstrip('\r\n').encode('utf8'),
                                        None)
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #18
0
 def get_resource(self, resource, dst, permissions=0o644):
     opener = resource_manager.get_opener(resource)
     if isinstance(opener, HttpOpener):
         tmp_path = opener.download_file(True, try_ungzip=True)
         shutil.copy(tmp_path, dst)
         logger.info('Successfully moved resource %s', dst)
     else:
         dst = opener.get_filename
     try:
         os.chmod(dst, permissions)
     except OSError:
         logger.warning('Cannot change permissions to %s', dst)
     return dst
Example #19
0
 def get_ammo_generator(self):
     """
     return ammo generator
     """
     af_readers = {
         'phantom': missile.AmmoFileReader,
         'slowlog': missile.SlowLogReader,
         'line': missile.LineReader,
         'uri': missile.UriReader,
         'uripost': missile.UriPostReader,
         'access': missile.AccessLogReader,
         'caseline': missile.CaseLineReader,
     }
     if self.uris and self.ammo_file:
         raise StepperConfigurationError(
             'Both uris and ammo file specified. You must specify only one of them'
         )
     elif self.uris:
         ammo_gen = missile.UriStyleGenerator(
             self.uris, self.headers, http_ver=self.http_ver)
     elif self.ammo_file:
         if self.ammo_type in af_readers:
             if self.ammo_type == 'phantom':
                 opener = resource.get_opener(self.ammo_file)
                 with opener(self.use_cache) as ammo:
                     try:
                         if not ammo.next()[0].isdigit():
                             self.ammo_type = 'uri'
                             self.log.info(
                                 "Setting ammo_type 'uri' because ammo is not started with digit and you did not specify ammo format"
                             )
                         else:
                             self.log.info(
                                 "Default ammo type ('phantom') used, use 'phantom.ammo_type' option to override it"
                             )
                     except StopIteration:
                         self.log.exception(
                             "Couldn't read first line of ammo file")
                         raise AmmoFileError(
                             "Couldn't read first line of ammo file")
         else:
             raise NotImplementedError(
                 'No such ammo type implemented: "%s"' % self.ammo_type)
         ammo_gen = af_readers[self.ammo_type](
             self.ammo_file, headers=self.headers, http_ver=self.http_ver, use_cache=self.use_cache)
     else:
         raise StepperConfigurationError(
             'Ammo not found. Specify uris or ammo file')
     self.log.info("Using %s ammo reader" % type(ammo_gen).__name__)
     return ammo_gen
Example #20
0
    def prepare(self):
        """ Phone preparements stage: install apps etc

        pipeline:
            install lightning
            install apks
            clean log
        """

        # install lightning
        self.lightning_apk_fname = resource.get_opener(
            self.lightning_apk_path).get_filename
        logger.info('Installing lightning apk ' + self.lightning_apk_fname)
        execute("adb -s {device_id} install -r -d -t {apk}".format(
            device_id=self.source, apk=self.lightning_apk_fname))

        # install apks
        for apk in self.test_apps:
            apk_fname = resource.get_opener(apk).get_filename
            execute("adb -s {device_id} install -r -d -t {apk}".format(
                device_id=self.source, apk=apk_fname))

        # clean logcat
        execute("adb -s {device_id} logcat -c".format(device_id=self.source))
Example #21
0
    def __iter__(self):
        def read_chunk_header(ammo_file):
            chunk_header = ''
            while chunk_header == '':
                line = ammo_file.readline()
                if isinstance(line, bytes):
                    line = line.decode('utf-8')
                if line == '':
                    return line
                chunk_header = line.strip('\r\n')
            return chunk_header

        opener = resource.get_opener(self.filename)
        with opener(self.use_cache) as ammo_file:
            info.status.af_size = opener.data_length
            # if we got StopIteration here, the file is empty
            chunk_header = read_chunk_header(ammo_file)
            while chunk_header:
                if chunk_header != '':
                    try:
                        fields = chunk_header.split()
                        chunk_size = int(fields[0])
                        if chunk_size == 0:
                            if info.status.loop_count == 0:
                                self.log.info(
                                    'Zero-sized chunk in ammo file at %s. Starting over.'
                                    % ammo_file.tell())
                            ammo_file.seek(0)
                            info.status.inc_loop_count()
                            chunk_header = read_chunk_header(ammo_file)
                            continue
                        marker = fields[1] if len(fields) > 1 else None
                        missile = ammo_file.read(chunk_size)
                        if len(missile) < chunk_size:
                            raise AmmoFileError(
                                "Unexpected end of file: read %s bytes instead of %s"
                                % (len(missile), chunk_size))
                        yield (missile, marker)
                    except (IndexError, ValueError) as e:
                        raise AmmoFileError(
                            "Error while reading ammo file. Position: %s, header: '%s', original exception: %s"
                            % (ammo_file.tell(), chunk_header, e))
                chunk_header = read_chunk_header(ammo_file)
                if chunk_header == '':
                    ammo_file.seek(0)
                    info.status.inc_loop_count()
                    chunk_header = read_chunk_header(ammo_file)
                info.status.af_position = ammo_file.tell()
Example #22
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 parts = line.rstrip('\r\n').split('\t', 1)
                 if len(parts) == 2:
                     yield (parts[1], parts[0])
                 elif len(parts) == 1:
                     yield (parts[0], None)
                 else:
                     raise RuntimeError("Unreachable branch")
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #23
0
 def get_resource(self, resource, dst, permissions=0644):
     opener = resource_manager.get_opener(resource)
     if isinstance(opener, HttpOpener):
         tmp_path = opener.download_file(True, try_ungzip=True)
         try:
             os.rename(tmp_path, dst)
             logger.info('Successfully moved resource %s', dst)
         except OSError as ex:
             logger.debug("Could not move resource %s\n%s", dst, ex)
     else:
         dst = opener.get_filename
     try:
         os.chmod(dst, permissions)
         logger.info('Permissions on %s have changed %d', dst, permissions)
     except OSError as ex:
         logger.debug("Could not chane pepermissions on %s\n%s", dst, ex)
     return dst
Example #24
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         request = ""
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 if line.startswith('#'):
                     if request != "":
                         yield (request, None)
                         request = ""
                 else:
                     request += line
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #25
0
    def __iter__(self):
        def read_chunk_header(ammo_file):
            chunk_header = ''
            while chunk_header == '':
                line = ammo_file.readline()
                if line == '':
                    return line
                chunk_header = line.strip('\r\n')
            return chunk_header

        opener = resource.get_opener(self.filename)
        with opener(self.use_cache) as ammo_file:
            info.status.af_size = opener.data_length
            # if we got StopIteration here, the file is empty
            chunk_header = read_chunk_header(ammo_file)
            while chunk_header:
                if chunk_header != '':
                    try:
                        fields = chunk_header.split()
                        chunk_size = int(fields[0])
                        if chunk_size == 0:
                            if info.status.loop_count == 0:
                                self.log.info(
                                    'Zero-sized chunk in ammo file at %s. Starting over.'
                                    % ammo_file.tell())
                            ammo_file.seek(0)
                            info.status.inc_loop_count()
                            chunk_header = read_chunk_header(ammo_file)
                            continue
                        marker = fields[1] if len(fields) > 1 else None
                        missile = ammo_file.read(chunk_size)
                        if len(missile) < chunk_size:
                            raise AmmoFileError(
                                "Unexpected end of file: read %s bytes instead of %s"
                                % (len(missile), chunk_size))
                        yield (missile, marker)
                    except (IndexError, ValueError) as e:
                        raise AmmoFileError(
                            "Error while reading ammo file. Position: %s, header: '%s', original exception: %s"
                            % (ammo_file.tell(), chunk_header, e))
                chunk_header = read_chunk_header(ammo_file)
                if chunk_header == '':
                    ammo_file.seek(0)
                    info.status.inc_loop_count()
                    chunk_header = read_chunk_header(ammo_file)
                info.status.af_position = ammo_file.tell()
Example #26
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener() as ammo_file:
         info.status.af_size = opener.data_length
         request = ""
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 if line.startswith('#'):
                     if request != "":
                         yield (request, None)
                         request = ""
                 else:
                     request += line
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #27
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener() as ammo_file:
         info.status.af_size = opener.data_length
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 parts = line.rstrip('\r\n').split('\t', 1)
                 if len(parts) == 2:
                     yield (parts[1], parts[0])
                 elif len(parts) == 1:
                     yield (parts[0], None)
                 else:
                     raise RuntimeError("Unreachable branch")
             ammo_file.seek(0)
             info.status.af_position = 0
             info.status.inc_loop_count()
Example #28
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         chunk_header = self.read_chunk_header(ammo_file)
         while chunk_header:
             if chunk_header != b'':
                 try:
                     fields = chunk_header.split()
                     chunk_size = int(fields[0])
                     if chunk_size == 0:
                         if info.status.loop_count == 0:
                             self.log.info(
                                 'Zero-sized chunk in ammo file at %s. Starting over.'
                                 % ammo_file.tell())
                         ammo_file.seek(0)
                         try:
                             info.status.inc_loop_count()
                         except LoopCountLimit:
                             break
                         chunk_header = self.read_chunk_header(ammo_file)
                         continue
                     marker = fields[1] if len(fields) > 1 else None
                     missile = ammo_file.read(chunk_size)
                     if len(missile) < chunk_size:
                         raise AmmoFileError(
                             "Unexpected end of file: read %s bytes instead of %s"
                             % (len(missile), chunk_size))
                     yield (missile, marker)
                 except (IndexError, ValueError) as e:
                     raise AmmoFileError(
                         "Error while reading ammo file. Position: %s, header: '%s', original exception: %s"
                         % (ammo_file.tell(), chunk_header, e))
             chunk_header = self.read_chunk_header(ammo_file)
             if chunk_header == b'':
                 ammo_file.seek(0)
                 try:
                     info.status.inc_loop_count()
                 except LoopCountLimit:
                     break
                 chunk_header = self.read_chunk_header(ammo_file)
             info.status.af_position = ammo_file.tell()
Example #29
0
 def __iter__(self):
     opener = resource.get_opener(self.filename)
     with opener(self.use_cache) as ammo_file:
         info.status.af_size = opener.data_length
         request = ""
         while True:
             for line in ammo_file:
                 info.status.af_position = ammo_file.tell()
                 if isinstance(line, bytes):
                     line = line.decode('utf-8')
                 if line.startswith('#'):
                     if request != "":
                         yield (request, None)
                         request = ""
                 else:
                     request += line
             ammo_file.seek(0)
             info.status.af_position = 0
             try:
                 info.status.inc_loop_count()
             except LoopCountLimit:
                 break
Example #30
0
    def __init__(self, config, core):
        """
        Args:
            self.config (VoltaConfig): module configuration data

        Attributes:
            self.source (string): path to data source, should be able to be opened by resource manager
                may be url, e.g. 'http://myhost.tld/path/to/file'
                may be device, e.g. '/dev/cu.wchusbserial1420'
                may be path to file, e.g. '/home/users/netort/path/to/file.data'
            self.chop_ratio (int): chop ratio for incoming data, 1 means 1 second (500 for sample_rate 500)
            self.grab_timeout (int): timeout for grabber
            self.sample_rate (int): volta box sample rate - depends on software and which type of volta box you use
            self.baud_rate (int): baud rate for device if device specified in source
        """
        self.core = core
        self.config = config
        self.pipeline = None
        self.grabber_q = None
        self.process_currents = None
        self.reader = None

        self.source = config.get_option('volta', 'source')
        self.chop_ratio = config.get_option('volta', 'chop_ratio')
        self.grab_timeout = config.get_option('volta', 'grab_timeout')
        self.slope = config.get_option('volta', 'slope')
        self.offset = config.get_option('volta', 'offset')
        self.precision = config.get_option('volta', 'precision')
        self.power_voltage = config.get_option('volta', 'power_voltage')
        self.sample_swap = config.get_option('volta', 'sample_swap', False)

        # initialize data source
        try:
            self.source_opener = resource.get_opener(self.source)
        except Exception:
            raise RuntimeError(
                'Device %s not found. Please check VoltaBox USB connection',
                self.source)
Example #31
0
    def __add_jmeter_components(self, jmx, jtl, variables):
        """ Genius idea by Alexey Lavrenyuk """
        with resource.get_opener(jmx)() as src_jmx:
            source_lines = [l.decode('utf-8') for l in src_jmx.readlines()]
        try:
            # In new Jmeter version (3.2 as example) WorkBench's plugin checkbox enabled by default
            # It totally crashes Yandex tank injection and raises XML Parse Exception
            closing = source_lines.pop(-1)
            if "WorkBenchGui" in source_lines[-5]:
                logger.info("WorkBench checkbox enabled...bypassing")
                last_string_count = 6
            else:
                last_string_count = 2
            while last_string_count > 0:
                closing = source_lines.pop(-1) + closing
                last_string_count -= 1
            logger.debug("Closing statement: %s", closing)
        except Exception as exc:
            raise RuntimeError("Failed to find the end of JMX XML: %s" % exc)

        udv_tpl = resource_string(__name__, 'config/jmeter_var_template.xml')
        udv_set = []
        for var_name, var_value in variables.items():
            udv_set.append(udv_tpl.decode('utf-8') % (var_name, var_name, var_value))
        udv = "\n".join(udv_set)

        if self.jmeter_ver >= 2.13:
            save_connect = '<connectTime>true</connectTime>'
        else:
            save_connect = ''

        if self.ext_log in ['errors', 'all']:
            level_map = {'errors': 'true', 'all': 'false'}
            tpl_resource = 'jmeter_writer_ext.xml'
            tpl_args = {
                'jtl': self.jtl_file,
                'udv': udv,
                'ext_log': self.ext_log_file,
                'ext_level': level_map[self.ext_log],
                'save_connect': save_connect
            }
        else:
            tpl_resource = 'jmeter_writer.xml'
            tpl_args = {
                'jtl': self.jtl_file,
                'udv': udv,
                'save_connect': save_connect
            }

        tpl = resource_string(__name__, 'config/' + tpl_resource)

        try:
            new_jmx = self.core.mkstemp(
                '.jmx', 'modified_', os.path.dirname(os.path.realpath(jmx)))
        except OSError as exc:
            logger.debug("Can't create modified jmx near original: %s", exc)
            new_jmx = self.core.mkstemp('.jmx', 'modified_')
        logger.debug("Modified JMX: %s", new_jmx)
        with open(new_jmx, "w") as fh:
            fh.write(''.join(source_lines))
            fh.write(tpl.decode('utf-8') % tpl_args)
            fh.write(closing)
        return new_jmx