
    g!1                        d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  G d de      Z G d de      Z G d d e      Z  G d! d"e      Z! G d# d$e      Z"y)%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)	QEMU_USER)SKIP_SYSCONS)PsutilTestCase)create_sockets)enum)is_namedtuple)kernel_version)pytestc                   r    e Zd Zd Zd Zd Zd Zej                  j                  e
xr ed      d        Zy)	TestAvailConstantsAPIsc                 V    t        t        d      t        xs t        xs t        k(  sJ y )NPROCFS_PATH)hasattrpsutilr   r
   r   selfs    P/var/www/openai/venv/lib/python3.12/site-packages/psutil/tests/test_contracts.pytest_PROCFS_PATHz'TestAvailConstantsAPIs.test_PROCFS_PATH3   s!    v}-%2G52GCHHH    c                 `   | j                   } |t        t        d      t                |t        t        d      t                |t        t        d      t                |t        t        d      t                |t        t        d      t                |t        t        d      t               y )NABOVE_NORMAL_PRIORITY_CLASSBELOW_NORMAL_PRIORITY_CLASSHIGH_PRIORITY_CLASSIDLE_PRIORITY_CLASSNORMAL_PRIORITY_CLASSREALTIME_PRIORITY_CLASSassertEqualr   r    r   r"   aes     r#   test_win_priorityz(TestAvailConstantsAPIs.test_win_priority6   sy    
76897C
76897C
76017;
76017;
7623W=
7645w?r%   c                     | j                   } |t        t        d      t                |t        t        d      t                |t        t        d      t                |t        t        d      t               y )NIOPRIO_CLASS_NONEIOPRIO_CLASS_RTIOPRIO_CLASS_BEIOPRIO_CLASS_IDLE)r.   r   r    r   r/   s     r#   test_linux_ioprio_linuxz.TestAvailConstantsAPIs.test_linux_ioprio_linux?   sU    
76./7
76,-u5
76,-u5
76./7r%   c                     | j                   } |t        t        d      t                |t        t        d      t                |t        t        d      t                |t        t        d      t               y )NIOPRIO_HIGHIOPRIO_NORMAL
IOPRIO_LOWIOPRIO_VERYLOWr-   r/   s     r#   test_linux_ioprio_windowsz0TestAvailConstantsAPIs.test_linux_ioprio_windowsF   sR    
76=)73
76?+W5
76<('2
76+,g6r%   %unsupported on GITHUB_ACTIONS + LINUXreasonc                    | j                   } |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d      t        xs t                |t        t        d	      t        xs t                |t        t        d
      t        xs t                |t        t        d      t        xs t                |t        t        d      t               t
        rt               dk\  r |t        t        d      t               t               dk\  r |t        t        d      t               t               dk\  r |t        t        d      t               t               dk\  r |t        t        d      t               t               dk\  r |t        t        d      t                |t        t        d      t                |t        t        d      t                |t        t        d      t               y )NRLIM_INFINITY	RLIMIT_ASRLIMIT_CORE
RLIMIT_CPURLIMIT_DATARLIMIT_FSIZERLIMIT_MEMLOCKRLIMIT_NOFILERLIMIT_NPROC
RLIMIT_RSSRLIMIT_STACKRLIMIT_LOCKS)         RLIMIT_MSGQUEUE)rN   rO      RLIMIT_NICERLIMIT_RTPRIO)rN   rO      RLIMIT_RTTIMERLIMIT_SIGPENDINGRLIMIT_SWAPRLIMIT_SBSIZERLIMIT_NPTS)r.   r   r    r   r   r	   r   r/   s     r#   test_rlimitz"TestAvailConstantsAPIs.test_rlimitM   s   
 
76?+U-=g>
76;')9':
76=)5+;G<
76<(%*:7;
76=)5+;G<
76>*E,<W=
76+,e.>w?
76?+U-=g>
76>*E,<W=
76<(%*:7;
76>*E,<W=
76>*E29,76#45u=:-76=159:-76?3U;:-76?3U;9,76#67?
76=)73
76?+W5
76=)73r%   N)__name__
__module____qualname__r$   r1   r7   r=   r   markskipifr   r   r[    r%   r#   r   r   2   sK    I@87 [[ 56  4	4r%   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)TestAvailSystemAPIsc                 6    t        t        d      t        k(  sJ y )Nwin_service_iterr   r    r   r!   s    r#   test_win_service_iterz)TestAvailSystemAPIs.test_win_service_iterr   s    v12g===r%   c                 6    t        t        d      t        k(  sJ y )Nwin_service_getrf   r!   s    r#   test_win_service_getz(TestAvailSystemAPIs.test_win_service_getu   s    v01W<<<r%   c                 v    t        t        d      t        xs t        xs t        xs t
        xs t        k(  sJ y )Ncpu_freq)r   r    r   r   r   r   r   r!   s    r#   test_cpu_freqz!TestAvailSystemAPIs.test_cpu_freqx   s2    vz*;U;g;;G
 	
 
