Files
test/automated-pytest-suite/utils/horizon/helper.py
Yang Liu 33756ac899 Initial submission for starlingx pytest framework.
Include:
- util modules. such as table_parser, ssh/localhost clients, cli module,
exception, logger, etc. Util modules are mostly used by keywords.
- keywords modules. These are helper functions that are used directly by
test functions.
- platform (with platform or platform_sanity marker) and stx-openstack
(with sanity, sx_sanity, cpe_sanity, or storage_sanity marker) sanity
testcases
- pytest config conftest, and test fixture modules
- test config file template/example

Required packages:
- python3.4 or python3.5
- pytest >=3.10,<4.0
- pexpect
- requests
- pyyaml
- selenium (firefox, ffmpeg, pyvirtualdisplay, Xvfb or Xephyr or Xvnc)

Limitations:
- Anything that requires copying from Test File Server will not work until
a public share is configured to shared test files. Tests skipped for now.

Co-Authored-By: Maria Yousaf <maria.yousaf@windriver.com>
Co-Authored-By: Marvin Huang <marvin.huang@windriver.com>
Co-Authored-By: Yosief Gebremariam <yosief.gebremariam@windriver.com>
Co-Authored-By: Paul Warner <paul.warner@windriver.com>
Co-Authored-By: Xueguang Ma <Xueguang.Ma@windriver.com>
Co-Authored-By: Charles Chen <charles.chen@windriver.com>
Co-Authored-By: Daniel Graziano <Daniel.Graziano@windriver.com>
Co-Authored-By: Jordan Li <jordan.li@windriver.com>
Co-Authored-By: Nimalini Rasa <nimalini.rasa@windriver.com>
Co-Authored-By: Senthil Mukundakumar <senthil.mukundakumar@windriver.com>
Co-Authored-By: Anuejyan Manokeran <anujeyan.manokeran@windriver.com>
Co-Authored-By: Peng Peng <peng.peng@windriver.com>
Co-Authored-By: Chris Winnicki <chris.winnicki@windriver.com>
Co-Authored-By: Joe Vimar <Joe.Vimar@windriver.com>
Co-Authored-By: Alex Kozyrev <alex.kozyrev@windriver.com>
Co-Authored-By: Jack Ding <jack.ding@windriver.com>
Co-Authored-By: Ming Lei <ming.lei@windriver.com>
Co-Authored-By: Ankit Jain <ankit.jain@windriver.com>
Co-Authored-By: Eric Barrett <eric.barrett@windriver.com>
Co-Authored-By: William Jia <william.jia@windriver.com>
Co-Authored-By: Joseph Richard <Joseph.Richard@windriver.com>
Co-Authored-By: Aldo Mcfarlane <aldo.mcfarlane@windriver.com>

Story: 2005892
Task: 33750
Signed-off-by: Yang Liu <yang.liu@windriver.com>

Change-Id: I7a88a47e09733d39f024144530f5abb9aee8cad2
2019-07-15 15:30:00 -04:00

94 lines
3.2 KiB
Python

import contextlib
import tempfile
import os
import time
from selenium import webdriver
try:
from pyvirtualdisplay import Display
except ImportError:
Display = None
from consts.proj_vars import ProjVar
from utils.tis_log import LOG
@contextlib.contextmanager
def gen_temporary_file(name='', suffix='.qcow2', size=10485760):
"""Generate temporary file with provided parameters.
:param name: file name except the extension /suffix
:param suffix: file extension/suffix
:param size: size of the file to create, bytes are generated randomly
:return: path to the generated file
"""
with tempfile.NamedTemporaryFile(prefix=name, suffix=suffix) as tmp_file:
tmp_file.write(os.urandom(size))
yield tmp_file.name
def gen_resource_name(resource="", timestamp=True):
"""Generate random resource name using uuid and timestamp.
Input fields are usually limited to 255 or 80 characters hence their
provide enough space for quite long resource names, but it might be
the case that maximum field length is quite restricted, it is then
necessary to consider using shorter resource argument or avoid using
timestamp by setting timestamp argument to False.
"""
fields = ['test']
if resource:
fields.append(resource)
if timestamp:
tstamp = time.strftime("%d-%m-%H-%M-%S")
fields.append(tstamp)
return "_".join(fields)
class HorizonDriver:
driver_info = []
@classmethod
def get_driver(cls):
if cls.driver_info:
return cls.driver_info[0][0]
LOG.info("Setting Firefox download preferences")
profile = webdriver.FirefoxProfile()
# Change default download directory to automation logs dir
# 2 - download to custom folder
horizon_dir = ProjVar.get_var('LOG_DIR') + '/horizon'
os.makedirs(horizon_dir, exist_ok=True)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting",
False)
profile.set_preference("browser.download.dir", horizon_dir)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
"text/plain,application/x-shellscript")
# profile.update_preferences()
display = None
if Display is not None:
display = Display(visible=ProjVar.get_var('HORIZON_VISIBLE'),
size=(1920, 1080))
display.start()
driver_ = webdriver.Firefox(firefox_profile=profile)
# driver_.maximize_window()
cls.driver_info.append((driver_, display))
LOG.info("Web driver created with download preference set")
return driver_
@classmethod
def quit_driver(cls, *driver_display):
if cls.driver_info:
driver_, display_ = cls.driver_info[0]
driver_.quit()
if display_:
display_.stop()
cls.driver_info = []
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 1)
LOG.info(
"Quit web driver and reset Firefox download folder to default")