U 1g[d@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddlm*Z*ddlm+Z+ddlm,Z,ddlm-Z-erddl.m/Z/dd l.m0Z0dd!l.m1Z1dd"l.m2Z2ej34ej35e6Z7d#Z8d$Z9d%Z:d&Z;d'Zd*d+Z?d,d-Z@d.d/ZAd0d1ZBd2d3ZCd4d5ZDd6d7ZEd8d9ZFd:d;ZGejHdd?ZJe'jKjLe d@dAGdBdCdCe#ZMe'jKjLe d@dAGdDdEdEe#ZNe'jKjLe d@dAGdFdGdGe#ZOe'jKjLe d@dAGdHdIdIe#ZPe'jKjLe d@dAGdJdKdKe#ZQe'jKjLe d@dAGdLdMdMe#ZRe'jKjLe d@dAGdNdOdOe#ZSe'jKjLe d@dAGdPdQdQe#ZTe'jKjLe d@dAGdRdSdSe#ZUe'jKjLe d@dAGdTdUdUe#ZVe'jKjLe d@dAGdVdWdWe#ZWe'jKjLe d@dAe'jKjLe dXdAGdYdZdZe#ZXe'jKjLe d@dAGd[d\d\e#ZYe'jKjLe d@dAGd]d^d^e#ZZe'jKjLe d@dAGd_d`d`e#Z[e'jKjLe d@dAGdadbdbe#Z\e'jKjLe d@dAGdcdddde#Z]e'jKjLe d@dAGdedfdfe#Z^e'jKjLe d@dAe'jKjLe dgdAGdhdidie#Z_e'jKjLe d@dAGdjdkdke#Z`e'jKjLe d@dAGdldmdme#Zae'jKjLe d@dAGdndodoe#Zbe'jKjLe d@dAGdpdqdqe#Zce'jKjLe d@dAGdrdsdse#Zde'jKjLe d@dAGdtdudue#ZedS)vzLinux specific tests.)divisionN)LINUX)PY3)FileNotFoundError) basestring)AARCH64)GITHUB_ACTIONS)GLOBAL_TIMEOUT) HAS_BATTERY) HAS_CPU_FREQ)HAS_GETLOADAVG) HAS_RLIMIT)PYPY)PYTEST_PARALLEL) QEMU_USER)TOLERANCE_DISK_USAGE)TOLERANCE_SYS_MEM)PsutilTestCase) ThreadTask) call_until)mock)pytest) reload_module)retry_on_failure) safe_rmpath)sh)skip_on_not_implemented)which) CLOCK_TICKS)RootFsDeviceFinder)calculate_avail_vmem) open_binaryiii'iiiz/sys/class/hwmon/hwmon*c Cs|ddl}|dd}tr"t|d}ttjtj}t|6t| | t t d|ddW5QRSQRXdSNrascii256s)fcntlrbytessocketAF_INET SOCK_DGRAM contextlibclosing inet_ntoaioctlfileno SIOCGIFADDRstructpackifnamer(sr8G/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_linux.pyget_ipv4_addressNs   r:c Cs|ddl}|dd}tr"t|d}ttjtj}t|6t| | t t d|ddW5QRSQRXdSr")r(rr)r*r+r,r-r.r/r0r1SIOCGIFNETMASKr3r4r5r8r8r9get_ipv4_netmask]s     r<c Cs|ddl}|dd}tr"t|d}ttjtj}t|6t| | t t d|ddW5QRSQRXdSr")r(rr)r*r+r,r-r.r/r0r1SIOCGIFBRDADDRr3r4r5r8r8r9get_ipv4_broadcastls     r>c CstdJ}g}|D]"}|}|d|kr||qt|dkrNtd|W5QRXtt|D]j}||d}g}tdt|dD]}||||dqd|} ttj | } t tj | ||<qd|S)Nz/proc/net/if_inet6rzcould not find interface %r:) opensplitappendlen ValueErrorrangejoinr* inet_ptonAF_INET6 inet_ntop) r6fZ all_fieldslinefieldsiZ unformattedgroupsj formattedpackedr8r8r9get_ipv6_addresses{s"      rTc sddl}|dd}tr"t|d}ttjtj}t|l|| t t d|}trfddnddl }|jdfdd |d d Ddd W5QRSQRXdS) Nrr#r$r%cSs|SNr8xr8r8r9ordszget_mac_address..ordcsg|]}d|qS)z%02x:r8).0charrXr8r9 sz#get_mac_address..r'r?)r(rr)r*r+r,r-r.r0r1 SIOCGIFHWADDRr3r4 __builtin__rXrH)r6r(r7infor`r8r\r9get_mac_addresss      rbcCstddgddid}|d}|D]F}|dr"|\}}}}tdd}|t|t|t|Sq"td d|d S) zQParse 'free' cmd and return swap memory's s total, used and free values. free-bLANGC.UTF-8env ZSwapztotal used freez&can't find 'Swap' in 'free' output: %sN)rrC startswith collections namedtupleintrFrH)outlinesrM_totalusedrcntr8r8r9 free_swaps    rtcCstddgddid}|d}|D]P}|dr"dd |d d D\}}}}tdd }||||||Sq"td d|dS)zSParse 'free' cmd and return physical memory's total, used and free values. rcrdrerfrgriZMemcss|]}t|VqdSrU)rmrZrWr8r8r9 szfree_physmem..ztotal used free shared outputz%can't find 'Mem' in 'free' output: %sN)rrCrjrkrlrFrH)rnrorMrqrrrcsharedrsr8r8r9 free_physmems  " rzcCsZtddgddid}|dD]*}|}||krt|ddSqtd |dS) Nvmstatz-srerfrgri rz can't find %r in 'vmstat' output)rrCstriprmrF)statrnrMr8r8r9r{s r{cCs@tddg}d|kr"tdttttd| dS)Nrcz-VUNKNOWNzcan't determine free versionz\d+r?) rr}rskiptuplemaprmrefindallrC)rnr8r8r9get_free_version_infos rc #sDfdd}ttrdnd}tj|d|d }|VW5QRXdS)zMock open() builtin and forces it to return a certain content for a given path. `pairs` is a {"path": "content", ...} dict. csT|kr@|}tr4t|tr(t|St|SqPt|Sn|f||SdSrU)r isinstancerioStringIOBytesIO)nameargskwargscontent orig_openpairsr8r9 open_mocks    z$mock_open_content..open_mock builtins.open__builtin__.openTcreate side_effectNrBrrpatch)rr patch_pointmr8rr9mock_open_contents   rc #sFfdd}ttrdnd}tj|d|d }|VW5QRXdS)zZMock open() builtin and raises `exc` if the path being opened matches `for_path`. cs"|krn|f||SdSrUr8rrrexcfor_pathrr8r9rsz&mock_open_exception..open_mockrrTrNr)rrrrrr8rr9mock_open_exceptions  rz LINUX onlyreasonc@sLeZdZddZeddZeddZeddZed d Zd S) "TestSystemVirtualMemoryAgainstFreecCs"tj}tj}||kstdSrU)rzrqpsutilvirtual_memoryAssertionErrorselfZ cli_value psutil_valuer8r8r9 test_totals z-TestSystemVirtualMemoryAgainstFree.test_totalcCsRtdkrtdtdkr(tdtj}tj}t||tksNt dS)Nr free version too oldr@rrfree version too recent) rrrrzrrrrabsrrrr8r8r9 test_useds     z,TestSystemVirtualMemoryAgainstFree.test_usedcCs*tj}tj}t||tks&tdSrU)rzrcrrrrrrr8r8r9 test_free's z,TestSystemVirtualMemoryAgainstFree.test_freecCsPt}|j}|dkrtdtj}t||tksLtd|||j fdS)Nrz%free does not support 'shared' column %s %s %s) rzryrrrrrrroutput)rrc free_valuerr8r8r9 test_shared-s  z.TestSystemVirtualMemoryAgainstFree.test_sharedcCsrtddg}|d}d|dkr.tdn@t|dd}tj}t||t ksnt d |||fdS) Nrcrdri availablerz(free does not support 'available' columnrwr?r) rrCrrrmrrrrrr)rrnrorrr8r8r9test_available8s      z1TestSystemVirtualMemoryAgainstFree.test_availableN) __name__ __module__ __qualname__rrrrrrr8r8r8r9rs   rc@sZeZdZddZeddZeddZeddZed d Zed d Z d S)$TestSystemVirtualMemoryAgainstVmstatcCs.tdd}tj}t||tks*tdS)Nz total memory)r{rrrqrrrrZ vmstat_valuerr8r8r9rJs  z/TestSystemVirtualMemoryAgainstVmstat.test_totalcCsVtdkrtdtdkr(tdtdd}tj}t||tksRt dS)Nrrrrz used memoryr) rrrr{rrrrrrrrr8r8r9rOs      z.TestSystemVirtualMemoryAgainstVmstat.test_usedcCs.tdd}tj}t||tks*tdS)Nz free memoryr)r{rrrcrrrrr8r8r9ras  z.TestSystemVirtualMemoryAgainstVmstat.test_freecCs.tdd}tj}t||tks*tdS)Nz buffer memoryr)r{rrbuffersrrrrr8r8r9 test_buffersgs  z1TestSystemVirtualMemoryAgainstVmstat.test_bufferscCs.tdd}tj}t||tks*tdS)Nz active memoryr)r{rractiverrrrr8r8r9 test_activems  z0TestSystemVirtualMemoryAgainstVmstat.test_activecCs.tdd}tj}t||tks*tdS)Nzinactive memoryr)r{rrinactiverrrrr8r8r9 test_inactivess  z2TestSystemVirtualMemoryAgainstVmstat.test_inactiveN) rrrrrrrrrrr8r8r8r9rHs    rc@sBeZdZddZeddZddZddZd d Zd d Z d S)TestSystemVirtualMemoryMocksc Csdtd}td|i>}tjdd$}tdt}|j sJt t |dksZt |d}dt |j kstt d t |j kst d t |j kst d t |j kst d t |j kst d t |j kst dt |j kst |jdkst |jdkst |jdks t |jdkst |jdks,t |jdkst t |dksNt |d}dt |j ksht |jdksvt |jdkst W5QRXW5QRXdS) Nz /proc/vmstatrTrrrwrzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0)rrrrrrrrrrrrErrrrrr8r8r9test_no_vmstat_mockedus"   z*TestSystemSwapMemory.test_no_vmstat_mockedc Cs|stdtd}t}W5QRX|jr:tddl m }| \}}}}}}}||9}||9}|j |ksztt |j|tkstdS)Nz!/proc/meminfo has no swap metricsz"psutil._pslinux.cext.linux_sysinfor)rrrrrrrrrZpsutil._psutil_linuxZ _psutil_linuxZ linux_sysinforqrrcr)rrswapZcextrprqrcZunit_multiplierr8r8r9test_meminfo_against_sysinfos    z1TestSystemSwapMemory.test_meminfo_against_sysinfoc Cs.tddi}t|js tW5QRXdS)Nr)rrrrrrrr8r8r9#test_emulate_meminfo_has_no_metricssz8TestSystemSwapMemory.test_emulate_meminfo_has_no_metricsN) rrr staticmethodrrrrrrrrrr8r8r8r9rLs   rc@seZdZddZdS)TestSystemCPUTimescCstj}tdtdd}ttt | d}|dkrLd|ksXt n d|ksXt |dkrnd|kszt n d|kszt |d krd |kst n d |kst dS) Nz \d+\.\d+\.\d+r.)r steal)rrr'Zguest)rrrZ guest_nice) r cpu_times_fieldsrrosunamerrrmrCr)rrNZ kernel_verZkernel_ver_infor8r8r9 test_fieldss   zTestSystemCPUTimes.test_fieldsN)rrrr r8r8r8r9rsrc@seZdZejjejd ddddZ ejjejd dddd Z ejje d  d dd d Z ejje d ddddZ ddZdS)TestSystemCPUCountLogical/sys/devices/system/cpu/onlinez-/sys/devices/system/cpu/online does not existrc CsVtd}|}W5QRXdt|krRt|ddd}t|ksRtdS)Nr -rw) rBrr}rrmrCr cpu_countr)rrLvaluer8r8r9test_against_sysdev_cpu_onlines   z8TestSystemCPUCountLogical.test_against_sysdev_cpu_online/sys/devices/system/cpuz&/sys/devices/system/cpu does not existcCs0td}tdd|D}t|ks,tdS)NrcSs g|]}td|dk r|qS)zcpu\d+$N)rsearchrur8r8r9r]szITestSystemCPUCountLogical.test_against_sysdev_cpu_num..)rlistdirrErr r)rZlscountr8r8r9test_against_sysdev_cpu_nums z5TestSystemCPUCountLogical.test_against_sysdev_cpu_numZnprocznproc utility not availablecCs$ttd}tjdd|ks tdS)Nz nproc --allTZlogical)rmrrr r)rnumr8r8r9test_against_nprocs z,TestSystemCPUCountLogical.test_against_nproclscpulscpu utility not availablecCs8td}tdd|dD}tjdd|ks4tdS)Nlscpu -pcSsg|]}|ds|qS)#rjrur8r8r9r]s z@TestSystemCPUCountLogical.test_against_lscpu..riTr)rrErCrr r)rrnrr8r8r9test_against_lscpusz,TestSystemCPUCountLogical.test_against_lscpuc Cs,ddl}|j}tjdtd}|j|ks6t|js@ttjddd<}|jdksbt|jdkspt|j dddkstW5QRXt d d }| }W5QRXt |}tjd|dd }|j|kstW5QRXtd d i&}|j|kst|jstW5QRXW5QRXdS) Nrzpsutil._pslinux.os.sysconfrrTrr /proc/stat /proc/cpuinforb return_valuerr)psutil._pslinux_pslinuxZcpu_count_logicalrrrFrrZ call_countZ call_argsrBrrrr)rroriginalrrLZ cpuinfo_data fake_filer8r8r9test_emulate_fallbackss4     z0TestSystemCPUCountLogical.test_emulate_fallbacksN)rrrrmarkskipifrpathexistsrrrrrr(r8r8r8r9r s*      r c@s:eZdZejjed ddddZddZdd Z d S) TestSystemCPUCountCoresrrrcCs\td}t}|dD]&}|ds|d}||dqtjddt|ksXtdS)Nrrir,rwFr) rsetrCrjaddrr rEr)rrnZcore_idsrMrNr8r8r9rs  z*TestSystemCPUCountCores.test_against_lscpuc CsPtj}tjdgd}tj}|js.tW5QRX|dk rL||ksLtdS)N glob.globr#)rr%cpu_count_coresrrrr)rZmeth_1rZmeth_2r8r8r9 test_method_2s   z%TestSystemCPUCountCores.test_method_2c Cs^tjdgd2}tjddd}tjdks2tW5QRXW5QRX|jsPt|jsZtdS)Nr1r2rTr)rrrr%r3rr)rm1m2r8r8r9test_emulate_nones & z)TestSystemCPUCountCores.test_emulate_noneN) rrrrr)r*rrr4r7r8r8r8r9r-s r-c@seZdZejje ddddZejje ddejjeddddZ ejje dddd Z ejje ddd d Z ejje ddd d Z dS)TestSystemCPUFrequency not supportedrc s@fdd}tjjtjd|ddts2tW5QRXdS)Ncs|drdS|SdS)N&/sys/devices/system/cpu/cpufreq/policyFrr+Z orig_existsr8r9path_exists_mock(s zMTestSystemCPUFrequency.test_emulate_use_second_file..path_exists_mockos.path.existsT)rr)rr+r,rrrcpu_freqr)rr=r8r<r9test_emulate_use_second_file%s z3TestSystemCPUFrequency.test_emulate_use_second_filez,aarch64 does not report mhz in /proc/cpuinfoc sfdd}tjjztjd|dnttjt}|sDt ||j dksRt |j dks`t tjddD] }|j dks~t |j dkslt qlW5QRXW5ttjttXdS)Ncs|drdS|SdS)Nz/sys/devices/system/cpu/Frr;Zos_path_existsr8r9r=;s zITestSystemCPUFrequency.test_emulate_use_cpuinfo..path_exists_mockr>rTZpercpu) rr+r,rrr%rrr?rmaxmin)rr=rfreqr8rAr9test_emulate_use_cpuinfo4s    z/TestSystemCPUFrequency.test_emulate_use_cpuinfoc sfdd}ttrdnd}tj||dftjdddLt}|jd ksRt|jd krj|jd ksjt|j d kr|j d kstW5QRXW5QRXdS) Ncs|dr|drtdS|dr<|dr.open_mockrrrr>Tr2@@rB@g@ rBrrrrr?currentrrErDrrrrFr8rOr9test_emulate_dataPs    z(TestSystemCPUFrequency.test_emulate_datac sfdd}ttrdnd}tj||dtjdddtjd d dtjdd }|d jd ksjt|d jdkr|d jdkst|d j dkr|d j dkst|djdkst|djdkr|djdkst|dj dkr|dj dkstW5QRXW5QRXW5QRXdS)Ncs|}|dr"|dr"tdS|dr@|dr@tdS|dr^|dr^tdS|dr||dr|td S|dr|drtd S|dr|drtd S|d krtd S|f||SdS)NrHz'/sys/devices/system/cpu/cpufreq/policy0s100000rJ200000rLs300000z'/sys/devices/system/cpu/cpufreq/policy1s400000rIrKr s#cpu MHz : 100 cpu MHz : 400rM)rrrnrOr8r9rss8       z@TestSystemCPUFrequency.test_emulate_multi_cpu..open_mockrrrr>Tr2!psutil._pslinux.cpu_count_logicalrrCrY@rBgi@gr@rwgy@rPrQrRrTr8rOr9test_emulate_multi_cpuqs(   z-TestSystemCPUFrequency.test_emulate_multi_cpuc sfdd}ttrdnd}tj||dPtjddd6tjd d dt}|jd ksbtW5QRXW5QRXW5QRXdS) NcsR|drttjdn6|dr,tdS|dkr>tdS|f||SdS)NrHrYz/cpuinfo_cur_freqrVr scpu MHz : 200)rNrrrrrrrOr8r9rs    zOTestSystemCPUFrequency.test_emulate_no_scaling_cur_freq_file..open_mockrrrr>Tr2rXrw)rBrrrrr?rSrrTr8rOr9%test_emulate_no_scaling_cur_freq_files  zrJrCrT)rraddrsaddrrhr8r8r9test_ipss    zTestSystemNetIfAddrs.test_ipsN)rrrrmr8r8r8r9rdsrdQEMU user not supportedc@sPeZdZejjed ddddZddZejjed dddd Z d S) TestSystemNetIfStatsifconfigifconfig utility not availablerc CsttD]b\}}ztd|}Wntk r8Yq X|jd|kksPt||jtt d|dks tq dS)N ifconfig %sZRUNNINGz(?i)MTU[: ](\d+)r) r net_if_statsrfr RuntimeErrorZisuprmturmrr)rrstatsrnr8r8r9test_against_ifconfig sz*TestSystemNetIfStats.test_against_ifconfigc CsLtD]:\}}td| }|jt|ksrrwr.z(.*) MTU:(\d+) Metric:(\d+)rzno matches were found)rrsrfrrtrrrErPr/grouplowerrCflagsrfail)rZ matches_foundrrvrnmatchZifconfig_flagsZ psutil_flagsr8r8r9 test_flagss(  zTestSystemNetIfStats.test_flagsN) rrrrr)r*rrwrxr~r8r8r8r9ros roc@s0eZdZejjed ddeddZdS)TestSystemNetIOCountersrprqrc Cs&dd}tjddd}|D]\}}z ||}Wntk rLYqYnXt|j|ddkshtt|j|ddkstt|j|d d kstt|j |d d kstt|j |d d kstt|j |dd kstt|j |dd kstt|j |dd kstqdS)NcSsi}td|}ttd|d|d<ttd|d|d<ttd|d|d<ttd|d |d <ttd |d|d <ttd |d |d <ttd|d|d<ttd|d|d<|S)NrrzRX packets[: ](\d+)r packets_recvzTX packets[: ](\d+) packets_sentzerrors[: ](\d+)errinrwerroutzdropped[: ](\d+)dropindropoutz#RX (?:packets \d+ +)?bytes[: ](\d+) bytes_recvz#TX (?:packets \d+ +)?bytes[: ](\d+) bytes_sent)rrmrr)Znicrrnr8r8r9rpEs& z?TestSystemNetIOCounters.test_against_ifconfig..ifconfigTF)Zpernicnowrapri(rrrrr rrr)rnet_io_countersrfrtrrrrrrrrrr)rrpZniorrvZ ifconfig_retr8r8r9rw@s&  z-TestSystemNetIOCounters.test_against_ifconfigN) rrrrr)r*rrrwr8r8r8r9r>s rc@s8eZdZejdedejdddddZdd Zd S) TestSystemNetConnectionsz psutil._pslinux.socket.inet_ntoprzpsutil._pslinux.supports_ipv6Fr2cCsRz*ttjtj}||j|dWntjk r@YnXtjdddS)N)z::1rZinet6kind) r*rJ SOCK_STREAMZ addCleanupclosebinderrorrnet_connections)rZ supports_ipv6rKr7r8r8r9test_emulate_ipv6_unsupportedts z6TestSystemNetConnections.test_emulate_ipv6_unsupportedc Cs<td}td|i}tjdd|js.tW5QRXdS)Na 0: 00000003 000 000 0001 03 462170 @/tmp/dbus-Qw2hMPIU3n 0: 00000003 000 000 0001 03 35010 @/tmp/dbus-tB2X8h69BQ 0: 00000003 000 000 0001 03 34424 @/tmp/dbus-cHy80Y8O 000000000000000000000000000000000000000000000000000000 z/proc/net/unixunixr)rrrrrrr)rrrr8r8r9test_emulate_unixs  z*TestSystemNetConnections.test_emulate_unixN)rrrrrrFrrr8r8r8r9rrs   rc@sBeZdZejjeed ddeddZ ddZ dd Z d S) TestSystemDiskPartitionsstatvfszos.statvfs() not availablercCsvdd}tjddD]\}t|j}||j\}}}}|j|ksDtt|j|tksZtt|j |tkstqdS)NcSsztd|}|d}|d|d}|dd\}}}}|dkrRd}t|t|t|}}}||||fS)Nzdf -P -B 1 "%s"rirr@nonerY)rr}rCpoprm)r+rnrorMdevrqrrrcr8r8r9dfs   z4TestSystemDiskPartitions.test_against_df..dfF)all) rdisk_partitions disk_usageZ mountpointrqrrrcrrr)rrpartusagerprqrrrcr8r8r9test_against_dfs  z(TestSystemDiskPartitions.test_against_dfc Cstd}|}W5QRXd|krLtD]}|jdkr,qq,|dnxtd}tj d|ddX}tj dd gd <}t}|j st |j st |st |d jdkst W5QRXW5QRXdS) Nz/proc/filesystemszfszcouldn't find any ZFS partitionz nodev zfs rTr"$psutil._pslinux.cext.disk_partitions)z /dev/sdb3/rrwr2r) rBrrrZfstyper|rrrrrr)rrLrrr'r5r6rr8r8r9 test_zfs_fss0       z$TestSystemDiskPartitions.test_zfs_fsc CsTzFtjddd.}tttW5QRX|js:t W5QRXW5dt_XdS)N/proczos.path.realpathz /non/existentr2) r PROCFS_PATHrrrraisesrrrrrr8r8r9test_emulate_realpath_fails z3TestSystemDiskPartitions.test_emulate_realpath_failN) rrrrr)r*hasattrrrrrrr8r8r8r9rs  rc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)TestSystemDiskIoCountersc Csd}td|itjdddtjdd}|jdks.is_storage_deviceTrrwrx) rrrrrrrrrr)rrrrr8r8r9test_emulate_exclude_partitions%s&   z8TestSystemDiskIoCounters.test_emulate_exclude_partitionsc CsTdd}tjdd}tjdd|dtjdd}W5QRXt|t|ksPtdS)NcSs|dkS)Nrr8r;r8r8r9r,Fsz?TestSystemDiskIoCounters.test_emulate_use_sysfs..existsT)rpsutil._pslinux.os.path.existsr)rrrrrEr)rr,ZwprocfsZwsysfsr8r8r9test_emulate_use_sysfsEs z/TestSystemDiskIoCounters.test_emulate_use_sysfsc CsFdd}tjdd|d$tttW5QRXW5QRXdS)NcSsdS)NFr8r;r8r8r9r,Qsz>TestSystemDiskIoCounters.test_emulate_not_impl..existsrTr)rrrrNotImplementedErrorrr)rr,r8r8r9test_emulate_not_implPs z.TestSystemDiskIoCounters.test_emulate_not_implN) rrrrrrrrrrr8r8r8r9rs  rc@sjeZdZddZddZejjeddddZ ejje d  d dejjeddd d Z d dZ dS)TestRootFsDeviceFindercCs(tdj}t||_t||_dS)Nr)rr~st_devmajorminor)rrr8r8r9setUp]s  zTestRootFsDeviceFinder.setUpc Cst}tjdr|ntt|W5QRXtjd|j|j fr\| ntt| W5QRX| dSNz/proc/partitionsz/sys/dev/block/%s:%s/uevent) rrr+r,ask_proc_partitionsrrrrrask_sys_dev_blockask_sys_class_block)rfinderr8r8r9test_call_methodsbs     z(TestRootFsDeviceFinder.test_call_methodszunsupported on GITHUB_ACTIONSrcCst}|dk std}}}tjdr6|}tjd|j|jfrV| }| }|ph|ph|}|r~|r~||ks~t|r|r||kst|r|r||kstdSr) rfindrrr+r,rrrrr)rrrrcbaser8r8r9test_comparisonsrs"     z'TestRootFsDeviceFinder.test_comparisonsZfindmntzfindmnt utility not availablecCs"t}td}||kstdS)Nzfindmnt -o SOURCE -rn /)rrrr)rrZ findmnt_valuer8r8r9test_against_findmnts z+TestRootFsDeviceFinder.test_against_findmntc CsltjddgdR}td}|js(ttsP|jdks:t|jt ks^tn|jdks^tW5QRXdS)Nr) /dev/rootrZext4rr2rr) rrrrrrrZdevicerr)rrrr8r8r9test_disk_partitions_mockeds  z2TestRootFsDeviceFinder.test_disk_partitions_mockedN) rrrrrrr)r*rrrrrr8r8r8r9r[s  rc@sbeZdZddZddZddZddZd d Zd d Ze e j j e d dddZddZdS)TestMisccCs(td}t}t|t|ks$tdS)Nz boot time)r{r boot_timermrrr8r8r9test_boot_timeszTestMisc.test_boot_timec s|}t|ttj|dd$}|d|d|dW5QRXztfdd}t rldnd }t j ||d t t ttt W5QRXttt jd d W5QRXttt W5QRXttt jd d W5QRXttt W5QRXttt jd d W5QRX|t _t d ksbttt d ksxtt jd d }t|d kstt jd d }ttt|d kstttj|dd$}|d|d|dW5QRXt d ks ttt jd d d ks$ttt d ks:ttttt jd d d ksZtW5QRXW5t|t t Xt jdkstdS)Nr~rzcpu 0 0 0 0 0 0 0 0 0 0 zcpu0 0 0 0 0 0 0 0 0 0 0 zcpu1 0 0 0 0 0 0 0 0 0 0 cs&|drttjd|f||S)Nrzrejecting access for test)rjrrrrrOr8r9rs  z4TestMisc.test_no_procfs_on_import..open_mockrrrTrCrzcpu 1 0 0 0 0 0 0 0 0 0 zcpu0 1 0 0 0 0 0 0 0 0 0 zcpu1 1 0 0 0 0 0 0 0 0 0 r) get_testfnrmkdirrBr+rHwriteshutilrmtreerrrrrrrrr cpu_percentcpu_times_percentrrsumr)rZ my_procfsrLrrZper_cpu_percentZper_cpu_times_percentr8rOr9test_no_procfs_on_importsV                 z!TestMisc.test_no_procfs_on_importc Cs>td}td|i8}t|js.ttjddttjddW5QRXtd}td|it}|jsttjdd}t}tjdd}|dkstt |dkstt |dkstt |dkstt t t |dkstt t t |dkst|j dks t|j dks0tW5QRXdS)Nz~ cpu 0 0 0 0 0 0 0 1 0 0 cpu0 0 0 0 0 0 0 0 1 0 0 cpu1 0 0 0 0 0 0 0 1 0 0 rTrCz~ cpu 1 0 0 0 0 0 0 0 0 0 cpu0 1 0 0 0 0 0 0 0 0 0 cpu1 1 0 0 0 0 0 0 0 0 0 rrY) rrrrrrrrrrrruser)rrrrZcpu_percent_percpurZcpu_times_percent_percpur8r8r9test_cpu_steal_decreases,      z TestMisc.test_cpu_steal_decreasec CsHtjddd0}tttjW5QRX|js:t W5QRXdS)NrTr) rrrrrtrr%rrrrr8r8r9test_boot_time_mockeds zTestMisc.test_boot_time_mockedcCs tD]}|jdkstqdS)N)z:0z:0.0)rZusershostr)rrr8r8r9 test_userss zTestMisc.test_usersc Cs@|}t|z|t_tttW5QRXttt W5QRXtttj ddW5QRXttt W5QRXttt W5QRXttt W5QRXttt W5QRXtttW5QRXttjtW5QRXW5dt_XdS)NrTrC)rrrrrrrrrrrrrrsrZ NoSuchProcessProcess)rZtdirr8r8r9test_procfs_path s.         zTestMisc.test_procfs_pathzskip if pytest-parallelrc Csttt}|}t|tr&dndks0tt|ddddj}|j |ksTtt|}| |t ksvtW5QRXdS)NrrcSs|jSrU)idrVr8r8r9Hrz)TestMisc.test_issue_687..)keyrw) rrrthreadsrErrsortedrpidas_dictZpids)rprtidptr8r8r9test_issue_687<s zTestMisc.test_issue_687c Cs8tddi"}tts t|js*tW5QRXdS)N/proc/%s/statusrY)rrZ pid_existsrgetpidrrrr8r8r9test_pid_exists_no_proc_statusNsz'TestMisc.test_pid_exists_no_proc_statusN)rrrrrrrrrrrr)r*rrrr8r8r8r9rsB' rz no batteryc@sjeZdZejjed ddddZddZdd Z d d Z d d Z ddZ ddZ ddZddZdS)TestSensorsBatteryZacpizacpi utility not availablercCsHtd}t|dddd}tj}t||dksDt dS)Nzacpi -br.rwrerY) rrmrCr}replacersensors_batterypercentrr)rrnZ acpi_valuerr8r8r9 test_percent_s zTestSensorsBattery.test_percentc sjfdd}ttrdnd}tj||d6}tjdks>ttjtj ksRt|j s\tW5QRXdS)Ncs(|drtdS|f||SdS)Nz AC0/onlinez AC/online1rNrrrrOr8r9rhs  z@TestSensorsBattery.test_emulate_power_plugged..open_mockrrrT) rBrrrrr power_pluggedrZsecsleftZPOWER_TIME_UNLIMITEDrrrrrr8rOr9test_emulate_power_pluggedfs  z-TestSensorsBattery.test_emulate_power_pluggedc sVfdd}ttrdnd}tj||d"}tjdks>t|jsHtW5QRXdS)Ncs@|drttjdn$|dr,tdS|f||SdS)NrrY/statusZchargingrNrrrrrrrOr8r9r{s    zBTestSensorsBattery.test_emulate_power_plugged_2..open_mockrrrT rBrrrrrrrrr r8rOr9test_emulate_power_plugged_2xs   z/TestSensorsBattery.test_emulate_power_plugged_2c sVfdd}ttrdnd}tj||d"}tjdks>t|jsHtW5QRXdS)Ncs(|drtdS|f||SdS)Nr0rrrOr8r9rs  zDTestSensorsBattery.test_emulate_power_not_plugged..open_mockrrrFr r r8rOr9test_emulate_power_not_pluggeds   z1TestSensorsBattery.test_emulate_power_not_pluggedc sVfdd}ttrdnd}tj||d"}tjdks>t|jsHtW5QRXdS)Ncs@|drttjdn$|dr,tdS|f||SdS)NrrYr Z dischargingr rrOr8r9rs    zFTestSensorsBattery.test_emulate_power_not_plugged_2..open_mockrrrFr r r8rOr9 test_emulate_power_not_plugged_2s   z3TestSensorsBattery.test_emulate_power_not_plugged_2c sVfdd}ttrdnd}tj||d"}tjdks>t|jsHtW5QRXdS)Ncs@|drttjdn$|dr,tdS|f||SdS)N)"/sys/class/power_supply/AC0/online!/sys/class/power_supply/AC/onlinerY#/sys/class/power_supply/BAT0/statuss???)rjrrrrrrrOr8r9rs    zETestSensorsBattery.test_emulate_power_undetermined..open_mockrrrr r r8rOr9test_emulate_power_undetermineds   z2TestSensorsBattery.test_emulate_power_undeterminedc Cs8tddi"}tjdks t|js*tW5QRXdS)N(/sys/class/power_supply/BAT0/energy_fullrr)rrrrrrrr8r8r9test_emulate_energy_full_0s z-TestSensorsBattery.test_emulate_energy_full_0c CsjtdttjdNtdttjd0tdditjdksHtW5QRXW5QRXW5QRXdS)NrrYz(/sys/class/power_supply/BAT0/charge_fullz%/sys/class/power_supply/BAT0/capacitys88X) rrrrrrrrrrr8r8r9"test_emulate_energy_full_not_avails  z5TestSensorsBattery.test_emulate_energy_full_not_availc CsptdttjdTtdttjd6tdttjdtjdksNtW5QRXW5QRXW5QRXdS)NrrYrr)rrrrrrrrrr8r8r9test_emulate_no_powers   z(TestSensorsBattery.test_emulate_no_powerN)rrrrr)r*rrr rrrrrrrr8r8r8r9r\s rc@seZdZddZdS)TestSensorsBatteryEmulatedc szfdd}ttrdnd}tjddgd0}tj||d}tdk sNtW5QRXW5QRX|jslt|jsvtdS) NcsP|drtdS|dr(tdS|dr.open_mockrrz os.listdirZBAT0r2r)rBrrrrrrr)rrrZmlistdirZmopenr8rOr9test_its  $ z"TestSensorsBatteryEmulated.test_itN)rrrrr8r8r8r9rsrc@seZdZddZddZdS)TestSensorsTemperaturesc sfdd}ttrdnd}tj||djtjddgdNtd d }|jd ks\t|jd ksjt|j d ksxt|j dkstW5QRXW5QRXdS)Ncsx|drtdS|dr(tdS|dr.open_mockrrrr1z/sys/class/hwmon/hwmon0/temp1r2rrr">@gD@I@ rBrrrrZsensors_temperaturesr"rrShighcritical)rrrtempr8rOr9test_emulate_class_hwmons  z0TestSensorsTemperatures.test_emulate_class_hwmonc sfdd}dd}ttr dnd}tj||djtjdd |d Ntd d }|jd ksdt|jdksrt|j dkst|j dkstW5QRXW5QRXdS)Ncsd|drtdS|dr(tdS|dr.open_mockcSs:|dkr gS|dkrgS|dkr&dgS|dkr6ddgSgS)Nz/sys/class/hwmon/hwmon*/temp*_*z&/sys/class/hwmon/hwmon*/device/temp*_*z /sys/class/thermal/thermal_zone*z /sys/class/thermal/thermal_zone0z,/sys/class/thermal/thermal_zone0/trip_point*z2/sys/class/thermal/thermal_zone1/trip_point_0_typez2/sys/class/thermal/thermal_zone1/trip_point_0_tempr8r;r8r8r9 glob_mock$szETestSensorsTemperatures.test_emulate_class_thermal..glob_mockrrrr1TrrrrYr%r&r')rrr-rr*r8rOr9test_emulate_class_thermals  z2TestSensorsTemperatures.test_emulate_class_thermalN)rrrr+r.r8r8r8r9r sr c@seZdZddZdS)TestSensorsFansc sfdd}ttrdnd}tj||dNtjddgd2td d }|jd ks\t|jd ksjtW5QRXW5QRXdS) NcsP|drtdS|dr(tdS|dr.open_mockrrrr1z/sys/class/hwmon/hwmon2/fan1r2rrr"i) rBrrrrZ sensors_fansr"rrS)rrrfanr8rOr9rU?s  z!TestSensorsFans.test_emulate_dataN)rrrrUr8r8r8r9r/=sr/c@seZdZeddZddZejje ddddZ d d Z d d Z d dZ ddZddZddZddZddZddZddZddZejje ddd d!Zd"d#Zd$d%Zd&d'Zd(S)) TestProcesscCs|}tj|j\}}}t|jjdd}t|tdd|DdksVt t|tdd|Ddksxt t|tdd|Ddkst dS)NF)groupedcSsg|]}|j|jqSr8)Z private_dirtyZ private_cleanrur8r8r9r]bsz?TestProcess.test_parse_smaps_vs_memory_maps..rcSsg|] }|jqSr8)pssrur8r8r9r]escSsg|] }|jqSr8)rrur8r8r9r]fs) Zspawn_testprocrr%rr _parse_smaps memory_mapsrrr)rZsprocussr4rmapsr8r8r9test_parse_smaps_vs_memory_maps\s"z+TestProcess.test_parse_smaps_vs_memory_mapsc Cs~td}tdt|iR}tjt}| \}}}|j sLt |dksXt |dksdt |dkspt W5QRXdS)Nan fffff0 r-xp 00000000 00:00 0 [vsyscall] Size: 1 kB Rss: 2 kB Pss: 3 kB Shared_Clean: 4 kB Shared_Dirty: 5 kB Private_Clean: 6 kB Private_Dirty: 7 kB Referenced: 8 kB Anonymous: 9 kB LazyFree: 10 kB AnonHugePages: 11 kB ShmemPmdMapped: 12 kB Shared_Hugetlb: 13 kB Private_Hugetlb: 14 kB Swap: 15 kB SwapPss: 16 kB KernelPageSize: 17 kB MMUPageSize: 18 kB Locked: 19 kB VmFlags: rd ex /proc/%s/smapsilri<) rrrrrrrr%rr5rr)rrrrr7r4rr8r8r9test_parse_smaps_mockedhs   z#TestProcess.test_parse_smaps_mockedzunreliable on PYPYrc Csldd}|}t|d||jdks.tW5QRXt|||jdksTtW5QRXt|d||jdks|tW5QRXt|d||jdkstW5QRXt|d||jdkstW5QRXt|d||jdkstW5QRXtrht|t|d ||jdks,tW5QRXt|t|d ||jdks^tW5QRXdS) NcSsZt}tt}|D].}|jtj|kr:|St|krqqqtddS)Nztimeout looking for test file) rrtimer open_filesr+rabspathrt)fnamerZ giveup_atfiler8r8r9 get_test_files   z7TestProcess.test_open_files_mode..get_test_filerrrzr+zw+za+rWzx+)rrBmoderrr)rrAZtestfnr8r8r9test_open_files_modes*         z TestProcess.test_open_files_modec stt|dtfddtjdtt j dd }gksZt |j sdt W5QRXtjdtt j dd }gkst |j st W5QRXW5QRXdS)NrcsttkSrUrEr=r8filesrr8r9rrz7TestProcess.test_open_files_file_gone..psutil._pslinux.os.readlinkrYr)rrr=rBrrrrOSErrorrrrrEINVALrr8rFr9test_open_files_file_gones$  z%TestProcess.test_open_files_file_gonec stt|d`tfddtr:dnd}tj|t t j dd }gksft |j spt W5QRXW5QRXdS)NrcsttkSrUrEr8rFr8r9rrz5TestProcess.test_open_files_fd_gone..rrrYr)rrr=rBrrrrrrrrrrrrrr8rFr9test_open_files_fd_gones  z#TestProcess.test_open_files_fd_gonec stt|dntfddd}tj|tt j dd6}td gksjt |j stt W5QRXW5QRXW5QRXdS)NrcsttkSrUrEr8rFr8r9rrz:TestProcess.test_open_files_enametoolong..rHrYrpsutil._pslinux.debug) rrr=rBrrrrrIr ENAMETOOLONGrrrLr8rFr9test_open_files_enametoolongs  z(TestProcess.test_open_files_enametoolongc CsDtjdid,}tjtdks,t|j s6tW5QRXdS)Nz)psutil._pslinux._psposix.get_terminal_mapr2) rrrr%rrrZterminalrrrr8r8r9test_terminal_mockedsz TestProcess.test_terminal_mockedc Cst}td}tjd|dd$}|ddgks8t|jsBtW5QRXtd}tjd|dd&}|dddgks~t|jstW5QRXdS) NzfoobarrTr"foobarz foobarrY rrrrrrZcmdlinerrrrr'rr8r8r9test_cmdline_mockeds&  zTestProcess.test_cmdline_mockedc Cst}td}tjd|dd$}|ddgks8t|jsBtW5QRXtd}tjd|dd&}|dddgks~t|jstW5QRXdS) Nzfoo bar rTr"rRrSz foo bar rYrTrUr8r8r9test_cmdline_spaces_mockeds&  z&TestProcess.test_cmdline_spaces_mockedc CsPt}td}tjd|dd$}|ddgks8t|jsBtW5QRXdS)Nzfoo barrTr"rRrSrTrUr8r8r9test_cmdline_mixed_separatorss z)TestProcess.test_cmdline_mixed_separatorsc CsFtjddd.tdks$ttdks8tW5QRXdS)NrHz/home/foo (deleted)r2z /home/foo)rrrrexercwdrr8r8r9!test_readlink_path_deleted_mocked!s z-TestProcess.test_readlink_path_deleted_mockedc sfdd}ttrdnd}tj||d(}t}|jsBt|gksNtW5QRXfdd}tj||d*t tj tW5QRXW5QRXdS)Ncs4|dtr ttjdn|f||SdSNz /proc/%s/taskrY)rjrrrrrrrOr8r9 open_mock_1-sz4TestProcess.test_threads_mocked..open_mock_1rrrcs4|dtr ttjdn|f||SdSr\)rjrrrrEPERMrrOr8r9 open_mock_2<sz4TestProcess.test_threads_mocked..open_mock_2) rBrrrrrrrrrrZ AccessDenied)rr]rrrr_r8rOr9test_threads_mocked(s     zTestProcess.test_threads_mockedc CsbtjdttjddB}tjdgd(t}|js>t |dksJt W5QRXW5QRXdS)Nzpsutil._pslinux.readlinkrYrzpsutil._pslinux.Process.cmdliner2) rrrIrrrrrYrr)rrrr8r8r9test_exe_mockedFs   zTestProcess.test_exe_mockedc CsZtdtttjd6}t}t t | W5QRX|j sLt W5QRXdS)Nr:rY)rrrrrrrrrrrr6rrrrrr8r8r9test_issue_1014Rs  zTestProcess.test_issue_1014r9c CstjdttjddT}tjddd:}t}|t tj }| tj W5QRXW5QRXW5QRX|j szt|j st|jj|jkst|jj|kstdS)Nzpsutil._pslinux.prlimitrYrz"psutil._pslinux.Process._is_zombieTr2)rrrIrZENOSYSrrrrrZ ZombieProcessZrlimitZ RLIMIT_NOFILErrrr)rr5r6rcmr8r8r9test_rlimit_zombie]s$ *  zTestProcess.test_rlimit_zombiec*CsZdddddddddddddddddddddd ddddddddddddddddd ddd g*}d |}td t|it}|d kst| tj kst| dkst| dt tkst|}|jdt kst|jdt kst|jdt kst|jdt ks&t|jdt ks:t|dksLtW5QRXdS)Nrz(cat)Z1234567r|z /proc/%s/statcatrwrrrr@rxr)rHrrrrrrrrstatusZ STATUS_ZOMBIEppidZ create_timerrrrsystem children_userchildren_systemZiowaitZcpu_num)rrrrcpur8r8r9test_stat_file_parsingqsr,z"TestProcess.test_stat_file_parsingc Cstd}tdt|it}|j dks>t |j dksPt | dks`t | }|jdksvt |jdkst |jdkst |}|jd kst |jd kst |jd kst |jttd kst W5QRXdS) Nz Uid: 1000 1001 1002 1003 Gid: 1004 1005 1006 1007 Threads: 66 Cpus_allowed: f Cpus_allowed_list: 0-7 voluntary_ctxt_switches: 12 nonvoluntary_ctxt_switches: 13rr Biiiiiir)rrrrrrrrnum_ctx_switches voluntaryr involuntary num_threadsuidsrealZ effectiveZsavedgids_proc_get_eligible_cpuslistrG)rrrr|r~r8r8r9test_status_file_parsingsz$TestProcess.test_status_file_parsingc Cs^tjdttjdd>}t}td |gksdirname__file__ZHEREr2Z SIOCGIFCONFr_r;r=rZEMPTY_TEMPERATURESr:r<r>rTrbrtrzr{rcontextmanagerrrr)r*rrrrrr r-r8r]r`rdrorrrrrrrrr r/r2rrr8r8r8r9s                                    91RZI'63AH8CzS