test_archive.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from datetime import datetime
  17. import os
  18. import shutil
  19. import tarfile
  20. import tempfile
  21. import unittest
  22. import pytest
  23. from elyra.util.archive import create_temp_archive
  24. class ArchiveTestCase(unittest.TestCase):
  25. temp_dir = tempfile.gettempdir()
  26. test_timestamp = datetime.now().strftime("%m%d%H%M%S")
  27. test_dir_name = "test_" + test_timestamp
  28. test_dir = None
  29. test_files = ["a.py", "b.py", "c.json", "d.txt", "e.ipynb"]
  30. def setUp(self):
  31. """
  32. Setup a temp folder with some files to be used
  33. during test cases
  34. """
  35. # create test files
  36. self.test_dir = tempfile.mkdtemp(self.test_dir_name)
  37. self._create_test_files(self.test_dir)
  38. def tearDown(self):
  39. if os.path.exists(self.test_dir):
  40. shutil.rmtree(self.test_dir)
  41. def test_archive_all(self):
  42. test_archive_name = "all-" + self.test_timestamp + ".tar.gz"
  43. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=["*"])
  44. self.assertArchivedContent(archive_path, self.test_files)
  45. def test_archive_empty_filter(self):
  46. test_archive_name = "all-" + self.test_timestamp + ".tar.gz"
  47. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=[])
  48. self.assertArchivedFileCount(archive_path, 0)
  49. def test_archive_no_filter(self):
  50. test_archive_name = "all-" + self.test_timestamp + ".tar.gz"
  51. archive_path = create_temp_archive(test_archive_name, self.test_dir)
  52. self.assertArchivedFileCount(archive_path, 0)
  53. def test_archive_by_filter(self):
  54. test_archive_name = "python-" + self.test_timestamp + ".tar.gz"
  55. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=["*.py"])
  56. self.assertArchivedContent(archive_path, ["a.py", "b.py"])
  57. def test_archive_by_sequence_wildcard(self):
  58. test_archive_name = "python-" + self.test_timestamp + ".tar.gz"
  59. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=["[ab].*"])
  60. self.assertArchivedContent(archive_path, ["a.py", "b.py"])
  61. def test_archive_by_excluded_sequence(self):
  62. test_archive_name = "python-" + self.test_timestamp + ".tar.gz"
  63. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=["*[!b].py"])
  64. self.assertArchivedContent(archive_path, ["a.py"])
  65. def test_archive_multiple_filters(self):
  66. test_archive_name = "multiple-" + self.test_timestamp + ".tar.gz"
  67. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=["*.json", "*.txt"])
  68. self.assertArchivedContent(archive_path, ["c.json", "d.txt"])
  69. def test_archive_require_complete(self):
  70. test_archive_name = "multiple-" + self.test_timestamp + ".tar.gz"
  71. archive_path = create_temp_archive(
  72. test_archive_name, self.test_dir, filenames=["*.json", "*.txt", "a.py"], require_complete=True
  73. )
  74. self.assertArchivedContent(archive_path, ["c.json", "d.txt", "a.py"])
  75. def test_archive_require_complete_fail(self):
  76. test_archive_name = "multiple-" + self.test_timestamp + ".tar.gz"
  77. # c.py does not exist and exception is expected
  78. with pytest.raises(FileNotFoundError) as ex:
  79. create_temp_archive(
  80. test_archive_name, self.test_dir, filenames=["*.json", "*.txt", "a.py", "c.py"], require_complete=True
  81. )
  82. assert "{'c.py'}" in str(ex) # ensure c.py is the only item not matched
  83. def test_archive_nonexistent_filter(self):
  84. test_archive_name = "empty-" + self.test_timestamp + ".tar.gz"
  85. archive_path = create_temp_archive(test_archive_name, self.test_dir, filenames=["*.yml"])
  86. self.assertArchivedContent(archive_path, [])
  87. def test_archive_with_subdirectories(self):
  88. subdir_name = os.path.join(self.test_dir, "subdir")
  89. os.makedirs(subdir_name)
  90. self._create_test_files(subdir_name)
  91. test_archive_name = "subdir-" + self.test_timestamp + ".tar.gz"
  92. archive_path = create_temp_archive(
  93. archive_name=test_archive_name, source_dir=self.test_dir, filenames=["*"], recursive=True
  94. )
  95. self.assertArchivedFileCount(archive_path, 10)
  96. def test_archive_with_subdirectories_no_filter(self):
  97. subdir_name = os.path.join(self.test_dir, "subdir")
  98. os.makedirs(subdir_name)
  99. self._create_test_files(subdir_name)
  100. test_archive_name = "subdir-" + self.test_timestamp + ".tar.gz"
  101. archive_path = create_temp_archive(archive_name=test_archive_name, source_dir=self.test_dir, recursive=True)
  102. self.assertArchivedFileCount(archive_path, 0)
  103. def test_archive_with_subdirectories_and_filters(self):
  104. subdir_name = os.path.join(self.test_dir, "subdir")
  105. os.makedirs(subdir_name)
  106. self._create_test_files(subdir_name)
  107. test_archive_name = "subdir-" + self.test_timestamp + ".tar.gz"
  108. archive_path = create_temp_archive(
  109. archive_name=test_archive_name, source_dir=self.test_dir, filenames=["subdir/*.py"], recursive=True
  110. )
  111. self.assertArchivedFileCount(archive_path, 2)
  112. self.assertArchivedContent(archive_path, ["subdir/a.py", "subdir/b.py"])
  113. def test_archive_with_second_level_subdirectories(self):
  114. subdir_name = os.path.join(self.test_dir, "subdir")
  115. os.makedirs(subdir_name)
  116. self._create_test_files(subdir_name)
  117. another_subdir_name = os.path.join(subdir_name, "another.subdir")
  118. os.makedirs(another_subdir_name)
  119. self._create_test_files(another_subdir_name)
  120. test_archive_name = "subdir-" + self.test_timestamp + ".tar.gz"
  121. archive_path = create_temp_archive(
  122. archive_name=test_archive_name, source_dir=self.test_dir, filenames=["*"], recursive=True
  123. )
  124. self.assertArchivedFileCount(archive_path, 15)
  125. def test_archive_with_second_level_subdirectories_and_nonexistent_filter(self):
  126. subdir_name = os.path.join(self.test_dir, "subdir")
  127. os.makedirs(subdir_name)
  128. self._create_test_files(subdir_name)
  129. another_subdir_name = os.path.join(subdir_name, "another.subdir")
  130. os.makedirs(another_subdir_name)
  131. self._create_test_files(another_subdir_name)
  132. test_archive_name = "subdir-" + self.test_timestamp + ".tar.gz"
  133. archive_path = create_temp_archive(
  134. archive_name=test_archive_name, source_dir=self.test_dir, filenames=["*.yml"], recursive=True
  135. )
  136. self.assertArchivedFileCount(archive_path, 0)
  137. def assertArchivedContent(self, archive_path, expected_content):
  138. actual_content = []
  139. with tarfile.open(archive_path, "r:gz") as tar:
  140. for tarinfo in tar:
  141. if tarinfo.isreg():
  142. actual_content.append(tarinfo.name)
  143. self.assertListEqual(sorted(actual_content), sorted(expected_content))
  144. def assertArchivedFileCount(self, archive_path, expected_number_of_files):
  145. n_files = 0
  146. with tarfile.open(archive_path, "r:gz") as tar:
  147. for tarinfo in tar:
  148. if tarinfo.isreg():
  149. n_files += 1
  150. self.assertEqual(expected_number_of_files, n_files)
  151. def _create_test_files(self, dir_name):
  152. for test_file in self.test_files:
  153. file_path = os.path.join(dir_name, test_file)
  154. with open(file_path, "a"):
  155. os.utime(file_path, None)