Provides connection check for package download. 65/18565/4
authorLuke Hinds <lukehinds@gmail.com>
Fri, 12 Aug 2016 14:14:47 +0000 (15:14 +0100)
committerLuke Hinds <lukehinds@gmail.com>
Fri, 12 Aug 2016 14:37:22 +0000 (15:37 +0100)
JIRA: FUNCTEST-388

Performs simple connection test, using socket from std python
library. If no connection can be made, socket will timeout and
gracefully move to trying the next compute / control node.

Change-Id: I0048d74d607c8824688c73791c9646f9e9186756
Signed-off-by: Luke Hinds <lukehinds@gmail.com>
testcases/security_scan/scripts/internet_check.py [new file with mode: 0644]
testcases/security_scan/security_scan.py

diff --git a/testcases/security_scan/scripts/internet_check.py b/testcases/security_scan/scripts/internet_check.py
new file mode 100644 (file)
index 0000000..1bed50a
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/python
+#
+# Copyright (c) 2016 Red Hat
+# Luke Hinds (lhinds@redhat.com)
+# This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Performs simple connection check, falls to default timeout of 10 seconds
+
+import socket
+
+TEST_HOST = "google.com"
+
+
+def is_connected():
+    try:
+        host = socket.gethostbyname(TEST_HOST)
+        socket.create_connection((host, 80), 2)
+        return True
+    except:
+        return False
+print is_connected()
index d39c290..36b7954 100755 (executable)
@@ -73,21 +73,27 @@ def run_tests(host, nodetype):
     port = cfgparse.get(nodetype, 'port')
     connect.logger.info("Host: {0} Selected Profile: {1}".format(host,
                                                                  nodetype))
-    connect.logger.info("Creating temp file structure..")
-    createfiles(host, port, user, localkey)
-    connect.logger.info("Installing OpenSCAP...")
-    install_pkg(host, port, user, localkey)
-    connect.logger.info("Running scan...")
-    run_scanner(host, port, user, localkey, nodetype)
-    clean = cfgparse.get(nodetype, 'clean')
-    connect.logger.info("Post installation tasks....")
-    post_tasks(host, port, user, localkey, nodetype)
-    if clean:
-        connect.logger.info("Cleaning down environment....")
-        connect.logger.info("Removing OpenSCAP....")
-        removepkg(host, port, user, localkey, nodetype)
-        connect.logger.info("Deleting tmp file and reports (remote)...")
-        cleandir(host, port, user, localkey, nodetype)
+    connect.logger.info("Checking internet for package installation...")
+    if internet_check(host, nodetype):
+        connect.logger.info("Internet Connection OK.")
+        connect.logger.info("Creating temp file structure..")
+        createfiles(host, port, user, localkey)
+        connect.logger.info("Installing OpenSCAP...")
+        install_pkg(host, port, user, localkey)
+        connect.logger.info("Running scan...")
+        run_scanner(host, port, user, localkey, nodetype)
+        clean = cfgparse.get(nodetype, 'clean')
+        connect.logger.info("Post installation tasks....")
+        post_tasks(host, port, user, localkey, nodetype)
+        if clean:
+            connect.logger.info("Cleaning down environment....")
+            connect.logger.info("Removing OpenSCAP....")
+            removepkg(host, port, user, localkey, nodetype)
+            connect.logger.info("Deleting tmp file and reports (remote)...")
+            cleandir(host, port, user, localkey, nodetype)
+    else:
+        connect.logger.error("Internet timeout. Moving on to next node..")
+        pass
 
 
 def nova_iterate():
@@ -106,6 +112,22 @@ def nova_iterate():
                 run_tests(host, nodetype)
 
 
+def internet_check(host, nodetype):
+    import connect
+    user = cfgparse.get(nodetype, 'user')
+    port = cfgparse.get(nodetype, 'port')
+    localpath = functest_dir + 'scripts/internet_check.py'
+    remotepath = '/tmp/internet_check.py'
+    com = 'python /tmp/internet_check.py'
+    testconnect = connect.ConnectionManager(host, port, user, localkey,
+                                            localpath, remotepath, com)
+    connectionresult = testconnect.remotescript()
+    if connectionresult.rstrip() == 'True':
+        return True
+    else:
+        return False
+
+
 def createfiles(host, port, user, localkey):
     import connect
     global tmpdir