
# MAKEFLAGS += --silent

TESTUSERID=182
TESTGROUPID=100
TESTUSERNAME=pmeunier
TEST_DB_CNF=~/.my.cnf
SESSNUM=1234
LOGPATH=/var/log/mw-service/service.log
EXT_PREFIX=/srv

ALLTESTS=check startvnc status resize startxapp screenshot stopvnc purgeoutputs notify_pretest_setup notify_command_finished notifyrestarted

.PHONY: prereq_aws cleanlog clean notify_pretest_setup

all: $(ALLTESTS)

clean: cleanlog clean_out
	rm -f $(ALLTESTS)

cleanlog:
	sudo rm -f $(LOGPATH)

prereq_aws:
	sudo grep EXT_PREFIX /etc/mw-service/exec.conf > /dev/null

check:
	sudo /usr/bin/maxwell_service check > check.out
	diff check.expected check.out
	touch check

startvnc:
	echo 'asdfadsfadfadf' | sudo /usr/bin/maxwell_service startvnc 1 780x680 24 > startvnc
	sudo grep done $(LOGPATH) || ( rm startvnc; exit 1 )

status: startvnc
	sudo /usr/bin/maxwell_service status 1 > status 2>status.err
	grep running status

# output is sent to log file when we try to redirect...
resize:
	sudo /usr/bin/maxwell_service resize 1 800x600 > resize
	sudo grep 'changed to 800x600' $(LOGPATH)

startxapp:
	sudo rm -f /var/log/mw-service/open-sessions/$(SESSNUM).err
	# /home/sessions: if LOCAL_SESSIONDIR,  copy session directories to local storage
	sudo rm -rf /home/sessions/pmeunier/$(SESSNUM)
	sudo /usr/bin/maxwell_fs erase_sessdir $(TESTUSERNAME) $(SESSNUM)
	sudo /usr/bin/maxwell_fs setup_dir $(TESTUSERNAME) $(SESSNUM)
	# start session 1234 for user apps, in container #1, to run workspace
	sudo /usr/bin/maxwell_service startxapp $(TESTUSERNAME) 1234 0 1 notify /apps/workspace/current/middleware/invoke -T r7 > startxapp
	# startxapp returns immediately with success while the tool starts in the background, need to check log files.
	# stdout redirect into startxapp file is always empty but is necessary so maxwell_service writes to log files.
	# wait for session log file.err to be created (REVISIT: use inotify instead of sleep?)
	sleep 10
	! ( sudo grep kill $(LOGPATH) || (rm -f startxapp && exit 1) )
	! ( sudo grep Error $(LOGPATH) || (rm -f startxapp && exit 1) )

screenshot: startxapp
	sudo /usr/bin/maxwell_service screenshot $(TESTUSERNAME) $(SESSNUM) 1 > screenshot

mount_paths: startxapp
	mkdir /tmp/testdir
	sudo /usr/bin/maxwell_service mount_paths /tmp/testdir
	rmdir /tmp/testdir

stopvnc: cleanlog startvnc
	sudo /usr/bin/maxwell_service stopvnc 1 > stopvnc
	rm startvnc*

killall:
	# this test requires a user to exist both in the account service and in the OS
	# 
	getent passwd $(TESTUSERNAME) > /dev/null || sudo useradd $(TESTUSERNAME)
	sudo su $(TESTUSERNAME) -c "sleep 50" &
	sudo /usr/bin/maxwell_service killall $(TESTUSERNAME)
	! ( pgrep -u $(TESTUSERNAME) > /dev/null )

killtree:

purgeoutputs:
	sudo touch /var/log/mw-service/open-sessions/$(SESSNUM).err
	sudo touch /var/log/mw-service/open-sessions/$(SESSNUM).out
	sudo /usr/bin/maxwell_service purgeoutputs $(SESSNUM)
	! [ -e /var/log/mw-service/open-sessions/$(SESSNUM).err ]
	! [ -e /var/log/mw-service/open-sessions/$(SESSNUM).out ]

notify_pretest_setup:
	# Notify tests assume that this is an all-in-one hub, with execution host being also the web server
	sudo grep MW_USER /etc/mw-service/*.conf  > /dev/null || ( echo 'For these tests, value for MW_USER must be specified in /etc/mw-service/exec.conf, try www-data or apache depending on OS' && exit 1 )
	# insert into database...  Assumes user that runs test has a .my.cnf file to connect to the test database.
	ls $(TEST_DB_CNF) > /dev/null
	mysql --defaults-file=$(TEST_DB_CNF) -e "delete from sessionlog"
	mysql --defaults-file=$(TEST_DB_CNF) -e "insert into sessionlog (exechost, sessnum) values ('127.0.0.1', $(SESSNUM))"
	sudo rm -rf /var/log/mw-service/open-sessions/
	sudo mkdir -p /var/log/mw-service/open-sessions/
	sudo touch /var/log/mw-service/open-sessions/$(SESSNUM).err
	sudo touch /var/log/mw-service/open-sessions/$(SESSNUM).out

notify_command_finished: notify_pretest_setup
	# notify about session 1234 explicitly and only
	sudo /usr/bin/maxwell_service notify_command_finished $(SESSNUM) localhost
	# client should have fetched these and deleted them
	! [ -e /var/log/mw-service/open-sessions/$(SESSNUM).err ]
	! [ -e /var/log/mw-service/open-sessions/$(SESSNUM).out ]

notifyrestarted: notify_pretest_setup
	# notify about sessions based on the existence of .err files
	sudo /usr/bin/maxwell_service notifyrestarted
	# client should have fetched these and deleted them
	! [ -e /var/log/mw-service/open-sessions/$(SESSNUM).err ]
	! [ -e /var/log/mw-service/open-sessions/$(SESSNUM).out ]

# anonymous sessions support functions, experimental, not tested
anonymous:
update_resources: 

clean_out:
	rm -f *.out
