KasmVNC/spec/vncserver_spec.py

170 lines
5.6 KiB
Python
Raw Normal View History

2021-09-10 13:14:56 +02:00
import os
2021-09-16 12:25:11 +02:00
import sys
2021-09-16 08:33:17 +02:00
import pexpect
import shutil
2021-09-10 13:14:56 +02:00
import subprocess
2021-09-15 07:38:37 +02:00
from path import Path
from mamba import description, context, it, fit, before, after
2021-09-10 13:14:56 +02:00
from expects import expect, equal
2021-09-15 07:46:49 +02:00
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'
running_xvnc = False
2021-09-15 07:46:49 +02:00
2021-09-14 09:27:53 +02:00
def clean_env():
home_dir = os.environ['HOME']
password_file = os.path.join(home_dir, ".kasmpasswd")
2021-09-15 07:38:37 +02:00
Path(password_file).remove_p()
vnc_dir = os.path.join(home_dir, ".vnc")
2021-09-15 07:38:37 +02:00
Path(vnc_dir).rmtree(ignore_errors=True)
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
2021-09-14 10:12:27 +02:00
def run_cmd(cmd, **kwargs):
2021-09-14 09:27:53 +02:00
completed_process = subprocess.run(cmd, shell=True, text=True,
capture_output=True,
2021-09-14 10:12:27 +02:00
executable='/bin/bash', **kwargs)
2021-09-14 09:27:53 +02:00
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))
2021-09-14 10:20:55 +02:00
def kill_xvnc():
global running_xvnc
if not running_xvnc:
return
2021-09-14 10:20:55 +02:00
run_cmd('vncserver -kill :1')
running_xvnc = False
2021-09-14 10:20:55 +02:00
2021-09-15 08:16:27 +02:00
def select_de(de_name):
try:
extra_args = f'-select-de {de_name}'
completed_process = start_xvnc(extra_args)
2021-09-15 08:16:27 +02:00
expect(completed_process.returncode).to(equal(0))
finally:
kill_xvnc()
def check_de_was_setup_to_run(de_name):
completed_process = run_cmd(f'grep -q {de_name} ~/.vnc/xstartup')
expect(completed_process.returncode).to(equal(0))
2021-09-10 13:14:56 +02:00
with description('vncserver') as self:
with before.each:
clean_env()
2021-09-15 07:39:35 +02:00
with after.each:
kill_xvnc()
2021-09-15 08:39:39 +02:00
with context('on the first run'):
2021-09-16 07:16:55 +02:00
with before.each:
2021-09-15 08:39:39 +02:00
add_kasmvnc_user_docker()
2021-09-15 07:55:48 +02:00
2021-09-16 07:16:55 +02:00
with it('asks user to select a DE'):
2021-09-15 08:39:39 +02:00
choose_cinnamon = "1\n"
completed_process = start_xvnc(input=choose_cinnamon)
2021-09-15 08:39:39 +02:00
expect(completed_process.returncode).to(equal(0))
2021-09-15 07:55:48 +02:00
2021-09-15 08:39:39 +02:00
check_de_was_setup_to_run('cinnamon')
2021-09-15 07:55:48 +02:00
2021-09-15 08:39:39 +02:00
with it('asks to select a DE, when ran with -select-de'):
choose_cinnamon = "1\n"
completed_process = start_xvnc('-select-de', input=choose_cinnamon)
2021-09-15 08:39:39 +02:00
expect(completed_process.returncode).to(equal(0))
2021-09-10 13:35:47 +02:00
2021-09-15 08:39:39 +02:00
check_de_was_setup_to_run('cinnamon')
2021-09-14 10:16:44 +02:00
2021-09-15 08:39:39 +02:00
with it('selects passed DE with -s'):
select_de('mate')
2021-09-15 08:39:39 +02:00
check_de_was_setup_to_run('mate')
2021-09-15 08:47:08 +02:00
with context('after DE was selected'):
2021-09-16 07:16:55 +02:00
with before.each:
2021-09-15 08:47:08 +02:00
add_kasmvnc_user_docker()
2021-09-16 07:16:55 +02:00
with it("don't ask to choose DE by default"):
2021-09-15 08:47:08 +02:00
select_de('mate')
completed_process = start_xvnc()
2021-09-15 08:47:08 +02:00
expect(completed_process.returncode).to(equal(0))
check_de_was_setup_to_run('mate')
with it('asks to select a DE, when ran with -select-de'):
select_de('mate')
choose_cinnamon_and_answer_yes = "1\ny\n"
completed_process = start_xvnc('-select-de',
input=choose_cinnamon_and_answer_yes)
2021-09-15 08:47:08 +02:00
expect(completed_process.returncode).to(equal(0))
check_de_was_setup_to_run('cinnamon')
with it('selects passed DE with -s'):
select_de('mate')
completed_process = start_xvnc('-select-de cinnamon')
2021-09-15 08:47:08 +02:00
expect(completed_process.returncode).to(equal(0))
check_de_was_setup_to_run('cinnamon')
2021-09-16 08:33:17 +02:00
with context('guided user creation'):
2021-09-16 09:23:17 +02:00
with it('asks to create a user if none exist'):
2021-09-16 08:33:17 +02:00
child = pexpect.spawn(f'{vncserver_cmd} -select-de cinnamon',
timeout=2)
child.expect('Enter username')
child.sendline()
child.expect('Password:')
child.sendline('password')
child.expect('Verify:')
child.sendline('password')
child.expect(pexpect.EOF)
child.close()
expect(child.exitstatus).to(equal(0))
home_dir = os.environ['HOME']
user = os.environ['USER']
2021-09-16 08:38:09 +02:00
completed_process = run_cmd(f'grep -qw {user} {home_dir}/.kasmpasswd')
2021-09-16 08:33:17 +02:00
expect(completed_process.returncode).to(equal(0))
2021-09-16 12:25:11 +02:00
with fit('specify custom username'):
custom_username = 'custom_username'
child = pexpect.spawn(f'{vncserver_cmd} -select-de cinnamon',
timeout=2)
2021-09-16 12:25:11 +02:00
# child.logfile = sys.stdout.buffer
child.expect('Enter username')
child.sendline(custom_username)
child.expect('Password:')
child.sendline('password')
child.expect('Verify:')
child.sendline('password')
child.expect(pexpect.EOF)
child.close()
expect(child.exitstatus).to(equal(0))
2021-09-16 12:25:11 +02:00
# TOOD: Xvnc won't bind to a port under pexpect, and its output is
# invisible.
home_dir = os.environ['HOME']
completed_process = run_cmd(f'grep -qw {custom_username} {home_dir}/.kasmpasswd')
expect(completed_process.returncode).to(equal(0))
2021-09-16 12:25:11 +02:00
os.system(f'pgrep Xvnc >/dev/null || cat {home_dir}/.vnc/*.log')