How to use abort_queue method in avocado

Best Python code snippet using avocado_python

AllocationContext.py

Source:AllocationContext.py Github

copy

Full Screen

1"""2Copyright 2017 ARM Limited3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12AllocationContext module, contains two classes that represent13allocation information of resources allocated for a test case.14These classes implement storing of Allocated resources as well as conversion of allocated15resources into Dut objects usable by test cases.16"""17import os18try:19 # Python 2.7 compatible import.20 from Queue import Queue21except ImportError:22 # Python 3 has renamed Queue to queue.23 from queue import Queue24from multiprocessing.pool import ThreadPool25from icetea_lib.DeviceConnectors.Dut import DutConnectionError26from icetea_lib.ResourceProvider.exceptions import ResourceInitError27from icetea_lib.ResourceProvider.Allocators.exceptions import AllocationError28from icetea_lib.build.build import Build29class AllocationContext(object):30 """31 Class AllocationContext32 Contains allocation and resource_id:s used to define an allocated resource.33 Also contains supplementary data in _alloc_data dictionary.34 """35 def __init__(self, resource_id=None, alloc_id=None, alloc_data=None):36 self._resource_id = resource_id37 self._alloc_id = alloc_id38 self._alloc_data = alloc_data if alloc_data else {}39 def get(self, key):40 """41 Getter for configuration dictionary items42 :param key: Key to look for in configuration.43 :param default: Default value to be returned if key is not found in configuration.44 Default is None45 :return: value of configuration[key] if key exists in configuration, else value of default.46 """47 return self._alloc_data.get(key)48 def set(self, key, value):49 """50 Setter for configuration values51 :param key:52 :param value:53 :return: Nothing54 """55 self._alloc_data[key] = value56 def get_alloc_data(self):57 """58 Get allocation data dictionary59 :return: Allocation data (dictionary)60 """61 return self._alloc_data62 @property63 def alloc_id(self):64 """65 Getter for alloc_id66 :return: alloc_id67 """68 return self._alloc_id69 @property70 def resource_id(self):71 """72 Getter for resource_id73 :return: resource_id74 """75 return self._resource_id76 def __getitem__(self, item):77 """78 __getitem__ implementation for AllocationContext79 enables context[item]80 :param item: item to return81 :return: self._alloc_data[item] or None if item does not exist in self._alloc_data82 """83 return self._alloc_data.get(item, None)84 def __setitem__(self, key, value):85 """86 __setitem__ implementation for AllocationContext.87 Replaces item in list space key with value.88 :param key: Name of context data item to replace/add89 :param value: Value to replace context data item with90 :return: Nothing91 """92 self._alloc_data[key] = value93class AllocationContextList(object):94 """95 Class AllocationContextList96 Is used for storing and handling Duts after they have been allocated.97 Contains methods to iterate over AllocationContext objects and initialize duts from98 AllocationContext objects.99 """100 def __init__(self, logger=None):101 self._allocation_contexts = []102 self.logger = logger103 self.duts = []104 self.dutinformations = []105 self._resource_configuration = None106 self._dut_initialization_functions = {}107 def __len__(self):108 """109 len implementation for AllocationContextList110 :return: len(self._allocation_contexts)111 """112 return len(self._allocation_contexts)113 def __iter__(self):114 """115 Implementation of __iter__ to allow for item in list loops116 :return: iterator to self._allocation_contexts117 """118 return iter(self._allocation_contexts)119 def __getitem__(self, item):120 """121 __getitem__ implementation for AllocationContextList122 enables list[index]123 :param item: index to return124 :return: self._allocation_contexts[item]125 :raises: TypeError if item is not integer.126 :raises: IndexError if item < 0 or item > len(self._allocation_contexts)127 """128 if not isinstance(item, int):129 raise TypeError("AllocationContextList get expects an integer index")130 if len(self._allocation_contexts) <= item:131 raise IndexError("list getitem out of bounds")132 elif item < 0:133 raise IndexError("AllocationContextList get not implemented for negative values.")134 return self._allocation_contexts[item]135 def __setitem__(self, key, value):136 """137 __setitem__ implementation for AllocationContextList.138 Replaces item in list space key with value.139 :param key: Index of list item to replace140 :param value: Value to replace list item with141 :return: Nothing142 :raises: TypeError if key is not an integer.143 :raises: IndexError if key < 0 or key > len(self._allocation_contexts)144 """145 if not isinstance(key, int):146 raise TypeError("AllocationContextList set expects an integer index")147 if len(self._allocation_contexts) <= key:148 raise IndexError("list setitem out of bounds")149 elif key < 0:150 raise IndexError("AllocationContextList set not implemented for negative indexes.")151 self._allocation_contexts[key] = value152 def set_dut_init_function(self, dut_type, fnctn):153 """154 Setter for dut initialization function155 :param dut_type: Dut type156 :param fnctn: callable157 :return: Nothing158 """159 self._dut_initialization_functions[dut_type] = fnctn160 def get_dut_init_function(self, dut_type):161 """162 Get dut initialization function163 :param dut_type: Dut type.164 :return: callable165 """166 return self._dut_initialization_functions.get(dut_type)167 def set_resconf(self, resconf):168 """169 Set resource configuration.170 :param resconf: ResourceConfig object171 :return: Nothing172 """173 self._resource_configuration = resconf174 def get_resconf(self):175 """176 Get resource configuration for this AllocationContextList.177 :return: ResourceConfig178 """179 return self._resource_configuration180 def set_logger(self, logger):181 """182 Set logger.183 :param logger: logging.logger184 :return: Nothing185 """186 self.logger = logger187 def get_duts(self):188 """189 Get list of duts.190 :return: list of Dut objects191 """192 return self.duts193 def get_dutinformations(self):194 """195 Get DutInformation objects of current duts.196 :return: list of DutInformation objects197 """198 return self.dutinformations199 def append(self, alloc_context):200 """201 Appends alloc_context to self._allocation_contexts. No type-checking done here.202 :param alloc_context: AllocationContext object.203 :return: Nothing204 """205 self._allocation_contexts.append(alloc_context)206 # pylint: disable=too-many-statements207 def init_duts(self, args): # pylint: disable=too-many-locals,too-many-branches208 """209 Initializes duts of different types based on configuration provided by AllocationContext.210 Able to do the initialization of duts in parallel, if --parallel_flash was provided.211 :param args: Argument Namespace object212 :return: list of initialized duts.213 """214 # TODO: Split into smaller chunks to reduce complexity.215 threads = []216 abort_queue = Queue()217 def thread_wrapper(*thread_args, **thread_kwargs):218 """219 Run initialization function for dut220 :param thread_args: arguments to pass to the function221 :param thread_kwargs: keyword arguments, (func: callable, abort_queue: Queue)222 :return: Result of func(*thread_args)223 """224 # pylint: disable=broad-except225 try:226 return thread_kwargs["func"](*thread_args)227 except Exception as error:228 thread_kwargs["abort_queue"].put((thread_args[2], error))229 for index, dut_conf in enumerate(self._allocation_contexts):230 dut_type = dut_conf.get("type")231 func = self.get_dut_init_function(dut_type)232 if func is None:233 continue234 threads.append(((self, dut_conf.get_alloc_data().get_requirements(), index + 1, args),235 {"func": func, "abort_queue": abort_queue}))236 try:237 thread_limit = len(threads) if args.parallel_flash else 1238 pool = ThreadPool(thread_limit)239 async_results = [pool.apply_async(func=thread_wrapper,240 args=t[0], kwds=t[1])241 for t in threads]242 # Wait for resources to be ready.243 [res.get() for res in async_results] # pylint: disable=expression-not-assigned244 pool.close()245 pool.join()246 if not abort_queue.empty():247 msg = "Dut Initialization failed, reason(s):"248 while not abort_queue.empty():249 dut_index, error = abort_queue.get()250 msg = "{}\nDUT index {} - {}".format(msg, dut_index, error)251 raise AllocationError(msg)252 # Sort duts to same order as in dut_conf_list253 self.duts.sort(key=lambda d: d.index)254 self.dutinformations.sort(key=lambda d: d.index)255 except KeyboardInterrupt:256 msg = "Received keyboard interrupt, waiting for flashing to finish"257 self.logger.info(msg)258 for dut in self.duts:259 dut.close_dut(False)260 dut.close_connection()261 if hasattr(dut, "release"):262 dut.release()263 dut = None264 raise265 except RuntimeError:266 self.logger.exception("RuntimeError during flashing")267 # ValueError is raised if ThreadPool is tried to initiate with268 # zero threads.269 except ValueError:270 self.logger.exception("No devices allocated")271 raise AllocationError("Dut Initialization failed!")272 except (DutConnectionError, TypeError):273 for dut in self.duts:274 if hasattr(dut, "release"):275 dut.release()276 raise AllocationError("Dut Initialization failed!")277 finally:278 if pool:279 pool.close()280 pool.join()281 self.logger.debug("Allocated following duts:")282 for dut in self.duts:283 dut.print_info()284 return self.duts285 def open_dut_connections(self):286 """287 Opens connections to Duts. Starts Dut read threads.288 :return: Nothing289 :raises DutConnectionError: if problems were encountered while opening dut connection.290 """291 for dut in self.duts:292 try:293 dut.start_dut_thread()294 if hasattr(dut, "command"):295 dut.open_dut(dut.command)296 else:297 dut.open_dut()298 except DutConnectionError:299 self.logger.exception("Failed when opening dut connection")300 dut.close_dut(False)301 dut.close_connection()302 dut = None303 raise304 def check_flashing_need(self, execution_type, build_id, force):305 """306 Check if flashing of local device is required.307 :param execution_type: Should be 'hardware'308 :param build_id: Build id, usually file name309 :param force: Forceflash flag310 :return: Boolean311 """312 binary_file_name = AllocationContextList.get_build(build_id)313 if binary_file_name:314 if execution_type == 'hardware' and os.path.isfile(binary_file_name):315 if not force:316 #@todo: Make a better check for binary compatibility317 extension_split = os.path.splitext(binary_file_name)318 extension = extension_split[-1].lower()319 if extension != '.bin' and extension != '.hex':320 self.logger.debug("File ('%s') is not supported to flash, skip it" %(321 build_id))322 return False323 return True324 return True325 else:326 raise ResourceInitError("Given binary %s does not exist" % build_id)327 else:328 raise ResourceInitError("Given binary %s does not exist" % build_id)329 @staticmethod330 def get_build(build_id):331 """332 Gets a file related to build_id.333 """334 try:335 build = Build.init(build_id)336 except NotImplementedError:337 return None...

