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