Skip to content

Commit

Permalink
Updated to list all available commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seman Said committed May 22, 2015
2 parents e3584e4 + 45e6d8b commit af036b4
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 77 deletions.
13 changes: 8 additions & 5 deletions chaos/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
class Kill(ChaosMonkeyBase):
"""Kills a process or shutdown a unit"""

jujud_cmd = 'kill-jujud'
mongod_cmd = 'kill-mongod'
group = 'kill'

def __init__(self):
self.group = 'kill'
super(Kill, self).__init__()

@classmethod
Expand Down Expand Up @@ -63,15 +66,15 @@ def get_chaos(self):
enable=self.kill_jujud,
disable=None,
group=self.group,
command_str='jujud',
description='Jujud process has been killed.'))
command_str=self.jujud_cmd,
description='Kill jujud process.'))
chaos.append(
Chaos(
enable=self.kill_mongodb,
disable=None,
group=self.group,
command_str='mongod',
description='Mongod process has been killed.'))
command_str=self.mongod_cmd,
description='Kill mongod process.'))
return chaos

def shutdown(self):
Expand Down
60 changes: 47 additions & 13 deletions runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from argparse import ArgumentParser
from argparse import ArgumentParser, RawDescriptionHelpFormatter
import errno
import logging
import os
Expand Down Expand Up @@ -139,36 +139,70 @@ def sig_handler(self, sig_num, frame):
self.stop_chaos = True
logging.debug('self.stop_chaos: {}'.format(self.stop_chaos))

@staticmethod
def list_all_commands():
all_chaos, _ = ChaosMonkey.get_all_chaos()
all_groups = ChaosMonkey.get_all_groups()
commands = {}
for group in all_groups:
commands[group] = [[c.command_str, c.description]
for c in all_chaos if c.group == group]
return commands


def setup_sig_handlers(handler):
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)


def display_all_commands():
commands = Runner.list_all_commands()
groups = commands.keys()
cmd_str = 'GROUP: a comma-separated list of group names.\n'
cmd_str += ' Valid groups: {}\n\n'.format(', '.join(groups))
cmd_str += 'COMMANDS: a comma-separated list of chaos commands:\n'
for group, values in commands.iteritems():
cmd_str += " Group: " + group + "\n"
for value in values:
cmd_str += " " + value[0] + ": " + value[1] + "\n"
cmd_str += "\n"
return cmd_str


def parse_args(argv=None):
parser = ArgumentParser()
commands = display_all_commands()
parser = ArgumentParser(
description="Run Chaos Monkey.", usage="[OPTIONS] path",
epilog=commands, formatter_class=RawDescriptionHelpFormatter)
parser.add_argument(
'path', help='An existing directory, to be used as a workspace.')
parser.add_argument(
'-et', '--enablement-timeout', default=10, type=int,
help="Enablement timeout in seconds")
help="Enablement timeout in seconds.", metavar='SECONDS')
parser.add_argument(
'-tt', '--total-timeout', type=int, help="Total timeout in seconds")
'-tt', '--total-timeout', type=int, help="Total timeout in seconds.",
metavar='SECONDS')
parser.add_argument(
'-lc', '--log-count', default=2, type=int,
'-lc', '--log-count', default=2, type=int, metavar='NUMBER',
help='The number of backups to keep.')
parser.add_argument(
'-ig', '--include-group',
help='Include these groups only in the test', default=None)
'-ig', '--include-group', metavar='GROUP',
help='Select chaos from only a specified group or set of groups. '
'All groups are included by default.',
default=None)
parser.add_argument(
'-eg', '--exclude-group',
help='Exclude groups from the test', default=None)
'-eg', '--exclude-group', metavar='GROUP',
help='Exclude a group or set of groups from selected chaos.',
default=None)
parser.add_argument(
'-ic', '--include-command',
help='Include commands in test.', default=None)
'-ic', '--include-command', metavar='COMMAND',
help="Select chaos from only a specified command or set of commands. "
"All commands are included by default.",
default=None)
parser.add_argument(
'-ec', '--exclude-command',
help='Exclude commands in the test', default=None)
'-ec', '--exclude-command', metavar='COMMAND',
help='Exclude a command or set of commands from selected chaos.',
default=None)
parser.add_argument(
'-dr', '--dry-run', dest='dry_run', action='store_true',
help='Do not actually run chaos operations.', default=False)
Expand Down
15 changes: 8 additions & 7 deletions tests/test_chaos_monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ChaosMonkey,
NotFound
)
from chaos.kill import Kill
from chaos_monkey_base import Chaos
from chaos.net import Net
from tests.common_test_base import CommonTestBase
Expand Down Expand Up @@ -205,33 +206,33 @@ def test_exclude_command(self):
self.assertTrue(all(c.command_str != 'deny-all' for c in cm.chaos))

def test_exclude_commands(self):
commands = ['deny-all', 'jujud']
commands = ['deny-all', Kill.jujud_cmd]
cm = ChaosMonkey.factory()
cm.include_group('all')
cm.exclude_command(commands)
self.assertGreaterEqual(len(cm.chaos), 1)
self.assertTrue(all(c.command_str not in commands for c in cm.chaos))

def test_include_and_exclude_commands(self):
commands = ['deny-all', 'jujud']
commands = ['deny-all', Kill.jujud_cmd]
cm = ChaosMonkey.factory()
cm.include_command(commands)
self.assertGreaterEqual(len(cm.chaos), 1)
self.assertTrue(all(c.command_str in commands for c in cm.chaos))
cm.exclude_command(['jujud'])
cm.exclude_command([Kill.jujud_cmd])
self.assertGreaterEqual(len(cm.chaos), 1)
self.assertTrue(all(c.command_str != 'jujud' for c in cm.chaos))
self.assertTrue(all(c.command_str != Kill.jujud_cmd for c in cm.chaos))

def test_include_group_and_include_command(self):
groups = ['net']
commands = ['jujud']
commands = [Kill.jujud_cmd]
cm = ChaosMonkey.factory()
cm.include_group(groups)
self.assertGreaterEqual(len(cm.chaos), 1)
self.assertTrue(all(c.group == 'net' for c in cm.chaos))
cm.include_command(commands)
self.assertGreaterEqual(len(cm.chaos), 1)
self.assertTrue(any(c.command_str == 'jujud' for c in cm.chaos))
self.assertTrue(any(c.command_str == Kill.jujud_cmd for c in cm.chaos))
self.assertTrue(any(c.group == 'net' for c in cm.chaos))
self.assertTrue(any(c.group == 'kill' for c in cm.chaos))

Expand Down Expand Up @@ -314,4 +315,4 @@ def _get_all_command_strings(self):
return get_all_net_commands() + get_all_kill_commands()

def _get_all_groups(self):
return ['net', 'kill']
return ['net', Kill.group]
6 changes: 3 additions & 3 deletions tests/test_kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def test_get_chaos_verify_method_calls(self):
kill = Kill()
chaos = kill.get_chaos()
for c in chaos:
if c.command_str == 'mongod':
if c.command_str == Kill.mongod_cmd:
self.assertEqual(c.enable, kill.kill_mongodb)
if c.command_str == 'jujud':
if c.command_str == Kill.jujud_cmd:
self.assertEqual(c.enable, kill.kill_jujud)
self.assertEqual(c.group, 'kill')
self.assertEqual(c.disable, None)
Expand All @@ -92,4 +92,4 @@ def test_restart_node(self):


def get_all_kill_commands():
return ['jujud', 'mongod']
return [Kill.jujud_cmd, Kill.mongod_cmd]
Loading

0 comments on commit af036b4

Please sign in to comment.