Best Python code snippet using avocado_python
memory_availability_test.py
Source:memory_availability_test.py
1import pytest2from openshift_checks.memory_availability import MemoryAvailability3@pytest.mark.parametrize('group_names,is_active', [4 (['oo_masters_to_config'], True),5 (['oo_nodes_to_config'], True),6 (['oo_etcd_to_config'], True),7 (['oo_masters_to_config', 'oo_nodes_to_config'], True),8 (['oo_masters_to_config', 'oo_etcd_to_config'], True),9 ([], False),10 (['lb'], False),11 (['nfs'], False),12])13def test_is_active(group_names, is_active):14 task_vars = dict(15 group_names=group_names,16 )17 assert MemoryAvailability(None, task_vars).is_active() == is_active18@pytest.mark.parametrize('group_names,configured_min,ansible_memtotal_mb', [19 (20 ['oo_masters_to_config'],21 0,22 17200,23 ),24 (25 ['oo_nodes_to_config'],26 0,27 8200,28 ),29 (30 ['oo_nodes_to_config'],31 1, # configure lower threshold32 2000, # too low for recommended but not for configured33 ),34 (35 ['oo_nodes_to_config'],36 2, # configure threshold where adjustment pushes it over37 1900,38 ),39 (40 ['oo_etcd_to_config'],41 0,42 8200,43 ),44 (45 ['oo_masters_to_config', 'oo_nodes_to_config'],46 0,47 17000,48 ),49])50def test_succeeds_with_recommended_memory(group_names, configured_min, ansible_memtotal_mb):51 task_vars = dict(52 group_names=group_names,53 openshift_check_min_host_memory_gb=configured_min,54 ansible_memtotal_mb=ansible_memtotal_mb,55 )56 result = MemoryAvailability(fake_execute_module, task_vars).run()57 assert not result.get('failed', False)58@pytest.mark.parametrize('group_names,configured_min,ansible_memtotal_mb,extra_words', [59 (60 ['oo_masters_to_config'],61 0,62 0,63 ['0.0 GiB'],64 ),65 (66 ['oo_nodes_to_config'],67 0,68 100,69 ['0.1 GiB'],70 ),71 (72 ['oo_nodes_to_config'],73 24, # configure higher threshold74 20 * 1024, # enough to meet recommended but not configured75 ['20.0 GiB'],76 ),77 (78 ['oo_nodes_to_config'],79 24, # configure higher threshold80 22 * 1024, # not enough for adjustment to push over threshold81 ['22.0 GiB'],82 ),83 (84 ['oo_etcd_to_config'],85 0,86 6 * 1024,87 ['6.0 GiB'],88 ),89 (90 ['oo_etcd_to_config', 'oo_masters_to_config'],91 0,92 9 * 1024, # enough memory for etcd, not enough for a master93 ['9.0 GiB'],94 ),95 (96 ['oo_nodes_to_config', 'oo_masters_to_config'],97 0,98 # enough memory for a node, not enough for a master99 11 * 1024,100 ['11.0 GiB'],101 ),102])103def test_fails_with_insufficient_memory(group_names, configured_min, ansible_memtotal_mb, extra_words):104 task_vars = dict(105 group_names=group_names,106 openshift_check_min_host_memory_gb=configured_min,107 ansible_memtotal_mb=ansible_memtotal_mb,108 )109 result = MemoryAvailability(fake_execute_module, task_vars).run()110 assert result.get('failed', False)111 for word in 'below recommended'.split() + extra_words:112 assert word in result['msg']113def fake_execute_module(*args):...
memStats.py
Source:memStats.py
1"""2memStats.py: This module retrieves the memory statistics for the system the program is being run on. Namely the amount of available and total RAM in MB and also the utilization of RAM in percentage. 3"""4import re5import time6from helperFunctions import readFile, round2, BasicGauge7memUsed = BasicGauge("memUsed")8memTotal = None9def getMemTotal():10 11 global memTotal12 13 if memTotal:14 return memTotal15 else:16 return 017def parseInfo(memFile, readTime): 18 """19 This function parses the contents of /proc/meminfo and returns the memory information in a dictionary.20 Parameters:21 memFile (str): The contents of /proc/meminfo22 Returns 23 list: A list that holds the available memory as a BasicGauge object, total memory in MB, and the memory utilization in percentage.24 """ 25 global memTotal26 if not memTotal: 27 try:28 # get the MemTotal value and convert it to MB from kB29 memTotal = float(re.findall(r'MemTotal: .*', memFile)[0].split(" ")[-2])30 memTotal = memTotal/102431 except:32 print("Error: Unable to retrieve MemTotal")33 memTotal = None 34 try:35 # get the MemAvailable value and convert it to MB from kB36 avail = float(re.findall(r'MemAvailable: .*', memFile)[0].split(" ")[-2])37 avail = avail/1024 38 memUsed.updateAll(memTotal-avail, readTime)39 except: 40 print("Error: Unable to retrieve memAvail")41 42 try:43 # calculate memory utilization during the time interval44 memUtil = round2((memUsed.calculateAverage()/memTotal)*100)45 except:46 print("Error: Unable to calculate memUtil")47 memUtil = 048 49 return [memTotal, memUsed, memUtil] 50def fetchAll():51 """52 This function reads /proc/meminfo and sends it to parseInfo() to be processed.53 Returns:54 dictionary: Dictionary holding memory information.55 """56 readTime = time.time()57 memFile = readFile("/proc/meminfo")58 if memFile:59 return parseInfo(memFile, readTime)60 else:61 print("Error: Unable to retrieve memory information")62 return None 63def printAll():64 memTotal, memUsed, memUtil = fetchAll()65 print("Util: {}%".format(memUtil))66# initialize the memInfo Dictionary67fetchAll()...
1.py
Source:1.py
1#!/usr/bin/env pypy2import argparse, re3parser = argparse.ArgumentParser()4parser.add_argument("input",type=str,nargs='?',default="input")5parser.add_argument("--p1",dest="p1",action='store_true')6parser.add_argument("--no-p1",dest="p1",action='store_false')7parser.add_argument("--p2",dest="p2",action='store_true')8parser.add_argument("--no-p2",dest="p2",action='store_false')9args = parser.parse_args()10if not args.p1 and not args.p2:11 args.p1 = True12 args.p2 = True13print "Input: %s P1: %s p2: %s" % (args.input,args.p1,args.p2)14lines = []15for x in open(args.input).readlines():16 x = x.strip()17 if not x:18 continue19 # Process input line20 lines.append(x)21def decode(x):22 output = []23 i = 124 while i < len(x)-1:25 c = x[i]26 i = i + 127 28 if c == "\\":29 c2 = x[i]30 i = i + 131 if c2 == "\\":32 output.append("\\")33 elif c2 == "\"":34 output.append("\"")35 elif c2 == "x":36 h1 = x[i]37 h2 = x[i+1]38 i = i + 239 output.append(".")40 else:41 output.append("\\")42 output.append("\\")43 else:44 output.append(c)45 return "".join(output)46def encode(x):47 output = ["\""]48 for c in x:49 if c == "\"":50 output.append("\\\"")51 elif c == "\\":52 output.append("\\\\")53 else:54 output.append(c)55 56 output.append("\"")57 58 return "".join(output)59if args.p1:60 print("Doing part 1")61 filetotal = 062 memtotal = 063 for x in lines:64 y = decode(x)65 print( "%s -> %s" % (x,y,))66 filetotal += len(x)67 memtotal += len(y)68 print("FileTotal: %s" % (filetotal,))69 print("MemTotal: %s" % (memtotal,))70 print("Diff: %s" % (filetotal - memtotal,))71 72if args.p2:73 print("Doing part 2")74 filetotal = 075 memtotal = 076 for x in lines:77 y = encode(x)78 print( "%s -> %s" % (x,y,))79 filetotal += len(x)80 memtotal += len(y)81 print("FileTotal: %s" % (filetotal,))82 print("MemTotal: %s" % (memtotal,))...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!