test_script.py 3.3 KB

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