test_script.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import unittest
  2. import os
  3. import time
  4. from core import OrderAnalyser
  5. from core.ConfigurationManager import BrainLoader
  6. from core.NeuronModule import MissingParameterException, InvalidParameterException
  7. from neurons.script.script import Script
  8. from core.FileManager import FileManager
  9. class TestScript(unittest.TestCase):
  10. def setUp(self):
  11. self.path = "path"
  12. self.random = "random"
  13. self.test_file = "/tmp/kalliope_text_shell.txt"
  14. def testParameters(self):
  15. def run_test_missing_param(parameters_to_test):
  16. with self.assertRaises(MissingParameterException):
  17. Script(**parameters_to_test)
  18. def run_test_invalid_param(parameters_to_test):
  19. with self.assertRaises(InvalidParameterException):
  20. Script(**parameters_to_test)
  21. # empty
  22. parameters = dict()
  23. run_test_missing_param(parameters)
  24. # missing path
  25. parameters = {
  26. "random": self.random
  27. }
  28. run_test_missing_param(parameters)
  29. # random path
  30. self.path = "/tmp/iamarandompath/anotherrandompath/kalliope"
  31. parameters = {
  32. "path": self.path
  33. }
  34. run_test_invalid_param(parameters)
  35. # Test Non executable file
  36. # Create the file and remove permissions to the user
  37. tmp_path = "/tmp/kalliope/tests/"
  38. tmp_file_path = tmp_path+"neuronScript"
  39. if not os.path.exists(tmp_path):
  40. os.makedirs(tmp_path)
  41. text_to_write = "[kalliope-test] TestScript - testParameters"
  42. with open(tmp_file_path, 'w') as myFile:
  43. myFile.write(text_to_write)
  44. os.chmod(tmp_file_path, 0600)
  45. # test the user does not have access
  46. self.path = tmp_file_path
  47. parameters = {
  48. "path": self.path
  49. }
  50. run_test_invalid_param(parameters)
  51. # Remove the tmp file
  52. os.chmod(tmp_file_path, 0700)
  53. os.remove(tmp_file_path)
  54. def test_script_execution(self):
  55. """
  56. Test we can run a script
  57. """
  58. param = {
  59. "path": "./test_script.sh"
  60. }
  61. Script(**param)
  62. self.assertTrue(os.path.isfile(self.test_file))
  63. # remove the tet file
  64. os.remove(self.test_file)
  65. def test_script_execution_async(self):
  66. """
  67. Test we can run a script asynchronously
  68. """
  69. param = {
  70. "path": "./test_script.sh",
  71. "async": True
  72. }
  73. Script(**param)
  74. # let the time to the thread to do its job
  75. time.sleep(0.5)
  76. self.assertTrue(os.path.isfile(self.test_file))
  77. # remove the test file
  78. os.remove(self.test_file)
  79. def test_script_content(self):
  80. """
  81. Test we can get a content from the launched script
  82. """
  83. text_to_write = 'kalliope'
  84. # we write a content into a file
  85. with open(self.test_file, 'w') as myFile:
  86. myFile.write(text_to_write)
  87. # get the output with the neuron
  88. parameters = {
  89. "path": "./test_script_cat.sh",
  90. }
  91. script = Script(**parameters)
  92. self.assertEqual(script.output, text_to_write)
  93. self.assertEqual(script.returncode, 0)
  94. # remove the tet file
  95. os.remove(self.test_file)
  96. if __name__ == '__main__':
  97. unittest.main()