ttcn3: Refactor ttcn3 launching bits into a testlib

This way new tests can be more easily created which run some specific
TTCN3 test.

Change-Id: Ic61c7b7db9cf3050dc4b101ef0fb181421577424
diff --git a/ttcn3/suites/ttcn3_bts_tests/lib/testlib.py b/ttcn3/suites/ttcn3_bts_tests/lib/testlib.py
new file mode 100644
index 0000000..6a2bbaa
--- /dev/null
+++ b/ttcn3/suites/ttcn3_bts_tests/lib/testlib.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+import os
+from mako.template import Template
+
+from osmo_gsm_tester.testenv import *
+
+def run_ttcn3(suite, test_obj, testdir, bts, osmocon, nat_rsl_ip, ttcn3_test_execute):
+    own_dir = testdir
+    script_file = os.path.join(testdir, 'scripts', 'run_ttcn3_docker.sh')
+    bts_tmpl_file = os.path.join(testdir, 'scripts', 'BTS_Tests.cfg.tmpl')
+    script_run_dir = test_obj.get_run_dir().new_dir('ttcn3')
+    bts_cfg_file = os.path.join(str(script_run_dir), 'BTS_Tests.cfg')
+    junit_ttcn3_dst_file = os.path.join(str(suite.trial.get_run_dir()), 'trial-') + test_obj.basename + '.xml'
+    if bts.bts_type() == 'osmo-bts-trx':
+        pcu_available = True
+        pcu_sk = bts.pcu_socket_path()
+    else: # PCU unix socket not available locally
+        pcu_available = False
+        pcu_sk = ''
+    docker_cmd = (script_file, str(script_run_dir), junit_ttcn3_dst_file, nat_rsl_ip, osmocon.l2_socket_path(), pcu_sk)
+
+    print('Creating template')
+    mytemplate = Template(filename=bts_tmpl_file)
+    r = mytemplate.render(btsvty_ctrl_hostname=bts.remote_addr(), pcu_available=pcu_available, ttcn3_test_execute=ttcn3_test_execute)
+    with open(bts_cfg_file, 'w') as f:
+        f.write(r)
+
+
+    print('Starting TTCN3 test suite')
+    proc = process.Process('ttcn3', script_run_dir, docker_cmd)
+    try:
+        proc.launch()
+        print('TTCN3 test suite launched, waiting until it finishes')
+        proc.wait(timeout=3600)
+    except Exception as e:
+        proc.terminate()
+        raise e
+
+    if proc.result != 0:
+        raise RuntimeError("run_ttcn3_docker.sh exited with error code %d" % proc.result)
+
+    print('Done')