test_file_manager.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import unittest
  2. import os
  3. import sys
  4. from kalliope.core.Utils.FileManager import FileManager
  5. class TestFileManager(unittest.TestCase):
  6. """
  7. Class to test FileManager
  8. """
  9. def setUp(self):
  10. pass
  11. def create_file_manager(self):
  12. file_manager = FileManager()
  13. self.assertIsInstance(FileManager, file_manager)
  14. def test_create_directory(self):
  15. """
  16. Test to create a new directory.
  17. """
  18. # set up
  19. cache_path = "/tmp/kalliope/tests/testDirectory"
  20. if os.path.exists(cache_path):
  21. os.removedirs(cache_path)
  22. # Test FileManager.create_directory
  23. FileManager.create_directory(cache_path)
  24. self.assertTrue(os.path.exists(cache_path),
  25. "Fail creating a directory to the path ")
  26. # Remove the directory
  27. os.removedirs(cache_path)
  28. def test_write_in_file(self):
  29. """
  30. Test to write in file.
  31. """
  32. # set up the context
  33. dir_path = "/tmp/kalliope/tests/"
  34. file_name = "test_FileManager_writeInFile"
  35. file_path = os.path.join(dir_path,file_name)
  36. in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
  37. if os.path.exists(file_path):
  38. os.remove(file_path)
  39. if not os.path.exists(dir_path):
  40. os.makedirs(dir_path)
  41. # Test FileManager.write_in_file
  42. FileManager.write_in_file(file_path=file_path, content=in_file_text)
  43. with open(file_path, 'r') as content_file:
  44. content = content_file.read()
  45. self.assertEqual(content, in_file_text,
  46. "Fail writing in the file ")
  47. if sys.version_info[0] > 2:
  48. # Test writing of bytes object for python3
  49. FileManager.write_in_file(file_path=file_path, content=bytes(in_file_text, 'utf-8'))
  50. with open(file_path, 'r') as content_file:
  51. content = content_file.read()
  52. self.assertEqual(content, in_file_text,
  53. "Fail writing in the file ")
  54. # Clean up
  55. if os.path.exists(file_path):
  56. os.remove(file_path)
  57. # run into IOError by trying to write something in root
  58. dir_path = "/root/"
  59. file_name = "test_FileManager_writeInFile"
  60. file_path = os.path.join(dir_path, file_name)
  61. self.assertFalse(FileManager.write_in_file(file_path=file_path, content=in_file_text))
  62. def test_file_is_empty(self):
  63. """
  64. Test that the file is empty
  65. """
  66. # set up the context
  67. dir_path = "/tmp/kalliope/tests/"
  68. file_name = "test_FileManager_fileIsEmpty"
  69. file_path = os.path.join(dir_path, file_name)
  70. if os.path.exists(file_path):
  71. os.remove(file_path)
  72. if not os.path.exists(dir_path):
  73. os.makedirs(dir_path)
  74. # Test FileManager.file_is_empty
  75. with open(file_path, "wb") as file_open:
  76. file_open.write(b"")
  77. file_open.close()
  78. self.assertTrue(FileManager.file_is_empty(file_path=file_path),
  79. "Fail matching to verify that file is empty ")
  80. # Clean up
  81. if os.path.exists(file_path):
  82. os.remove(file_path)
  83. def test_remove_file(self):
  84. """
  85. Test to remove a file
  86. """
  87. # set up the context
  88. dir_path = "/tmp/kalliope/tests/"
  89. file_name = "test_FileManager_fileRemove"
  90. file_path = os.path.join(dir_path, file_name)
  91. if os.path.exists(file_path):
  92. os.remove(file_path)
  93. if not os.path.exists(dir_path):
  94. os.makedirs(dir_path)
  95. # Test to remove the file
  96. # FileManager.remove_file
  97. with open(file_path, "wb") as file_open:
  98. file_open.write(b"")
  99. file_open.close()
  100. FileManager.remove_file(file_path=file_path)
  101. self.assertFalse(os.path.exists(file_path),
  102. "Fail removing the file")
  103. def test_is_path_creatable(self):
  104. """
  105. Test if the path is creatable for the user
  106. Does the user has the permission to use this path ?
  107. """
  108. # set up the context
  109. dir_path = "/tmp/kalliope/tests/"
  110. file_name = "test_FileManager_filePathCreatable"
  111. file_path = os.path.join(dir_path, file_name)
  112. if os.path.exists(file_path):
  113. os.remove(file_path)
  114. if not os.path.exists(dir_path):
  115. os.makedirs(dir_path)
  116. # test not allowed : return False
  117. not_allowed_root_path = "/root/"
  118. not_allowed_path = os.path.join(not_allowed_root_path, file_name)
  119. self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
  120. "Fail to assert not accessing this path ")
  121. # test allowed : return True
  122. self.assertTrue(FileManager.is_path_creatable(file_path))
  123. def test_is_path_exists_or_creatable(self):
  124. """
  125. Test the _is_path_exists_or_creatable
  126. 4 scenarii :
  127. - the file exists and is creatable : return True
  128. - the file does not exist but is creatable : return True
  129. - the file exists but is not allowed : return True --> need a review !
  130. - the file does not exist and is not allowed : return False
  131. """
  132. # set up the context
  133. dir_path = "/tmp/kalliope/tests/"
  134. file_name = "test_FileManager_fileIsPathExistsOrCreatable"
  135. file_path = os.path.join(dir_path, file_name)
  136. if os.path.exists(file_path):
  137. os.remove(file_path)
  138. if not os.path.exists(dir_path):
  139. os.makedirs(dir_path)
  140. # Test the file exist and creatable : return True
  141. with open(file_path, "wb") as file_open:
  142. file_open.write(b"[Kalliope] Test Running the test_is_path_exists_or_creatable method")
  143. file_open.close()
  144. self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
  145. "Fail to assert the file exist ")
  146. # test the file not exist but creatable : return True
  147. os.remove(file_path)
  148. self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
  149. "Fail asserting the file does not exist ")
  150. # test the file exist but not creatable : return True
  151. # file_exist_not_allowed = "/root/.ssh/known_hosts"
  152. # self.assertTrue(FileManager.is_path_creatable(file_exist_not_allowed))
  153. # test the file not exist and not allowed : return False
  154. not_allowed_root_path = "/root/"
  155. not_allowed_path = os.path.join(not_allowed_root_path, file_name)
  156. self.assertFalse(FileManager.is_path_creatable(not_allowed_path),
  157. "Fail to assert not accessing this path ")
  158. if __name__ == '__main__':
  159. unittest.main()