From a015f63679877d592bd34f078451522eda132dfe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Tue, 7 Feb 2017 22:30:59 +0100
Subject: [PATCH] Handle socket error in Nginx test.

---
 tests/test_nginx_vhosts.py | 63 ++++++++++++++++++++------------------
 1 file changed, 34 insertions(+), 29 deletions(-)

diff --git a/tests/test_nginx_vhosts.py b/tests/test_nginx_vhosts.py
index 61bc0a4a..e413f8c9 100755
--- a/tests/test_nginx_vhosts.py
+++ b/tests/test_nginx_vhosts.py
@@ -53,39 +53,44 @@ for name in os.listdir(nginx_dir):
         found = True
         url = '%s://%s' % ('https' if https else 'http', domain)
         sys.stdout.write('Testing url "%s": ' % url)
-        ip = socket.gethostbyname(domain)
-        if ip != '127.0.0.1':
-            sys.stdout.write('\033[91mKO (domain is not resolved with 127.0.0.1)\033[0m')
+        try:
+            ip = socket.gethostbyname(domain)
+        except Exception as e:
+            sys.stdout.write('\033[91mKO (domain is not resolved: %s)\033[0m' % e)
             errors += 1
         else:
-            try:
-                req = requests.get(url, verify=False, proxies={'http': '', 'https': ''}, timeout=10)
-            except Exception as e:
-                code = str(e)
-            else:
-                code = req.status_code
-            if code not in (200, 403):
-                sys.stdout.write('\033[91mKO (%s)\033[0m' % code)
+            if ip != '127.0.0.1':
+                sys.stdout.write('\033[91mKO (domain is not resolved with 127.0.0.1)\033[0m')
                 errors += 1
             else:
-                sys.stdout.write('\033[92mOK (%s)\033[0m' % code)
-                if 'mediaserver' in name and wowza_dir:
-                    # test /streaming url
-                    sys.stdout.write(', streaming: ')
-                    try:
-                        req = requests.get(url + '/streaming/', verify=False, proxies={'http': '', 'https': ''}, timeout=10)
-                    except Exception as e:
-                        code = str(e)
-                    else:
-                        code = req.status_code
-                    if code != 200:
-                        sys.stdout.write('\033[91mKO (%s)\033[0m' % code)
-                        errors += 1
-                    elif 'Wowza Streaming Engine' not in req.text:
-                        sys.stdout.write('\033[91mKO (%s, %s)\033[0m' % (code, req.text.replace('\n', ' ')[:200]))
-                        errors += 1
-                    else:
-                        sys.stdout.write('\033[92mOK (%s)\033[0m' % code)
+                try:
+                    req = requests.get(url, verify=False, proxies={'http': '', 'https': ''}, timeout=10)
+                except Exception as e:
+                    code = str(e)
+                else:
+                    code = req.status_code
+                if code not in (200, 403):
+                    sys.stdout.write('\033[91mKO (%s)\033[0m' % code)
+                    errors += 1
+                else:
+                    sys.stdout.write('\033[92mOK (%s)\033[0m' % code)
+                    if 'mediaserver' in name and wowza_dir:
+                        # test /streaming url
+                        sys.stdout.write(', streaming: ')
+                        try:
+                            req = requests.get(url + '/streaming/', verify=False, proxies={'http': '', 'https': ''}, timeout=10)
+                        except Exception as e:
+                            code = str(e)
+                        else:
+                            code = req.status_code
+                        if code != 200:
+                            sys.stdout.write('\033[91mKO (%s)\033[0m' % code)
+                            errors += 1
+                        elif 'Wowza Streaming Engine' not in req.text:
+                            sys.stdout.write('\033[91mKO (%s, %s)\033[0m' % (code, req.text.replace('\n', ' ')[:200]))
+                            errors += 1
+                        else:
+                            sys.stdout.write('\033[92mOK (%s)\033[0m' % code)
         sys.stdout.write('.\n')
 
 if errors:
-- 
GitLab