U 1gD@sdZddlZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddlm Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZe rNddlZddlZddlm Z d,ddZ!ddZ"ddZ#ddZ$d d!Z%d"d#Z&ej'j(e d$d%Gd&d'd'eZ)ej'j(e d$d%Gd(d)d)eZ*ej'j(e d$d%Gd*d+d+eZ+dS)-zPOSIX specific tests.N)AIX)BSD)LINUX)MACOS)OPENBSD)POSIX)SUNOS)AARCH64)HAS_NET_IO_COUNTERS) PYTHON_EXE) QEMU_USER)PsutilTestCase)mock)pytest)retry_on_failure)sh)skip_on_access_denied)spawn_testproc) terminate)which) getpagesizec Csdg}tr|d|dk r0|dt|gnts8trD|dn |dtr^|dd}|d |gt|}tr|n|d d}g}|D]8}| }z t |}Wnt k rYnX||q|dkr|S|d SdS) zwWrapper for calling the ps command with a little bit of cross-platform support for a narrow range of features. psz --no-headersNz-pz-AZaxstartZstimez-or) rappendextendstrrrreplacer splitlinesstripint ValueError)fmtpidcmdoutput all_outputliner(G/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_posix.pyr/s0      rcCs>d}tr d}t||}tr6d|dks.t|dS|dS)NcommandZcommz /bin/qemu-rr)rrsplitr AssertionError)r#fieldr*r(r(r)ps_namegsr.cCs0d}ts trd}t||}tdd|}|S)Nr*argsz\(python.*?\)$)rrrresubr)r#r-outr(r(r)ps_argsrs  r4cCsd}tr d}t||S)NZrssZrssizerrr#r-r(r(r)ps_rss|sr7cCsd}tr d}t||S)NZvszZvsizer5r6r(r(r)ps_vszsr8c Csztd|}Wn>tk rR}z dt|kr@tdW5d}~XYnX|dd}|}t|dd}t|dd}t|dd}t |d  d d }||||fS) Nzdf -k %sz device busyzdf returned EBUSY r%r0) rr RuntimeErrorrlowerrskipr+r floatr) devicer3errr'fields sys_totalsys_usedsys_free sys_percentr(r(r)dfs rJz POSIX onlyreasonc@seZdZdZeddZeddZddZdd Zd d Z d d Z ddZ e e ddZe e ddZddZddZddZddZejjepeddddZd d!Ze d"d#Zejjed$dejjed%dd&d'Zd(S)) TestProcesszBCompare psutil results against 'ps' command line utility (mainly).cCsttddgtjdj|_dS)Nz-Ez-O)stdin)rr subprocessPIPEr#clsr(r(r) setUpClassszTestProcess.setUpClasscCst|jdSN)rr#rQr(r(r) tearDownClassszTestProcess.tearDownClasscCs,td|j}t|j}||ks(tdS)Nppid)rr#psutilProcessrVr,)selfZppid_psZ ppid_psutilr(r(r) test_ppids zTestProcess.test_ppidcCs.td|j}t|jj}||ks*tdS)Nuid)rr#rWrXuidsrealr,)rYZuid_psZ uid_psutilr(r(r)test_uids zTestProcess.test_uidcCs.td|j}t|jj}||ks*tdS)NZrgid)rr#rWrXZgidsr]r,)rYZgid_psZ gid_psutilr(r(r)test_gids zTestProcess.test_gidcCs,td|j}t|j}||ks(tdS)Nuser)rr#rWrXusernamer,)rYZ username_psZusername_psutilr(r(r) test_usernames zTestProcess.test_usernamec CsJt}tjdtd*}|t|jks2t |j sXsz,TestSystemAPIs.test_pids..csg|]}|kr|qSr(r(r) pids_psutilr(r)rXsz difference: ) sortedrrWZpidsrrinsertrfailr)rY differencer()rrr) test_pidsKs  zTestSystemAPIs.test_pidszunreliable on SUNOSrKifconfigzno ifconfig cmdz not supportedcCsLtd}tjddD]2}|D]}||r qq |d||fqdS)Nz ifconfig -aT)Zpernicz/couldn't find %s nic in 'ifconfig -a' output %s)rrWZnet_io_countersr+rr)rYr%Znicr'r(r(r)test_nic_names_s  zTestSystemAPIs.test_nic_namesc Cstd}|std|d}dd|D}dd|D}t|ttksXt|j t|dVt tD]B\}}|j ||kst|j ||kst|j dk rxt|j qxW5QRXdS)Nwho -uno users on this systemr9cSsg|]}|dqS)rr+rr(r(r)rvsz-TestSystemAPIs.test_users..cSsg|]}|dqS)rrrr(r(r)rwsrWZwho)rrrrAr+rrWusersr,subTest enumeraterrZterminalr#rX)rYr3linesrZ terminalsidxur(r(r) test_usersps   zTestSystemAPIs.test_usersc Cstd}|stdd}td|}|r4d}nNtd|}|rJd}n8td|}|r`d}n"td |}|rd}d d |D}|std ||jt|d @t tD],\}}t j |j  |}|||kstqW5QRXdS)Nrrz\d\d\d\d-\d\d-\d\d \d\d:\d\dz%Y-%m-%d %H:%Mz[A-Z][a-z][a-z] \d\d \d\d:\d\dz %b %d %H:%Mz[A-Z][a-z][a-z] \d\dz%b %dz[a-z][a-z][a-z] \d\dcSsg|] }|qSr() capitalizerr(r(r)rsz5TestSystemAPIs.test_users_started..z(cannot interpret tstamp in who output %sr)rrrrAr1findallrrWrrrrstartedrr,)rYr3ZtstamprrrZ psutil_valuer(r(r)test_users_starteds:     z!TestSystemAPIs.test_users_startedc CsVtjdttjdd6}tttj t W5QRX|j sHt W5QRXdS)Nzpsutil._psposix.os.killr0rc)rrdOSErrorerrnoEBADFrr~rW_psposixZ pid_existsrogetpidrfr,rYmr(r(r)test_pid_exists_let_raises  z(TestSystemAPIs.test_pid_exists_let_raisec CsVtjdttjdd6}tttj t W5QRX|j sHt W5QRXdS)Npsutil._psposix.os.waitpidr0rc)rrdrrrrr~rWrwait_pidrorrfr,rr(r(r)test_os_waitpid_let_raises  z(TestSystemAPIs.test_os_waitpid_let_raisec Cs^tjdttjdd>}ttjj tjj t ddW5QRX|j sPtW5QRXdS)Nrr0rcg{Gz?)timeout)rrdrrZEINTRrr~rWrTimeoutExpiredrrorrfr,rr(r(r)test_os_waitpid_eintrs z$TestSystemAPIs.test_os_waitpid_eintrc CsNtjddd6}tttjt W5QRX|j s@t W5QRXdS)Nr)rrw) rrdrr~r!rWrrrorrfr,rr(r(r)test_os_waitpid_bad_ret_statuss z-TestSystemAPIs.test_os_waitpid_bad_ret_statuszunreliable on AIXc Csd}tjddD]}t|j}zt|j\}}}}WnPtk r}z2t|}d|ksld|ksld|krtWYqW5d}~XYqXt |j ||kst t |j ||kst t |j ||kst t |j|dkst qdS)Ni@F)allzno such file or directoryzraw devices not supportedzpermission deniedr)rWZdisk_partitions disk_usageZ mountpointrJrCr?rr@abstotalr,usedfreepercent) rYZ tolerancepartusagerFrGrHrIrDr(r(r)test_disk_usages&  zTestSystemAPIs.test_disk_usageN)rrrrrrrrrrrr rrrrrrrrrr(r(r(r)rGs$   &    rc@seZdZddZdS)TestMisccCs4t}|dkst|tks"t|tjks0tdS)Nr)rr,resourcemmapPAGESIZE)rYZpagesizer(r(r)test_getpagesizes zTestMisc.test_getpagesizeN)rrrrr(r(r(r)rsr)N),rrrror1rOrjrWrrrrrrrZ psutil.testsr r r r r rrrrrrrrrrZpsutil._psutil_posixrrr.r4r7r8rJrrrMrrr(r(r(r)sZ                      8  -#