Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #17 from ashcrow/use-argparse
Browse files Browse the repository at this point in the history
LGTM

Testing provides the SUCCESS and Failures as expected using rest_calls.py and new script.py
  • Loading branch information
cooktheryan committed Feb 3, 2016
2 parents 1cee75a + 4dc2ea5 commit 4863ba5
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 9 deletions.
61 changes: 52 additions & 9 deletions src/commissaire/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def router(q): # pragma: no cover
'''


def create_app(store): # pragma: no cover
def create_app(store):
"""
Creates a new WSGI compliant commissaire application.
Expand Down Expand Up @@ -131,20 +131,59 @@ def create_app(store): # pragma: no cover
return app


def cli_etcd_or_default(name, cli, default, ds):
"""
Returns the value for an option in the following order:
CLI switch, etcd value, default.
:param name: The name of the switch/etcd key.
:type name: str
:param cli: The argparse value.
:type cli: list
:param default: The default value if CLI and etcd have no values.
:param ds: Etcd client
:type ds: etcd.Client
"""
result = None
if cli:
result = cli[0]
logging.info('Using CLI for {0} configuration.'.format(name))
else:
try:
result = ds.get('/commissaire/config/{0}'.format(name)).value
logging.info('Using Etcd for {0} configuration.'.format(name))
except etcd.EtcdKeyNotFound:
logging.info(
'No CLI or etcd defined for {0}.'
' Using default of {1}.'.format(name, default))
result = default
return result


def main(): # pragma: no cover
"""
Main script entry point.
"""
import sys
import argparse
import urlparse

# TODO: Use argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'--listen-interface', '-i', type=str, help='Interface to listen on')
parser.add_argument(
'--listen-port', '-p', type=int, help='Port to listen on')
parser.add_argument(
'etcd_uri', type=str, nargs=1, help='Full URI for etcd')
args = parser.parse_args()

try:
etcd_uri = urlparse.urlparse(sys.argv[1])
etcd_uri = urlparse.urlparse(args.etcd_uri[0])
# Verify we have what we need
if None in (etcd_uri.port, etcd_uri.hostname, etcd_uri.scheme):
raise Exception
except:
sys.stdout.write(
'You must provide an etcd url. EX: http://127.0.0.1:2379\n')
raise SystemExit(1)
parser.error(
'You must provide a full etcd URI. EX: http://127.0.0.1:2379')

ds = etcd.Client(host=etcd_uri.hostname, port=etcd_uri.port)

Expand All @@ -159,16 +198,20 @@ def main(): # pragma: no cover
except etcd.EtcdConnectionFailed as ecf:
err = 'Unable to connect to Etcd: {0}. Exiting ...'.format(ecf)
logging.fatal(err)
sys.stderr.write('{0}\n'.format(err))
parser.error('{0}\n'.format(err))
raise SystemExit(1)

interface = cli_etcd_or_default(
'listeninterface', args.listen_interface, '0.0.0.0', ds)
port = cli_etcd_or_default('listenport', args.listen_port, 8000, ds)

POOLS['investigator'].spawn(investigator, INVESTIGATE_QUEUE, ds)
# watch_thread = gevent.spawn(host_watcher, ROUTER_QUEUE, ds)
# router_thread = gevent.spawn(router, ROUTER_QUEUE)

app = create_app(ds)
try:
WSGIServer(('0.0.0.0', 8000), app).serve_forever()
WSGIServer((interface, int(port)), app).serve_forever()
except KeyboardInterrupt:
pass

Expand Down
77 changes: 77 additions & 0 deletions test/test_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test cases for the commissaire.script module.
"""

import falcon
import etcd

from . import TestCase
from argparse import Namespace
from mock import MagicMock
from commissaire import script


class Test_CreateApp(TestCase):
"""
Tests for the create_app function.
"""

def test_create_app(self):
"""
Verify cli_etcd_or_default works with cli input.
"""
store = MagicMock(get=MagicMock(side_effect=etcd.EtcdKeyNotFound))
app = script.create_app(store)
self.assertTrue(isinstance(app, falcon.API))
self.assertEquals(2, len(app._middleware))


class Test_CliEtcdOrDefault(TestCase):
"""
Tests for the cli_etcd_or_default function.
"""

def test_cli_etcd_or_default_with_cli_input(self):
"""
Verify cli_etcd_or_default works with cli input.
"""
cli = Namespace(test=['test'])
ds = MagicMock(get=MagicMock(side_effect=etcd.EtcdKeyNotFound))
self.assertEquals(
'test',
script.cli_etcd_or_default('test', cli.test, 'default', ds))

def test_cli_etcd_or_default_with_default_fallback(self):
"""
Verify cli_etcd_or_default falls to default with no other input.
"""
cli = Namespace(test=None)
ds = MagicMock(get=MagicMock(side_effect=etcd.EtcdKeyNotFound))
self.assertEquals(
'default',
script.cli_etcd_or_default('test', cli.test, 'default', ds))

def test_cli_etcd_or_default_with_etcd_result(self):
"""
Verify cli_etcd_or_default uses etcd result when present.
"""
cli = Namespace(test=None)
ds = MagicMock(
get=MagicMock(return_value=MagicMock(value='frometcd')))
self.assertEquals(
'frometcd',
script.cli_etcd_or_default('test', cli.test, 'default', ds))

0 comments on commit 4863ba5

Please sign in to comment.