Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
Test all basic commands resolve to correct class
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanhall committed Feb 13, 2017
1 parent 05201f1 commit a058fdb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 50 deletions.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,26 @@ Or provide the hostname with each command:
::

envmgr get MyService health in prod --host=environmentmanager.acme.com


Development
-----------
Install development dependencies:

::

pip install -e .


Run Tests
^^^^^^^^^^
Run all tests:

::

pytest -v





21 changes: 14 additions & 7 deletions envmgr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
from inspect import getmembers, isclass
from docopt import docopt
from . import __version__ as VERSION
from envmgr.commands import ASG, Deploy, Patch, Publish, Service, Toggle

commands = {
'asg':ASG,
'deploy':Deploy,
'patch':Patch,
'publish':Publish,
'service':Service,
'toggle':Toggle
}


def except_hook(exec_type, value, trace_back):
print(value)
Expand All @@ -58,21 +69,17 @@ def except_hook(exec_type, value, trace_back):

def main():
"""Main CLI entrypoint."""
import envmgr.commands

options = docopt(__doc__, version=VERSION)
priority_order = ["asg", "deploy", "patch", "toggle", "publish", "service"]

cmd_opts = options.copy()

if cmd_opts["<service>"] is not None:
cmd_opts["service"] = True

for cmd in priority_order:
if cmd_opts[cmd]:
module = getattr(envmgr.commands, cmd)
envmgr.commands = getmembers(module, isclass)
command = [command[1] for command in envmgr.commands if command[0] != 'BaseCommand'][0]
command = command(options)
CommandClass = commands[cmd]
command = CommandClass(options)
command.run()
return

Expand Down
7 changes: 1 addition & 6 deletions envmgr/commands/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from json import dumps
from envmgr.commands.base import BaseCommand

class GetService(BaseCommand):
class Service(BaseCommand):

def run(self):
if self.cmds['healthy']:
Expand All @@ -18,7 +18,6 @@ def run(self):
self.get_overall_health(**self.cli_args)
elif self.cmds['slice']:
self.get_service_slice(**self.cli_args)


def wait_for_healthy_service(self, service, env, slice=None):
while True:
Expand All @@ -35,29 +34,25 @@ def get_overall_health(self, service, env):
self.show_result(result, messages)
return all( service["OverallHealth"] == "Healthy" for service in services )


def get_service_health(self, service, slice, env):
result = self.api.get_service_health(service, env, slice)
message = self.format_health(result)
self.show_result(result, message)
return result["OverallHealth"] == "Healthy"


def get_service_slice(self, service, env):
active = "true" if self.cmds['active'] else "false"
result = self.api.get_service_slices(service, env, active)
messages = [ self.format_slice(slice) for slice in result ]
self.show_result(result, messages)


def format_health(self, service):
slice = service['Slice']
status = service['OverallHealth']
n_healthy = service['InstancesCount']['Healthy']
n_total = service['InstancesCount']['Total']
return "{0} is {1} ({2} of {3} instances Healthy)".format(slice, status, n_healthy, n_total)


def format_slice(self, slice):
name = slice['Name']
status = slice['State']
Expand Down
25 changes: 2 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
""" Copyright (c) Trainline Limited, 2017. All rights reserved. See LICENSE.txt in the project root for license information. """

from setuptools import Command, find_packages, setup
from setuptools import find_packages, setup
from codecs import open
from os.path import abspath, dirname, join
from subprocess import call

from envmgr import __version__

this_dir = abspath(dirname(__file__))
with open(join(this_dir, 'README.rst'), encoding='utf-8') as file:
long_description = file.read()


class RunTests(Command):

description = 'run tests'
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
print('is it running')
errno = call(['py.test', '--cov=envmgr', '--cov-report=term-missing'])
#raise SystemExit(errno)


setup(
name = 'envmgr-cli',
version = __version__,
Expand All @@ -46,13 +26,12 @@ def run(self):
'environment_manager==0.0.9'
],
extras_require = {
'test': ['coverage', 'pytest', 'pytest-cov']
'test': ['pytest', 'mock']
},
entry_points = {
'console_scripts': [
'envmgr=envmgr.cli:main',
],
},
cmdclass = {'test':RunTests},
)

82 changes: 68 additions & 14 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,76 @@

""" CLI entry point tests """

from subprocess import PIPE, Popen as popen
import sys

from unittest import TestCase
from envmgr import __version__ as VERSION
from mock import patch
from envmgr.cli import main

from envmgr import cli
class TestCLI(TestCase):

# @patch('environment_manager.EMApi')
@patch('envmgr.cli.Service.run')
def test_get_service_health(self, run):
self.assert_command('get MockService health in mk-1', run)

class TestCLI(TestCase):
@patch('envmgr.cli.Service.run')
def test_get_service_health_with_slice(self, run):
self.assert_command('get AcmeService health in mk-2 green', run)

@patch('envmgr.cli.Service.run')
def test_get_service_active_slice(self, run):
self.assert_command('get CoolService active slice in mk-22', run)

@patch('envmgr.cli.Service.run')
def test_get_service_inactive_slice(self, run):
self.assert_command('get CoolService inactive slice in mk-22', run)

@patch('envmgr.cli.Service.run')
def test_wait_for_healthy_service(self, run):
self.assert_command('wait-for healthy CoolService in mk-22', run)

@patch('envmgr.cli.ASG.run')
def test_get_asg_status(self, run):
self.assert_command('get asg mk-auto-scale status in mk-22', run)

@patch('envmgr.cli.ASG.run')
def test_get_asg_schedule(self, run):
self.assert_command('get asg mk-auto-scale schedule in mk-22', run)

@patch('envmgr.cli.ASG.run')
def test_wait_for_asg(self, run):
self.assert_command('wait-for asg mk-auto-scale in mk-22', run)

@patch('envmgr.cli.ASG.run')
def test_schedule_asg(self, run):
self.assert_command('schedule asg mk-auto-scale on in mk-22', run)

@patch('envmgr.cli.Deploy.run')
def test_deploy_service(self, run):
self.assert_command('deploy MyService 1.4.0 in prod-1', run)

@patch('envmgr.cli.Deploy.run')
def test_get_deploy_status(self, run):
self.assert_command('get deploy status a2fbb0c0-ed4c-11e6-85b1-2b6d1cb68994', run)

@patch('envmgr.cli.Deploy.run')
def test_wait_for_deploy(self, run):
self.assert_command('wait-for deploy a2fbb0c0-ed4c-11e6-85b1-2b6d1cb68994', run)

@patch('envmgr.cli.Publish.run')
def test_publish_service(self, run):
self.assert_command('publish build-22.zip as AcmeService 1.2.3', run)

@patch('envmgr.cli.Toggle.run')
def test_toggle_service(self, run):
self.assert_command('toggle MyService in mock-3', run)

@patch('envmgr.cli.Patch.run')
def test_get_patch_status(self, run):
self.assert_command('get team-1 patch status in prod', run)

"""
def test_returns_version_info(self):
output = popen(['envmgr', '--version'], stdout=PIPE).communicate()[0]
self.assertEqual(output.strip(), VERSION)
"""
def assert_command(self, cmd, func):
argv = ['/usr/local/bin/envmgr'] + cmd.split(' ')
with patch.object(sys, 'argv', argv):
main()
func.assert_called_once()

def test_service_health(self):
cli.main()
self.assertEqual(1,2)

0 comments on commit a058fdb

Please sign in to comment.