Skip to content

Commit

Permalink
Test metrics better, more flag handling checks
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Oct 31, 2018
1 parent c8dba8e commit 64d5d5a
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 4 deletions.
23 changes: 20 additions & 3 deletions tests/test-invalid-client-flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
try:
# create certs
root = RootCert('root')
root.create_signed_cert('server')
root.create_signed_cert('client')

# start ghostunnel with bad flags
# start ghostunnel with bad proxy
ghostunnel = run_ghostunnel(['client',
'--listen={0}:13001'.format(LOCALHOST),
'--target={0}:13002'.format(LOCALHOST),
'--keystore=server.p12',
'--keystore=client.p12',
'--connect-proxy=ftp://invalid',
'--cacert=root.crt',
'--status={0}:{1}'.format(LOCALHOST,
Expand All @@ -26,5 +26,22 @@
'ghostunnel terminated with zero, though flags were invalid')
else:
print_ok("OK (terminated)")

# start ghostunnel with bad client listen addr
ghostunnel = run_ghostunnel(['client',
'--listen=invalid',
'--target={0}:13002'.format(LOCALHOST),
'--keystore=client.p12',
'--cacert=root.crt',
'--status={0}:{1}'.format(LOCALHOST,
STATUS_PORT)])

# wait for ghostunnel to exit and make sure error code is not zero
ret = ghostunnel.wait(timeout=20)
if ret == 0:
raise Exception(
'ghostunnel terminated with zero, though flags were invalid')
else:
print_ok("OK (terminated)")
finally:
terminate(ghostunnel)
38 changes: 37 additions & 1 deletion tests/test-invalid-server-flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
root = RootCert('root')
root.create_signed_cert('server')

# start ghostunnel with bad flags
# start ghostunnel with bad access flags
ghostunnel = run_ghostunnel(['server',
'--listen={0}:13001'.format(LOCALHOST),
'--target={0}:13002'.format(LOCALHOST),
Expand All @@ -25,5 +25,41 @@
'ghostunnel terminated with zero, though flags were invalid')
else:
print_ok("OK (terminated)")

# start ghostunnel with bad listen addr
ghostunnel = run_ghostunnel(['server',
'--listen=invalid',
'--target={0}:13002'.format(LOCALHOST),
'--allow-all',
'--keystore=server.p12',
'--cacert=root.crt',
'--status={0}:{1}'.format(LOCALHOST,
STATUS_PORT)])

# wait for ghostunnel to exit and make sure error code is not zero
ret = ghostunnel.wait(timeout=20)
if ret == 0:
raise Exception(
'ghostunnel terminated with zero, though flags were invalid')
else:
print_ok("OK (terminated)")

# start ghostunnel with bad URI pattern
ghostunnel = run_ghostunnel(['server',
'--listen={0}:13001'.format(LOCALHOST),
'--target={0}:13002'.format(LOCALHOST),
'--keystore=server.p12',
'--allow-uri=spiffe://**/**/**',
'--cacert=root.crt',
'--status={0}:{1}'.format(LOCALHOST,
STATUS_PORT)])

# wait for ghostunnel to exit and make sure error code is not zero
ret = ghostunnel.wait(timeout=20)
if ret == 0:
raise Exception(
'ghostunnel terminated with zero, though flags were invalid')
else:
print_ok("OK (terminated)")
finally:
terminate(ghostunnel)
82 changes: 82 additions & 0 deletions tests/test-server-metrics-endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3

"""
Test that ensures that metrics endpoint works.
"""

from common import LOCALHOST, RootCert, STATUS_PORT, TcpClient, print_ok, run_ghostunnel, terminate
import urllib.request
import urllib.error
import urllib.parse
import time
import json

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

# start ghostunnel
ghostunnel = run_ghostunnel(['server',
'--listen={0}:13001'.format(LOCALHOST),
'--target={0}:13002'.format(LOCALHOST),
'--keystore=server.p12',
'--cacert=root.crt',
'--allow-ou=client',
'--enable-pprof',
'--status={0}:{1}'.format(LOCALHOST,
STATUS_PORT)])

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

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

# Load JSON metrics
received_metrics = json.loads(str(urlopen(
"https://{0}:{1}/_metrics?format=json".format(LOCALHOST, STATUS_PORT)).read(), 'utf-8'))

if not isinstance(received_metrics, list):
raise Exception("ghostunnel metrics expected to be JSON list")

# some metrics we expect to be present
expected_metrics = [
"ghostunnel.accept.total",
"ghostunnel.accept.success",
"ghostunnel.accept.timeout",
"ghostunnel.accept.error",
"ghostunnel.conn.open",
"ghostunnel.conn.lifetime.count",
"ghostunnel.conn.lifetime.min",
"ghostunnel.conn.lifetime.max",
"ghostunnel.conn.lifetime.mean",
"ghostunnel.conn.lifetime.50-percentile",
"ghostunnel.conn.lifetime.75-percentile",
"ghostunnel.conn.lifetime.95-percentile",
"ghostunnel.conn.lifetime.99-percentile",
"ghostunnel.conn.handshake.count",
"ghostunnel.conn.handshake.min",
"ghostunnel.conn.handshake.max",
"ghostunnel.conn.handshake.mean",
"ghostunnel.conn.handshake.50-percentile",
"ghostunnel.conn.handshake.75-percentile",
"ghostunnel.conn.handshake.95-percentile",
"ghostunnel.conn.handshake.99-percentile",
]

metrics_found = [item['metric'] for item in received_metrics]
missing_metrics = [metric for metric in expected_metrics if metric not in metrics_found]

if missing_metrics:
raise Exception('missing metrics from ghostunnel instance: %s' % missing_metrics)

# Load Prometheus metrics
metrics = str(urlopen(
"https://{0}:{1}/_metrics?format=prometheus".format(LOCALHOST, STATUS_PORT)).read(), 'utf-8')

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

0 comments on commit 64d5d5a

Please sign in to comment.