Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curl command generation #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions hemApp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def set(self, key, value):
def get(self, key):
return self.data.get(key, None)

logging.captureWarnings(True)

def setup_logging(
default_path='logging.yaml',
Expand Down Expand Up @@ -113,6 +112,17 @@ def __init__(self, name, test, metrics=None, storage=None):
self.metrics = metrics
self.storage = storage

def curl_command(self):
command = "curl "
if not self.verify:
command = command + "-v "
if self.certificate:
command = command + "--key " + self.certificate + " --cert " + self.certificate + " "
for header in self.headers:
command = command + "-H \"{}: {}\" ".format(header, self.headers[header]) + " "
command = command + self.url
return command

def get_jwt(self, auth={}):
j = requests.post(
auth['url'],
Expand Down Expand Up @@ -238,9 +248,9 @@ def test_list(self, param_list):
def report_failure(self, param, message):
""" Display errors """
try:
click.echo(click.style("{} Failed with {}".format(param, message.split('\n')[0]),fg='red'))
self.logger.error("{} Failed with {}".format(param, message.split('\n')[0]))
except AttributeError:
click.echo(message)
self.logger.error(message)


def discover_hosts(src, metrics=None):
Expand Down Expand Up @@ -307,3 +317,12 @@ def run_tests(config, metrics=None, storage=None):
metrics.store()
return (end - start)

def curl_commands(config):
for test_name in config['tests']:
test = config['tests'][test_name]
CHECK = Check(
test_name,
test,
metrics=None,
storage=None)
click.echo(CHECK.curl_command())
10 changes: 10 additions & 0 deletions hemApp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import hemApp
import time
import os
import urllib3

@click.command()
@click.version_option()
@click.option('-v', '--verbose',
Expand All @@ -13,6 +15,8 @@
@click.option('-c', '--config', required=False,
help="Specifies an alternative config file",
type=click.Path())
@click.option('--curl/--no-curl', default=False,
help="Only show example curl commands")
def main(**kwargs):
if kwargs['verbose'] > 1:
loglevel = logging.DEBUG
Expand All @@ -26,6 +30,8 @@ def main(**kwargs):
config = yaml.safe_load(config_file.read())
logging.config.dictConfig(config)

# As hem supports per check verification settings, don't warn on these
urllib3.disable_warnings()
logging.getLogger().setLevel(level=loglevel)
logger = logging.getLogger(__name__)
config = hemApp.load_config(kwargs['config'])
Expand All @@ -39,6 +45,10 @@ def main(**kwargs):
logger.info("Frequency is {}".format(frequency))
logger.info(config)
storage = hemApp.HemStore()
if kwargs['curl']:
hemApp.curl_commands(config)
exit()

while True:
duration = hemApp.run_tests(config, metrics, storage)
try:
Expand Down