From a463796aa4b01db6119c5ea6c44d220013160a35 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Tue, 31 Jan 2017 18:09:15 +0100
Subject: [PATCH] Added MS tests (refs #20214).

---
 tester.py | 76 ++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 55 insertions(+), 21 deletions(-)

diff --git a/tester.py b/tester.py
index f5ca0a66..842ad6d7 100755
--- a/tester.py
+++ b/tester.py
@@ -35,7 +35,8 @@ sys.stderr = sys.stdout
 
 
 class Tester():
-    USAGE = '''%s [-e] [-d] [-h]
+    USAGE = '''%s [-ms] [-e] [-d] [-h]
+    -ms: run MediaServer tests in addition of standard tests.
     -e: send email with report.
     -d: debug mode (can be started with non root users).
     -h: show this message.''' % __file__
@@ -59,8 +60,6 @@ class Tester():
             sys.path.append(root_dir)
         # Check that this script is run by root
         debug = '-d' in args
-        if debug:
-            args.remove('-d')
         whoami = subprocess.check_output(['whoami']).decode('utf-8').strip()
         if whoami != 'root' and not debug:
             log('This script should be run as root user.')
@@ -72,9 +71,11 @@ class Tester():
             sys.exit(1)
         # Check for email value
         email = '-e' in args
-        if email:
-            args.remove('-e')
-        exit_code = self.run_tests(email)
+        ms = '-ms' in args
+        tests = self.discover_tests(ms)
+        if not tests:
+            sys.exit(1)
+        exit_code = self.run_tests(tests, email)
         sys.exit(exit_code)
 
     def get_file_description(self, path):
@@ -98,28 +99,61 @@ class Tester():
                     break
         return description.strip()
 
-    def run_tests(self, email=False):
+    def discover_tests(self, ms=False):
+        tests = list()
+        # Get standard tests
         path = os.path.join(self.root_dir, 'tests')
         if not os.path.isdir(path):
             log('The tests dir is missing ("%s").' % path)
-            return 1
-        os.chdir(path)
+            return tests
         names = os.listdir(path)
         names.sort()
         if not names:
             log('The tests dir is empty ("%s").' % path)
-            return 1
+            return tests
+        for name in names:
+            test_path = os.path.join(path, name)
+            description = self.get_file_description(test_path)
+            tests.append((name, description, [test_path]))
+        # Get MediaServer tests
+        if ms:
+            # Clone testing suite
+            ms_path = os.path.join(path, 'ms-testing-suite')
+            if os.path.exists(ms_path):
+                os.chdir(ms_path)
+                subprocess.check_call(['git', 'pull'])
+                os.chdir(self.root_dir)
+            else:
+                subprocess.check_call(['git', 'clone', 'https://git.ubicast.net/mediaserver/ms-testing-suite.git', ms_path])
+            ms_tests = ['ms_vod_tester.py']
+            # Get MS instances
+            users = list()
+            for user in os.listdir('/home'):
+                if os.path.exists('/home/%s/msinstance' % user):
+                    users.append(user)
+            # check that Wowza is installed
+            wowza_dir = '/usr/local/WowzaStreamingEngine'
+            if not os.path.exists(wowza_dir):
+                log('Info: Wowza is not installed ("%s" does not exist).' % wowza_dir)
+            else:
+                ms_tests.append('ms_live_tester.py')
+            # Add tests to list
+            for user in users:
+                for name in ms_tests:
+                    test_path = os.path.join(path, name)
+                    description = self.get_file_description(test_path)
+                    tests.append(('%s (%s)' % (name, user), description, [test_path, user]))
+        return tests
+
+    def run_tests(self, tests, email=False):
         results = list()
         exit_code = 0
         # Run all tests
-        for name in names:
+        for name, description, command in tests:
             log('\033[1;95m-- Test "%s" --\033[0;0m' % name)
-            test_path = os.path.join(path, name)
-            # Get test description
-            description = self.get_file_description(test_path)
             # Run test
             try:
-                p = subprocess.Popen([test_path], stdin=sys.stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                p = subprocess.Popen(command, stdin=sys.stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                 out, err = p.communicate()
                 if out:
                     log(out.decode('utf-8').strip())
@@ -130,22 +164,22 @@ class Tester():
             except Exception as e:
                 exit_code = 1
                 log(e)
-                results.append((False, test_path, description))
+                results.append((name, description, command, False))
             else:
-                results.append((True, test_path, description))
+                results.append((name, description, command, True))
+        # Display results
         log('\nTests results:')
         html_report = '<table border="1">'
         html_report += '\n<tr><th>Test</th><th>Result</th><th>Description</th></tr>'
-        for success, test_path, description in results:
-            file_name = os.path.basename(test_path)
+        for name, description, command, success in results:
             if success:
                 html_result = '<span style="color: green;">success</span>'
                 term_result = '\033[92msuccess\033[0m'
             else:
                 html_result = '<span style="color: red;">failure</span>'
                 term_result = '\033[91mfailure\033[0m'
-            log('  %s: %s' % (file_name, term_result))
-            html_report += '\n<tr><td>%s</td><td>%s</td><td>%s</td></tr>' % (file_name, html_result, description.replace('\n', '<br/>\n'))
+            log('  %s: %s' % (name, term_result))
+            html_report += '\n<tr><td>%s</td><td>%s</td><td>%s</td></tr>' % (name, html_result, description.replace('\n', '<br/>\n'))
         html_report += '\n</table>'
         # Send email
         if email:
-- 
GitLab