From 8e040e58a11c3479e46f4fd216bbef4c93957e66 Mon Sep 17 00:00:00 2001 From: Dmitry Maksyoma Date: Tue, 7 Dec 2021 20:40:12 +1300 Subject: [PATCH] Specs: extract spec_helper.py --- spec/helper/__init__.py | 0 spec/helper/spec_helper.py | 70 ++++++++++++++++++++++++++++++++++++++ spec/vncserver_spec.py | 70 ++------------------------------------ 3 files changed, 73 insertions(+), 67 deletions(-) create mode 100644 spec/helper/__init__.py create mode 100644 spec/helper/spec_helper.py diff --git a/spec/helper/__init__.py b/spec/helper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spec/helper/spec_helper.py b/spec/helper/spec_helper.py new file mode 100644 index 0000000..42cc0aa --- /dev/null +++ b/spec/helper/spec_helper.py @@ -0,0 +1,70 @@ +import os +import shutil +import subprocess +import pexpect +from path import Path +from expects import expect, equal + +vncserver_cmd = 'vncserver :1 -cert /etc/ssl/certs/ssl-cert-snakeoil.pem -key /etc/ssl/private/ssl-cert-snakeoil.key -sslOnly -FrameRate=24 -interface 0.0.0.0 -httpd /usr/share/kasmvnc/www -depth 24 -geometry 1280x1050 -log *:stderr:100' +running_xvnc = False + + +def clean_env(): + clean_kasm_users() + + home_dir = os.environ['HOME'] + vnc_dir = os.path.join(home_dir, ".vnc") + Path(vnc_dir).rmtree(ignore_errors=True) + + +def clean_kasm_users(): + home_dir = os.environ['HOME'] + password_file = os.path.join(home_dir, ".kasmpasswd") + Path(password_file).remove_p() + + +def start_xvnc_pexpect(extra_args="", **kwargs): + global running_xvnc + + # ":;" is a hack. Without it, Xvnc doesn't run. No idea what happens, but + # when I run top, Xvnc just isn't there. I suspect a race. + child = pexpect.spawn('/bin/bash', + ['-ic', f':;{vncserver_cmd} {extra_args}'], + timeout=5, encoding='utf-8', **kwargs) + running_xvnc = True + + return child + + +def start_xvnc(extra_args="", **kwargs): + global running_xvnc + completed_process = run_cmd(f'{vncserver_cmd} {extra_args}', **kwargs) + if completed_process.returncode == 0: + running_xvnc = True + + return completed_process + + +def run_cmd(cmd, **kwargs): + completed_process = subprocess.run(cmd, shell=True, text=True, + capture_output=True, + executable='/bin/bash', **kwargs) + if completed_process.returncode != 0: + print(completed_process.stdout) + print(completed_process.stderr) + + return completed_process + + +def add_kasmvnc_user_docker(): + completed_process = run_cmd('echo -e "password\\npassword\\n" | vncpasswd -u docker') + expect(completed_process.returncode).to(equal(0)) + + +def kill_xvnc(): + global running_xvnc + if not running_xvnc: + return + + run_cmd('vncserver -kill :1') + running_xvnc = False diff --git a/spec/vncserver_spec.py b/spec/vncserver_spec.py index 7e2648a..c3ffa53 100644 --- a/spec/vncserver_spec.py +++ b/spec/vncserver_spec.py @@ -1,80 +1,16 @@ import os import sys import pexpect -import shutil -import subprocess -from path import Path from mamba import description, context, it, fit, before, after from expects import expect, equal +from helper.spec_helper import start_xvnc, kill_xvnc, run_cmd, clean_env, \ + add_kasmvnc_user_docker, clean_kasm_users, start_xvnc_pexpect + # WIP. Plan to move to start_xvnc_pexpect(), because pexpect provides a way to # wait for vncserver output. start_xvnc() just blindly prints input to vncserver # without knowing what it prints back. -vncserver_cmd = 'vncserver :1 -cert /etc/ssl/certs/ssl-cert-snakeoil.pem -key /etc/ssl/private/ssl-cert-snakeoil.key -sslOnly -FrameRate=24 -interface 0.0.0.0 -httpd /usr/share/kasmvnc/www -depth 24 -geometry 1280x1050 -log *:stderr:100' -running_xvnc = False - - -def clean_env(): - clean_kasm_users() - - home_dir = os.environ['HOME'] - vnc_dir = os.path.join(home_dir, ".vnc") - Path(vnc_dir).rmtree(ignore_errors=True) - - -def clean_kasm_users(): - home_dir = os.environ['HOME'] - password_file = os.path.join(home_dir, ".kasmpasswd") - Path(password_file).remove_p() - - -def start_xvnc_pexpect(extra_args="", **kwargs): - global running_xvnc - - # ":;" is a hack. Without it, Xvnc doesn't run. No idea what happens, but - # when I run top, Xvnc just isn't there. I suspect a race. - child = pexpect.spawn('/bin/bash', - ['-ic', f':;{vncserver_cmd} {extra_args}'], - timeout=5, encoding='utf-8', **kwargs) - running_xvnc = True - - return child - - -def start_xvnc(extra_args="", **kwargs): - global running_xvnc - completed_process = run_cmd(f'{vncserver_cmd} {extra_args}', **kwargs) - if completed_process.returncode == 0: - running_xvnc = True - - return completed_process - - -def run_cmd(cmd, **kwargs): - completed_process = subprocess.run(cmd, shell=True, text=True, - capture_output=True, - executable='/bin/bash', **kwargs) - if completed_process.returncode != 0: - print(completed_process.stdout) - print(completed_process.stderr) - - return completed_process - - -def add_kasmvnc_user_docker(): - completed_process = run_cmd('echo -e "password\\npassword\\n" | vncpasswd -u docker') - expect(completed_process.returncode).to(equal(0)) - - -def kill_xvnc(): - global running_xvnc - if not running_xvnc: - return - - run_cmd('vncserver -kill :1') - running_xvnc = False - def select_de(de_name): try: