3 ̪e2@sdZddlZddlZddlZddlZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZGdddeZGdddeZGdddeZGdd d eZ Gd!d"d"eZ!e"d#krdd$l#m$Z$e$e%dS)%zContracts tests. These tests mainly check API sanity in terms of returned types and APIs availability. Some of these are duplicates of tests test_system.py and test_process.py. N)AIX)FREEBSD)LINUX)MACOS)NETBSD)OPENBSD)POSIX)SUNOS)WINDOWS)long)GITHUB_ACTIONS) HAS_CPU_FREQ)HAS_NET_IO_COUNTERS)HAS_SENSORS_FANS)HAS_SENSORS_TEMPERATURES)PYPY) SKIP_SYSCONS)PsutilTestCase)create_sockets)enum) is_namedtuple)kernel_versionc@sDeZdZddZddZddZddZeje o2e d d d Z d S) TestAvailConstantsAPIscCs|jttdtptptdS)N PROCFS_PATH) assertEqualhasattrpsutilrr r)selfrG/usr/local/lib64/python3.6/site-packages/psutil/tests/test_contracts.pytest_PROCFS_PATH2sz'TestAvailConstantsAPIs.test_PROCFS_PATHcCsj|j}|ttdt|ttdt|ttdt|ttdt|ttdt|ttdtdS)NZABOVE_NORMAL_PRIORITY_CLASSZBELOW_NORMAL_PRIORITY_CLASSZHIGH_PRIORITY_CLASSZIDLE_PRIORITY_CLASSZNORMAL_PRIORITY_CLASSZREALTIME_PRIORITY_CLASS)rrrr )raerrrtest_win_priority5sz(TestAvailConstantsAPIs.test_win_prioritycCsJ|j}|ttdt|ttdt|ttdt|ttdtdS)NZIOPRIO_CLASS_NONEZIOPRIO_CLASS_RTZIOPRIO_CLASS_BEZIOPRIO_CLASS_IDLE)rrrr)rr!rrrtest_linux_ioprio_linux>s z.TestAvailConstantsAPIs.test_linux_ioprio_linuxcCsJ|j}|ttdt|ttdt|ttdt|ttdtdS)NZ IOPRIO_HIGHZ IOPRIO_NORMALZ IOPRIO_LOWZIOPRIO_VERYLOW)rrrr )rr!rrrtest_linux_ioprio_windowsEs z0TestAvailConstantsAPIs.test_linux_ioprio_windowsz%unsupported on GITHUB_ACTIONS + LINUXcCs|j}|ttdtpt|ttdtp*t|ttdtp>t|ttdtpRt|ttdtpft|ttdtpzt|ttdtpt|ttdtpt|ttd tpt|ttd tpt|ttd tpt|ttd ttrtdkr|ttdttdkr0|ttdttdkrL|ttdttdkrh|ttdttdkr|ttdt|ttdt|ttdt|ttdtdS)NZ RLIM_INFINITYZ RLIMIT_ASZ RLIMIT_COREZ RLIMIT_CPUZ RLIMIT_DATAZ RLIMIT_FSIZEZRLIMIT_MEMLOCKZ RLIMIT_NOFILEZ RLIMIT_NPROCZ RLIMIT_RSSZ RLIMIT_STACKZ RLIMIT_LOCKSZRLIMIT_MSGQUEUE Z RLIMIT_NICEZ RLIMIT_RTPRIOZ RLIMIT_RTTIMEZRLIMIT_SIGPENDINGZ RLIMIT_SWAPZ RLIMIT_SBSIZEZ RLIMIT_NPTS)r%r&r')r%r&r()r%r&r()r%r&r))r%r&r')rrrrrrr)rr!rrr test_rlimitLs6     z"TestAvailConstantsAPIs.test_rlimitN) __name__ __module__ __qualname__r r"r#r$unittestskipIfr rr*rrrrr1s  rc@s<eZdZddZddZddZddZd d Zd d Zd S)TestAvailSystemAPIscCs|jttdtdS)NZwin_service_iter)rrrr )rrrrtest_win_service_iterpsz)TestAvailSystemAPIs.test_win_service_itercCs|jttdtdS)NZwin_service_get)rrrr )rrrrtest_win_service_getssz(TestAvailSystemAPIs.test_win_service_getcCs&|jttdtptptptptdS)Ncpu_freq)rrrrrr rr)rrrr test_cpu_freqvsz!TestAvailSystemAPIs.test_cpu_freqcCs|jttdtptdS)Nsensors_temperatures)rrrrr)rrrrtest_sensors_temperatures|sz-TestAvailSystemAPIs.test_sensors_temperaturescCs|jttdtdS)N sensors_fans)rrrr)rrrrtest_sensors_fanssz%TestAvailSystemAPIs.test_sensors_fanscCs"|jttdtptptptdS)NZsensors_battery)rrrrr rr)rrrr test_batterysz TestAvailSystemAPIs.test_batteryN) r+r,r-r1r2r4r6r8r9rrrrr0os r0c@s|eZdZddZddZddZddZd d Zej e o:e d d d Z ddZ ddZddZddZddZddZdS)TestAvailProcessAPIscCs4|jttjdtp,tp,tp,tp,tp,t p,t p,t dS)Nenviron) rrrProcessrrr rr rrr)rrrr test_environs z!TestAvailProcessAPIs.test_environcCs|jttjdtdS)Nuids)rrrr<r)rrrr test_uidsszTestAvailProcessAPIs.test_uidscCs|jttjdtdS)Nr>)rrrr<r)rrrr test_gidsszTestAvailProcessAPIs.test_gidscCs|jttjdtdS)Nterminal)rrrr<r)rrrr test_terminalsz"TestAvailProcessAPIs.test_terminalcCs|jttjdtptdS)Nionice)rrrr<rr )rrrr test_ionicesz TestAvailProcessAPIs.test_ionicez%unsupported on GITHUB_ACTIONS + LINUXcCs|jttjdtptdS)NZrlimit)rrrr<rr)rrrrr*sz TestAvailProcessAPIs.test_rlimitcCs"ttjd}|j|tpt dS)NZ io_counters)rrr<rrr )rhasitrrrtest_io_counterss z%TestAvailProcessAPIs.test_io_counterscCs|jttjdtdS)NZnum_fds)rrrr<r)rrrr test_num_fdssz!TestAvailProcessAPIs.test_num_fdscCs|jttjdtdS)NZ num_handles)rrrr<r )rrrrtest_num_handlessz%TestAvailProcessAPIs.test_num_handlescCs |jttjdtptptdS)NZ cpu_affinity)rrrr<rr r)rrrrtest_cpu_affinitys z&TestAvailProcessAPIs.test_cpu_affinitycCs |jttjdtptptdS)NZcpu_num)rrrr<rrr )rrrr test_cpu_numsz!TestAvailProcessAPIs.test_cpu_numcCs*ttjd}|j|tp tp tp t dS)NZ memory_maps)rrr<rrrrr)rrErrrtest_memory_mapss z%TestAvailProcessAPIs.test_memory_mapsN)r+r,r-r=r?r@rBrDr.r/r rr*rFrGrHrIrJrKrrrrr:s r:c@seZdZdZeddZedfddZddZd d Z d d Z d dZ e j eoXejdkde j e dddZddZddZe j edddZddZddZe j e ddd Ze j e dd!d"Ze j e dd#d$Zd%d&Zd'd(Z d)S)*TestSystemAPITypeszCheck the return types of system related APIs. Mainly we want to test we never return unicode on Python 2, see: https://github.com/giampaolo/psutil/issues/1039. cCstj|_dS)N)rr<proc)clsrrr setUpClassszTestSystemAPITypes.setUpClassTcCs:t|s tx(|D] }|j|||r|j|dqWdS)Nr)rAssertionErrorassertIsInstanceZassertGreaterEqual)rnttype_Zgezeronrrrassert_ntuple_of_numss    z(TestSystemAPITypes.assert_ntuple_of_numscCs2|jtjxtjddD]}|j|qWdS)NT)Zpercpu)rUrZ cpu_times)rrRrrrtest_cpu_timessz!TestSystemAPITypes.test_cpu_timescCs,|jtjddt|jtjddtdS)N)intervalgh㈵>)rQrZ cpu_percentfloat)rrrrtest_cpu_percentsz#TestSystemAPITypes.test_cpu_percentcCs(|jtjdd|jtjdddS)N)rWg-C6?)rUrZcpu_times_percent)rrrrtest_cpu_times_percentsz)TestSystemAPITypes.test_cpu_times_percentcCs|jtjtdS)N)rQr cpu_countint)rrrrtest_cpu_countsz!TestSystemAPITypes.test_cpu_countarm64zskipped due to #1892z not supportedcCs2tjdkr|jd|jtjtttfddS)Nzcpu_freq() returns None)rS)rr3ZskipTestrUrXr\r )rrrrr4s  z TestSystemAPITypes.test_cpu_freqcCs@x:tjddjD]&\}}|j|t|j|ttfdqWdS)NT)Zperdisk)rS)rZdisk_io_countersitemsrQstrrUr\r )rkvrrrtest_disk_io_counterss z(TestSystemAPITypes.test_disk_io_counterscCszxttjD]h}|j|jt|j|jt|j|jt|j|jt|j|jt t df|j|j t t dfq WdS)N) rZdisk_partitionsrQdevicer` mountpointfstypeoptsmaxfiler\typemaxpath)rZdiskrrrtest_disk_partitionssz'TestSystemAPITypes.test_disk_partitionsz requires rootc CsRtBtjd}|jt|tt|x|D]}t|s0tq0WWdQRXdS)Nall)rrZnet_connectionsrlensetrrP)rretconnrrrtest_net_connectionss   z'TestSystemAPITypes.test_net_connectionscCsxtjjD]\}}|j|txt|D]l}tdk rLt rL|j|jtjn|j|jt |j|j t|j|j tt df|j|j tt dfq(WqWdS)N)rZ net_if_addrsr_rQr`rrfamilyIntEnumr\addressnetmaskri broadcast)rifnameaddrsaddrrrrtest_net_if_addrs s  z$TestSystemAPITypes.test_net_if_addrscCs|xvtjjD]f\}}|j|t|j|jttdk rJ|j|jtj n|j|jt |j|j t |j|j t qWdS)N) rZ net_if_statsr_rQr`isupboolrduplexrsr\speedmtu)rrwinforrrtest_net_if_statss z$TestSystemAPITypes.test_net_if_statscCs&x tjddD]}|j|tqWdS)NT)Zpernic)rZnet_io_countersrQr`)rrwrrrtest_net_io_counters&sz'TestSystemAPITypes.test_net_io_counterscCs^xXtjjD]H\}}|j|tx2|D]*}|j|jt|j|jttt dfq(WqWdS)N) rr7r_rQr`labelcurrentrXr\ri)rnameunitsunitrrrr8,s   z$TestSystemAPITypes.test_sensors_fanscCsxtjjD]x\}}|j|txb|D]Z}|j|jt|j|jttt df|j|j ttt df|j|j ttt dfq(WqWdS)N) rr5r_rQr`rrrXr\rihighcritical)rrrrrrrr65s  z,TestSystemAPITypes.test_sensors_temperaturescCs|jtjtdS)N)rQrZ boot_timerX)rrrrtest_boot_time@sz!TestSystemAPITypes.test_boot_timecCsfx`tjD]T}|j|jt|j|jttdf|j|jttdf|j|jt tdfq WdS)N) rZusersrQrr`rArihostpidr\)ruserrrr test_usersDs zTestSystemAPITypes.test_usersN)!r+r,r-__doc__ classmethodrOrXrUrVrYrZr]r.r/rplatformmachiner r4rcrkrrqrzrrrrr8rr6rrrrrrrLs(      rLc@s"eZdZeje dddZdS)TestProcessWaitTypez not POSIXcCsXtj|jj}|j|j}|j|tj t dk rH|j |t j n |j |t dS)N) rr<Zspawn_testprocr terminatewaitrsignalSIGTERMrrQrsr\)rpcoderrrtest_negative_signalNsz(TestProcessWaitType.test_negative_signalN)r+r,r-r.r/rrrrrrrMsr__main__) run_from_name)&rrrr.rrrrrrrrr r Zpsutil._compatr Z psutil.testsr r rrrrrrrrrrrr0r:rLrr+Zpsutil.tests.runnerr__file__rrrr sF                      >?