selftest: Move tests into own subdirectories

Change-Id: Id21cda19df131e24402e1a593036e1e33a914920
diff --git a/selftest/lock_test/_prep.py b/selftest/lock_test/_prep.py
new file mode 120000
index 0000000..9cea3fe
--- /dev/null
+++ b/selftest/lock_test/_prep.py
@@ -0,0 +1 @@
+../_prep.py
\ No newline at end of file
diff --git a/selftest/lock_test/lock_test.err b/selftest/lock_test/lock_test.err
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/selftest/lock_test/lock_test.err
diff --git a/selftest/lock_test/lock_test.ok b/selftest/lock_test/lock_test.ok
new file mode 100644
index 0000000..011a89c
--- /dev/null
+++ b/selftest/lock_test/lock_test.ok
@@ -0,0 +1,14 @@
+creating files
+launch a program that locks a given file, it will create $dir/lock_test
+wait until this lock_test lock file was created by program
+expecting the lock file to reflect "long name"
+launched first, locked by: 'long name'
+launching second program, should find the lock intact and wait
+launched second, locked by: 'long name'
+drop the first lock, $f1 removal signals the first process to stop locking
+wait for first program to carry out the lock release
+now expecting second program to lock
+waited, locked by: 'shorter'
+release the second program also
+expecting the lock to be gone
+waited more, locked by: ''
diff --git a/selftest/lock_test/lock_test.sh b/selftest/lock_test/lock_test.sh
new file mode 100755
index 0000000..9e4f44a
--- /dev/null
+++ b/selftest/lock_test/lock_test.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+owndir="$(dirname -- "$0")"
+
+echo 'creating files'
+dir="$(mktemp -d)"
+n1="long name"
+f1="$dir/$n1"
+touch "$f1"
+n2="shorter"
+f2="$dir/$n2"
+touch "$f2"
+sync
+
+echo 'launch a program that locks a given file, it will create $dir/lock_test'
+python3 $owndir/lock_test_help.py "$dir" "$n1" &
+
+echo 'wait until this lock_test lock file was created by program'
+while [ ! -f "$dir/lock_test" ]; do
+  sleep .1
+done
+sync
+
+echo 'expecting the lock file to reflect "long name"'
+echo "launched first, locked by: '$(cat "$dir/lock_test")'"
+
+echo 'launching second program, should find the lock intact and wait'
+python3 $owndir/lock_test_help.py "$dir" "$n2" &
+while [ ! -f "$f2.ready" ]; do
+  sleep .1
+done
+sleep 1
+sync
+echo "launched second, locked by: '$(cat "$dir/lock_test")'"
+
+echo 'drop the first lock, $f1 removal signals the first process to stop locking'
+rm "$f1"
+
+echo 'wait for first program to carry out the lock release'
+while [ ! -f "$f1.done" ]; do
+  sleep .1
+done
+
+echo 'now expecting second program to lock'
+echo "waited, locked by: '$(cat "$dir/lock_test")'"
+
+echo 'release the second program also'
+rm "$f2"
+while [ ! -f "$f2.done" ]; do
+  sleep .1
+done
+
+echo 'expecting the lock to be gone'
+echo "waited more, locked by: '$(cat "$dir/lock_test")'"
+rm -rf "$dir"
diff --git a/selftest/lock_test/lock_test_help.py b/selftest/lock_test/lock_test_help.py
new file mode 100644
index 0000000..d68bbf8
--- /dev/null
+++ b/selftest/lock_test/lock_test_help.py
@@ -0,0 +1,25 @@
+import sys
+import time
+import os
+
+import _prep
+
+from osmo_gsm_tester.core.util import FileLock, touch_file
+
+testdir, name = sys.argv[1:]
+stop_signalling_file = os.path.join(testdir, name)
+if not os.path.isfile(stop_signalling_file):
+    print('expected a stop-file %r' % stop_signalling_file)
+    exit(1)
+
+lockfile_path = os.path.join(testdir, 'lock_test')
+fl = FileLock(lockfile_path, name)
+
+touch_file(stop_signalling_file + '.ready')
+
+with fl:
+    while os.path.exists(stop_signalling_file):
+        time.sleep(.1)
+touch_file(stop_signalling_file + '.done')
+
+# vim: expandtab tabstop=4 shiftwidth=4