[master 2/5] Convert the proxy script to a prereq.
dashea
installerbot-noreply at redhat.com
Thu Oct 8 21:37:43 UTC 2015
From: David Shea <dshea at redhat.com>
This way the meat of the script is just a regular python file that can
be syntax highlighted and pylinted and tested as such. Convert the
python script to a %pre script in the prereqs for the proxy tests.
---
tests/kickstart_tests/proxy-auth.sh | 4 ++
tests/kickstart_tests/proxy-cmdline.sh | 4 ++
tests/kickstart_tests/proxy-kickstart.sh | 4 ++
tests/kickstart_tests/scripts/Makefile.prereqs | 11 ++++
tests/kickstart_tests/scripts/proxy-common.ks | 79 --------------------------
tests/kickstart_tests/scripts/proxy.py | 77 +++++++++++++++++++++++++
6 files changed, 100 insertions(+), 79 deletions(-)
delete mode 100644 tests/kickstart_tests/scripts/proxy-common.ks
create mode 100644 tests/kickstart_tests/scripts/proxy.py
diff --git a/tests/kickstart_tests/proxy-auth.sh b/tests/kickstart_tests/proxy-auth.sh
index c5cf37e..f42f71d 100755
--- a/tests/kickstart_tests/proxy-auth.sh
+++ b/tests/kickstart_tests/proxy-auth.sh
@@ -21,6 +21,10 @@ TESTTYPE="method proxy"
. ${KSTESTDIR}/functions.sh
+prereqs() {
+ echo proxy-common.ks
+}
+
prepare() {
ks=$1
tmpdir=$2
diff --git a/tests/kickstart_tests/proxy-cmdline.sh b/tests/kickstart_tests/proxy-cmdline.sh
index 6054524..ad93044 100755
--- a/tests/kickstart_tests/proxy-cmdline.sh
+++ b/tests/kickstart_tests/proxy-cmdline.sh
@@ -21,6 +21,10 @@ TESTTYPE="method proxy"
. ${KSTESTDIR}/functions.sh
+prereqs() {
+ echo proxy-common.ks
+}
+
kernel_args() {
echo vnc inst.proxy=http://127.0.0.1:8080
}
diff --git a/tests/kickstart_tests/proxy-kickstart.sh b/tests/kickstart_tests/proxy-kickstart.sh
index c3e1d94..bdca190 100755
--- a/tests/kickstart_tests/proxy-kickstart.sh
+++ b/tests/kickstart_tests/proxy-kickstart.sh
@@ -21,6 +21,10 @@ TESTTYPE="method proxy"
. ${KSTESTDIR}/functions.sh
+prereqs() {
+ echo proxy-common.ks
+}
+
prepare() {
ks=$1
tmpdir=$2
diff --git a/tests/kickstart_tests/scripts/Makefile.prereqs b/tests/kickstart_tests/scripts/Makefile.prereqs
index 5d85b17..698b9f8 100644
--- a/tests/kickstart_tests/scripts/Makefile.prereqs
+++ b/tests/kickstart_tests/scripts/Makefile.prereqs
@@ -11,3 +11,14 @@
# This is a regular Makefile without any automake weirdness, so just keep it
# friendly.
+
+proxy-common.ks: proxy.py
+ @# Wrap the proxy server in a %pre script that writes the server file to
+ @# to location in the installer environment and runs it in the background
+ @server_py="$$(< proxy.py)"; \
+ echo '%pre --erroronfail' > $@
+ echo 'cat - << "EOF" > /tmp/proxy-test.py' >> $@
+ cat proxy.py >> $@
+ echo 'EOF' >> $@
+ echo 'python3 /tmp/proxy-test.py > /dev/null 2>&1 &' >> $@
+ echo '%end' >> $@
diff --git a/tests/kickstart_tests/scripts/proxy-common.ks b/tests/kickstart_tests/scripts/proxy-common.ks
deleted file mode 100644
index a95097a..0000000
--- a/tests/kickstart_tests/scripts/proxy-common.ks
+++ /dev/null
@@ -1,79 +0,0 @@
-# Start a super-simple proxy server on localhost
-# A list of proxied requests will be saved to /tmp/proxy.log
-%pre --erroronfail
-# Write the proxy script to a file in /tmp
-cat - << "EOF" > /tmp/proxy-test.py
-from http.server import SimpleHTTPRequestHandler
-import socketserver
-from urllib.request import urlopen
-import os
-from base64 import b64decode
-
-import logging
-log = logging.getLogger("proxy_test")
-log_handler = logging.FileHandler('/tmp/proxy.log')
-log.setLevel(logging.INFO)
-log.addHandler(log_handler)
-
-class ProxyHandler(SimpleHTTPRequestHandler):
- def send_authenticate(self):
- self.send_response(407)
- self.send_header('Proxy-Authenticate', 'Basic realm=proxy-test')
- self.end_headers()
-
- def authenticate(self):
- # If there is no /tmp/proxy.password file, anything goes
- if not os.path.exists('/tmp/proxy.password'):
- return True
-
- # If there is no Authorization header, send a 407, Proxy authentication required
- authorization = self.headers['Proxy-Authorization']
- if not authorization:
- self.send_authenticate()
- return False
-
- # Parse the Authorization header. It should be "Basic" followed
- # user:pass encoded in base64
- if not authorization.startswith('Basic'):
- self.send_authenticate()
- return False
-
- try:
- client_auth = b64decode(authorization.split()[1])
- except IndexError:
- self.send_authenticate()
- return False
-
- with open('/tmp/proxy.password', 'rb') as f:
- server_auth = f.readline().strip()
-
- if client_auth != server_auth:
- self.send_authenticate()
- return False
-
- return True
-
- def do_GET(self):
- if not self.authenticate():
- return
-
- # Log the path then proxy the request via urllib
- log.info(self.path)
- data = urlopen(self.path).read()
- self.send_response(200)
- self.send_header('Content-Length', str(len(data)))
- self.end_headers()
- self.wfile.write(data)
-
-class ProxyServer(socketserver.TCPServer):
- allow_reuse_address = True
-
- def __init__(self):
- socketserver.TCPServer.__init__(self, ('', 8080), ProxyHandler)
-
-ProxyServer().serve_forever()
-EOF
-
-# Run the server in the background and exit
-python3 /tmp/proxy-test.py > /dev/null 2>&1 &
-%end
diff --git a/tests/kickstart_tests/scripts/proxy.py b/tests/kickstart_tests/scripts/proxy.py
new file mode 100644
index 0000000..15849e9
--- /dev/null
+++ b/tests/kickstart_tests/scripts/proxy.py
@@ -0,0 +1,77 @@
+#!/usr/bin/python3
+
+# Start a super-simple proxy server on localhost
+# A list of proxied requests will be saved to /tmp/proxy.log
+
+# Ignore any interruptible calls
+# pylint: disable=interruptible-system-call
+
+from http.server import SimpleHTTPRequestHandler
+import socketserver
+from urllib.request import urlopen
+import os
+from base64 import b64decode
+
+import logging
+log = logging.getLogger("proxy_test")
+log_handler = logging.FileHandler('/tmp/proxy.log')
+log.setLevel(logging.INFO)
+log.addHandler(log_handler)
+
+class ProxyHandler(SimpleHTTPRequestHandler):
+ def send_authenticate(self):
+ self.send_response(407)
+ self.send_header('Proxy-Authenticate', 'Basic realm=proxy-test')
+ self.end_headers()
+
+ def authenticate(self):
+ # If there is no /tmp/proxy.password file, anything goes
+ if not os.path.exists('/tmp/proxy.password'):
+ return True
+
+ # If there is no Authorization header, send a 407, Proxy authentication required
+ authorization = self.headers['Proxy-Authorization']
+ if not authorization:
+ self.send_authenticate()
+ return False
+
+ # Parse the Authorization header. It should be "Basic" followed
+ # user:pass encoded in base64
+ if not authorization.startswith('Basic'):
+ self.send_authenticate()
+ return False
+
+ try:
+ client_auth = b64decode(authorization.split()[1])
+ except IndexError:
+ self.send_authenticate()
+ return False
+
+ with open('/tmp/proxy.password', 'rb') as f:
+ server_auth = f.readline().strip()
+
+ if client_auth != server_auth:
+ self.send_authenticate()
+ return False
+
+ return True
+
+ def do_GET(self):
+ if not self.authenticate():
+ return
+
+ # Log the path then proxy the request via urllib
+ log.info(self.path)
+ data = urlopen(self.path).read()
+ self.send_response(200)
+ self.send_header('Content-Length', str(len(data)))
+ self.end_headers()
+ self.wfile.write(data)
+
+class ProxyServer(socketserver.TCPServer):
+ allow_reuse_address = True
+
+ def __init__(self):
+ socketserver.TCPServer.__init__(self, ('', 8080), ProxyHandler)
+
+ProxyServer().serve_forever()
--
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/0e4decf7fdfc76c31db13b7bcc80d379e1c27bcf
More information about the anaconda-patches
mailing list