3 ֪eri@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlm 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-ddl,m.Z.ddl,m/Z/ddl,m0Z0ej1j2ej1j3e4Z5d Z6d!Z7d"Z8d#Z9d$Z:erd%Z;ejd& Zd+d,Z?d-d.Z@d/d0ZAd1d2ZBd3d4ZCd5d6ZDd7d8ZEejFd9d:ZGejFd;d<ZHejIe d=Gd>d?d?e"ZJejIe d=Gd@dAdAe"ZKejIe d=GdBdCdCe"ZLejIe d=GdDdEdEe"ZMejIe d=GdFdGdGe"ZNejIe d=GdHdIdIe"ZOejIe d=GdJdKdKe"ZPejIe d=GdLdMdMe"ZQejIe d=GdNdOdOe"ZRejIe d=GdPdQdQe"ZSejIe d=GdRdSdSe"ZTejIe d=GdTdUdUe"ZUejIe d=GdVdWdWe"ZVejIe d=GdXdYdYe"ZWejIe d=GdZd[d[e"ZXejIe d=Gd\d]d]e"ZYejIe d=Gd^d_d_e"ZZejIe d=Gd`dadae"Z[ejIe d=ejIe dbGdcdddde"Z\ejIe d=Gdedfdfe"Z]ejIe d=Gdgdhdhe"Z^ejIe d=Gdidjdje"Z_ejIe d=Gdkdldle"Z`ejIe d=Gdmdndne"ZaejIe d=Gdodpdpe"ZbecdqkrddrldmeZeeee4dS)szLinux specific tests.)divisionN)LINUX)PY3)FileNotFoundError) basestring)u)GITHUB_ACTIONS)GLOBAL_TIMEOUT) HAS_BATTERY) HAS_CPU_FREQ)HAS_GETLOADAVG) HAS_RLIMIT)PYPY)TOLERANCE_DISK_USAGE)TOLERANCE_SYS_MEM)PsutilTestCase) ThreadTask) call_until)mock) 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*cCspddl}|dd}tr"t|d}tjtjtj}tj|*tj|j |j t t j d|ddSQRXdS)Nrascii256s)fcntlrbytessocketAF_INET SOCK_DGRAM contextlibclosing inet_ntoaioctlfileno SIOCGIFADDRstructpack)ifnamer$sr3C/usr/local/lib64/python3.6/site-packages/psutil/tests/test_linux.pyget_ipv4_addressLs   r5cCspddl}|dd}tr"t|d}tjtjtj}tj|*tj|j |j t t j d|ddSQRXdS)Nrrr r!r"r#)r$rr%r&r'r(r)r*r+r,r-SIOCGIFNETMASKr/r0)r1r$r2r3r3r4get_ipv4_netmask[s   r7cCspddl}|dd}tr"t|d}tjtjtj}tj|*tj|j |j t t j d|ddSQRXdS)Nrrr r!r"r#)r$rr%r&r'r(r)r*r+r,r-SIOCGIFBRDADDRr/r0)r1r$r2r3r3r4get_ipv4_broadcastjs   r9c CstdR}g}x.|jD]"}|j}|d|kr|j|qWt|dkrVtd|WdQRXx~tt|D]n}||d}g}x.tdt|dD]}|j|||dqWdj|} tj tj | } tj tj | ||<qnW|S)Nz/proc/net/if_inet6rzcould not find interface %r:) open readlinessplitappendlen ValueErrorrangejoinr& inet_ptonAF_INET6 inet_ntop) r1fZ all_fieldslinefieldsiZ unformattedgroupsj formattedZpackedr3r3r4get_ipv6_addressesys"     rPc sddl}|dd}tr"t|d}tjtjtj}tj|`|j|j t t j d|}trfddnddl }|jdjfdd |d d Ddd SQRXdS)Nrrr r!cSs|S)Nr3)xr3r3r4ordszget_mac_address..ordcsg|]}d|qS)z%02x:r3).0char)rRr3r4 sz#get_mac_address..r#r:r=)r$rr%r&r'r(r)r*r,r- SIOCGIFHWADDRr/r0 __builtin__rRrE)r1r$r2inforYr3)rRr4get_mac_addresss    r[cCstddgddid}|jd}xJ|D]B}|jdr$|j\}}}}tjdd}|t|t|t|Sq$Wtd dj|d S) zQParse 'free' cmd and return swap memory's s total, used and free values. freez-bLANGzC.UTF-8)env ZSwapztotal used freez&can't find 'Swap' in 'free' output: %sN)rr@ startswith collections namedtupleintrCrE)outlinesrJ_totalusedr\ntr3r3r4 free_swaps    rjcCstddgddid}|jd}xT|D]L}|jdr$dd |jd d D\}}}}tjdd }||||||Sq$Wtd dj|dS)zSParse 'free' cmd and return physical memory's total, used and free values. r\z-br]zC.UTF-8)r^r_ZMemcss|]}t|VqdS)N)rc)rTrQr3r3r4 szfree_physmem..r:ztotal used free shared outputz%can't find 'Mem' in 'free' output: %sN)rr@r`rarbrCrE)rdrerJrgrhr\sharedrir3r3r4 free_physmems   "rncCsZtddgddid}x4|jdD]&}|j}||kr t|jddSq Wtd |dS) Nvmstatz-sr]zC.UTF-8)r^r_ rz can't find %r in 'vmstat' output)rr@striprcrC)statrdrJr3r3r4ros rocCs@tddgj}d|kr"tjdttttjd|j dS)Nr\z-VUNKNOWNzcan't determine free versionz\d+r:r=) rrqunittestSkipTesttuplemaprcrefindallr@)rdr3r3r4get_free_version_infos rzc #sDfdd}ttrdnd}tj|d|d }|VWdQRXdS)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(tj|Stj|SqPtj|Sn|f||SdS)N)r isinstancerioStringIOBytesIO)nameargskwargscontent) orig_openpairsr3r4 open_mocks    z$mock_open_content..open_mockz builtins.openz__builtin__.openT)create side_effectN)r>rrpatch)rr patch_pointmr3)rrr4mock_open_contents   rc #sFfdd}ttrdnd}tj|d|d }|VWdQRXdS)zZMock open() builtin and raises `exc` if the path being opened matches `for_path`. cs"|krn|f||SdS)Nr3)rrr)excfor_pathrr3r4rsz&mock_open_exception..open_mockz builtins.openz__builtin__.openT)rrN)r>rrr)rrrrrr3)rrrr4mock_open_exceptions  rz LINUX onlyc@sLeZdZddZeddZeddZeddZed d Zd S) "TestSystemVirtualMemoryAgainstFreecCs"tj}tjj}|j||dS)N)rnrgpsutilvirtual_memory assertEqual)self cli_value psutil_valuer3r3r4 test_totals z-TestSystemVirtualMemoryAgainstFree.test_totalcCs:tdkr|jdtj}tjj}|j||tddS)N zold free version)delta)rrr)rzskipTestrnrhrrassertAlmostEqualr)rrrr3r3r4 test_useds    z,TestSystemVirtualMemoryAgainstFree.test_usedcCs&tj}tjj}|j||tddS)N)r)rnr\rrrr)rrrr3r3r4 test_free!s z,TestSystemVirtualMemoryAgainstFree.test_freecCsJt}|j}|dkrtjdtjj}|j||td|||jfddS)Nrz%free does not support 'shared' columnz %s %s %s)rmsg) rnrmrtrurrrroutput)rr\ free_valuerr3r3r4 test_shared)s  z.TestSystemVirtualMemoryAgainstFree.test_sharedcCsltddg}|jd}d|dkr.tjdn:t|djd }tjj}|j||t d|||fd dS) Nr\z-br_ availablerz(free does not support 'available' columnr:z %s %s %s)rrr=) rr@rtrurcrrrrr)rrdrerrr3r3r4test_available7s     z1TestSystemVirtualMemoryAgainstFree.test_availableN) __name__ __module__ __qualname__rrrrrrr3r3r3r4r s rc@sZeZdZddZeddZeddZeddZed d Zed d Z d S)$TestSystemVirtualMemoryAgainstVmstatcCs*tdd}tjj}|j||tddS)Nz total memoryi)r)rorrrgrr)r vmstat_valuerr3r3r4rLs  z/TestSystemVirtualMemoryAgainstVmstat.test_totalcCs>tdkr|jdtdd}tjj}|j||tddS)Nrrzold free versionz used memoryi)r)rrr)rzrrorrrhrr)rrrr3r3r4rSs     z.TestSystemVirtualMemoryAgainstVmstat.test_usedcCs*tdd}tjj}|j||tddS)Nz free memoryi)r)rorrr\rr)rrrr3r3r4ras  z.TestSystemVirtualMemoryAgainstVmstat.test_freecCs*tdd}tjj}|j||tddS)Nz buffer memoryi)r)rorrbuffersrr)rrrr3r3r4 test_buffersis  z1TestSystemVirtualMemoryAgainstVmstat.test_bufferscCs*tdd}tjj}|j||tddS)Nz active memoryi)r)rorractiverr)rrrr3r3r4 test_activeqs  z0TestSystemVirtualMemoryAgainstVmstat.test_activecCs*tdd}tjj}|j||tddS)Nzinactive memoryi)r)rorrinactiverr)rrrr3r3r4 test_inactiveys  z2TestSystemVirtualMemoryAgainstVmstat.test_inactiveN) rrrrrrrrrrr3r3r3r4rJs rc@sBeZdZddZeddZddZddZd d Zd d Z d S)TestSystemVirtualMemoryMockscCsZtjdj}td|i4}tjdd}tjdtj}|j sJt |j t |d|d}|j dt|j|j d t|j|j d t|j|j d t|j|j d t|j|j d t|j|j dt|j|j |jd|j |jd|j |jd|j |jd|j |jd|j |jd|j |jdWdQRXWdQRXdS)NaL Active(anon): 6145416 kB Active(file): 2950064 kB Inactive(anon): 574764 kB Inactive(file): 1567648 kB MemAvailable: -1 kB MemFree: 2057400 kB MemTotal: 16325648 kB SReclaimable: 346648 kB z /proc/meminfoT)recordalwaysr:rz#memory stats couldn't be determinedcachedrmrrrr)textwrapdedentencoderwarningscatch_warnings simplefilterrrcalledAssertionErrorrrBassertInstrmessagerrrrmrrslab)rrrwsretwr3r3r4test_warnings_on_missess0   z4TestSystemVirtualMemoryMocks.test_warnings_on_missesc Csi}td4}x,|D]$}|j}t|dd||d<qWWdQRXt|}d|kr~|d}t|||d}|j|ddS)Nz /proc/meminfor:irs MemAvailable:dr)rr@rcrabsZ assertLess)rZmemsrIrJrKabZ diff_percentr3r3r4test_avail_old_percents  &z3TestSystemVirtualMemoryMocks.test_avail_old_percentcCs|tjdj}td|iX}tjdd}tj}WdQRX|jsFt |j |j d |d}|j dt |jWdQRXdS) Na Active: 9444728 kB Active(anon): 6145416 kB Active(file): 2950064 kB Buffers: 287952 kB Cached: 4818144 kB Inactive(file): 1578132 kB Inactive(anon): 574764 kB Inactive(file): 1567648 kB MemAvailable: 6574984 kB MemFree: 2057400 kB MemTotal: 16325648 kB Shmem: 577588 kB SReclaimable: 346648 kB z /proc/meminfoT)riSdirz,inactive memory stats couldn't be determinedl ")rrrrrrrrrrrrrrr)rrrrrrr3r3r4 test_avail_old_comes_from_kernels  z=TestSystemVirtualMemoryMocks.test_avail_old_comes_from_kernelcCs|tjdj}td|iX}tjdd}tj}WdQRX|jsFt |j |j d |d}|j d t |jWdQRXdS) Nat Active: 9444728 kB Active(anon): 6145416 kB Buffers: 287952 kB Cached: 4818144 kB Inactive(file): 1578132 kB Inactive(anon): 574764 kB MemFree: 2057400 kB MemTotal: 16325648 kB Shmem: 577588 kB z /proc/meminfoT)ridiiIrz,inactive memory stats couldn't be determinedi}l'Ll`LG)rrrrrrrrrrrrrrr)rrrrrrr3r3r4test_avail_old_missing_fieldss  z:TestSystemVirtualMemoryMocks.test_avail_old_missing_fieldscCstjdj}td|iltdttjdNtj dd6}t j }|j |j d|d }|jd t|jWdQRXWdQRXWdQRXdS)Na Active: 9444728 kB Active(anon): 6145416 kB Active(file): 2950064 kB Buffers: 287952 kB Cached: 4818144 kB Inactive(file): 1578132 kB Inactive(anon): 574764 kB Inactive(file): 1567648 kB MemFree: 2057400 kB MemTotal: 16325648 kB Shmem: 577588 kB SReclaimable: 346648 kB z /proc/meminfoz/proc/zoneinfozno such file or directoryT)ridiiIrz,inactive memory stats couldn't be determinedi}l'Ll`LG)rrrrrIOErrorerrnoENOENTrrrrrrrrr)rrrrrr3r3r4test_avail_old_missing_zoneinfos  zread)rIdatar3r3r4meminfo_has_swap_infoZs z*TestSystemSwapMemory.meminfo_has_swap_infocCs"tj}tjj}|j||tdS)N)r)rjrgr swap_memoryrr)rrrr3r3r4ras zTestSystemSwapMemory.test_totalcCs"tj}tjj}|j||tdS)N)r)rjrhrrrr)rrrr3r3r4rhs zTestSystemSwapMemory.test_usedcCs"tj}tjj}|j||tdS)N)r)rjr\rrrr)rrrr3r3r4rps zTestSystemSwapMemory.test_freecCstjddd}tjddh}tjdtj}|js:t|j t |d|d}|j dt |j |j |jd|j |jdWdQRXWdQRXdS) Nzpsutil._common.openT)r)rrr:rz9'sin' and 'sout' swap memory stats couldn't be determined)rrrrrrrrrrrBrrrsinsout)rrrrrr3r3r4test_missing_sin_soutxs   z*TestSystemSwapMemory.test_missing_sin_soutcCstdttjd}tjddh}tjdtj}|j s>t |j t |d|d}|j dt|j|j |jd|j |jdWdQRXWdQRXdS) Nz /proc/vmstatzno such file or directoryT)rrr:rzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0)rrrrrrrrrrrrrBrrrrr)rrrrrr3r3r4test_no_vmstat_mockeds   z*TestSystemSwapMemory.test_no_vmstat_mockedcCs|jstjdStjd}tj}WdQRX|j srrqrrcr@rr cpu_count)rrIvaluer3r3r4test_against_sysdev_cpu_onlines   z8TestSystemCPUCountLogical.test_against_sysdev_cpu_onlinez/sys/devices/system/cpuz&/sys/devices/system/cpu does not existcCs0tjd}tdd|D}|jtj|dS)Nz/sys/devices/system/cpucSs g|]}tjd|dk r|qS)zcpu\d+$N)rxsearch)rTrQr3r3r4rVszITestSystemCPUCountLogical.test_against_sysdev_cpu_num..)rlistdirrBrrr)rZlscountr3r3r4test_against_sysdev_cpu_nums z5TestSystemCPUCountLogical.test_against_sysdev_cpu_numZnprocznproc utility not availablecCs$ttd}|jtjdd|dS)Nz nproc --allT)logical)rcrrrr)rnumr3r3r4test_against_nprocs z,TestSystemCPUCountLogical.test_against_nproclscpuzlscpu utility not availablecCs8td}tdd|jdD}|jtjdd|dS)Nzlscpu -pcSsg|]}|jds|qS)#)r`)rTrQr3r3r4rVsz@TestSystemCPUCountLogical.test_against_lscpu..r_T)r)rrBr@rrr)rrdrr3r3r4test_against_lscpusz,TestSystemCPUCountLogical.test_against_lscpuc&Cs&ddl}|jj}tjdtd}|j|jj||js>ttjddd:}|j |jj|j|j d|j|j dddWdQRXt d d }|j }WdQRXtj|}tjd|dd }|j|jj|WdQRXtd d i$}|j|jj||jstWdQRXWdQRXdS) Nrzpsutil._pslinux.os.sysconf)rzpsutil._common.openT)rrz /proc/statz /proc/cpuinforb) return_valuerr)psutil._pslinux_pslinuxZcpu_count_logicalrrrCrrr assertIsNoneZ call_count call_argsr>rr|r~r)rroriginalrrIZ cpuinfo_data fake_filer3r3r4test_emulate_fallbackss*       z0TestSystemCPUCountLogical.test_emulate_fallbacksN)rrrrtskipIfrpathexistsrrrrrrr3r3r3r4rs   rc@s6eZdZejed dddZddZddZd S) TestSystemCPUCountCoresrzlscpu utility not availablecCs`td}t}x4|jdD]&}|jds|jd}|j|dqW|jtjddt|dS)Nzlscpu -pr_r,r:F)r) rsetr@r`addrrrrB)rrdZcore_idsrJrKr3r3r4rs  z*TestSystemCPUCountCores.test_against_lscpuc CsPtjj}tjdgd}tjj}|js.tWdQRX|dk rL|j||dS)Nz glob.glob)r)rrcpu_count_coresrrrrr)rZmeth_1rZmeth_2r3r3r4 test_method_2 s   z%TestSystemCPUCountCores.test_method_2cCs\tjdgd0}tjddd}|jtjjWdQRXWdQRX|jsNt|jsXtdS)Nz glob.glob)rzpsutil._common.openT)r)rrrrrrrr)rm1m2r3r3r4test_emulate_none(s $ z)TestSystemCPUCountCores.test_emulate_noneN) rrrrtrrrrrr3r3r3r4r s r c@szeZdZeje dddZeje dddZeje dddZeje ddd Z eje dd d Z d S) TestSystemCPUFrequencyz not supportedc s@fdd}tjjtjd|ddtjs2tWdQRXdS)Ncs|jdrdS|SdS)Nz&/sys/devices/system/cpu/cpufreq/policyF)r`)r ) orig_existsr3r4path_exists_mock5s zMTestSystemCPUFrequency.test_emulate_use_second_file..path_exists_mockzos.path.existsT)rr)rr r rrrcpu_freqr)rrr3)rr4test_emulate_use_second_file2s  z3TestSystemCPUFrequency.test_emulate_use_second_filecsfdd}tjjztjd|drttjtj}|sDt ||j |j d|j |j dx0tjddD] }|j |j d|j |j dqnWWdQRXWdttjttXdS)Ncs|jdrdS|SdS)Nz/sys/devices/system/cpu/F)r`)r )os_path_existsr3r4rEs zITestSystemCPUFrequency.test_emulate_use_cpuinfo..path_exists_mockzos.path.exists)rgT)percpu) rr r rrrrrrrrmaxmin)rrrfreqr3)rr4test_emulate_use_cpuinfoAs     z/TestSystemCPUFrequency.test_emulate_use_cpuinfocsfdd}ttrdnd}tj||dftjdddLtj}|j|jd |jd krj|j|jd |j d kr|j|j d WdQRXWdQRXdS) Ncs|jdr|jdrtjdS|jdr<|jdr.open_mockz builtins.openz__builtin__.open)rzos.path.existsT)rg@@gg@g@) r>rrrrrrcurrentrr)rrrrr3)rr4test_emulate_dataZs    z(TestSystemCPUFrequency.test_emulate_datacsfdd}ttrdnd}tj||dtjdddtjd d dtjdd }|j|d jd |d jdkr|j|d jd|d j dkr|j|d j d|j|djd|djdkr|j|djd|dj dkr|j|dj dWdQRXWdQRXWdQRXdS)Ncs|}|jdr"|jdr"tjdS|jdr@|jdr@tjdS|jdr^|jdr^tjdS|jdr||jdr|tjd S|jdr|jdrtjd S|jdr|jdrtjd S|d krtjd S|f||SdS)Nz/scaling_cur_freqz'/sys/devices/system/cpu/cpufreq/policy0s100000z/scaling_min_freqs200000z/scaling_max_freqs300000z'/sys/devices/system/cpu/cpufreq/policy1s400000s500000s600000z /proc/cpuinfos#cpu MHz : 100 cpu MHz : 400)rr`r|r~)rrrn)rr3r4r}s,       z@TestSystemCPUFrequency.test_emulate_multi_cpu..open_mockz builtins.openz__builtin__.open)rzos.path.existsT)rz!psutil._pslinux.cpu_count_logicalr)rrgY@ggi@gr@r:gy@g@@g@) r>rrrrrrr rr)rrrrr3)rr4test_emulate_multi_cpu{s$    z-TestSystemCPUFrequency.test_emulate_multi_cpucsfdd}ttrdnd}tj||dPtjddd6tjd d dtj}|j|jd WdQRXWdQRXWdQRXdS) NcsR|jdrttjdn6|jdr,tjdS|dkr>tjdS|f||SdS)Nz/scaling_cur_freqrSz/cpuinfo_cur_freqs200000z /proc/cpuinfoscpu MHz : 200)rrrrr|r~)rrr)rr3r4rs    zOTestSystemCPUFrequency.test_emulate_no_scaling_cur_freq_file..open_mockz builtins.openz__builtin__.open)rzos.path.existsT)rz!psutil._pslinux.cpu_count_logicalr:)r>rrrrrrr )rrrrr3)rr4%test_emulate_no_scaling_cur_freq_files   zrr@rfloat)rrrIZ proc_valuer3r3r4test_getloadavgs  zTestLoadAvg.test_getloadavgN)rrrrtrr r/r3r3r3r4r,sr,c@seZdZddZdS)TestSystemNetIfAddrscCsxtjjD]\}}x|D]}|jtjkr@|j|jt|q|jtj kr|j|jt ||j|j t ||j dk r|j|j t|q|jt|dq|jtjkr|jjdd}|j|t|qWqWdS)Nz0.0.0.0%r)rZ net_if_addrsitemsfamilyZAF_LINKraddressr[r&r'r5netmaskr7 broadcastr9rGr@rrP)rraddrsaddrr4r3r3r4test_ipss     zTestSystemNetIfAddrs.test_ipsN)rrrr9r3r3r3r4r0sr0c@sHeZdZejed dddZddZejed dddZd S) TestSystemNetIfStatsifconfigzifconfig utility not availablecCsxxrtjjD]b\}}ytd|}Wntk r:YqX|j|jd|k|d|j|jtt j d|dqWdS)Nz ifconfig %sRUNNING)rz(?i)MTU[: ](\d+)r) r net_if_statsr2r RuntimeErrorrisupmturcrxry)rrstatsrdr3r3r4test_against_ifconfigsz*TestSystemNetIfStats.test_against_ifconfigc CsPxJtjjD]:\}}td| }|j|jt|jjWdQRXqWdS)Nz/sys/class/net/%s/mtu) rr=r2r>rr@rcrrq)rrrArIr3r3r4test_mtuszTestSystemNetIfStats.test_mtuc Csd}xtjjD]\}}ytd|}Wntk r>YqXtjd|}|rt|jdkr|d7}t |j dj j d}t |j j d}|j||qtjd|}|rt|jdkr|d7}t |j dj j }t |j j d}|j||qW|s |jd dS) Nrz ifconfig %szflags=(\d+)?<(.*?)>rr:r z(.*) MTU:(\d+) Metric:(\d+)rzno matches were found)rr=r2rr>rxrrBrMr grouplowerr@flagsrfail)rZ matches_foundrrArdmatchZifconfig_flagsZ psutil_flagsr3r3r4 test_flags$s(  zTestSystemNetIfStats.test_flagsN) rrrrtrrrBrCrIr3r3r3r4r:s r:c@s,eZdZejed deddZdS)TestSystemNetIOCountersr;zifconfig utility not availablecCsdd}tjddd}x|jD]\}}y ||}Wntk rJw YnX|j|j|ddd |j|j|d dd |j|j|d dd |j|j|d dd |j|j |d dd |j|j |ddd |j|j |ddd |j|j |ddd q WdS)NcSsi}td|}ttjd|d|d<ttjd|d|d<ttjd|d|d<ttjd|d |d <ttjd |d|d <ttjd |d |d <ttjd|d|d<ttjd|d|d<|S)Nz ifconfig %szRX packets[: ](\d+)r packets_recvzTX packets[: ](\d+) packets_sentzerrors[: ](\d+)errinr:erroutzdropped[: ](\d+)dropindropoutz#RX (?:packets \d+ +)?bytes[: ](\d+) bytes_recvz#TX (?:packets \d+ +)?bytes[: ](\d+) bytes_sent)rrcrxry)Znicrrdr3r3r4r;Hs z?TestSystemNetIOCounters.test_against_ifconfig..ifconfigTF)ZpernicnowraprQirl)rrRrKrLrM rNrOrPii) rnet_io_countersr2r>rrQrRrKrLrMrNrOrP)rr;ZniorrAZ ifconfig_retr3r3r4rBEs. z-TestSystemNetIOCounters.test_against_ifconfigN)rrrrtrrrrBr3r3r3r4rJCsrJc@s8eZdZejdedejdddddZdd Zd S) TestSystemNetConnectionsz psutil._pslinux.socket.inet_ntop)rzpsutil._pslinux.supports_ipv6F)rc CsRy*tjtjtj}|j|j|jdWntjk r@YnXtjdddS)N::1rinet6)kind)rWr) r&rG SOCK_STREAMZ addCleanupclosebinderrorrnet_connections)r supports_ipv6rHr2r3r3r4test_emulate_ipv6_unsupporteds z6TestSystemNetConnections.test_emulate_ipv6_unsupportedc Cs<tjd}td|i}tjdd|js.tWdQRXdS)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/unixunix)rY)rrrrr^rr)rrrr3r3r4test_emulate_unixs  z*TestSystemNetConnections.test_emulate_unixN)rrrrrrCr`rbr3r3r3r4rV}s  rVc@s>eZdZejeed deddZddZ ddZ d S) TestSystemDiskPartitionsstatvfszos.statvfs() not availablecCsrdd}xdtjddD]T}tj|j}||j\}}}}|j|j||j|j|td|j|j |tdqWdS)NcSsztd|j}|jd}|jd|jd}|jdd\}}}}|dkrRd}t|t|t|}}}||||fS)Nzdf -P -B 1 "%s"r_rr;nonerS)rrqr@poprc)r rdrerJdevrgrhr\r3r3r4dfs   z4TestSystemDiskPartitions.test_against_df..dfF)all)r) rdisk_partitions disk_usage mountpointrrgrr\rrh)rrhpartusagerfrgrhr\r3r3r4test_against_dfs  z(TestSystemDiskPartitions.test_against_dfcCstd}|j}WdQRXd|krNxtjD]}|jdkr.Pq.W|jdn|tjtd}t j d|ddX}t j ddgd <}tj}|j st |j st |st |j |d jdWdQRXWdQRXdS)Nz/proc/filesystemszfszcouldn't find any ZFS partitionz nodev zfs zpsutil._common.openT)rrz$psutil._pslinux.cext.disk_partitions /dev/sdb3/rw)rr)rqrrrprs)r>rrrjfstyperGr|r}rrrrrr)rrIrrmrrrrr3r3r4 test_zfs_fss(       z$TestSystemDiskPartitions.test_zfs_fscCsTzFtjddd.}|jttjWdQRX|js:tWdQRXWddt_XdS)Nzos.path.realpathz /non/existent)rz/proc) rr assertRaisesrrrjrr PROCFS_PATH)rrr3r3r4test_emulate_realpath_fails  z3TestSystemDiskPartitions.test_emulate_realpath_failN) rrrrtrhasattrrrrorurxr3r3r3r4rcsrcc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)TestSystemDiskIoCounterscCsd}td|itjdddtjdd}|j|jd|j|jd |j|jd t |j|j d |j|j d |j|j d |j|j dt |j|jd|j|jdWdQRXWdQRXdS)Nz+ 3 0 1 hda 2 3 4 5 6 7 8 9 10 11 12z/proc/diskstatsz!psutil._pslinux.is_storage_deviceT)rF)rSr:rrr;rlrrrrT)rrrrdisk_io_countersr read_countread_merged_count read_bytes SECTOR_SIZE read_time write_countwrite_merged_count write_bytes write_time busy_time)rrrr3r3r4test_emulate_kernel_2_4s  z0TestSystemDiskIoCounters.test_emulate_kernel_2_4cCsd}td|itjdddtjdd}|j|jd|j|jd |j|jd t |j|j d |j|j d |j|j d |j|j dt |j|jd|j|jdWdQRXWdQRXdS)Nz' 3 0 hda 1 2 3 4 5 6 7 8 9 10 11z/proc/diskstatsz!psutil._pslinux.is_storage_deviceT)rF)rSr:rrr;rlrrrrT)rrrrr{rr|r}r~rrrrrrr)rrrr3r3r4test_emulate_kernel_2_6_fulls  z5TestSystemDiskIoCounters.test_emulate_kernel_2_6_fullcCstdditjdddtjdd}|j|jd|j|jd t|j|j d |j|j d t|j|j d |j|j d |j|j d |j|jd |j|jd WdQRXWdQRXdS) Nz/proc/diskstatsz 3 1 hda 1 2 3 4z!psutil._pslinux.is_storage_deviceT)rF)rSr:rrr;r)rrrrr{rr|r~rrrr}rrrr)rrr3r3r4test_emulate_kernel_2_6_limited s  z8TestSystemDiskIoCounters.test_emulate_kernel_2_6_limitedcCstjd}td|itjdddltjddd}|jt|d|j|d j d |j|d j d |j|d j d |j|d j d WdQRXWdQRXdS) Nzx 3 0 nvme0n1 1 2 3 4 5 6 7 8 9 10 11 3 0 nvme0n1p1 1 2 3 4 5 6 7 8 9 10 11 z/proc/diskstatsz!psutil._pslinux.is_storage_deviceF)rT)perdiskrSrnvme0n1r:Z nvme0n1p1rl) rrrrrrr{rrBr|r)rrrr3r3r4test_emulate_include_partitionss z8TestSystemDiskIoCounters.test_emulate_include_partitionsc Cstjd}td|i8tjdddtjddd}|j|WdQRXWdQRXdd}tjd}td|iLtjdd |d 0tjddd}|j|j d |j|j d WdQRXWdQRXdS) Nzx 3 0 nvme0n1 1 2 3 4 5 6 7 8 9 10 11 3 0 nvme0n1p1 1 2 3 4 5 6 7 8 9 10 11 z/proc/diskstatsz!psutil._pslinux.is_storage_deviceF)r)rrScSs|dkS)Nrr3)rr3r3r4is_storage_deviceBszSTestSystemDiskIoCounters.test_emulate_exclude_partitions..is_storage_deviceT)rrr:rl) rrrrrrr{rrr|r)rrrrr3r3r4test_emulate_exclude_partitions2s$  z8TestSystemDiskIoCounters.test_emulate_exclude_partitionsc CsTdd}tjdd}tjdd|dtjdd}WdQRX|jt|t|dS)NcSs|dkr dSdS)Nz/proc/diskstatsFTr3)r r3r3r4r Tsz?TestSystemDiskIoCounters.test_emulate_use_sysfs..existsT)rzpsutil._pslinux.os.path.exists)rr)rr{rrrrB)rr ZwprocfsZwsysfsr3r3r4test_emulate_use_sysfsSs  z/TestSystemDiskIoCounters.test_emulate_use_sysfsc Cs6dd}tjdd|d|jttjWdQRXdS)NcSsdS)NFr3)r r3r3r4r asz>TestSystemDiskIoCounters.test_emulate_not_impl..existszpsutil._pslinux.os.path.existsT)rr)rrrvNotImplementedErrorrr{)rr r3r3r4test_emulate_not_impl`sz.TestSystemDiskIoCounters.test_emulate_not_implN) rrrrrrrrrrr3r3r3r4rzs! rzc@s^eZdZddZddZejedddZeje d d ejedd d Z d d Z dS)TestRootFsDeviceFindercCs(tjdj}tj||_tj||_dS)Nrr)rrrst_devmajorminor)rrgr3r3r4setUpls  zTestRootFsDeviceFinder.setUpcCsft}tjjdr|jn|jt|jtjjd|j|jfrL|j n|jt|j |j dS)Nz/proc/partitionsz/sys/dev/block/%s:%s/uevent) rrr r ask_proc_partitionsrvrrrask_sys_dev_blockask_sys_class_block)rfinderr3r3r4test_call_methodsqs   z(TestRootFsDeviceFinder.test_call_methodszunsupported on GITHUB_ACTIONScCst}|j|jd}}}tjjdr4|j}tjjd|j|jfrT|j }|j }|pf|pf|}|r||r||j |||r|r|j |||r|r|j ||dS)Nz/proc/partitionsz/sys/dev/block/%s:%s/uevent) rassertIsNotNonefindrr r rrrrrr)rrrrcbaser3r3r4test_comparisonss      z'TestRootFsDeviceFinder.test_comparisonsZfindmntzfindmnt utility not availablecCs"tj}td}|j||dS)Nzfindmnt -o SOURCE -rn /)rrrr)rrZ findmnt_valuer3r3r4test_against_findmnts z+TestRootFsDeviceFinder.test_against_findmntc CsltjddgdR}tjd}|js(ttsP|j|jd|j |jt j n|j |jdWdQRXdS) Nz$psutil._pslinux.cext.disk_partitions /dev/rootrrext4rs)rr)rrrrrs) rrrrjrrrassertNotEqualdevicerrr)rrrmr3r3r4test_disk_partitions_mockeds   z2TestRootFsDeviceFinder.test_disk_partitions_mockedN) rrrrrrtrrrrrrr3r3r3r4rjs rc@sReZdZddZddZddZddZd d Zd d Ze d dZ ddZ dS)TestMisccCs(td}tj}|jt|t|dS)Nz boot time)ror boot_timerrc)rrrr3r3r4test_boot_timeszTestMisc.test_boot_timec"s|j}tj|ttjj|dd$}|jd|jd|jdWdQRXztfdd}trldnd }tj ||d nt t |j t t j|j t t jd d |j t t j|j t t jd d |j t t j|j t t jd d |t _|jt jd |jtt jd t jd d }|jt|d t jd d }|jttt|d ttjj|dd$}|jd|jd|jdWdQRX|jt jd |jtt jd d d |jtt jd |jtttt jd d d WdQRXWdtj|t t X|jt jddS)Nrrrzcpu 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&|jdrttjd|f||S)Nz/proczrejecting access for test)r`rrr)rrr)rr3r4rs  z4TestMisc.test_no_procfs_on_import..open_mockz builtins.openz__builtin__.open)rT)rrzcpu 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 z/proc) get_testfnrmkdirr>r rEwriterrrrrrvrr cpu_percentcpu_times_percentrwrsumrwrshutilrmtree)rZ my_procfsrIrrZper_cpu_percentZper_cpu_times_percentr3)rr4test_no_procfs_on_importsL         (  z!TestMisc.test_no_procfs_on_importcCs8tjdj}td|i8}tj|js.ttjddtjtjddWdQRXtjdj}td|itj}|jsttjdd}tj}tjdd}|j |d|j t |d|j t |d|j t |d|j t t t |d|j t t t |d|j |j d|j |jdWdQRXdS)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 z /proc/statT)rz~ 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 rgY@)rrrrrrrrrrrrwrruser)rrrrZcpu_percent_percpurZcpu_times_percent_percpur3r3r4test_cpu_steal_decreases0        z TestMisc.test_cpu_steal_decreasec Cs8tjddd }|jttjj|js*tWdQRXdS)Nzpsutil._common.openT)r) rrrvr>rrrrr)rrr3r3r4test_boot_time_mockedszTestMisc.test_boot_time_mockedcCs$xtjD]}|j|jdq WdS)N:0:0.0)rr)rZusersrhost)rrr3r3r4 test_users szTestMisc.test_usersc Cs|j}tj|z|t_|jttj|jttj|jttjdd|jttj |jttj |jttj |jttj |jttj |jtjtjWddt_XdS)NT)rz/proc)rrrrrwrvrrrrr^rUr=rj NoSuchProcessProcess)rZtdirr3r3r4test_procfs_path&s zTestMisc.test_procfs_pathc Cs|tltj}|j}|jt|dt|ddddj}|j|j |tj|}|j |j |tj WdQRXdS)NrcSs|jS)N)id)rQr3r3r4Dsz)TestMisc.test_issue_687..)keyr:) rrrthreadsrrBsortedrrpidZas_dictrZpids)rprtidptr3r3r4test_issue_6879s zTestMisc.test_issue_687c Cs8tddi"}tjtjs t|js*tWdQRXdS)Nz/proc/%s/statusrS)rrZ pid_existsrgetpidrr)rrr3r3r4test_pid_exists_no_proc_statusJsz'TestMisc.test_pid_exists_no_proc_statusN) rrrrrrrrrrrrr3r3r3r4rs>'rz no batteryc@sfeZdZejed dddZddZddZd d Z d d Z d dZ ddZ ddZ ddZdS)TestSensorsBatteryZacpizacpi utility not availablecCsDtd}t|jddjjdd}tjj}|j||dddS)Nzacpi -br r:r1rS)r) rrcr@rqreplacersensors_batterypercentr)rrdZ acpi_valuerr3r3r4 test_percent[s zTestSensorsBattery.test_percentc sjfdd}ttrdnd}tj||d6}|jtjjd|jtjjtj |j s\t WdQRXdS)Ncs(|jdrtjdS|f||SdS)N AC0/online AC/online1)rr)rr|r~)rrr)rr3r4rds  z@TestSensorsBattery.test_emulate_power_plugged..open_mockz builtins.openz__builtin__.open)rT) r>rrrrrr power_pluggedsecsleftPOWER_TIME_UNLIMITEDrr)rrrrr3)rr4test_emulate_power_pluggedbs  z-TestSensorsBattery.test_emulate_power_pluggedc sVfdd}ttrdnd}tj||d"}|jtjjd|jsHt WdQRXdS)NcsD|jdrttjdn(|jdr0tjtdS|f||SdS)N AC0/online AC/onlinerSz/statusZcharging)rr)rrrrr|r}r)rrr)rr3r4rvs   zBTestSensorsBattery.test_emulate_power_plugged_2..open_mockz builtins.openz__builtin__.open)rT) r>rrrrrrrrr)rrrrr3)rr4test_emulate_power_plugged_2ss   z/TestSensorsBattery.test_emulate_power_plugged_2c sVfdd}ttrdnd}tj||d"}|jtjjd|jsHt WdQRXdS)Ncs(|jdrtjdS|f||SdS)N AC0/online AC/online0)rr)rr|r~)rrr)rr3r4rs  zDTestSensorsBattery.test_emulate_power_not_plugged..open_mockz builtins.openz__builtin__.open)rF) r>rrrrrrrrr)rrrrr3)rr4test_emulate_power_not_pluggeds   z1TestSensorsBattery.test_emulate_power_not_pluggedc sVfdd}ttrdnd}tj||d"}|jtjjd|jsHt WdQRXdS)NcsD|jdrttjdn(|jdr0tjtdS|f||SdS)N AC0/online AC/onlinerSz/statusZ discharging)rr)rrrrr|r}r)rrr)rr3r4rs   zFTestSensorsBattery.test_emulate_power_not_plugged_2..open_mockz builtins.openz__builtin__.open)rF) r>rrrrrrrrr)rrrrr3)rr4 test_emulate_power_not_plugged_2s   z3TestSensorsBattery.test_emulate_power_not_plugged_2c sTfdd}ttrdnd}tj||d }|jtjj|jsFt WdQRXdS)Ncs@|jdrttjdn$|jdr,tjdS|f||SdS)N"/sys/class/power_supply/AC0/online!/sys/class/power_supply/AC/onlinerSz#/sys/class/power_supply/BAT0/statuss???)rr)r`rrrr|r~)rrr)rr3r4rs  zETestSensorsBattery.test_emulate_power_undetermined..open_mockz builtins.openz__builtin__.open)r) r>rrrrrrrrr)rrrrr3)rr4test_emulate_power_undetermineds   z2TestSensorsBattery.test_emulate_power_undeterminedc Cs8tddi"}|jtjjd|js*tWdQRXdS)Nz(/sys/class/power_supply/BAT0/energy_fullrr)rrrrrrr)rrr3r3r4test_emulate_energy_full_0s  z-TestSensorsBattery.test_emulate_energy_full_0cCsjtdttjdNtdttjd0tddi|jtjjdWdQRXWdQRXWdQRXdS)Nz(/sys/class/power_supply/BAT0/energy_fullrSz(/sys/class/power_supply/BAT0/charge_fullz%/sys/class/power_supply/BAT0/capacitys88X) rrrrrrrrr)rr3r3r4"test_emulate_energy_full_not_avails z5TestSensorsBattery.test_emulate_energy_full_not_availcCsntdttjdRtdttjd4tdttjd|jtjjWdQRXWdQRXWdQRXdS)Nz!/sys/class/power_supply/AC/onlinerSz"/sys/class/power_supply/AC0/onlinez#/sys/class/power_supply/BAT0/status)rrrrrrrr)rr3r3r4test_emulate_no_powersz(TestSensorsBattery.test_emulate_no_powerN)rrrrtrrrrrrrrrrrr3r3r3r4rXsrc@seZdZddZdS)TestSensorsBatteryEmulatedcsxfdd}ttrdnd}tjddgd.}tj||d}|jtjWdQRXWdQRX|jsjt|jsttdS) Ncs\|jdrtjtdS|jdr0tjtdS|jdrHtjtdS|f||SdS)Nz /energy_nowZ60000000z /power_now0z /energy_fullZ60000001)rr|r}r)rrr)rr3r4rs   z5TestSensorsBatteryEmulated.test_it..open_mockz builtins.openz__builtin__.openz os.listdirZBAT0)r)r) r>rrrrrrrr)rrrZmlistdirZmopenr3)rr4test_its  " z"TestSensorsBatteryEmulated.test_itN)rrrrr3r3r3r4rsrc@seZdZddZddZdS)TestSensorsTemperaturescsfdd}ttrdnd}tj||djtjddgdNtjd d }|j|jd |j|jd |j|j d |j|j dWdQRXWdQRXdS)Ncs|jdrtjtdS|jdr0tjtdS|jdrDtjdS|jdrXtjdS|jd rltjd S|f||SdS) Nz/namerz /temp1_labellabelz /temp1_inputs30000z /temp1_maxs40000z /temp1_crits50000)rr|r}rr~)rrr)rr3r4rs        zCTestSensorsTemperatures.test_emulate_class_hwmon..open_mockz builtins.openz__builtin__.open)rz glob.globz/sys/class/hwmon/hwmon0/temp1)rrrrg>@gD@gI@) r>rrrrsensors_temperaturesrrr highcritical)rrrtempr3)rr4test_emulate_class_hwmons  z0TestSensorsTemperatures.test_emulate_class_hwmoncsfdd}dd}ttr dnd}tj||djtjdd |d Ntjd d }|j|jd |j|jd|j|j d|j|j dWdQRXWdQRXdS)Ncsl|jdrtjdS|jdr(tjdS|jdr@tjtdS|jdrXtjtdS|f||SdS) NZ0_temps50000rs30000Z0_typertyper)rr|r~r}r)rrr)rr3r4rs      zETestSensorsTemperatures.test_emulate_class_thermal..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_tempr3)r r3r3r4 glob_mockszETestSensorsTemperatures.test_emulate_class_thermal..glob_mockz builtins.openz__builtin__.open)rz glob.globT)rrrrrSg>@gI@) r>rrrrrrrr rr)rrrrrr3)rr4test_emulate_class_thermals  z2TestSensorsTemperatures.test_emulate_class_thermalN)rrrrrr3r3r3r4rsrc@seZdZddZdS)TestSensorsFanscsfdd}ttrdnd}tj||dNtjddgd2tjd d }|j|jd |j|jd WdQRXWdQRXdS) Ncs\|jdrtjtdS|jdr0tjtdS|jdrHtjtdS|f||SdS)Nz/namerz /fan1_labelrz /fan1_input2000)rr|r}r)rrr)rr3r4r;s   z4TestSensorsFans.test_emulate_data..open_mockz builtins.openz__builtin__.open)rz glob.globz/sys/class/hwmon/hwmon2/fan1)rrrri) r>rrrrZ sensors_fansrrr )rrrZfanr3)rr4r!:s  z!TestSensorsFans.test_emulate_dataN)rrrr!r3r3r3r4r8src@seZdZeddZddZejedddZ dd Z d d Z d d Z ddZ ddZddZddZddZddZddZddZeje ddd Zd!d"Zd#d$Zd%d&Zd'S)( TestProcesscCs|j}tjj|jj\}}}tj|jjdd}|j|tdd|Ddd|j|tdd|Ddd|j|tdd|DdddS) NF)ZgroupedcSsg|]}|j|jqSr3)Z private_dirtyZ private_clean)rTrQr3r3r4rV^sz?TestProcess.test_parse_smaps_vs_memory_maps..i)rcSsg|] }|jqSr3)pss)rTrQr3r3r4rVascSsg|] }|jqSr3)r)rTrQr3r3r4rVbs) Zspawn_testprocrrrr _parse_smaps memory_mapsrr)rZsprocussrrmapsr3r3r4test_parse_smaps_vs_memory_mapsWsz+TestProcess.test_parse_smaps_vs_memory_mapsc Cs~tjdj}tdtj|iR}tjjtj}|j \}}}|j sLt |j |d |j |d |j |d WdQRXdS)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 z/proc/%s/smapsrrirr ili i<) rrrrrrrrrrrrr)rrrrrrrr3r3r4test_parse_smaps_mockedds    z#TestProcess.test_parse_smaps_mockedzunreliable on PYPYc;Cshdd}|j}t|d|j||jdWdQRXt||j||jdWdQRXt|d|j||jdWdQRXt|d|j||jdWdQRXt|d|j||jdWdQRXt|d|j||jdWdQRXtrdt|t|d |j||jdWdQRXt|t|d |j||jdWdQRXdS) NcSs\tj}tjt}x:x4|jD](}|jtjj|kr:|Stj|kr Pq WqWtddS)Nztimeout looking for test file) rrtimer open_filesr rabspathr>)fnamerZ giveup_atfiler3r3r4 get_test_files   z7TestProcess.test_open_files_mode..get_test_filerrrzr+zw+za+rQzx+)rr>rmoderr)rrZtestfnr3r3r4test_open_files_modes*         z TestProcess.test_open_files_modecCstj}|j}t|jdt|jdt|tjdt t j dd }|j |jg|j sftWdQRXtjdt t jdd }|j |jg|j stWdQRXWdQRXdS)Nrzlen(ret) != %izpsutil._pslinux.os.readlinkrS)r)rrrr>rrrBrrOSErrorrrrrrEINVAL)rrfilesrr3r3r4test_open_files_file_gones z%TestProcess.test_open_files_file_gonecCstj}|j}t|jdbt|jdt|trrrrBrrrrrrrrr)rrr rrr3r3r4test_open_files_fd_gones z#TestProcess.test_open_files_fd_gonecCstj}|j}t|jdpt|jdt|d}tj|t t j dd6}tjd |j |jg|j svtWdQRXWdQRXWdQRXdS)Nrzlen(ret) != %izpsutil._pslinux.os.readlinkrS)rzpsutil._pslinux.debug)rrrr>rrrBrrrr ENAMETOOLONGrrr)rrr rrr3r3r4test_open_files_enametoolongs z(TestProcess.test_open_files_enametoolongc CsBtjdid*}|jtjjtjj|j s4t WdQRXdS)Nz)psutil._pslinux._psposix.get_terminal_map)r) rrrrrrrrterminalrr)rrr3r3r4test_terminal_mockeds  z TestProcess.test_terminal_mockedcCstj}tjtd}tjd|dd$}|j|jddg|j sFt WdQRXtjtd}tjd|dd&}|j|jdddg|j st WdQRXdS) Nzfoobarzpsutil._common.openT)rrfoobarz foobarrS) rrr|r}rrrrcmdlinerr)rrrrr3r3r4test_cmdline_mockeds  zTestProcess.test_cmdline_mockedcCstj}tjtd}tjd|dd$}|j|jddg|j sFt WdQRXtjtd}tjd|dd&}|j|jdddg|j st WdQRXdS) Nzfoo bar zpsutil._common.openT)rrrrz foo bar rS) rrr|r}rrrrrrr)rrrrr3r3r4test_cmdline_spaces_mockeds  z&TestProcess.test_cmdline_spaces_mockedc CsTtj}tjtd}tjd|dd$}|j|jddg|j sFt WdQRXdS)Nzfoo barzpsutil._common.openT)rrrr) rrr|r}rrrrrrr)rrrrr3r3r4test_cmdline_mixed_separatorss z)TestProcess.test_cmdline_mixed_separatorsc CsFtjddd.|jtjjd|jtjjdWdQRXdS)Nzpsutil._pslinux.os.readlinkz/home/foo (deleted))rz /home/foo)rrrrrexecwd)rr3r3r4!test_readlink_path_deleted_mockeds z-TestProcess.test_readlink_path_deleted_mockedcsfdd}ttrdnd}tj||d(}tjj}|jsBt|j |gWdQRXfdd}tj||d|j tj tjjWdQRXdS)Ncs4|jdtjr ttjdn|f||SdS)Nz /proc/%s/taskrS)r`rrrrr)rrr)rr3r4 open_mock_1+sz4TestProcess.test_threads_mocked..open_mock_1z builtins.openz__builtin__.open)rcs4|jdtjr ttjdn|f||SdS)Nz /proc/%s/taskrS)r`rrrrEPERM)rrr)rr3r4 open_mock_2:sz4TestProcess.test_threads_mocked..open_mock_2) r>rrrrrrrrrrv AccessDenied)rrrrrrr3)rr4test_threads_mocked&s     zTestProcess.test_threads_mockedc CsHtjdttjdd(}tjj}|js.t |j |dWdQRXdS)Nzpsutil._pslinux.readlinkrS)r) rrrrrrrrrrr)rrrr3r3r4test_exe_mockedCs   zTestProcess.test_exe_mockedcCsZtdtjttjd6}tj}|jt |j WdQRX|j sLt WdQRXdS)Nz/proc/%s/smapsrS) rrrrrrrrrvrrrr)rrrr3r3r4test_issue_1014Ks zTestProcess.test_issue_1014z not supportedcCstjdttjddT}tjddd:}tj}|j|jtj }|j tj WdQRXWdQRXWdQRX|j szt |j st |j|jj|j|j|jj|jdS)Nzpsutil._pslinux.prlimitrS)rz"psutil._pslinux.Process._is_zombieT)r)rrrrZENOSYSrrrrv ZombieProcessZrlimitZ RLIMIT_NOFILErrr exceptionr)rrrrrr3r3r4test_rlimit_zombieVs *  zTestProcess.test_rlimit_zombiec*CsRdddddddddddddddddddddd ddddddddddddddddd ddd g*}d j|j}td tj|itj}|j|jd |j|j tj |j|j d|j|j dt tj|j}|j|jdt |j|jdt |j|jdt |j|jdt |j|jdt |j|jdWdQRXdS)Nrz(cat)Z1234567rpz /proc/%s/statcatr:rrrr;rlr)rErrrrrrrrstatus STATUS_ZOMBIEppidZ create_timerrrrsystem children_userchildren_systemZiowaitZcpu_num)rrrrZcpur3r3r4test_stat_file_parsingjsrz"TestProcess.test_stat_file_parsingc Cstjdj}tdtj|itj}|j|j j d|j|j j d|j|j d|j }|j|jd|j|jd|j|jd|j}|j|jd |j|jd |j|jd |j|jjttd WdQRXdS) 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: 13z/proc/%s/statusrrBiiiiiir)rrrrrrrrrnum_ctx_switches voluntary involuntary num_threadsuidsreal effectivesavedgids_proc_get_eligible_cpuslistrD)rrrr9r=r3r3r4test_status_file_parsings  z$TestProcess.test_status_file_parsingcCs^tjdttjdd>}tj}tjd |j|jg|j sFt WdQRXWdQRXdS)Nzpsutil._pslinux.os.readlinkrS)rzpsutil._pslinux.debug) rrrrr rrrZ connectionsrr)rrrr3r3r4test_connections_enametoolongs z)TestProcess.test_connections_enametoolongN)rrrrrrrtrrrr r rrrrrrrrr r r#r3rArBr3r3r3r4rUs$ "%  >rc@sreZdZdZeddZddZddZdd Zd d Z d d Z ddZ ddZ e ddZddZddZdS)TestProcessAgainstStatusa /proc/pid/stat and /proc/pid/status have many values in common. Whenever possible, psutil uses /proc/pid/stat (it's faster). For all those cases we check that the value found in /proc/pid/stat (by psutil) matches the one found in /proc/pid/status. cCstj|_dS)N)rrproc)clsr3r3r4 setUpClasssz#TestProcessAgainstStatus.setUpClasscCs|tjjd|jj^}xJ|D]B}|j}|j|r|jdd}yt|St k r\|SXqWt d|WdQRXdS)Nz/proc/%s/status rz can't find %r) r _psplatform open_textrDrrqr` partitionrcrC)rZ linestartrIrJrr3r3r4read_status_files   z)TestProcessAgainstStatus.read_status_filecCs |jd}|j|jj|dS)NzName:)rKrrDr)rrr3r3r4 test_names z"TestProcessAgainstStatus.test_namecCsH|jd}||jdd|jd}|jdd}|j|jj|dS)NzState:(r:)rpr)rKrrfindrrrDr-)rrr3r3r4 test_statuss  z$TestProcessAgainstStatus.test_statuscCs |jd}|j|jj|dS)NzPPid:)rKrrDr/)rrr3r3r4 test_ppids z"TestProcessAgainstStatus.test_ppidcCs |jd}|j|jj|dS)NzThreads:)rKrrDr8)rrr3r3r4test_num_threadss z)TestProcessAgainstStatus.test_num_threadscCs:|jd}ttt|jdd}|j|jj|dS)NzUid:r:r;)rKrvrwrcr@rrDr9)rrr3r3r4 test_uidss z"TestProcessAgainstStatus.test_uidscCs:|jd}ttt|jdd}|j|jj|dS)NzGid:r:r;)rKrvrwrcr@rrDr=)rrr3r3r4 test_gids s z"TestProcessAgainstStatus.test_gidscCs@|jd}|j|jjj||jd}|j|jjj|dS)Nzvoluntary_ctxt_switches:znonvoluntary_ctxt_switches:)rKrrDr5r6r7)rrr3r3r4test_num_ctx_switches s  z.TestProcessAgainstStatus.test_num_ctx_switchescCsN|jd}dt|krJtt|jd\}}|j|jjtt ||ddS)NzCpus_allowed_list:rr:) rKrrwrcr@rrDZ cpu_affinityr@rD)rrZmin_Zmax_r3r3r4test_cpu_affinity s   z*TestProcessAgainstStatus.test_cpu_affinityc CsT|jd}tjd}|jjjWdQRXdt|krF|j sPtn |jsPtdS)NzCpus_allowed_list:zpsutil._pslinux.per_cpu_timesr) rKrrrDr>r?rrr)rrrr3r3r4test_cpu_affinity_eligible_cpus s    z8TestProcessAgainstStatus.test_cpu_affinity_eligible_cpusN)rrr__doc__ classmethodrFrKrLrPrQrRrSrTrrUrVrWr3r3r3r4rCs rCc@seZdZddZdS) TestUtilsc Cs<tjddd$}|jtjjdd|js.tWdQRXdS)Nz os.readlinkz foo (deleted))rrr)rrrrrHreadlinkrr)rrr3r3r4 test_readlink% szTestUtils.test_readlinkN)rrrr\r3r3r3r4rZ# srZ__main__) run_from_name)frX __future__rrar)rglobr|rrxrr&r/rrrtrrrZpsutil._compatrrrrZ psutil.testsrr r r r r rrrrrrrrrrrrrrrrrrr rdirname__file__HEREr.Z SIOCGIFCONFrXr6r8rZEMPTY_TEMPERATURESr5r7r9rPr[rjrnrorzcontextmanagerrrrrrrrrrr rr&r,r0r:rJrVrcrzrrrrrrrrCrZrZpsutil.tests.runnerr^r3r3r3r4s                               =7VaE )29C D)  CzT