U 1gO@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl m Z ddl mZddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddlm*Z*ddlm+Z+dd lm,Z,dd!lm-Z-dd"lm.Z.dd#lm/Z/dd$lm0Z0dd%lm1Z1dd&lm2Z2Gd'd(d(e-Z3Gd)d*d*e-Z4Gd+d,d,e-Z5Gd-d.d.e-Z6Gd/d0d0e-Z7Gd1d2d2e-Z8Gd3d4d4e-Z9Gd5d6d6e-Z:dS)7zTests for system APIS.N)AIX)BSD)FREEBSD)LINUX)MACOS)NETBSD)OPENBSD)POSIX)SUNOS)WINDOWS)PY3)FileNotFoundError)long)ASCII_FS) CI_TESTING)DEVNULL)GITHUB_ACTIONS)GLOBAL_TIMEOUT) HAS_BATTERY) HAS_CPU_FREQ)HAS_GETLOADAVG)HAS_NET_IO_COUNTERS)HAS_SENSORS_BATTERY)HAS_SENSORS_FANS)HAS_SENSORS_TEMPERATURES)IS_64BIT) MACOS_12PLUS)PYPY) QEMU_USER)UNICODE_SUFFIX)PsutilTestCase)check_net_address)enum)mock)pytest)retry_on_failurec@s<eZdZddZddZddZddZd d Zd d Zd S)TestProcessItercCs~tddtDkst|}|jddtDksBtt|j}|| |jddtDksztdS)NcSsg|] }|jqSpid.0xr'r'H/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_system.py Csz5TestProcessIter.test_pid_presence..cSsg|] }|jqSr'r(r*r'r'r-r.EscSsg|] }|jqSr'r(r*r'r'r-r.Is) osgetpidpsutil process_iterAssertionErrorspawn_testprocr)ProcesskillwaitselfZsprocpr'r'r-test_pid_presenceBs z!TestProcessIter.test_pid_presencecCs>ddtD}t|dddtt|dddks:tdS)NcSsg|]}|qSr'r'r*r'r'r-r.Lsz6TestProcessIter.test_no_duplicates..cSs|jSNr(r,r'r'r-Mz4TestProcessIter.test_no_duplicates..)keycSs|jSr<r(r=r'r'r-r>Nr?)r1r2sortedsetr3)r9lsr'r'r-test_no_duplicatesKs z"TestProcessIter.test_no_duplicatesc CsftttdD]L}tjdttd ttjdgdgksLt W5QRXtj qdSNzpsutil.Process.as_dictZ side_effect cpu_timesattrs) listr1r2ranger#patchZ NoSuchProcessr/r0r3 cache_clearr9r,r'r'r-test_emulate_nspQs   $z TestProcessIter.test_emulate_nspc CsvtttdD]\}tjdttd0t tjttjdgdW5QRXW5QRXtj qdSrE) rKr1r2rLr#rM AccessDeniedr/r0r$raisesrNrOr'r'r-test_emulate_access_denied[s   &z*TestProcessIter.test_emulate_access_deniedc Cs`tjdgdD]}t|jdgkstqtjdgdD]}t|jdgks:tq:ttttjdgdW5QRXt j dt dddJ}tjddgdD](}|jddkst|jddkstq|j stW5QRXt j dt dddZ}t }tjddg|d D].}|jd|ks.t|jddkstq|j sRtW5QRXdS) Nr)rIZfooz$psutil._psplatform.Process.cpu_timesrrGrH)rJZad_value)r1r2rKinfokeysr3r$rR ValueErrorr#rMrQcalledobject)r9r:mflagr'r'r- test_attrsfs8    zTestProcessIter.test_attrscCs.tttjsttjtjr*tdSr<)rKr1r2Z_pmapr3rNr9r'r'r-test_cache_clears   z TestProcessIter.test_cache_clearN) __name__ __module__ __qualname__r;rDrPrSr\r^r'r'r'r-r&As    r&c@sTeZdZejjeoeddddZejjeo0eddddZ ddZ d d Z d S) TestProcessAPIsz-spawn_testproc() unreliable on PYPY + WINDOWSreasonc sfdd}g|}|}|}dd|||fD}tttj|ddW5QRXtttj|ddW5QRXt}tj|d |d \}}t|d kst|gkstt |d kstgkst|D]} t | d rtqt ddd} | | ||\}}|j dd|Dks4ttrT|jtj kshtn|jdksht|j gkszt|D]} t | d r~tq~t ddd} | | | ||\}}tt|j |j |j gkst|D]} t | d stqdS)Ncs|jdSr<)appendr))r:pidsr'r-callbacksz1TestProcessAPIs.test_wait_procs..callbackcSsg|]}t|jqSr'r1r5r)r*r'r'r-r.sz3TestProcessAPIs.test_wait_procs..)timeout)rhg{Gz?rkrhg? returncodecSs<tj|d|d\}}t|dks$tt|dks4t||fS)NQ?rmrlrFr1 wait_procslenr3procsrhgonealiver'r'r-test_1s z/TestProcessAPIs.test_wait_procs..test_1cSsg|] }|jqSr'r(r*r'r'r-r.scSs<tj|d|d\}}t|dks$tt|dks4t||fS)Nrqrmrnrrrrur'r'r-test_2s z/TestProcessAPIs.test_wait_procs..test_2)r4r$rRrWr1rs TypeErrortimer3rthasattrr% terminater)r poprosignalSIGTERMrB) r9rhsproc1sproc2sproc3rvtrwrxr:ryrzr'rfr-test_wait_procssJ       "zTestProcessAPIs.test_wait_procscCsL|}|}|}dd|||fD}|D] }|q0t|dS)NcSsg|]}t|jqSr'rir*r'r'r-r.sz>TestProcessAPIs.test_wait_procs_no_timeout..)r4r~r1rs)r9rrrrvr:r'r'r-test_wait_procs_no_timeouts z*TestProcessAPIs.test_wait_procs_no_timeoutcCsp|}t|jstt|j}||t|jrDttdrRttddtkksltdS)Nrjr) r4r1 pid_existsr)r3r5r6r7rgr8r'r'r-test_pid_existss zTestProcessAPIs.test_pid_existsc Cst}|D]F}zt|s tWq tk rPtd|tksLtYq Xq tt|dt|d}|D]}t|rrtqrdS)Ng?i:i>)r1rgrr3r|sleeprLmax)r9rgr)r'r'r-test_pid_exists_2s z!TestProcessAPIs.test_pid_exists_2N) r_r`rar$markskipifrr rrrrr'r'r'r-rbs ;  rbc@sFeZdZddZejjeo"e ddddZ ddZ d d Z d S) TestMiscAPIscCs6t}t|tst|dks"t|tks2tdSNr)r1Z boot_time isinstancefloatr3r|)r9Zbtr'r'r-test_boot_times zTestMiscAPIs.test_boot_timeunreliable on CIrcc Cst}|gkst|D]}|j|d|js4tt|jtsDtt|jttdfs\t|j dk r~t|j ttdfs~t|j|j |j dkstt j |j t str|jdkstn t|jW5QRXqdS)N)user)r1usersr3subTestnamerstrZterminaltypehoststarteddatetime fromtimestampr rr)r5)r9rrr'r'r- test_userss    zTestMiscAPIs.test_userscCs&tj}tt_z tW5|t_XdSr<)sysstdoutrr1test)r9rr'r'r- test_test s  zTestMiscAPIs.test_testc Csddddddddd g }|D]}ttt|tst|qtjd krFtjsNttjrXt| dd t j krtj szt| dnd t j krtjsttjtjtjgd dkst| d| d| d| dn^dt j ksdt j krtjst| d n&dt j krhtjs:t| dn"tjsRttjr^t| d|D]}tt|rlt|qldS)Nr r rrrrrrr posixlinuxZbsdTrlsunossolarisdarwin)rgetattrr1boolr3r/rr r removerplatformlowerrrrrrcountr r)r9namesrr'r'r-test_os_constantssX                     zTestMiscAPIs.test_os_constantsN) r_r`rarr$rrrr1rrrrr'r'r'r-rs   rc@seZdZddZddZdS)TestMemoryAPIscCst}|jdkst||jdks,t|d|jkrBdksLnt||jdks^t||jdkspt||jD]l}t ||}|dkrt |t t fst|dkrv|dks| d||f||jkrv| d||j||fqvdS)Nrdpercenttotalz %r < 0 (%s)z%r > total (total=%s, %s=%s))r1Zvirtual_memoryrr3 availablerusedfree_fieldsrrintrfail)r9memrvaluer'r'r-test_virtual_memoryHs&     z"TestMemoryAPIs.test_virtual_memorycCst}|jdkst|jdks(t||jdks:t||jdkrX|jdksjt|n|jdksjt|d|jkrdksnt||jdkst||j dkst|dS)N)rrrrsinsoutrr) r1Z swap_memoryrr3rrrrrr)r9rr'r'r-test_swap_memory\s   zTestMemoryAPIs.test_swap_memoryN)r_r`rarrr'r'r'r-rGsrc@seZdZddZddZddZddZd d Zd d Zd dZ e j j e oLeddddZddZddZddZddZddZddZdd Ze j j eoed!kd"de j j e d#dd$d%Ze j j e d#dd&d'Zd(S)) TestCpuAPIsc Csvt}|dk st|ttjddks,t|dks8ttjdrrtd}| }W5QRXd|krrt ddS)NTpercpurlz /proc/cpuinfoz physical idz#cpuinfo doesn't include physical id) r1 cpu_countr3rtrHr/pathexistsopenreadr$skip)r9logicalfdZ cpuinfo_datar'r'r-test_cpu_count_logicalts    z"TestCpuAPIs.test_cpu_count_logicalcCsht}tjdd}|dkr&tdtrLtdddkrL|dksdtn|dksXt||ksdtdS)NFrzcpu_count_cores() is NonerF)rlrl)r1rr$rr rgetwindowsversionr3)r9rZcoresr'r'r-test_cpu_count_coress   z TestCpuAPIs.test_cpu_count_coresc CszdD]p}tjd|d }tdks(t|js2tW5QRXtjd|d$}tjdddks`t|jsjtW5QRXqdS)N)rjrNz$psutil._psplatform.cpu_count_logicalZ return_valuez"psutil._psplatform.cpu_count_coresFr)r#rMr1rr3rX)r9valrZr'r'r-test_cpu_count_noneszTestCpuAPIs.test_cpu_count_nonecCsjd}t}t||D]&}t|ts*t|dks6t||7}qtt|t|ddks^tt|dS)Nrrr) r1rHsumrrr3roundabsr)r9rtimescp_timer'r'r-test_cpu_timess  zTestCpuAPIs.test_cpu_timescCsLtt}tt}t|kr>tt}||krdSq|ddS)Nztime remained the same)rr1rHr|rr)r9t1Zstop_att2r'r'r-test_cpu_times_time_increasess    z)TestCpuAPIs.test_cpu_times_time_increasescCstjddD]b}d}t||D]&}t|ts2t|dks>t||7}q tt|t|ddksftt|q t tjdddt tjddkstdS)NTrrrrF) r1rHrrrr3rrrrt)r9rrrr'r'r-test_per_cpu_timess    zTestCpuAPIs.test_per_cpu_timescCstjdd}tt}t|kr.|dStjdd}t||D]4\}}t|t|}}||}|dkrDdSqDqdS)NTrrkg?)r1rHr|rrzipZ_cpu_busy_time)r9Ztot1Z giveup_atZtot2rr differencer'r'r-test_per_cpu_times_2s     z TestCpuAPIs.test_per_cpu_times_2zunreliable on OPENBSD + CIrcc Csxt}tjdd}|ddt|D}|jD]@}|j|||d&tt||t||dkshtW5QRXq2dS)NTrcSsg|] }t|qSr')r)r+numr'r'r-r.sz9TestCpuAPIs.test_cpu_times_comparison..)fieldbaseper_cpurl) r1rH_makerrrrrr3)r9rrZ summed_valuesrr'r'r-test_cpu_times_comparisons  z%TestCpuAPIs.test_cpu_times_comparisonc Csz>t|tst|dkst|dk s(t|dtks}tjdd}|D]}||||q(|t||||}qtttjddW5QRXdSr)r1cpu_times_percentrLrrr$rRrWrr'r'r-test_cpu_times_percent"s    z"TestCpuAPIs.test_cpu_times_percentcCsztjddd}t|tks"ttdD]J}tjddd}|D].}|D]}||||qH|t|||q@|}q*dS)NrTrr)r1rrtrr3rLrr)r9rrrcpurr'r'r-test_per_cpu_times_percent-s z&TestCpuAPIs.test_per_cpu_times_percentc CsjtjddddtjddD}tjd|d0tjddD]}|D]}||ddqFq>W5QRXdS)NTrcSs*g|]"}|ddtt|jDqS)cSsg|]}dqS)rr'r*r'r'r-r.<szNTestCpuAPIs.test_per_cpu_times_percent_negative...)rrLrtrr*r'r'r-r.;szCTestCpuAPIs.test_per_cpu_times_percent_negative..zpsutil.cpu_timesr)r1rrHr#rMr)r9Z zero_timesrrr'r'r-#test_per_cpu_times_percent_negative8s  z/TestCpuAPIs.test_per_cpu_times_percent_negativecCsTt}|jdkst|jD]2}t||}|dks6tts|dkr|dkstqdS)N) ctx_switches interruptsZsoft_interruptsZsyscallsr)rr)r1Z cpu_statsrr3rr)r9infosrrr'r'r-test_cpu_statsDs    zTestCpuAPIs.test_cpu_statsarm64zskipped due to #1892 not supportedcCs`dd}tjdd}tr&|s&td|s2t||tjddgtr\t|tks\tdS)NcSsl|D]b}|jdkst|jdkr0|j|jks0t|jD].}t||}t|tttfsXt|dks6tq6qdS)N)currentminrrr) rr3rrrrrrr)rCntrrr'r'r-check_lsZs   z+TestCpuAPIs.test_cpu_freq..check_lsTrzreturns empty list on FreeBSDF) r1Zcpu_freqrr$rr3rrtr)r9rrCr'r'r- test_cpu_freqUs   zTestCpuAPIs.test_cpu_freqcCs@t}t|dkst|D]}t|ts.t|dkstqdS)Nrnr)r1 getloadavgrtr3rr)r9Zloadavgloadr'r'r-test_getloadavgns zTestCpuAPIs.test_getloadavgN)r_r`rarrrrrrrr$rrrrrrrrrrrrrrmachinerrrr r'r'r'r-rss6   "        rc@seZdZejjeoe ddddZejje ddddZ dd Z d d Z ejje ofejd  d dejjeo~e ddddZddZdS) TestDiskAPIszunreliable on PYPY32 + 32BITrcc Cs(tt}|jdkst|jdks.t||jdks@t||jdksRt||j|jksft||j|jkszt|d|j krdksnt|j t t drt t}d}|j|jkstt |j|j|kstt st |j|j|kst|}ttt|W5QRXdS)N)rrrrrr disk_usageiP)r1r r/getcwdrr3rrrrr}shutilrrZ get_testfnr$rRr )r9usageZ shutil_usageZ tolerancefnamer'r'r-test_disk_usagexs$"  zTestDiskAPIs.test_disk_usageznot an ASCII fsc Cs$ttttW5QRXdSr<)r$rRUnicodeEncodeErrorr1r rr]r'r'r-test_disk_usage_unicodes z$TestDiskAPIs.test_disk_usage_unicodecCstddS)N.)r1r r]r'r'r-test_disk_usage_bytessz"TestDiskAPIs.test_disk_usage_bytesc Csrdd}tjdd}|st|D]b}||tr.check_ntupleF)allZcdromTcSs.tj|}tj|s&tj|}q |Sr<)r/rabspathismountdirnamer)rr'r'r-find_mount_points  z;TestDiskAPIs.test_disk_partitions..find_mount_pointcSsg|]}|jr|jqSr')rrr*r'r'r-r.sz5TestDiskAPIs.test_disk_partitions..)r1Zdisk_partitionsr3r rr r/rrrrrstatOSErrorrrerrnoZEIOEPERMEACCES__file__)r9rrCZdiskrr mountZmountsr'r'r-test_disk_partitionss>    z!TestDiskAPIs.test_disk_partitionsz/proc/diskstatsz3/proc/diskstats not available on this linux versionrcCsvdd}tjdd}|dk s$td||tjdd}t|tt|ksPt|D]}|sdt||||qTdS)NcSs|d|jkst|d|jks$t|d|jks6t|d|jksHttsts|d|jksbt|d|jksttt r|d|j kst|d|j kst|d |j kstnt r|d|j kst|jD]}t||dkst|qdS) NrrlrFrnr)Z read_countr3Z write_count read_bytes write_bytesrrZ read_timeZ write_timerZread_merged_countZwrite_merged_countZ busy_timerrr)rrr'r'r-rs z8TestDiskAPIs.test_disk_io_counters..check_ntupleFZperdiskzno disks on this system?T)r1disk_io_countersr3rtrBr9rretr@r'r'r-test_disk_io_counterss   z"TestDiskAPIs.test_disk_io_countersc CsPtjdid8}tjdddks$ttjddiks8t|jsBtW5QRXdS)Nz#psutil._psplatform.disk_io_countersrFr/T)r#rMr1r0r3rXr9rZr'r'r-test_disk_io_counters_no_diskssz+TestDiskAPIs.test_disk_io_counters_no_disksN)r_r`rar$rrrrrrrrr(rr/rrrr1r0r3r5r'r'r'r-r ws&  :  r c@seZdZejje ddddZejje ddddZejje dddd Z d d Z ejje ddd d Z ejje pepe ddddZdS) TestNetAPIsrrccCsddd}tjdd}||tjdd}|gks4t|D]&}|sDtt|tsRt|||q8dS)NcSs(|d|jkst|d|jks$t|d|jks6t|d|jksHt|d|jksZt|d|jkslt|d|jks~t|d|jkst|jdkst||jdkst||jdkst||jdkst||jdkst||jdkst||jdkst||jdks$t|dS) NrrlrFrnr)r*rr+) Z bytes_sentr3Z bytes_recvZ packets_sentZ packets_recvZerrinZerroutZdropinZdropoutrr'r'r-r s z6TestNetAPIs.test_net_io_counters..check_ntupleFZpernicT)r1net_io_countersr3rrr1r'r'r-test_net_io_counterss   z TestNetAPIs.test_net_io_countersc CsPtjdid8}tjdddks$ttjddiks8t|jsBtW5QRXdS)Nz"psutil._psplatform.net_io_countersrFr7T)r#rMr1r8r3rXr4r'r'r-test_net_io_counters_no_nics$sz(TestNetAPIs.test_net_io_counters_no_nicszQEMU user not supportedc Cszt}|st|t}ttjtjtjg}| D]\}}t |t sPtt t|t |ksht|D]}t |j tstt |jt stt |jt tdfstt |jt tdfst|j |ksttrtst |j tjst||jr|j tjkr8t|j }t|||jdfW5QRXnj|j tjkrt|jdtjtjdtjd}|\} } } } } t| | | }t||| W5QRX|j|j|j|jfD]*}|dk r|j tjkrt||j q|jr|jdkstql|jrl|jdksltqlq8t s&t!s&t"rFt#tdrvtjtjksvtn0t$r`tjtj%ksvtnt&rvtjdksvtdS)NrAF_LINKrj)'r1 net_if_addrsr3 net_if_statsrBsocketAF_INETAF_INET6r;itemsrrrtfamilyraddressnetmaskr broadcastr rr"IntEnumisup contextlibclosingbind getaddrinfo SOCK_STREAM AI_PASSIVEZptpr!rrr r}r AF_PACKETr )r9nicsZ nic_statsZfamiliesZnicaddrsaddrsrUafsocktypeprotoZ _canonnamesaipr'r'r-test_net_if_addrs/sl        zTestNetAPIs.test_net_if_addrsc Csztrdtjddddfg}ndg}tjd|dB}tdd}|jsJttr^|jdksltn|jdksltW5QRXdS) Nem1z06:3d:29)rYrjz06-3d-29NNNzpsutil._psplatform.net_if_addrsrrz06:3d:29:00:00:00z06-3d-29-00-00-00) r r1r;r#rMr<rXr3rC)r9r2rZrQr'r'r- test_net_if_addrs_mac_null_bytesus z,TestNetAPIs.test_net_if_addrs_mac_null_bytesc Cst}|st|tjtjtjf}|D]p\}}t|tsBt|\}}}}} t|t s^t||ksjt||ksvt|dkst|dkstt| ts,tq,dSr) r1r=r3ZNIC_DUPLEX_FULLZNIC_DUPLEX_HALFZNIC_DUPLEX_UNKNOWNrArrr) r9rOZ all_duplexesrstatsrGZduplexspeedZmtuflagsr'r'r-test_net_if_statss     zTestNetAPIs.test_net_if_statszLINUX or BSD or MACOS specificc CsDtjdttjdd$}t}|iks,t|js6tW5QRXdS)Nzpsutil._psutil_posix.net_if_mturTrG) r#rMr"r#ZENODEVr1r=r3rX)r9rZr2r'r'r-test_net_if_stats_enodevs  z$TestNetAPIs.test_net_if_stats_enodevN)r_r`rar$rrrr9r:rrXrZr^rrrr_r'r'r'r-r6s   E  r6c@seZdZejje ddddZejje ddddZejje ddejje dddd Z ejje ddd d Z d S) TestSensorsAPIsrrccCst}|D]x\}}t|ts&t|D]\}t|jts>t|jdk rV|jdksVt|jdk rn|jdksnt|j dk r*|j dks*tq*qdSr) r1sensors_temperaturesrArrr3labelrhighcritical)r9tempsrentriesentryr'r'r-test_sensors_temperaturess   z)TestSensorsAPIs.test_sensors_temperaturesc Cspddgi}tjd|dN}tjdddd}|js8t|jdksFt|jd ksTt|jd ksbtW5QRXdS) NZcoretemp)rbgI@gN@gQ@z'psutil._psplatform.sensors_temperaturesrT)Z fahrenheitrg^@ga@gc@) r#rMr1rarXr3rrcrd)r9drZrer'r'r-#test_sensors_temperatures_fahreneits  z3TestSensorsAPIs.test_sensors_temperatures_fahreneitz no batterycCspt}|jdkst|jdks$t|jtjtjfkrF|jdks\tn|jtjkr\|js\tt|jt sltdS)Nrr) r1Zsensors_batteryrr3ZsecsleftZPOWER_TIME_UNKNOWNZPOWER_TIME_UNLIMITEDZ power_pluggedrr)r9r2r'r'r-test_sensors_batterys  z$TestSensorsAPIs.test_sensors_batterycCsht}|D]R\}}t|ts&t|D]6}t|jts>tt|jtt fsRt|jdks*tq*qdSr) r1Z sensors_fansrArrr3rbrrr)r9Zfansrrfrgr'r'r-test_sensors_fanssz!TestSensorsAPIs.test_sensors_fansN)r_r`rar$rrrrhrjrrrkrrlr'r'r'r-r`s   r`);__doc__rHrr#r/rrrrr>rr|r1rrrrrrrr r r Zpsutil._compatr r rZ psutil.testsrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&rbrrrr r6r`r'r'r'r-sx                                    HfX,#