U 1gN@sdZddlZddlZddlZddlZddlZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZerddlmZeZedkoedZndZdZddZddZejje ddGddde Zejje ddGddde Zejje ddGddde Zejje ddGd d!d!e Z ejje d"dGd#d$d$e Z!dS)%z$Tests specific to all BSD platforms.N)BSD)FREEBSD)NETBSD)OPENBSD) HAS_BATTERY)TOLERANCE_SYS_MEM)PsutilTestCase)pytest)retry_on_failure)sh)spawn_testproc) terminate)which) getpagesizemuseFcCsntd|}tr(||ddd}nts0trF||ddd}z t|WStk rh|YSXdS)zmExpects a sysctl command with an argument and parse the result returning only the value of interest. zsysctl z: N=)r rfindrrint ValueError)cmdlineresultrE/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_bsd.pysysctl-s  rcCs>td}|dD]}||rq.qtdt|dS)z+Thin wrapper around 'muse' cmdline utility.r zline not foundr)r split startswithrr)fieldoutlinerrrr<s  zBSD onlyreasonc@seZdZdZeddZeddZejj e dddd Z d d Z ejj e d  d dddZejj e d  d dejj e ddddZejj e d ddddZdS) BSDTestCasez)Generic tests common to all BSD variants.cCstj|_dSNr pidclsrrr setUpClassPszBSDTestCase.setUpClasscCst|jdSr%r r'r(rrr tearDownClassTszBSDTestCase.tearDownClassz -o lstart doesn't work on NETBSDr"cCsPtd|j}|dd}t|j}tdt |}||ksLt dS)Nzps -o lstart -p %sZSTARTEDz%a %b %e %H:%M:%S %Y) r r'replacestrippsutilProcessZ create_timetimestrftime localtimeAssertionError)selfoutputZstart_psZ start_psutilrrrtest_process_create_timeXsz$BSDTestCase.test_process_create_timecCsdd}tjddD]}t|j}||j\}}}}|j|ksDt|j|ksRtt|j|dkrx| d|j|ft|j |dkr| d|j |fqdS)NcSstd|}|d}|d|d}|dd\}}}}|dkrRd}t|d}t|d}t|d}||||fS)Nz df -k "%s"rrnoner-)r r/rpopr)pathr linesr!devtotalusedfreerrrdfes      z"BSDTestCase.test_disks..dfF)allizpsutil=%s, df=%s) r0Zdisk_partitions disk_usageZ mountpointZdevicer5r@absrBZfailrA)r6rCpartusager?r@rArBrrr test_disksbs  zBSDTestCase.test_disksrzsysctl cmd not availablecCs td}tjdd|kstdS)Nzhw.ncpuT)Zlogical)rr0 cpu_countr5r6Zsystrrrtest_cpu_count_logical}sz"BSDTestCase.test_cpu_count_logicalzskipped on NETBSDcCstd}|tjkstdS)Nz hw.physmem)rr0virtual_memoryr@r5r6numrrrtest_virtual_memory_totalsz%BSDTestCase.test_virtual_memory_totalifconfigzifconfig cmd not availablec CsxtD]f\}}ztd|}Wntk r8Yq X|jd|kksLtd|kr |jtt d|dks tq dS)Nz ifconfig %sZRUNNINGmtuz mtu (\d+)r) r0Z net_if_statsitemsr RuntimeErrorZisupr5rRrrefindall)r6namestatsr rrrtest_net_if_statsszBSDTestCase.test_net_if_statsN)__name__ __module__ __qualname____doc__ classmethodr*r,r markskipifrr8rIrrLrPrYrrrrr$Ls*     r$z FREEBSD onlyc@sfeZdZeddZeddZeddZddZd d Z d d Z ed dZ eddZ dS)FreeBSDPsutilTestCasecCstj|_dSr%r&r(rrrr*sz FreeBSDPsutilTestCase.setUpClasscCst|jdSr%r+r(rrrr,sz#FreeBSDPsutilTestCase.tearDownClassc Cstd|j}t|jjdd}|ddd}|r|}|}|dd\}}}} } |} d||f| jks|tt | | j kst| j ds4|d | j ks4tq4dS) Nzprocstat -v %sF)groupedrrz%s-%s[ ) r r'r0r1Z memory_mapsrr<addrr5rZrssr=r) r6r mapsr>r!fields_startstopZ_permsresmaprrrtest_memory_mapss z&FreeBSDPsutilTestCase.test_memory_mapscCs<td|j}t|j|dddks8tdS)Nzprocstat -b %srr)r r'r0r1exerr5r6r rrrtest_exeszFreeBSDPsutilTestCase.test_execCsLtd|j}dt|jd|ddddksHtdS)Nzprocstat -c %s rrr)r r'joinr0r1rrr5rqrrr test_cmdlinesz"FreeBSDPsutilTestCase.test_cmdlinec Cstd|j}|dddd\}}}}}}t|j}|} |} | jt|ksbt | j t|kstt | j t|kst | jt|kst | j t|kst | j t|kst dS)Nzprocstat -s %srrr) r r'rr0r1uidsgidsrealrr5Z effectiveZsaved) r6r ZeuidZruidZsuidZegidZrgidZsgidprwrxrrrtest_uids_gidss& z$FreeBSDPsutilTestCase.test_uids_gidscCsg}td|j}t|j}|dD]}|}d|krrt|d}|j }||ksft | dq(d|kr(t|d}|j }||kst | dq(t |dkrtddS)Nprocstat -r %srz voluntary contextroz involuntary contextr)couldn't find lines match in procstat out)r r'r0r1rlowerr/rZnum_ctx_switchesZ voluntaryr5appendZ involuntarylenrTr6Ztestedr rzr!Z pstat_valueZ psutil_valuerrrtest_ctx_switchess"         z'FreeBSDPsutilTestCase.test_ctx_switchescCsg}td|j}t|j}|dD]}|}d|krtd|ddd}|j }||kstt | dq(d|kr(td|ddd}|j }||kst | dq(t |dkrtd dS) Nr|rz user timez0.ro.z system timerr})r r'r0r1rr~r/floatZ cpu_timesuserr5rsystemrrTrrrrtest_cpu_timess"         z$FreeBSDPsutilTestCase.test_cpu_timesN) rZr[r\r^r*r,r rnrrrur{rrrrrrras     rac@seZdZeddZddZeddZeddZed d Z ed d Z ed dZ eddZ e jje ddddZe jje ddeddZe jje ddeddZe jje ddeddZe jje ddeddZe jje ddeddZe jje ddedd Zd!d"Zd#d$Zd%d&Zed'd(Zd)d*Zd+d,Zd-d.Zd/d0Ze jje d1dd2d3Z!e jje d1dd4d5Z"e jje d6dd7d8Z#d9d:Z$d;S)<FreeBSDSystemTestCasecCsRtdd}td|}|s,td|dd|ddD\}}}|||fS) Nz swapinfo -kroz\s+zCan't parse swapinfo: %scss|]}t|dVqdS)r;N)r).0rzrrr sz7FreeBSDSystemTestCase.parse_swapinfo..rr9)r splitlinesrUrr)r7partsr@rArBrrrparse_swapinfos   z$FreeBSDSystemTestCase.parse_swapinfocCsd}ztt|}Wntk r2tdYnXtj|ksFtd}t|}t| d dd}t| d dd}tj |ksttj |kstdS)Nzdev.cpu.0.freqz#frequencies not supported by kernelzdev.cpu.0.freq_levelsr/ro) rrrTr skipr0Zcpu_freqcurrentr5rmaxmin)r6sensor sysctl_resultZmax_freqZmin_freqrrr!test_cpu_frequency_against_sysctlsz7FreeBSDSystemTestCase.test_cpu_frequency_against_sysctlcCs*tdt}ttj|tks&tdS)Nzvm.stats.vm.v_active_count)rPAGESIZErFr0rMactiverr5rKrrrtest_vmem_actives z&FreeBSDSystemTestCase.test_vmem_activecCs*tdt}ttj|tks&tdS)Nzvm.stats.vm.v_inactive_count)rrrFr0rMinactiverr5rKrrrtest_vmem_inactive!s z(FreeBSDSystemTestCase.test_vmem_inactivecCs*tdt}ttj|tks&tdS)Nzvm.stats.vm.v_wire_count)rrrFr0rMwiredrr5rKrrrtest_vmem_wired&s z%FreeBSDSystemTestCase.test_vmem_wiredcCs*tdt}ttj|tks&tdS)Nzvm.stats.vm.v_cache_count)rrrFr0rMcachedrr5rKrrrtest_vmem_cached+s z&FreeBSDSystemTestCase.test_vmem_cachedcCs*tdt}ttj|tks&tdS)Nzvm.stats.vm.v_free_count)rrrFr0rMrBrr5rKrrrtest_vmem_free0s z$FreeBSDSystemTestCase.test_vmem_freecCs&td}ttj|tks"tdS)Nz vfs.bufspace)rrFr0rMbuffersrr5rKrrrtest_vmem_buffers5sz'FreeBSDSystemTestCase.test_vmem_bufferszmuse not installedr"cCstd}tj|kstdS)NZTotal)rr0rMr@r5rNrrrtest_muse_vmem_total<sz*FreeBSDSystemTestCase.test_muse_vmem_totalcCs&td}ttj|tks"tdS)NZActive)rrFr0rMrrr5rNrrrtest_muse_vmem_activeAsz+FreeBSDSystemTestCase.test_muse_vmem_activecCs&td}ttj|tks"tdS)NZInactive)rrFr0rMrrr5rNrrrtest_muse_vmem_inactiveGsz-FreeBSDSystemTestCase.test_muse_vmem_inactivecCs&td}ttj|tks"tdS)NZWired)rrFr0rMrrr5rNrrrtest_muse_vmem_wiredMsz*FreeBSDSystemTestCase.test_muse_vmem_wiredcCs&td}ttj|tks"tdS)NCache)rrFr0rMrrr5rNrrrtest_muse_vmem_cachedSsz+FreeBSDSystemTestCase.test_muse_vmem_cachedcCs&td}ttj|tks"tdS)NZFree)rrFr0rMrBrr5rNrrrtest_muse_vmem_freeYsz)FreeBSDSystemTestCase.test_muse_vmem_freecCs&td}ttj|tks"tdS)NBuffer)rrFr0rMrrr5rNrrrtest_muse_vmem_buffers_sz,FreeBSDSystemTestCase.test_muse_vmem_bufferscCs"ttjtddkstdS)Nzvm.stats.sys.v_swtch)rFr0 cpu_stats ctx_switchesrr5r6rrrtest_cpu_stats_ctx_switchesesz1FreeBSDSystemTestCase.test_cpu_stats_ctx_switchescCs"ttjtddkstdS)Nzvm.stats.sys.v_intrr)rFr0r interruptsrr5rrrrtest_cpu_stats_interruptsnsz/FreeBSDSystemTestCase.test_cpu_stats_interruptscCs"ttjtddkstdS)Nzvm.stats.sys.v_softr)rFr0rZsoft_interruptsrr5rrrrtest_cpu_stats_soft_interruptstsz4FreeBSDSystemTestCase.test_cpu_stats_soft_interruptscCs"ttjtddkstdS)Nzvm.stats.sys.v_syscalli@ )rFr0rZsyscallsrr5rrrrtest_cpu_stats_syscalls}sz-FreeBSDSystemTestCase.test_cpu_stats_syscallscCs,|\}}}ttj|tks(tdSr%)rrFr0 swap_memoryrBrr5)r6_total_usedrBrrrtest_swapmem_freesz'FreeBSDSystemTestCase.test_swapmem_freecCs,|\}}}ttj|tks(tdSr%)rrFr0rrArr5)r6rrA_freerrrtest_swapmem_usedsz'FreeBSDSystemTestCase.test_swapmem_usedcCs,|\}}}ttj|tks(tdSr%)rrFr0rr@rr5)r6r@rrrrrtest_swapmem_totalsz(FreeBSDSystemTestCase.test_swapmem_totalcCsLtd}||ddd}|d|d}t|}|tksHtdS)Nzsysctl kern.boottimez sec = ,)rrrr0 boot_timer5)r6sZbtimerrrtest_boot_times z$FreeBSDSystemTestCase.test_boot_timez no batterycCsdd}td}tdd|dD}t}t|ddd }|d }|j|ksZt|d krt|j tj kstn||j |kstdS) NcSs(t|d\}}t|d\}}d||fS)N<z%d:%02d)divmod)ZsecsmZ_shrrr secs2hourssz>FreeBSDSystemTestCase.test_sensors_battery..secs2hoursz acpiconf -i 0cSs(g|] }|dd|ddfqS) rro)r)rxrrr sz>FreeBSDSystemTestCase.test_sensors_battery..rzRemaining capacity:%r-zRemaining time:unknown) r dictrr0sensors_batteryrr.percentr5secsleftZPOWER_TIME_UNLIMITED)r6rr rhZmetricsrZremaining_timerrrtest_sensors_batterysz*FreeBSDSystemTestCase.test_sensors_batterycCsltjtdksttjtddkks0ttj}|dkrTtddkshtn|tddkshtdS)Nhw.acpi.battery.lifehw.acpi.aclinerrhw.acpi.battery.timeror)r0rrrr5Z power_pluggedr)r6rrrr#test_sensors_battery_against_sysctls    z9FreeBSDSystemTestCase.test_sensors_battery_against_sysctlz has batteryc CsBtttdtdtdW5QRXtdks>tdS)Nrrr)r ZraisesrTrr0rr5rrrrtest_sensors_battery_no_batterys  z5FreeBSDSystemTestCase.test_sensors_battery_no_batteryc Cstd}t|D]}d|}zttt|dd}Wntk rXtdYnXt t d|j |dks|t d|}ttt|dd}t d|j |kst qdS)NTzdev.cpu.%s.temperatureroz$temperatures not supported by kernelZcoretemprezdev.cpu.%s.coretemp.tjmax)r0rJrangerrrrTr rrFZsensors_temperaturesrr5high)r6Znum_cpuscpurrrrr(test_sensors_temperatures_against_sysctls*  z>FreeBSDSystemTestCase.test_sensors_temperatures_against_sysctlN)%rZr[r\ staticmethodrrr rrrrrrr r_r`MUSE_AVAILABLErrrrrrrrrrrrrrrrrrrrrrrrrsf                     rz OPENBSD onlyc@seZdZddZdS)OpenBSDTestCasecCs6td}tj|d}tjt}||ks2tdS)Nz kern.boottimez%a %b %d %H:%M:%S %Y)rdatetimestrptime fromtimestampr0rr5)r6rZsys_btZ psutil_btrrrrszOpenBSDTestCase.test_boot_timeN)rZr[r\rrrrrrsrz NETBSD onlyc@sheZdZeddZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZdS)NetBSDTestCasec Cs\td>}|D]2}||rt|ddW5QRSqW5QRXtd|dS)Nz /proc/meminforr;z can't find %s)openrrrr)Zlook_forfr!rrr parse_meminfos   0zNetBSDTestCase.parse_meminfocCstj|dkstdS)Nz MemTotal:)r0rMr@rr5rrrrtest_vmem_total szNetBSDTestCase.test_vmem_totalcCs$ttj|dtks tdS)NzMemFree:)rFr0rMrBrrr5rrrrr szNetBSDTestCase.test_vmem_freecCs$ttj|dtks tdS)NzBuffers:)rFr0rMrrrr5rrrrrsz NetBSDTestCase.test_vmem_bufferscCs$ttj|dtks tdS)Nz MemShared:)rFr0rMZsharedrrr5rrrrtest_vmem_sharedszNetBSDTestCase.test_vmem_sharedcCs$ttj|dtks tdS)NzCached:)rFr0rMrrrr5rrrrr$szNetBSDTestCase.test_vmem_cachedcCs$ttj|dtks tdS)Nz SwapTotal:)rFr0rr@rrr5rrrrr,sz!NetBSDTestCase.test_swapmem_totalcCs$ttj|dtks tdS)Nz SwapFree:)rFr0rrBrrr5rrrrr2sz NetBSDTestCase.test_swapmem_freecCs"t}|j|j|jkstdSr%)r0rrAr@rBr5)r6Zsmemrrrr8sz NetBSDTestCase.test_swapmem_usedc Csdtdd6}|D]"}|drt|d}qs  z(NetBSDTestCase.test_cpu_stats_interruptsc Csdtdd6}|D]"}|drt|d}q sJ              QXx