test_file_manager.py 5.9 KB

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