poky/bitbake/lib/toaster/tests/functional/functional_helpers.py
Alexander Lussier-Cullen dc608a5bbe bitbake: bitbake: toaster: add functional testing toaster error details
Functional tests can sometimes fail to initialize toaster.
Most often this is due to a conflict on port 8000.
Add command information about whichever other process is running on
that port to better describe the initialization failure.

(Bitbake rev: da7f91d6dbe8703fb12d58ec95f077349d0005c8)

Signed-off-by: Alexander Lussier-Cullen <alexander.lussier-cullen@savoirfairelinux.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2023-12-13 11:49:18 +00:00

117 lines
4.0 KiB
Python

#! /usr/bin/env python3
#
# BitBake Toaster functional tests implementation
#
# Copyright (C) 2017 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
import os
import logging
import subprocess
import signal
import time
import re
from tests.browser.selenium_helpers_base import SeleniumTestCaseBase
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
logger = logging.getLogger("toaster")
class SeleniumFunctionalTestCase(SeleniumTestCaseBase):
wait_toaster_time = 5
@classmethod
def setUpClass(cls):
# So that the buildinfo helper uses the test database'
if os.environ.get('DJANGO_SETTINGS_MODULE', '') != \
'toastermain.settings_test':
raise RuntimeError("Please initialise django with the tests settings: "
"DJANGO_SETTINGS_MODULE='toastermain.settings_test'")
# start toaster
cmd = "bash -c 'source toaster start'"
cls.p = subprocess.Popen(
cmd,
cwd=os.environ.get("BUILDDIR"),
shell=True)
if cls.p.wait() != 0:
port_use = os.popen("lsof -i -P -n | grep '8000 (LISTEN)'").read().strip()
message = ''
if port_use:
process_id = port_use.split()[1]
process = os.popen(f"ps -o cmd= -p {process_id}").read().strip()
message = f"Port 8000 occupied by {process}"
raise RuntimeError(f"Can't initialize toaster. {message}")
super(SeleniumFunctionalTestCase, cls).setUpClass()
cls.live_server_url = 'http://localhost:8000/'
@classmethod
def tearDownClass(cls):
super(SeleniumFunctionalTestCase, cls).tearDownClass()
# XXX: source toaster stop gets blocked, to review why?
# from now send SIGTERM by hand
time.sleep(cls.wait_toaster_time)
builddir = os.environ.get("BUILDDIR")
with open(os.path.join(builddir, '.toastermain.pid'), 'r') as f:
toastermain_pid = int(f.read())
os.kill(toastermain_pid, signal.SIGTERM)
with open(os.path.join(builddir, '.runbuilds.pid'), 'r') as f:
runbuilds_pid = int(f.read())
os.kill(runbuilds_pid, signal.SIGTERM)
cls.p.kill()
def get_URL(self):
rc=self.get_page_source()
project_url=re.search(r"(projectPageUrl\s:\s\")(.*)(\",)",rc)
return project_url.group(2)
def find_element_by_link_text_in_table(self, table_id, link_text):
"""
Assume there're multiple suitable "find_element_by_link_text".
In this circumstance we need to specify "table".
"""
try:
table_element = self.get_table_element(table_id)
element = table_element.find_element(By.LINK_TEXT, link_text)
except NoSuchElementException:
print('no element found')
raise
return element
def get_table_element(self, table_id, *coordinate):
if len(coordinate) == 0:
#return whole-table element
element_xpath = "//*[@id='" + table_id + "']"
try:
element = self.driver.find_element(By.XPATH, element_xpath)
except NoSuchElementException:
raise
return element
row = coordinate[0]
if len(coordinate) == 1:
#return whole-row element
element_xpath = "//*[@id='" + table_id + "']/tbody/tr[" + str(row) + "]"
try:
element = self.driver.find_element(By.XPATH, element_xpath)
except NoSuchElementException:
return False
return element
#now we are looking for an element with specified X and Y
column = coordinate[1]
element_xpath = "//*[@id='" + table_id + "']/tbody/tr[" + str(row) + "]/td[" + str(column) + "]"
try:
element = self.driver.find_element(By.XPATH, element_xpath)
except NoSuchElementException:
return False
return element