r%   c                 F    t        t        d      t        xs t        k(  sJ y )Nsensors_temperatures)r   r    r   r   r!   s    r#   test_sensors_temperaturesz-TestAvailSystemAPIs.test_sensors_temperatures}   s    v565;KGLLLr%   c                 6    t        t        d      t        k(  sJ y )Nsensors_fans)r   r    r   r!   s    r#   test_sensors_fansz%TestAvailSystemAPIs.test_sensors_fans   s    v~.%777r%   c                 f    t        t        d      t        xs t        xs t        xs t
        k(  sJ y )Nsensors_battery)r   r    r   r   r   r   r!   s    r#   test_batteryz TestAvailSystemAPIs.test_battery   s.    v010W005
 	
 
r%   N)	r\   r]   r^   rg   rj   rm   rp   rs   rv   ra   r%   r#   rc   rc   q   s!    >=

M8
r%   rc   c                       e Zd Zd Zd Zd Zd Zd Zej                  j                  exr ed      d        Zd	 Zd
 Zd Zd Zd Zd Zy)TestAvailProcessAPIsc                     t        t        j                  d      t        xs6 t        xs. t
        xs& t        xs t        xs t        xs t        xs t        k(  sJ y )Nenviron)r   r    Processr   r   r   r   r
   r   r   r   r!   s    r#   test_environz!TestAvailProcessAPIs.test_environ   sd    v~~y1   	
   	
 		
 	
