Skip to content

Commit

Permalink
Add integration tests for --quiet flag
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Mar 7, 2019
1 parent 25609fc commit 5ef0634
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 3 deletions.
15 changes: 13 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@
STATUS_PORT = 13100
TIMEOUT = 5

def run_ghostunnel(args):
def run_ghostunnel(args, stdout=None, stderr=None):
"""Helper to run ghostunnel in integration test mode"""

# Default shuthdown timeout to speed up tests (otherwise defaults to 5m)
if not any('shutdown-timeout' in f for f in args):
args.append('--shutdown-timeout=5s')

# Pass args through env var into integration test hook
os.environ["GHOSTUNNEL_INTEGRATION_TEST"] = "true"
os.environ["GHOSTUNNEL_INTEGRATION_ARGS"] = json.dumps(args)

# Print args for debugging
print_ok("running with args:\n {0}".format(' \ \n '.join(args)))

# Run it, hook up stdout/stderr if desired
test = os.path.basename(sys.argv[0]).replace('.py', '.out')
return Popen([
'../ghostunnel.test',
'-test.run=TestIntegrationMain',
'-test.coverprofile=coverage-{0}'.format(test)])
'-test.coverprofile=coverage-{0}'.format(test)],
stdout=stdout, stderr=stderr)

def terminate(ghostunnel):
"""Gracefully terminate ghostunnel (with timeout)"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test-server-allow-uri-san.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
pair1 = SocketPair(
TlsClient('client1', 'root', 13001), TcpServer(13002))
pair1.validate_can_send_from_client("toto", "pair1 works")
pair1.validate_can_send_from_server
pair1.validate_can_send_from_server("toto", "pair1 works")

try:
pair2 = SocketPair(
Expand Down
70 changes: 70 additions & 0 deletions tests/test-server-quiet-all-logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3

"""
Ensures that --quiet=all disables any and all logging.
"""

from common import LOCALHOST, RootCert, STATUS_PORT, TcpClient, TlsClient, TcpServer, print_ok, run_ghostunnel, terminate, SocketPair
import urllib.request
import urllib.error
import urllib.parse
import os
import signal
import subprocess
import json

if __name__ == '__main__':
ghostunnel = None
try:
# create certs
root = RootCert('root')
root.create_signed_cert('server')
root.create_signed_cert('client')

# start ghostunnel
# hack: point target to STATUS_PORT so that /_status doesn't 503.
ghostunnel = run_ghostunnel(['server',
'--quiet=all',
'--listen={0}:13001'.format(LOCALHOST),
'--target={0}:13002'.format(LOCALHOST),
'--keystore=server.p12',
'--cacert=root.crt',
'--allow-ou=client',
'--status={0}:{1}'.format(LOCALHOST,
STATUS_PORT)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

def urlopen(path):
return urllib.request.urlopen(path, cafile='root.crt')

# block until ghostunnel is up
TcpClient(STATUS_PORT).connect(20)

# send some requests to status endpoints
metrics = json.loads(str(urlopen(
'https://{0}:{1}/_metrics'.format(LOCALHOST, STATUS_PORT)).read(), 'utf-8'))

# send some data through proxy
pair1 = SocketPair(
TlsClient('client', 'root', 13001), TcpServer(13002))
pair1.validate_can_send_from_client('toto', 'works')
pair1.validate_can_send_from_server('toto', 'works')
pair1.cleanup()

terminate(ghostunnel)

# make sure no logs printed
out, err = ghostunnel.communicate()

print('stdout (len={0}):'.format(len(out)))
print(out)
print('stderr (len={0}):'.format(len(err)))
print(err)

if len(err) > 0:
raise Exception('ghostunnel logged to stderr with --quiet=all')

print_ok('OK')
finally:
terminate(ghostunnel)
70 changes: 70 additions & 0 deletions tests/test-server-quiet-conn-logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3

"""
Ensures that --quiet=conns disables logging about new connections.
"""

from common import LOCALHOST, RootCert, STATUS_PORT, TcpClient, TlsClient, TcpServer, print_ok, run_ghostunnel, terminate, SocketPair
import urllib.request
import urllib.error
import urllib.parse
import os
import signal
import subprocess
import json

if __name__ == '__main__':
ghostunnel = None
try:
# create certs
root = RootCert('root')
root.create_signed_cert('server')
root.create_signed_cert('client')

# start ghostunnel
# hack: point target to STATUS_PORT so that /_status doesn't 503.
ghostunnel = run_ghostunnel(['server',
'--quiet=conns',
'--listen={0}:13001'.format(LOCALHOST),
'--target={0}:13002'.format(LOCALHOST),
'--keystore=server.p12',
'--cacert=root.crt',
'--allow-ou=client',
'--status={0}:{1}'.format(LOCALHOST,
STATUS_PORT)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

def urlopen(path):
return urllib.request.urlopen(path, cafile='root.crt')

# block until ghostunnel is up
TcpClient(STATUS_PORT).connect(20)

# send some requests to status endpoints
metrics = json.loads(str(urlopen(
'https://{0}:{1}/_metrics'.format(LOCALHOST, STATUS_PORT)).read(), 'utf-8'))

# send some data through proxy
pair1 = SocketPair(
TlsClient('client', 'root', 13001), TcpServer(13002))
pair1.validate_can_send_from_client('toto', 'works')
pair1.validate_can_send_from_server('toto', 'works')
pair1.cleanup()

terminate(ghostunnel)

# make sure no logs printed
out, err = ghostunnel.communicate()

print('stdout (len={0}):'.format(len(out)))
print(out)
print('stderr (len={0}):'.format(len(err)))
print(err)

if 'opening pipe' in err.decode('utf-8'):
raise Exception('ghostunnel logged connection log to stderr with --quiet=all')

print_ok('OK')
finally:
terminate(ghostunnel)

0 comments on commit 5ef0634

Please sign in to comment.