Full Screen

Full Screen

util.py

Source:util.py Github

copy

Full Screen

1#2# Copyright 2015 by Justin MacCallum, Alberto Perez, Ken Dill3# All rights reserved4#5import os6import shutil7import tempfile8import contextlib9import time10from functools import wraps11import logging12import logging.handlers13import SocketServer14import struct15import pickle16import Queue17@contextlib.contextmanager18def in_temp_dir():19 """Context manager to run in temporary directory"""20 try:21 cwd = os.getcwd()22 tmpdir = tempfile.mkdtemp()23 os.chdir(tmpdir)24 yield25 finally:26 os.chdir(cwd)27 shutil.rmtree(tmpdir)28def log_timing(dest_logger):29 def wrap(func):30 @wraps(func)31 def wrapper(*args, **kwds):32 t1 = time.time()33 res = func(*args, **kwds)34 t2 = time.time()35 dest_logger.debug('%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0))36 return res37 return wrapper38 return wrap39class LogRecordStreamHandler(SocketServer.StreamRequestHandler):40 """Handler for a streaming logging request.41 This basically logs the record using whatever logging policy is42 configured locally.43 """44 def handle(self):45 """46 Handle multiple requests - each expected to be a 4-byte length,47 followed by the LogRecord in pickle format. Logs the record48 according to whatever policy is configured locally.49 """50 while True:51 chunk = self.connection.recv(4)52 if len(chunk) < 4:53 break54 slen = struct.unpack('>L', chunk)[0]55 chunk = self.connection.recv(slen)56 while len(chunk) < slen:57 chunk = chunk + self.connection.recv(slen - len(chunk))58 obj = self.unPickle(chunk)59 record = logging.makeLogRecord(obj)60 self.handleLogRecord(record)61 def unPickle(self, data):62 return pickle.loads(data)63 def handleLogRecord(self, record):64 # if a name is specified, we use the named logger rather than the one65 # implied by the record.66 if self.server.logname is not None:67 name = self.server.logname68 else:69 name = record.name70 logger = logging.getLogger(name)71 # N.B. EVERY record gets logged. This is because Logger.handle72 # is normally called AFTER logger-level filtering. If you want73 # to do filtering, do it at the client end to save wasting74 # cycles and network bandwidth!75 logger.handle(record)76class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer):77 """78 Simple TCP socket-based logging receiver.79 The configure_logging_and_launch_listener function should be launched80 in another process. The socket number can be retrieved81 from socket_queue and the logger can be told to abort82 through the abort_queue.83 """84 allow_reuse_address = 185 def __init__(self, host, abort_queue, socket_queue, handler=LogRecordStreamHandler):86 # we request port zero, which should get an unused, non-privileged port87 SocketServer.ThreadingTCPServer.__init__(self, (host, 0), handler)88 # queue used to communicate from the main MELD process that the89 # LogRecordSocketReciever should abort90 self.abort_queue = abort_queue91 # queue used to communicate the listening port back to the92 # main MELD process93 self.socket_queue = socket_queue94 # send our port number back to the launching process95 self.socket_queue.put(self.server_address)96 self.abort = 097 self.timeout = 198 self.logname = None99 def serve_until_stopped(self):100 import select101 abort = 0102 while not abort:103 rd, wr, ex = select.select([self.socket.fileno()],104 [], [],105 self.timeout)106 if rd:107 self.handle_request()108 # check the abort queue to see if we should terminate109 try:110 abort = self.abort_queue.get(block=False)111 except Queue.Empty:112 pass113class HostNameContextFilter(logging.Filter):114 """Filter class that adds hostid information to logging records."""115 def __init__(self, hostid):116 logging.Filter.__init__(self)117 self.hostid = hostid118 def filter(self, record):119 record.hostid = self.hostid120 return True121def configure_logging_and_launch_listener(host, abort_queue, socket_queue):122 fmt = '%(hostid)s %(asctime)s %(levelname)s %(name)s: %(message)s'123 datefmt = '%Y-%m-%d %H:%M:%S'124 logging.basicConfig(filename='remd.log', format=fmt, datefmt=datefmt)125 receiver = LogRecordSocketReceiver(host, abort_queue, socket_queue)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful