test_file_manager.py 6.3 KB

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