test_archive.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ## Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
  2. ## Use of this source code is governed by a BSD-style license that can be
  3. ## found in the COPYING file.
  4. """Automatic testing for handling archives
  5. """
  6. import os
  7. import sys
  8. import stat
  9. import errno
  10. import unittest
  11. import tempfile
  12. import qibuild
  13. class ArchiveTestCase(unittest.TestCase):
  14. def setUp(self):
  15. self.tmp = tempfile.mkdtemp(prefix="tmp-archive-test")
  16. def tearDown(self):
  17. qibuild.sh.rm(self.tmp)
  18. def test_zip_extract(self):
  19. # Create some files in the temp dir:
  20. src = os.path.join(self.tmp, "src")
  21. os.mkdir(src)
  22. # Create a empty dir called a, and two files named
  23. # b and c
  24. a = os.path.join(src, "a")
  25. os.mkdir(a)
  26. b = os.path.join(a, "b")
  27. with open(b, "w") as fp:
  28. fp.write("b\n")
  29. c = os.path.join(a, "c")
  30. with open(c, "w") as fp:
  31. fp.write("c\n")
  32. archive = qibuild.archive.zip(a)
  33. dest = os.path.join(self.tmp, "dest")
  34. os.mkdir(dest)
  35. qibuild.archive.extract(archive, dest)
  36. ls_r = qibuild.sh.ls_r(dest)
  37. self.assertEquals(ls_r, ["a/b", "a/c"])
  38. def test_zip_extract_ro(self):
  39. src = os.path.join(self.tmp, "src")
  40. os.mkdir(src)
  41. # Create a empty dir called a, and two files named
  42. # b and c
  43. a = os.path.join(src, "a")
  44. os.mkdir(a)
  45. ro = os.path.join(a, "ro")
  46. with open(ro, "w") as fp:
  47. fp.write("ro\n")
  48. # 200:
  49. os.chmod(ro, stat.S_IRUSR)
  50. archive = qibuild.archive.zip(a)
  51. dest = os.path.join(self.tmp, "dest")
  52. os.mkdir(dest)
  53. qibuild.archive.extract(archive, dest)
  54. ls_r = qibuild.sh.ls_r(dest)
  55. self.assertEquals(ls_r, ["a/ro"])
  56. dest_ro = os.path.join(dest, "a", "ro")
  57. # check that the dest is readonly:
  58. error = None
  59. try:
  60. open(dest_ro, "w")
  61. except IOError as e:
  62. error = e
  63. self.assertFalse(error is None)
  64. self.assertEquals(error.errno, errno.EACCES)
  65. def test_zip_extract_ro_dir(self):
  66. src = os.path.join(self.tmp, "src")
  67. os.mkdir(src)
  68. ro1 = os.path.join(src, "ro1")
  69. os.mkdir(ro1)
  70. ro2 = os.path.join(ro1, "ro2")
  71. os.mkdir(ro2)
  72. a = os.path.join(ro2, "a")
  73. with open(a, "w") as fp:
  74. fp.write("a\n")
  75. # RO dir inside an other RO dir
  76. os.chmod(ro2, stat.S_IRUSR | stat.S_IXUSR)
  77. os.chmod(ro1, stat.S_IRUSR | stat.S_IXUSR)
  78. archive = qibuild.archive.zip(src)
  79. dest = os.path.join(self.tmp, "dest")
  80. os.mkdir(dest)
  81. qibuild.archive.extract(archive, dest)
  82. ls_r = qibuild.sh.ls_r(dest)
  83. self.assertEquals(ls_r, ["src/ro1/ro2/a"])
  84. def test_extract_preserve_executables_from_zip(self):
  85. zip = qibuild.command.find_program("zip")
  86. if not zip:
  87. return
  88. src = os.path.join(self.tmp, "src")
  89. os.mkdir(src)
  90. a_exe = os.path.join(src, "a.exe")
  91. with open(a_exe, "w") as fp:
  92. fp.write("a_exe\n")
  93. st_700 = stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR
  94. os.chmod(a_exe, st_700)
  95. qibuild.command.call(["zip", "-r", "src.zip", "src"],
  96. cwd=self.tmp)
  97. archive = os.path.join(self.tmp, "src.zip")
  98. dest = os.path.join(self.tmp, "dest")
  99. os.mkdir(dest)
  100. qibuild.archive.extract_zip(archive, dest)
  101. dest_exe = os.path.join(dest, "src", "a.exe")
  102. st_mode = os.stat(dest_exe).st_mode
  103. self.assertEquals(st_mode, 100700)
  104. def test_extract_change_topdir(self):
  105. src = os.path.join(self.tmp, "src")
  106. os.mkdir(src)
  107. a_long_dir = os.path.join(src, "a_long_dir")
  108. os.mkdir(a_long_dir)
  109. b = os.path.join(a_long_dir, "b")
  110. with open(b, "w") as fp:
  111. fp.write("b\n")
  112. dest = os.path.join(self.tmp, "dest")
  113. os.mkdir(dest)
  114. tar_gz = qibuild.archive.zip_unix(a_long_dir)
  115. qibuild.archive.extract(tar_gz, dest, topdir="a")
  116. a = os.path.join(dest, "a")
  117. ls_r = qibuild.sh.ls_r(a)
  118. self.assertEquals(ls_r, ["b"])
  119. a_zip = qibuild.archive.zip_win(a_long_dir)
  120. qibuild.archive.extract(a_zip, dest, topdir="aa")
  121. aa = os.path.join(dest, "aa")
  122. ls_r = qibuild.sh.ls_r(aa)
  123. self.assertEquals(ls_r, ["b"])
  124. def test_extract_change_topdir_already_correct(self):
  125. src = os.path.join(self.tmp, "src")
  126. os.mkdir(src)
  127. a_dir = os.path.join(src, "a")
  128. os.mkdir(a_dir)
  129. tar_gz = qibuild.archive.zip_unix(a_dir)
  130. dest = os.path.join(self.tmp, "dest")
  131. qibuild.archive.extract(tar_gz, dest, topdir="a")
  132. ls_r = qibuild.sh.ls_r(dest)
  133. self.assertEquals(ls_r, ["a/"])
  134. def test_extract_with_symlink(self):
  135. if sys.platform.startswith("win"):
  136. return
  137. src = os.path.join(self.tmp, "src")
  138. os.mkdir(src)
  139. a_dir = os.path.join(src, "a_dir")
  140. os.mkdir(a_dir)
  141. a_file = os.path.join(a_dir, "a_file")
  142. with open(a_file, "w") as fp:
  143. fp.write("a_file\n")
  144. a_link = os.path.join(a_dir, "a_link")
  145. os.symlink("a_file", a_link)
  146. tar_gz = qibuild.archive.zip_unix(a_dir)
  147. dest = os.path.join(self.tmp, "dest")
  148. os.mkdir(dest)
  149. qibuild.archive.extract(tar_gz, dest)
  150. ls_r = qibuild.sh.ls_r(dest)
  151. self.assertEquals(ls_r,
  152. ['a_dir/a_file', 'a_dir/a_link'])
  153. dest_link = os.path.join(dest, "a_dir", "a_link")
  154. self.assertTrue(os.path.islink(dest_link))
  155. dest_target = os.readlink(dest_link)
  156. self.assertEquals(dest_target, "a_file")
  157. def test_extract_with_symlink_and_change_topdir(self):
  158. if sys.platform.startswith("win"):
  159. return
  160. src = os.path.join(self.tmp, "src")
  161. os.mkdir(src)
  162. a_long_dir = os.path.join(src, "a_long_dir")
  163. os.mkdir(a_long_dir)
  164. a_file = os.path.join(a_long_dir, "a_file")
  165. with open(a_file, "w") as fp:
  166. fp.write("a_file\n")
  167. a_link = os.path.join(a_long_dir, "a_link")
  168. os.symlink("a_file", a_link)
  169. tar_gz = qibuild.archive.zip_unix(a_long_dir)
  170. dest = os.path.join(self.tmp, "dest")
  171. os.mkdir(dest)
  172. qibuild.archive.extract(tar_gz, dest, topdir="a_dir")
  173. ls_r = qibuild.sh.ls_r(dest)
  174. self.assertEquals(ls_r,
  175. ['a_dir/a_file', 'a_dir/a_link'])
  176. dest_link = os.path.join(dest, "a_dir", "a_link")
  177. self.assertTrue(os.path.islink(dest_link))
  178. dest_target = os.readlink(dest_link)
  179. self.assertEquals(dest_target, "a_file")
  180. if __name__ == "__main__":
  181. unittest.main()