test_runbuilds.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /usr/bin/env python
  2. #
  3. # BitBake Toaster Implementation
  4. #
  5. # Copyright (C) 2016 Intel Corporation
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. import os
  10. from django.test import TestCase
  11. from django.core import management
  12. from orm.models import signal_runbuilds
  13. import threading
  14. import time
  15. import subprocess
  16. import signal
  17. class KillRunbuilds(threading.Thread):
  18. """ Kill the runbuilds process after an amount of time """
  19. def __init__(self, *args, **kwargs):
  20. super(KillRunbuilds, self).__init__(*args, **kwargs)
  21. self.setDaemon(True)
  22. def run(self):
  23. time.sleep(5)
  24. signal_runbuilds()
  25. time.sleep(1)
  26. pidfile_path = os.path.join(os.environ.get("BUILDDIR", "."),
  27. ".runbuilds.pid")
  28. with open(pidfile_path) as pidfile:
  29. pid = pidfile.read()
  30. os.kill(int(pid), signal.SIGTERM)
  31. class TestCommands(TestCase):
  32. """ Sanity test that runbuilds executes OK """
  33. def setUp(self):
  34. os.environ.setdefault("DJANGO_SETTINGS_MODULE",
  35. "toastermain.settings_test")
  36. os.environ.setdefault("BUILDDIR",
  37. "/tmp/")
  38. # Setup a real database if needed for runbuilds process
  39. # to connect to
  40. management.call_command('migrate')
  41. def test_runbuilds_command(self):
  42. kill_runbuilds = KillRunbuilds()
  43. kill_runbuilds.start()
  44. manage_py = os.path.join(
  45. os.path.dirname(os.path.abspath(__file__)),
  46. os.pardir,
  47. os.pardir,
  48. "manage.py")
  49. command = "%s runbuilds" % manage_py
  50. process = subprocess.Popen(command,
  51. shell=True,
  52. stdout=subprocess.PIPE,
  53. stderr=subprocess.PIPE)
  54. (out, err) = process.communicate()
  55. process.wait()
  56. self.assertNotEqual(process.returncode, 1,
  57. "Runbuilds returned an error %s" % err)