1#! /usr/bin/env python3 2# 3# BitBake Toaster Implementation 4# 5# Copyright (C) 2016 Intel Corporation 6# 7# SPDX-License-Identifier: GPL-2.0-only 8# 9 10import os 11 12from django.test import TestCase 13from django.core import management 14 15from orm.models import signal_runbuilds 16 17import threading 18import time 19import subprocess 20import signal 21 22 23class 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 42class 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) 77