r%   c                 J    t        t        j                  d      t        k(  sJ y Nuidsr   r    r{   r	   r!   s    r#   	test_uidszTestAvailProcessAPIs.test_uids       v~~v.%777r%   c                 J    t        t        j                  d      t        k(  sJ y r~   r   r!   s    r#   	test_gidszTestAvailProcessAPIs.test_gids   r   r%   c                 J    t        t        j                  d      t        k(  sJ y )Nterminalr   r!   s    r#   test_terminalz"TestAvailProcessAPIs.test_terminal   s    v~~z2e;;;r%   c                 Z    t        t        j                  d      t        xs t        k(  sJ y )Nionice)r   r    r{   r   r   r!   s    r#   test_ionicez TestAvailProcessAPIs.test_ionice   s     v~~x0U5EgFFFr%   r>   r?   c                 Z    t        t        j                  d      t        xs t        k(  sJ y )Nrlimit)r   r    r{   r   r   r!   s    r#   r[   z TestAvailProcessAPIs.test_rlimit   s"    
 v~~x0U5EgFFFr%   c                 `    t        t        j                  d      }|t        xs t         k(  sJ y )Nio_counters)r   r    r{   r   r
   r"   hasits     r#   test_io_countersz%TestAvailProcessAPIs.test_io_counters   s'    6enu-...r%   c                 J    t        t        j                  d      t        k(  sJ y )Nnum_fdsr   r!   s    r#   test_num_fdsz!TestAvailProcessAPIs.test_num_fds   s    v~~y1U:::r%   c                 J    t        t        j                  d      t        k(  sJ y )Nnum_handles)r   r    r{   r   r!   s    r#   test_num_handlesz%TestAvailProcessAPIs.test_num_handles   s    v~~}5@@@r%   c                 j    t        t        j                  d      t        xs t        xs t
        k(  sJ y )Ncpu_affinity)r   r    r{   r   r   r   r!   s    r#   test_cpu_affinityz&TestAvailProcessAPIs.test_cpu_affinity   s,    v~~~6'W'
 	
 
r%   c                 j    t        t        j                  d      t        xs t        xs t
        k(  sJ y )Ncpu_num)r   r    r{   r   r   r
   r!   s    r#   test_cpu_numz!TestAvailProcessAPIs.test_cpu_num   s,    v~~y1%W%
 	
 
r%   c                     t        t        j                  d      }|t        xs t        xs t
        xs t         k(  sJ y )Nmemory_maps)r   r    r{   r   r   r   r   r   s     r#   test_memory_mapsz%TestAvailProcessAPIs.test_memory_maps   s2    6g??3?%@AAAr%   N)r\   r]   r^   r|   r   r   r   r   r   r_   r`   r   r   r[   r   r   r   r   r   r   ra   r%   r#   rx   rx      sq    

88<G [[ 56  G	G/;A



Br%   rx   c                   z   e Zd ZdZed        ZedfdZd Zd Z	d Z
d Zej                  j                  exr  ej"                         d	k(  d
      ej                  j                  e d      d               Zd Zd Zej                  j                  ed      d        Zd Zej                  j                  ed      d        Zej                  j                  e d      d        Zej                  j                  e d      d        Zej                  j                  e d      d        Z d Z!d Z"y)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.
    c                 6    t        j                         | _        y N)r    r{   proc)clss    r#   
setUpClasszTestSystemAPITypes.setUpClass   s    >>#r%   Tc                 \    t        |      sJ |D ]  }t        ||      sJ |s|dk\  rJ  y )Nr   )r   
isinstance)r"   nttype_gezerons        r#   assert_ntuple_of_numsz(TestSystemAPITypes.assert_ntuple_of_nums   s8    R   Aa'''Avv r%   c                     | j                  t        j                                t        j                  d      D ]  }| j                  |        y )NT)percpu)r   r    	cpu_times)r"   r   s     r#   test_cpu_timesz!TestSystemAPITypes.test_cpu_times   s<    ""6#3#3#56""$/B&&r* 0r%   c                     t        t        j                  d       t              sJ t        t        j                  d      t              sJ y )Nintervalgh㈵>)r   r    cpu_percentfloatr!   s    r#   test_cpu_percentz#TestSystemAPITypes.test_cpu_percent   s8    &,,d;UCCC&,,g>FFFr%   c                     | j                  t        j                  d              | j                  t        j                  d             y )Nr   g-C6?)r   r    cpu_times_percentr!   s    r#   test_cpu_times_percentz)TestSystemAPITypes.test_cpu_times_percent   s4    ""6#;#;T#JK""6#;#;V#LMr%   c                 L    t        t        j                         t              sJ y r   )r   r    	cpu_countintr!   s    r#   test_cpu_countz!TestSystemAPITypes.test_cpu_count   s    &**,c222r%   arm64zskipped due to #1892r?   znot supportedc                     t        j                         t        j                  d      | j	                  t        j                         t
        t        t        f       y )Nzcpu_freq() returns Noner   )r    rl   r   skipr   r   r   r   r!   s    r#   rm   z TestSystemAPITypes.test_cpu_freq   sB    
 ??$++788""6??#4UC<N"Or%   c                     t        j                  d      j                         D ]4  \  }}t        |t              sJ | j                  |t        t        f       6 y )NT)perdiskr   )r    disk_io_countersitemsr   strr   r   r   )r"   kvs      r#   test_disk_io_countersz(TestSystemAPITypes.test_disk_io_counters   sL    ++D9??ADAqa%%%&&qd&< Br%   c                    t        j                         D ]r  }t        |j                  t              sJ t        |j
                  t              sJ t        |j                  t              sJ t        |j                  t              rrJ  y r   )r    disk_partitionsr   devicer   
mountpointfstypeopts)r"   disks     r#   test_disk_partitionsz'TestSystemAPITypes.test_disk_partitions   se    **,Ddkk3///doos333dkk3///dii---	 -r%   zrequires rootc                     t               5  t        j                  d      }t        |      t        t	        |            k(  sJ |D ]  }t        |      rJ  	 d d d        y # 1 sw Y   y xY w)Nall)r   r    net_connectionslensetr   )r"   retconns      r#   test_net_connectionsz'TestSystemAPITypes.test_net_connections   sV    ((/Cs8s3s8},,,$T***  s   AA!A!!A*c           	         t        j                         j                         D ]  \  }}t        |t              sJ |D ]  }t
        ,t        s&t        |j                  t
        j                        sJ t        |j                  t              sJ t        |j                  t              sJ t        |j                  t        t        d       f      sJ t        |j                  t        t        d       f      rJ   y r   )r    net_if_addrsr   r   r   r   r   familyIntEnumr   addressnetmasktype	broadcast)r"   ifnameaddrsaddrs       r#   test_net_if_addrsz$TestSystemAPITypes.test_net_if_addrs  s    #00288:MFEfc***#D%dkk4<<@@@%dkk3777!$,,444!$,,d4j0ABBB!$..3T
