
    &                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKr	SSK
r
SSKrSSKrSSKrSSKrSSKrSSKJs  Jr  SSKJr  SS	KJr  SS
KJr  SSKJr  SrS rS r " S S\R8                  5      rg)4Base test case class for unit and integration tests.    )absolute_import)print_function)division)unicode_literals)wrapsN)unittest)UTF8)NA_ID)NA_MODE?   c                      [        U SS5        U $ )z.Wrapper for cases that are not parallelizable.is_parallelizableF)setattr)tests    ,platform/gsutil/gslib/tests/testcase/base.pyNotParallelizabler   )   s     
$#U+	+    c                 >   ^  [        T 5      U 4S j5       nSUl        U$ )zFWrapper function for cases that require running in a separate process.c                     > T" U 0 UD6$ N )argskwargsfuncs     r   RequiresIsolationFunc0RequiresIsolation.<locals>.RequiresIsolationFunc3   s       r   T)r   requires_isolation)r   r   s   ` r   RequiresIsolationr   0   s,     	;! ! .2*	r   c                       \ rS rSrSrS rS rS rS rS r	SS jr
SS	 jrSS jrSS jrS
S
S
S
\\\4S jrSS jrSrg
)GsUtilTestCase;   r   c                 Z   [         R                  (       a"  U R                  U l        U R                  U l        [        R                  (       a  SU l        SU l	        SU l
        OB[        R                  R                  SSS5      R                  5       U l        SU l	        SU l
        / U l        g )	NXMLs3amzGSUtil
prefer_apiJSONgsgoog)sixPY2assertRegexpMatchesassertRegexassertNotRegexpMatchesassertNotRegexutilRUN_S3_TESTStest_apidefault_providerprovider_custom_metabotoconfiggetuppertempdirsselfs    r   setUpGsUtilTestCase.setUp>   s}    
ww11d 77ddm"d"'dkkoohfEKKMdm"d"(dDMr   c                     U R                   (       aC  U R                   R                  5       n[        R                  " USS9  U R                   (       a  MB  g g )NT)ignore_errors)r;   popshutilrmtree)r=   tmpdirs     r   tearDownGsUtilTestCase.tearDownL   s4    
--}}  "fmmF$/ ---r   c                 F    U R                  UR                  S5      U5        g )N
)assertEqualcount)r=   textnumliness      r   assertNumLinesGsUtilTestCase.assertNumLinesQ   s    TZZ%x0r   c                     U R                   $ r   )_testMethodNamer<   s    r   GetTestMethodName GsUtilTestCase.GetTestMethodNameT   s    r   c                 4    S[         R                  " S5      -  $ )z<Creates a random string of hex characters 8 characters long.z%08xl        )random	randranger<   s    r   MakeRandomTestString#GsUtilTestCase.MakeRandomTestStringW   s    F$$V,,,r   c                 ^   SR                  X R                  5       US9nUS[        S-
   nSR                  X@R                  5       S9n[	        U5      [	        U5      -   nU(       a+  US:X  a!  U[        :  a  U R                  SU-   U-   5        XC-  nUS:X  a  [        R                  " U5      nU$ )	a;  Creates a temporary name that is most-likely unique.

Args:
  kind (str): A string indicating what kind of test name this is.
  prefix (str): Prefix prepended to the temporary name.
  suffix (str): Suffix string appended to end of temporary name.

Returns:
  (str) The temporary name. If `kind` was "bucket", the temporary name may
  have coerced this string, including the supplied `prefix`, such that it
  contains only characters that are valid across all supported storage
  providers (e.g. replacing "_" with "-", converting uppercase letters to
  lowercase, etc.).
z#{prefix}gsutil-test-{method}-{kind})prefixmethodkindN   z{name}-{rand})namerandbucketzTried to create a psuedo-random bucket name with a specific suffix, but the generated name was too long and there was not enough room for the suffix. Please use shorter strings or perform name randomization manually.
Requested name: )formatrR   MAX_BUCKET_LENGTHrW   lenfailr2   MakeBucketNameValid)r=   r\   rZ   suffixr^   total_name_lens         r   MakeTempNameGsUtilTestCase.MakeTempName[   s     177446T 8 CD'"R'(D!!t2K2K2M!NDYV,N		n/@@		= @DD GMM	N
 ndx%%d+dKr   c                 &    U R                  XS9S-   $ )NrZ   u   材)rh   )r=   r\   rZ   s      r   MakeTempUnicodeName"GsUtilTestCase.MakeTempUnicodeName}   s    T1E99r   Nc                    [         R                  " U R                  S5      S9nU R                  R	                  U5         [        U5        [        U5       H.  u  pVUnUc  SU-  R                  S5      nU R                  X6US9  M0     U$ ! [         a2    [        U5       Vs/ s H  o@R                  S5      PM     Os  snf nn Nzf = f)aN  Creates a temporary directory on disk.

The directory and all of its contents will be deleted after the test.

Args:
  test_files: The number of test files to place in the directory or a list
              of test file names.
  contents: The contents for each generated test file.

Returns:
  The path to the new temporary directory.
	directoryrk   fileztest %dascii)rE   	file_namecontents)tempfilemkdtemprh   r;   appenditer	TypeErrorrange	enumerateencodeCreateTempFile)r=   
test_filesrs   rE   _ir^   contents_files           r   CreateTempDirGsUtilTestCase.CreateTempDir   s     T%6%6{%CDFMM J
: Z(m		"Q..w7
-P	 )
 M  J7<Z7HI7H!%%f-7HIjIjJs    B
 
C!B<;CCc                 X   U=(       d    U R                  5       nU=(       d    U R                  S5      n[        U[        R                  5      (       a   [
        R                  R                  X5      nO![
        R                  R                  " U/UQ76 n[
        R                  " U5        U$ )a  Creates a temporary fifo file on disk. Should not be used on Windows.

Args:
  tmpdir: The temporary directory to place the file in. If not specified, a
      new temporary directory is created.
  file_name: The name to use for the file. If not specified, a temporary
      test file name is constructed. This can also be a tuple, where
      ('dir', 'foo') means to create a file named 'foo' inside a
      subdirectory named 'dir'.

Returns:
  The path to the new temporary fifo.
fifo)	r   rh   
isinstancer,   string_typesospathjoinmkfifo)r=   rE   rr   fpaths       r   CreateTempFifoGsUtilTestCase.CreateTempFifo   sy     +t))+F6T..v6I)S--..ggll6-eggll6.I.eIIeLr   c                 f   [         R                  " U=(       d    U R                  5       5      nU=(       d    U R                  [	        S5      5      n[        U[         R                  [         R                  45      (       a5  [        R                  R                  U[         R                  " U5      5      nO;[        [         R                  U5      n[        R                  R                  " U/UQ76 n[        R                  R                  [        R                  R                  U5      5      (       d3  [        R                  " [        R                  R                  U5      5        [        U[         R                  5      (       a  UR                  [         5      n[#        US5       n	Ub  UOU R                  [	        S5      5      n[        U[$        5      (       a  ['        U5      nO[         R(                  " U5      nU	R+                  U5        SSS5        Ub  [        R,                  " XU45        U[.        :w  d  [1        U5      [.        :w  a   [        R2                  " X[1        U5      5        [1        U5      [4        :w  a!  [        R6                  " U[1        US5      5        U$ ! , (       d  f       N= f)at  Creates a temporary file on disk.

Note: if mode, uid, or gid are present, they must be validated by
ValidateFilePermissionAccess and ValidatePOSIXMode before calling this
function.

Args:
  tmpdir: The temporary directory to place the file in. If not specified, a
          new temporary directory is created.
  contents: The contents to write to the file. If not specified, a test
            string is constructed and written to the file. Since the file
            is opened 'wb', the contents must be bytes.
  file_name: The name to use for the file. If not specified, a temporary
             test file name is constructed. This can also be a tuple, where
             ('dir', 'foo') means to create a file named 'foo' inside a
             subdirectory named 'dir'.
  mtime: The modification time of the file in POSIX time (seconds since
         UTC 1970-01-01). If not specified, this defaults to the current
         system time.
  mode: The POSIX mode for the file. Must be a base-8 3-digit integer
        represented as a string.
  uid: A POSIX user ID.
  gid: A POSIX group ID.

Returns:
  The path to the new temporary file.
rp   wbNrs      )r,   
ensure_strr   rh   strr   	text_typebinary_typer   r   r   mapisdirdirnamemakedirsdecoder
   open	bytearraybytesensure_binarywriteutimer   intchownr   chmod)
r=   rE   rs   rr   mtimemodeuidgidr   fs
             r   r|   GsUtilTestCase.CreateTempFile   s   H ^^F:d&8&8&:;F;T..s6{;I)cmmS__=>>ggll63>>)#<=ecnni0iggll6.I.e77==/00kk"''//%()%))ll4 e	eT	a&2(8I8I
j/9h	Hi	(	(?$$X.ggh 
 hhuen%
e|s3x5(hhu3s8$
4yGhhuc$l#L 
	s   $A(J""
J0c                 V   [        U[        R                  5      (       a  [        R                  " X$S9nO,[        R                  " UR
                  UR                  U-  S9nUR                  U5      (       d3  U=(       d    SnU< SUR
                  < SU< 3nU R                  U5      eg)aJ  Like assertRegexpMatches, but allows specifying additional re flags.

Args:
  text: The text in which to search for pattern.
  pattern: The pattern to search for; should be either a string or compiled
      regex returned from re.compile().
  msg: The message to be displayed if pattern is not found in text. The
      values for pattern and text will be included after this message.
  flags: Additional flags from the re module to be included when compiling
      pattern. If pattern is a regex that was compiled with existing flags,
      these, flags will be added via a bitwise-or.
)flagszRegex didn't matchz: z not found in N)	r   r,   r   recompilepatternr   searchfailureException)r=   rL   r   msgr   failure_msgs         r   assertRegexpMatchesWithFlags+GsUtilTestCase.assertRegexpMatchesWithFlags   s     '3++,,

70g

7??'--%2GHg>>$00k0;W__046k!!+..	  r   )r1   r/   r5   r6   r;   r4   ) r   )r   )r   N)NN)Nr   )__name__
__module____qualname____firstlineno____doc__r>   rF   rN   rR   rW   rh   rl   r   r   r   r   r|   r   __static_attributes__r   r   r   r!   r!   ;   sU    <0
1 -D:40 !"#!?B/r   r!   )r   
__future__r   r   r   r   	functoolsr   os.pathr   rU   r   rC   rt   r,   r7   gslib.tests.utiltestsr2   r	   gslib.utils.constantsr
   gslib.utils.posix_utilr   r   rb   r   r   TestCaser!   r   r   r   <module>r      sd    ; & %  '    	   
    % & ( * L/X&& L/r   