Add Services modal with independent lifecycle controls and persistent autostart
This commit is contained in:
36
tests/test_services_browser.py
Normal file
36
tests/test_services_browser.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""Chromium UI test with mocked API; runtime/HTTP covered separately."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
from playwright.sync_api import sync_playwright
|
||||
root=Path(__file__).resolve().parents[1]/'websetup'
|
||||
state=dict(defaultPassword=False,groups={k:[] for k in ['1','2','3','4','5','12','13','17','21','45','200']},text='',revision='a',csrf='b',validation={'errors':[]},devices=[],backups=[],path='/test',services=True)
|
||||
prefs={'netware':False,'browser':True};revision='1';actions=[];fail=False
|
||||
status={'mars-nwe-pcap.service':'ActiveState=inactive','mars-pcapd.service':'ActiveState=inactive','browser-admin':'ActiveState=active'}
|
||||
def fixture(source,path,body):
|
||||
global revision,prefs
|
||||
if path=='/api/state':return state
|
||||
if path=='/api/status':return status
|
||||
if path=='/api/services':
|
||||
if body:
|
||||
assert body['confirm']
|
||||
if body['op']=='autostart':
|
||||
assert body['revision']==revision;prefs=body['autostart'];revision='2';actions.append('save')
|
||||
else:actions.append(body['target']+':'+body['action'])
|
||||
return {'revision':revision,'autostart':prefs,'status':status,'available':True}
|
||||
raise AssertionError(path)
|
||||
with sync_playwright() as p:
|
||||
b=p.chromium.launch(executable_path=os.environ.get('CHROMIUM_PATH'),args=['--no-sandbox'])
|
||||
for width in [1280,390]:
|
||||
page=b.new_page(viewport={'width':width,'height':900});errors=[];page.on('pageerror',lambda e:errors.append(str(e)));page.on('dialog',lambda d:d.accept());page.expose_binding('fixture',fixture)
|
||||
page.evaluate("window.fetch=async(path,opts)=>({ok:true,status:200,json:()=>window.fixture(path,opts?.body?JSON.parse(opts.body):null)})")
|
||||
html=(root/'index.html').read_text().replace('<link rel="stylesheet" href="/style.css">','<style>'+(root/'style.css').read_text()+'</style>').replace('<script src="/app.js" defer></script>','');page.set_content(html);page.add_script_tag(content=(root/'app.js').read_text());page.wait_for_function("document.getElementById('configPath').textContent==='/test'")
|
||||
page.locator('#servicesOpen').click();page.locator('#servicesAuto-netware').set_checked(not prefs['netware']);page.locator('#servicesAuto-browser').set_checked(not prefs['browser']);page.locator('#servicesSave').click();page.wait_for_function("document.getElementById('servicesMessage').textContent.includes('gespeichert')")
|
||||
for target in ['netware','browser']:
|
||||
for action in ['start','restart','stop']:
|
||||
page.locator(f'[data-target="{target}"][data-action="{action}"]').click();page.wait_for_function("!document.getElementById('servicesRefresh').disabled")
|
||||
assert actions[-1]==target+':'+action
|
||||
page.locator('#servicesClose').click();page.locator('#servicesOpen').click();page.wait_for_function("!document.getElementById('servicesRefresh').disabled")
|
||||
assert page.locator('#servicesAuto-netware').is_checked()==prefs['netware'];assert not errors,errors
|
||||
page.screenshot(path=str(root.parent/f'build/services-{width}.png'));page.close()
|
||||
b.close()
|
||||
print('PASS Services Chromium desktop/mobile: save, persistence readback, both service groups and all actions')
|
||||
@@ -20,4 +20,19 @@ static int broker_pid(void){json_object*o=supervisor_status();const char*s=js(o,
|
||||
static int browser_pid(void){json_object*o=supervisor_status();const char*s=js(o,"browser-admin");const char*p=strstr(s,"PID=");int v=p?atoi(p+4):0;json_object_put(o);return v;}
|
||||
static int await_browser(int old,int tries){for(int i=0;i<tries;i++){supervisor_tick();int p=browser_pid();if(p&&p!=old)return p;usleep(20000);}return 0;}
|
||||
static int await_running(int old){for(int i=0;i<200;i++){supervisor_tick();int p=server_pid();if(p&&p!=old&&broker_pid())return p;usleep(20000);}return 0;}
|
||||
int main(int argc,char**argv){(void)argc;if(strstr(argv[0],"mock-broker"))return mock_child(1);if(strstr(argv[0],"mock-server")||strstr(argv[0],"mock-browser"))return mock_child(0);char tmp[]="/tmp/mars-supervisor-XXXXXX";assert(mkdtemp(tmp));char socketfile[108];snprintf(socketfile,sizeof socketfile,"%s/transport.sock",tmp);supervisor_test_socket(socketfile);setenv("MARS_TEST_BROKER_SOCKET",socketfile,1);snprintf(broker_path,sizeof broker_path,"%s/mock-broker",tmp);snprintf(server_path,sizeof server_path,"%s/mock-server",tmp);char exe[PATH_MAX];ssize_t n=readlink("/proc/self/exe",exe,sizeof exe-1);assert(n>0);exe[n]=0;assert(symlink(exe,broker_path)==0);assert(symlink(exe,server_path)==0);char browserfile[PATH_MAX],reloadfile[PATH_MAX];snprintf(browserfile,sizeof browserfile,"%s/mock-browser",tmp);snprintf(reloadfile,sizeof reloadfile,"%s/browser-reload",tmp);assert(symlink(exe,browserfile)==0);supervisor_test_browser(browserfile,reloadfile);int browserfirst=await_browser(0,200);assert(browserfirst>0);assert(!server_pid()&&!broker_pid());assert(supervisor_request("start")==0);int first=await_running(0);assert(first>0);int firstb=broker_pid();usleep(100000);FILE*mark=fopen(reloadfile,"w");assert(mark);fclose(mark);int browsersecond=await_browser(browserfirst,200);assert(browsersecond>0&&browsersecond!=browserfirst);assert(server_pid()==first&&broker_pid()==firstb);assert(access(reloadfile,F_OK)!=0);usleep(100000);assert(kill(browsersecond,SIGKILL)==0);int browserthird=await_browser(browsersecond,400);assert(browserthird>0&&browserthird!=browsersecond);assert(server_pid()==first&&broker_pid()==firstb);assert(supervisor_request("restart")==0);int second=await_running(first);assert(second>0&&second!=first&&broker_pid()!=firstb);usleep(100000);assert(supervisor_request("stop")==0);for(int i=0;i<200&&(server_pid()||broker_pid());i++){supervisor_tick();usleep(20000);}assert(!server_pid()&&!broker_pid());assert(browser_pid()==browserthird);assert(supervisor_request("start")==0);assert(await_running(0));usleep(100000);supervisor_shutdown();assert(!server_pid()&&!broker_pid()&&!browser_pid());unlink(browserfile);unlink(socketfile);unlink(broker_path);unlink(server_path);rmdir(tmp);puts("PASS supervisor: start, restart, stop, restart after stop, shutdown; browser independent start, update restart, crash recovery, shutdown (mock child processes)");return 0;}
|
||||
int main(int argc,char**argv){(void)argc;if(strstr(argv[0],"mock-broker"))return mock_child(1);if(strstr(argv[0],"mock-server")||strstr(argv[0],"mock-browser"))return mock_child(0);char tmp[]="/tmp/mars-supervisor-XXXXXX";assert(mkdtemp(tmp));snprintf(config_path,sizeof config_path,"%s/nwserv.conf",tmp);
|
||||
assert(supervisor_boot_preferences(0)==0);
|
||||
json_object *prefs=supervisor_preferences();assert(prefs);char revision[65];snprintf(revision,sizeof revision,"%s",js(prefs,"revision"));json_object_put(prefs);
|
||||
json_object *next=json_tokener_parse("{\"netware\":true,\"browser\":false}");
|
||||
assert(supervisor_preferences_save(next,"stale")==-2);
|
||||
assert(supervisor_preferences_save(next,revision)==0);json_object_put(next);
|
||||
assert(supervisor_boot_preferences(0)==1);supervisor_tick();assert(!browser_pid());
|
||||
assert(supervisor_browser_request("start")==0);
|
||||
char socketfile[108];snprintf(socketfile,sizeof socketfile,"%s/transport.sock",tmp);supervisor_test_socket(socketfile);setenv("MARS_TEST_BROKER_SOCKET",socketfile,1);snprintf(broker_path,sizeof broker_path,"%s/mock-broker",tmp);snprintf(server_path,sizeof server_path,"%s/mock-server",tmp);char exe[PATH_MAX];ssize_t n=readlink("/proc/self/exe",exe,sizeof exe-1);assert(n>0);exe[n]=0;assert(symlink(exe,broker_path)==0);assert(symlink(exe,server_path)==0);char browserfile[PATH_MAX],reloadfile[PATH_MAX];snprintf(browserfile,sizeof browserfile,"%s/mock-browser",tmp);snprintf(reloadfile,sizeof reloadfile,"%s/browser-reload",tmp);assert(symlink(exe,browserfile)==0);supervisor_test_browser(browserfile,reloadfile);int browserfirst=await_browser(0,200);assert(browserfirst>0);assert(!server_pid()&&!broker_pid());assert(supervisor_request("start")==0);int first=await_running(0);assert(first>0);int firstb=broker_pid();usleep(100000);FILE*mark=fopen(reloadfile,"w");assert(mark);fclose(mark);int browsersecond=await_browser(browserfirst,200);assert(browsersecond>0&&browsersecond!=browserfirst);assert(server_pid()==first&&broker_pid()==firstb);assert(access(reloadfile,F_OK)!=0);usleep(100000);assert(kill(browsersecond,SIGKILL)==0);int browserthird=await_browser(browsersecond,400);assert(browserthird>0&&browserthird!=browsersecond);assert(server_pid()==first&&broker_pid()==firstb);assert(supervisor_request("restart")==0);int second=await_running(first);assert(second>0&&second!=first&&broker_pid()!=firstb);usleep(100000);assert(supervisor_request("stop")==0);for(int i=0;i<200&&(server_pid()||broker_pid());i++){supervisor_tick();usleep(20000);}assert(!server_pid()&&!broker_pid());assert(browser_pid()==browserthird);
|
||||
assert(supervisor_browser_request("invalid")==-1);
|
||||
assert(supervisor_browser_request("stop")==0);
|
||||
for(int i=0;i<200&&browser_pid();i++){supervisor_tick();usleep(20000);}assert(!browser_pid());
|
||||
for(int i=0;i<10;i++)supervisor_tick();assert(!browser_pid());
|
||||
assert(supervisor_browser_request("start")==0);int manual=await_browser(0,200);assert(manual);
|
||||
usleep(100000);assert(supervisor_browser_request("restart")==0);assert(await_browser(manual,200));
|
||||
assert(supervisor_request("start")==0);assert(await_running(0));usleep(100000);supervisor_shutdown();assert(!server_pid()&&!broker_pid()&&!browser_pid());unlink(browserfile);unlink(socketfile);unlink(broker_path);unlink(server_path);char prefsfile[PATH_MAX];snprintf(prefsfile,sizeof prefsfile,"%s/nwserv.conf.services.json",tmp);unlink(prefsfile);snprintf(prefsfile,sizeof prefsfile,"%s/nwserv.conf.services.json.previous",tmp);unlink(prefsfile);rmdir(tmp);puts("PASS supervisor: start, restart, stop, restart after stop, shutdown; browser independent start, update restart, crash recovery, shutdown (mock child processes)");return 0;}
|
||||
|
||||
@@ -30,6 +30,7 @@ status(){ curl -sS -o "$d/response" -w '%{http_code}' "$@"; }
|
||||
start
|
||||
[[ $(status "$base/api/state") == 401 ]]
|
||||
[[ $(status "$base/api/directory") == 401 ]]
|
||||
[[ $(status "$base/api/services") == 401 ]]
|
||||
for endpoint in client-macclassic client-macclassic-source mac-opentransport; do
|
||||
[[ $(status "$base/api/browser/$endpoint") == 401 ]]
|
||||
done
|
||||
@@ -55,6 +56,12 @@ cmp "$d/download.zip" "$d/www/drivers/WIN95.zip"
|
||||
[[ $(status -b "$d/cookie" "$base/drivers/WINXP.zip") == 404 ]]
|
||||
get api/drivers | jq -e '.platforms==["WIN95"]' >/dev/null
|
||||
csrf=$(jq -r .csrf <<<"$s")
|
||||
prefs=$(get api/services); jq -e '(.autostart.netware|not) and .autostart.browser' <<<"$prefs" >/dev/null
|
||||
request=$(jq -c '{op:"autostart",revision,autostart:{netware:true,browser:false},confirm:true}' <<<"$prefs")
|
||||
[[ $(status -b "$d/cookie" -H "Origin: $base" -H 'Content-Type: application/json' -d "$request" "$base/api/services") == 403 ]]
|
||||
post api/services "$request" | jq -e '.autostart.netware and (.autostart.browser|not)' >/dev/null
|
||||
[[ $(status -b "$d/cookie" -H "Origin: $base" -H 'Content-Type: application/json' -H "X-Mars-CSRF: $csrf" -d "$request" "$base/api/services") == 409 ]]
|
||||
[[ $(stat -c %a "$cfg.services.json") == 600 ]]
|
||||
dir=$(get api/directory); jq -e '(.nativeNds|not) and (.configured|not) and (.liveAvailable|not)' <<<"$dir" >/dev/null
|
||||
request=$(jq -c '{revision,config,confirm:true}' <<<"$dir")
|
||||
post api/directory "$request" | jq -e .ok >/dev/null
|
||||
@@ -74,4 +81,6 @@ post api/logout '{}' | jq -e .ok >/dev/null
|
||||
[[ $(status -b "$d/cookie" "$base/api/state") == 401 ]]
|
||||
kill "$pid"; wait "$pid"; pid=''; start
|
||||
post api/login '{"user":"admin","password":"NativeTestPassword123"}' | jq -e .ok >/dev/null
|
||||
get api/services | jq -e '.autostart.netware and (.autostart.browser|not)' >/dev/null
|
||||
printf 'PASS: services authenticated persistence, CSRF, conflict, reboot readback.\n'
|
||||
printf 'PASS: real CivetWeb login, origin/host/CSRF, default-password guard, persistence, logout, preview/save/backup (when Ethernet exists).\n'
|
||||
|
||||
Reference in New Issue
Block a user