From df1e233cc7212df8b04e3544790136b9331cd6e5 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Tue, 18 Apr 2017 11:19:46 +0200
Subject: [PATCH] NEO: wait that the MySQL server is started before running
 tests

It already happened that the first tests of the suite failed
because the MySQL server was not fully started:

    OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'.../var/run/mariadb.sock\' (2 "No such file or directory")')
---
 software/neoppod/runTestSuite.in | 62 ++++++++++++++++----------------
 software/neoppod/software.cfg    |  2 +-
 2 files changed, 33 insertions(+), 31 deletions(-)

diff --git a/software/neoppod/runTestSuite.in b/software/neoppod/runTestSuite.in
index 22508e075..06fc56b09 100644
--- a/software/neoppod/runTestSuite.in
+++ b/software/neoppod/runTestSuite.in
@@ -4,7 +4,7 @@
 """
 import argparse, os, re, shutil, subprocess, sys, traceback
 from erp5.util import taskdistribution
-from time import gmtime, strftime
+from time import gmtime, sleep, strftime, time
 
 # pattern to get test counts from stdout
 SUMMARY_RE = re.compile(
@@ -82,7 +82,8 @@ def main():
     if not test_result_line:
       break
 
-    temp = os.path.join(TEMP_DIRECTORY, 'tests-' + test_result_line.name)
+    adapter = test_result_line.name
+    temp = os.path.join(TEMP_DIRECTORY, 'tests-' + adapter)
     if os.path.exists(temp):
       shutil.rmtree(temp)
     os.mkdir(temp)
@@ -90,46 +91,47 @@ def main():
     args = [RUN_NEO_TESTS_COMMAND, '-ufz']
     command = ' '.join(args)
     env = {'TEMP': temp,
-           'NEO_TESTS_ADAPTER': test_result_line.name,
+           'NEO_TESTS_ADAPTER': adapter,
            'NEO_TEST_ZODB_FUNCTIONAL': '1',
-           'NEO_DB_USER': 'root',
-           'NEO_DB_SOCKET': NEO_DB_SOCKET}
+           'NEO_DB_USER': 'root'}
     try:
+      if adapter == 'MySQL':
+        env['NEO_DB_SOCKET'] = NEO_DB_SOCKET
+        timeout = time() + 60
+        while not os.path.exists(NEO_DB_SOCKET):
+          if timeout < time():
+            raise RuntimeError("MySQL server not started")
+          sleep(1)
       with open(os.devnull) as stdin:
         p = subprocess.Popen(args, stdin=stdin, stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE, env=env)
     except Exception:
-      # Catch any exception here, to warn user instead of being silent,
-      # by generating fake error result
-      result = dict(status_code=-1,
-                    command=command,
-                    stderr=traceback.format_exc(),
-                    stdout='')
-      # XXX: inform test node master of error
-      raise EnvironmentError(result)
+      end = time()
+      stderr = traceback.format_exc()
+      status_dict = {}
+    else:
+      stdout, stderr = p.communicate()
+      end = time()
+      test_count, unexpected_count, expected_count, skip_count, duration = \
+        parseTestStdOut(stdout)
 
-    # parse test stdout / stderr, hint to speed up use files first!
-    stdout, stderr = p.communicate()
-    date = strftime("%Y/%m/%d %H:%M:%S", gmtime())
-    test_count, unexpected_count, expected_count, skip_count, duration = \
-      parseTestStdOut(stdout)
+      status_dict = dict(
+        test_count = test_count,
+        error_count = unexpected_count, # XXX
+        failure_count = expected_count, # XXX
+        skip_count = skip_count,
+        stdout= stdout)
 
-    # print to stdout so we can see in testnode logs
-    sys.stdout.write(stdout)
+      # print to stdout so we can see in testnode logs
+      sys.stdout.write(stdout)
     sys.stderr.write(stderr)
 
     # report status back to Nexedi ERP5
     test_result_line.stop(
-        test_count = test_count,
-        error_count = unexpected_count, # XXX
-        failure_count = expected_count, # XXX
-        skip_count = skip_count,
-        duration = duration,
-        date = date,
-        command = command,
-        stdout= stdout,
-        stderr= stderr,
-        html_test_result='')
+      command = command,
+      date = strftime("%Y/%m/%d %H:%M:%S", gmtime(end)),
+      stderr=stderr,
+      **status_dict)
 
 if __name__ == "__main__":
     main()
diff --git a/software/neoppod/software.cfg b/software/neoppod/software.cfg
index c8eb53c5c..2eddc3ef2 100644
--- a/software/neoppod/software.cfg
+++ b/software/neoppod/software.cfg
@@ -26,7 +26,7 @@ md5sum = ee8401a4e7d82bf488a57e3399f9ce48
 [runTestSuite.in]
 recipe = slapos.recipe.build:download
 url = ${:_profile_base_location_}/${:_buildout_section_name_}
-md5sum = 050593aef62fd4aa241d8ad378111c36
+md5sum = c5599e0086f85999e222a3bef627f93b
 
 [runTestSuite_py]
 recipe = zc.recipe.egg
-- 
2.30.9