2CDDD  ;r%   zQEMU user not supportedc                    t        j                         j                         D ]  \  }}t        |t              sJ t        |j
                  t              sJ t        &t        |j                  t        j                        sJ t        |j                  t              sJ t        |j                  t              sJ t        |j                  t              rJ  y r   )r    net_if_statsr   r   r   isupboolr   duplexr   r   speedmtu)r"   r   infos      r#   test_net_if_statsz$TestSystemAPITypes.test_net_if_stats  s     #//1779LFDfc***dii...!$++t||<<<!$++s333djj#...dhh,,, :r%   c                 ^    t        j                  d      D ]  }t        |t              rJ  y )NT)pernic)r    net_io_countersr   r   )r"   r   s     r#   test_net_io_countersz'TestSystemAPITypes.test_net_io_counters"  s)     ,,D9Ffc*** :r%   c           
         t        j                         j                         D ]f  \  }}t        |t              sJ |D ]J  }t        |j
                  t              sJ t        |j                  t        t        t        d       f      rJJ  h y r   )
r    rr   r   r   r   labelcurrentr   r   r   r"   nameunitsunits       r#   rs   z$TestSystemAPITypes.test_sensors_fans(  sq     "..0668KD%dC(((!$**c222!$,,T$Z0HIII  9r%   c           
         t        j                         j                         D ]  \  }}t        |t              sJ |D ]  }t        |j
                  t              sJ t        |j                  t        t        t        d       f      sJ t        |j                  t        t        t        d       f      sJ t        |j                  t        t        t        d       f      rJ   y r   )r    ro   r   r   r   r   r   r   r   r   highcriticalr   s       r#   rp   z,TestSystemAPITypes.test_sensors_temperatures1  s     "668>>@KD%dC(((!$**c222!$,,T$Z0HIII!$))eS$t*-EFFF!$--%d4j1IJJJ	  Ar%   c                 L    t        t        j                         t              sJ y r   )r   r    	boot_timer   r!   s    r#   test_boot_timez!TestSystemAPITypes.test_boot_time<  s    &**,e444r%   c                 X   t        j                         D ]  }t        |j                  t              sJ t        |j
                  t        t        d       f      sJ t        |j                  t        t        d       f      sJ t        |j                  t        t        d       f      rJ  y r   )
r    usersr   r   r   r   r   hostpidr   )r"   users     r#   
test_userszTestSystemAPITypes.test_users@  s}    LLNDdii---dmmc4:->???dii#tDz):;;;dhhd4j(9:::	 #r%   N)#r\   r]   r^   __doc__classmethodr   r   r   r   r   r   r   r   r_   r`   r   platformmachiner   rm   r   r   r   r   r   r   r   r   r   r   rs   r   rp   r  r  ra   r%   r#   r   r      s   
 $ $ /4D +
GN3 [[/"(""$/8N   [[L(AP BP
=. [[_=+ >+E [[	*CD
- E
- [[//H+ I+
 [[,,_EJ FJ [[44_MK NK5;r%   r   c                   T    e Zd Zej                  j                  e d      d        Zy)TestProcessWaitTypez	not POSIXr?   c                 4   t        j                  | j                         j                        }|j	                          |j                         }|t        j                   k(  sJ t        t        |t        j                        sJ y t        |t              sJ y r   )r    r{   spawn_testprocr  	terminatewaitsignalSIGTERMr   r   r   r   )r"   pcodes      r#   test_negative_signalz(TestProcessWaitType.test_negative_signalJ  sq    NN4..0445	vvx&&&dDLL111dC(((r%   N)r\   r]   r^   r   r_   r`   r	   r  ra   r%   r#   r  r  I  s*    [[E	+6) 7)r%   r  )#r  r
  r  r    r   r   r   r   r   r   r	   r
   r   psutil._compatr   psutil.testsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rc   rx   r   r  ra   r%   r#   <module>r     s   
              ' % , ) 1  " % ' '  & ' <4^ <4~
. 
06B> 6B|; ;D
). 
)r%   