gfPdZddlZddlZddlZddlZddlZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZer3ddlmZeZejdkr edrdndZndZdZdZdZeje dGdde Zeje dGdde Zeje dGdde Zeje dGdd e Zeje d!Gd"d#e Z e!d$krdd%l"m#Z#e#e$dSdS)&z$Tests specific to all BSD platforms.N)BSD)FREEBSD)NETBSD)OPENBSD)spawn_testproc) HAS_BATTERY)PsutilTestCase)retry_on_failure)sh)TOLERANCE_SYS_MEM) terminate)unittest)which) getpagesizemuseTFctd|z}tr!||ddzd}n.tstr ||ddzd} t |S#t $r|cYSwxYw)zmExpects a sysctl command with an argument and parse the result returning only the value of interest. zsysctl z: N=)r rfindrrint ValueError)cmdlineresults w/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/psutil/tests/test_bsd.pysysctlr-s G# $ $F/ D))A-../ /F/ C((1,--.6{{  s*A99 BBctd}|dD]}||rntdt |dS)z+Thin wrapper around 'muse' cmdline utility.r zline not foundr)r split startswithrr)fieldoutlines rrr<so V**C $++ ??5 ! !  E )*** tzz||A  zBSD onlyceZdZdZedZedZeje ddZ dZ eje d dd Z eje d dd Zd Zd S) BSDTestCasez)Generic tests common to all BSD variants.c6tj|_dSNrpidclss r setUpClasszBSDTestCase.setUpClassP ""&r$c.t|jdSr(r r*r+s r tearDownClasszBSDTestCase.tearDownClassT#'r$z -o lstart doesn't work on NETBSDcTtd|jz}|dd}t j|j}tjdtj |}| ||dS)Nzps -o lstart -p %sSTARTEDz%a %b %e %H:%M:%S %Y) r r*replacestrippsutilProcess create_timetimestrftime localtime assertEqual)selfoutputstart_ps start_psutils rtest_process_create_timez$BSDTestCase.test_process_create_timeXs(48344>>)R006688~dh//;;== }%;%)^L%A%ACC  <00000r$cd}tjdD]}tj|j}||j\}}}}||j|||j|t|j|z dkr | d|jd|t|j |z dkr | d|j d|dS)Nctd|z}|d}|d|d}|dd\}}}}|dkrd}t |dz}t |dz}t |dz}||||fS)Nz df -k "%s"rrnoner5)r r7rpopr)pathr"linesr#devtotalusedfrees rdfz"BSDTestCase.test_disks..dfds\D())//11CIIdOOE IIaLLL99Q<devicerMabsrOfailrN)r?rPpartusagerLrMrNrOs r test_diskszBSDTestCase.test_disksas * * **u555 C CD%do66E%'R%8%8 "Cd   T[# . . .   U[% 0 0 05:$%%(888  DDABBB5:$%%(888  DDABBB C Cr$rzsysctl cmd not availablecvtd}|tjd|dS)Nzhw.ncpuT)logical)rr>r8 cpu_countr?systs rtest_cpu_count_logicalz"BSDTestCase.test_cpu_count_logical|s9i   )$777>>>>>r$c|td}||tjjdS)Nz hw.physmem)rr>r8virtual_memoryrMr?nums rtest_virtual_memory_totalz%BSDTestCase.test_virtual_memory_totals7\"" f355;<<<<isuprirrefindall RuntimeError)r?namestatsr"s rtest_net_if_statszBSDTestCase.test_net_if_statss!.006688 L LKD% L-..  Y#-=3 GGGC<<$$UY%(L#)F)Fq)I%J%JLLL       L LsB## B0/B0N)__name__ __module__ __qualname____doc__ classmethodr-r1rskipIfrrCrZrr`rerrr$rr&r&Ls33''['[X_V?@@11A@1CCC6X_x(*DEE??FE?X_x(*DEE==FE= L L L L Lr$r&z FREEBSD onlyceZdZedZedZedZdZdZ dZ edZ edZ d S) FreeBSDPsutilTestCasec6tj|_dSr(r)r+s rr-z FreeBSDPsutilTestCase.setUpClassr.r$c.t|jdSr(r0r+s rr1z#FreeBSDPsutilTestCase.tearDownClassr2r$crtd|jz}tj|jd}|ddd}|r|}|}|dd\}}}} } |} ||d|| j|t| | j | j ds!||d | j |dSdS) Nzprocstat -v %sF)groupedrr-[ ) r r*r8r9 memory_mapsrrIr>addrrrssrJr ) r?r"mapsrKr#fields_startstoppermsresmaps rtest_memory_mapsz&FreeBSDPsutilTestCase.test_memory_mapss+!DH,--~dh''33E3BB $# 799;;DZZ\\F)/ &AudE3((**C   tt4ch ? ? ?   SXXsw / / /8&&s++ 7  SX666 7 7 7 7 7r$ctd|jz}|tj|j|ddddS)Nzprocstat -b %srr)r r*r>r8r9exerr?r"s rtest_exezFreeBSDPsutilTestCase.test_exesv!DH,-- 1155774+1133B7 9 9 9 9 9r$cdtd|jz}|dt j|jd|dddddS)Nzprocstat -c %s rrr)r r*r>joinr8r9rrrs r test_cmdlinez"FreeBSDPsutilTestCase.test_cmdlines!DH,-- &.":":"B"B"D"DEE#))D//!"4":":"<"realr effectivesaved) r?r"euidruidsuidegidrgidsgidprrs rtest_uids_gidsz$FreeBSDPsutilTestCase.test_uids_gidss2!DH,---0YYt__Q-?-E-E-G-G!-L*dD$d N48 $ $vvxxvvxx CII... T333 SYY/// CII... T333 SYY/////r$cg}td|jz}tj|j}|dD]}|}d|vrlt|d}|j }| ||| dd|vrkt|d}|j }| ||| d t|dkrtddS)Nprocstat -r %srz voluntary contextrz involuntary contextr)couldn't find lines match in procstat out)r r*r8r9rlowerr7rnum_ctx_switches voluntaryr>append involuntarylenror?testedr"rr# pstat_value psutil_values rtest_ctx_switchesz'FreeBSDPsutilTestCase.test_ctx_switchessM!DH,-- N48 $ $IIdOO $ $D::<<%%''D#t++!$**,,r"233  1133=   l;;; d####'4//!$**,,r"233  1133?   l;;; d### v;;!  JKK K  r$c\g}td|jz}tj|j}|dD]@}|}d|vrtd|dddz}|j }| ||| dd|vrtd|dddz}|j }| ||| dBt|dkrtd dS) Nrrz user timez0.r.z system timerr)r r*r8r9rrr7float cpu_timesuserr>rsystemrrors rtest_cpu_timesz$FreeBSDPsutilTestCase.test_cpu_timessu!DH,-- N48 $ $IIdOO $ $D::<<%%''Dd""#D4::<<+;+A+A#+F+Fr+J$JKK  {{}}1   l;;; d####$&&#D4::<<+;+A+A#+F+Fr+J$JKK  {{}}3   l;;; d### v;;!  JKK K  r$N) rsrtrurwr-r1r rrrrrrryr$rr{r{s''['[ 7 7 7999 CCC 0 0 0LLL&LLLLLr$r{c>eZdZedZdZedZedZedZ edZ edZ edZ e je d d Ze je d ed Ze je d ed Ze je d ed Ze je d edZe je d edZe je d edZdZdZdZedZdZdZdZdZe je ddZ e je ddZ!e jeddZ"dZ#dS) FreeBSDSystemTestCasectdd}tjd|}|st d|zd|ddD\}}}|||fS)Nz swapinfo -krz\s+zCan't parse swapinfo: %sc3:K|]}t|dzVdS)rHN)r).0rs r z7FreeBSDSystemTestCase.parse_swapinfo..s,??qSVVd]??????r$rrF)r splitlinesrmrr)r@partsrMrNrOs rparse_swapinfoz$FreeBSDSystemTestCase.parse_swapinfosM""--//3(( B7&@AA A@?E!A#J???tTdD  r$cd} tt|}n%#t$r|dYnwxYw|t jj|d}t|}t|ddd}t|ddd}|t jj ||t jj |dS)Nzdev.cpu.0.freqz#frequencies not supported by kernelzdev.cpu.0.freq_levelsr/r) rrroskipTestr>r8cpu_freqcurrentrmaxmin)r?sensor sysctl_resultmax_freqmin_freqs r!test_cpu_frequency_against_sysctlz7FreeBSDSystemTestCase.test_cpu_frequency_against_sysctls9" Av//MM A A A MM? @ @ @ @ @ A **2MBBB(v }**,,Q/55c::1=>>}**,,R066s;;A>?? **.999 **.99999s!AActdtz}|tjj|t dS)Nzvm.stats.vm.v_active_countdelta)rPAGESIZEassertAlmostEqualr8rbactiver r^s rtest_vmem_activez&FreeBSDSystemTestCase.test_vmem_activesO233h> v466=t%6  8 8 8 8 8r$ctdtz}|tjj|t dS)Nzvm.stats.vm.v_inactive_countr)rrrr8rbinactiver r^s rtest_vmem_inactivez(FreeBSDSystemTestCase.test_vmem_inactivesO455@ v466?%6  8 8 8 8 8r$ctdtz}|tjj|t dS)Nzvm.stats.vm.v_wire_countr)rrrr8rbwiredr r^s rtest_vmem_wiredz%FreeBSDSystemTestCase.test_vmem_wired$sO011H< v466%6  8 8 8 8 8r$zmuse not installedc|td}|tjj|dS)NTotal)rr>r8rbrMrcs rtest_muse_vmem_totalz*FreeBSDSystemTestCase.test_muse_vmem_total>s57mm .006<<<<%6  8 8 8 8 8r$c||tjjt dddS)Nzvm.stats.sys.v_swtchr)rr8 cpu_stats ctx_switchesrr?s rtest_cpu_stats_ctx_switchesz1FreeBSDSystemTestCase.test_cpu_stats_ctx_switchesmsJ v/11>%&<==T  K K K K Kr$c||tjjt dddS)Nzvm.stats.sys.v_intrrr)rr8r interruptsrrs rtest_cpu_stats_interruptsz/FreeBSDSystemTestCase.test_cpu_stats_interruptsqsJ v/11<%&;<??v  O O O O Or$c|\}}}|tjj|t dSNr)rrr8 swap_memoryrOr r?rMrNrOs rtest_swapmem_freez'FreeBSDSystemTestCase.test_swapmem_freeZ //11tT    %t3D  F F F F Fr$c|\}}}|tjj|t dSr )rrr8r rNr rs rtest_swapmem_usedz'FreeBSDSystemTestCase.test_swapmem_usedrr$c|\}}}|tjj|t dSr )rrr8r rMr rs rtest_swapmem_totalz(FreeBSDSystemTestCase.test_swapmem_totalsZ //11tT    &5F  H H H H Hr$c td}||ddzd}|d|d}t|}||t jdS)Nzsysctl kern.boottimez sec = ,)rrrr>r8 boot_time)r?sbtimes rtest_boot_timez$FreeBSDSystemTestCase.test_boot_timesx ) * * affY!#$$ % lqvvc{{lOA  0 2 233333r$z no batterycd}td}td|dD}tj}t |ddd}|d}||j||d kr'||j tj dS|||j |dS) Nc\t|d\}}t|d\}}d||fzS)N<z%d:%02d)divmod)secsmrhs r secs2hoursz>FreeBSDSystemTestCase.test_sensors_battery..secs2hourss5$##DAq!R==DAq1v% %r$z acpiconf -i 0czg|]8}|dd|ddf9S) rr)r)rxs r z>FreeBSDSystemTestCase.test_sensors_battery..sK111 a(!''$--*;<111r$rzRemaining capacity:%r5zRemaining time:unknown) r dictrr8sensors_batteryrr6r>percentsecsleftPOWER_TIME_UNLIMITED)r?r#r"rmetricsr,remaining_times rtest_sensors_batteryz*FreeBSDSystemTestCase.test_sensors_batterys & & & !!11 # $11122(**f23;;CDDEE 12 '222 Y & &   W-v/J K K K K K   ZZ(899> J J J J Jr$c|tjjt d|tjjt ddktjj}|dkr%|t dddS||t ddzdS)Nhw.acpi.battery.lifehw.acpi.aclinerrhw.acpi.battery.timerr)r>r8r+r,r power_pluggedr-)r?r-s r#test_sensors_battery_against_sysctlz9FreeBSDSystemTestCase.test_sensors_battery_against_sysctls /119 677 9 9 9 /11? 011Q6 8 8 8)++4 a<<   V$:;;R @ @ @ @ @   Xv.D'E'E'J K K K K Kr$z has batteryc|t5tdtdtddddn #1swxYwY|t jdS)Nr3r5r4) assertRaisesror assertIsNoner8r+rs rtest_sensors_battery_no_batteryz5FreeBSDSystemTestCase.test_sensors_battery_no_batterys   | , , % % ) * * * ) * * * # $ $ $ % % % % % % % % % % % % % % % &02233333s.AAAc`tjd}t|D]}d|z} tt t |dd}n%#t $r|dYnwxYw|tj d|j |dd|z}tt t |dd}| tj d|j | dS) NTzdev.cpu.%s.temperaturerz$temperatures not supported by kernelcoretemprrzdev.cpu.%s.coretemp.tjmax) r8r]rangerrrrorrsensors_temperaturesrr>high)r?num_cpuscpurrs r(test_sensors_temperatures_against_sysctlz>FreeBSDSystemTestCase.test_sensors_temperatures_against_sysctlsC#D))??  C-3F F #E&.."*=$>$> ? ?  F F F DEEEEE F  " "+--j9#>FR # ) ) )136FfVnnSbS&9 : :;;M   +--j9#>C      s1ABBN)$rsrtru staticmethodrrr rrrrrrrrxMUSE_AVAILABLErrrrrrrrrrr rrrrrr1r7r;rCryr$rrrs ! !\ !:::,888 888 888 888 888 888X_')=>>==?>=X_')=>>88?>8 X_')=>>88?>8 X_')=>>88?>8 X_')=>>88?>8 X_')=>>88?>8 X_')=>>88?>8 KKKJJJJJJOOOFFF FFF HHH444X__l33KK43K$X__l33 L L43 LX_[-0044104r$rz OPENBSD onlyceZdZdZdS)OpenBSDTestCasectd}tj|d}tjt j}|||dS)Nz kern.boottimez%a %b %d %H:%M:%S %Y)rdatetimestrptime fromtimestampr8rr>)r?rsys_bt psutil_bts rrzOpenBSDTestCase.test_boot_timesc ? # #"++A/EFF%33F4D4F4FGG  +++++r$N)rsrtrurryr$rrGrGs#,,,,,r$rGz NETBSD onlycZeZdZedZdZdZdZdZdZ dZ dZ d Z d Z d S) NetBSDTestCasectdd5}|D]O}||r8t|ddzccdddSP dddn #1swxYwYt d|z)Nz /proc/meminfortrrHz can't find %s)openr rrr)look_forfr#s r parse_meminfozNetBSDTestCase.parse_meminfos /4 ( ( 7A 7 7??8,,7tzz||A//$666 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 783444sAA1#A11A58A5c|tjj|ddS)Nz MemTotal:)r>r8rbrMrUrs rtest_vmem_totalzNetBSDTestCase.test_vmem_totalsH   ! # # )4+=+=k+J+J L L L L Lr$c|tjj|dt dS)NzMemFree:r)rr8rbrOrUr rs rrzNetBSDTestCase.test_vmem_freesK   ! # # ($*<*,>|,L,L#  % % % % %r$c|tjj|dt dS)Nz SwapTotal:r)rr8r rMrUr rs rrz!NetBSDTestCase.test_swapmem_totalsK    &(:(:<(H(H#  % % % % %r$c|tjj|dt dS)Nz SwapFree:r)rr8r rOrUr rs rrz NetBSDTestCase.test_swapmem_freesK    %t'9'9+'F'F#  % % % % %r$c|tj}||j|j|jz dSr()r8r r>rNrMrO)r?smems rrz NetBSDTestCase.test_swapmem_useds7!## DJ$:;;;;;r$cXtdd5}|D]@}|dr)t|d}nAt d dddn #1swxYwY|t jj|ddS)N /proc/statrbsintrrcouldn't find linerr) rRr rrrrr8rr)r?rTr#rs rrz(NetBSDTestCase.test_cpu_stats_interruptss , % % 7 7 7??7++!$TZZ\\!_!5!5JE!!5666  7 7 7 7 7 7 7 7 7 7 7 7 7 7 7      ):T  C C C C CAA11A58A5cXtdd5}|D]@}|dr)t|d}nAt d dddn #1swxYwY|t jj|ddS)Nrbrcsctxtrrdrr) rRr rrrrr8rr)r?rTr#rs rrz*NetBSDTestCase.test_cpu_stats_ctx_switches*s , % % 7 7 7??7++#&tzz||A#7#7LE!!5666  7 7 7 7 7 7 7 7 7 7 7 7 7 7 7      +\  G G G G GreN)rsrtrurDrUrWrrr\rrrrrryr$rrOrOs55\5LLL%%% %%% %%% %%% %%% <<< C C C G G G G Gr$rO__main__) run_from_name)%rvrIosrmr;r8rrrr psutil.testsrrr r r r r rrpsutil._psutil_posixrrgetuidrErrrxr&r{rrGrOrspsutil.tests.runnerrh__file__ryr$rross+* ''''''$$$$$$''''''))))))******""""""!!!!!!000000{}}H&RY[[A--%%---TTUNNHN      S*%%CLCLCLCLCL.CLCL&%CLVWn--VLVLVLVLVLNVLVL.-VLrWn--iiiiiNii.-ibWn--,,,,,n,,.-,V]++?G?G?G?G?G^?G?G,+?GD z111111M(r$