def filter_commands(self, include_group=None, exclude_group=None, include_command=None, exclude_command=None): """Perform command selection and exclusion. See random_chaos() for the description of the parameters. """ all_groups = ChaosMonkey.get_all_groups() all_commands = ChaosMonkey.get_all_commands() self.chaos_monkey.reset_command_selection() # If any groups and any commands are not included, assume the intent # is to include all groups and all commands. if not include_group and not include_command: self.chaos_monkey.include_group('all') if include_group: include_group = self._validate(include_group, all_groups) self.chaos_monkey.include_group(include_group) if exclude_group: exclude_group = self._validate(exclude_group, all_groups) self.chaos_monkey.exclude_group(exclude_group) if include_command: include_command = self._validate(include_command, all_commands) self.chaos_monkey.include_command(include_command) if exclude_command: exclude_command = self._validate(exclude_command, all_commands) self.chaos_monkey.exclude_command(exclude_command)
def filter_commands(self, include_group=None, exclude_group=None, include_command=None, exclude_command=None): """Perform command selection and exclusion. See random_chaos() for the description of the parameters. """ all_groups = ChaosMonkey.get_all_groups() all_commands = ChaosMonkey.get_all_commands() self.chaos_monkey.reset_command_selection() # If any groups and any commands are not included, assume the intent # is to include all groups and all commands. if not include_group and not include_command: self.chaos_monkey.include_group('all') if include_group: include_group = self._validate(include_group, all_groups) self.chaos_monkey.include_group(include_group) if exclude_group: exclude_group = self._validate(exclude_group, all_groups) self.chaos_monkey.exclude_group(exclude_group) if include_command: include_command = self._validate( include_command, all_commands) self.chaos_monkey.include_command(include_command) if exclude_command: exclude_command = self._validate( exclude_command, all_commands) self.chaos_monkey.exclude_command(exclude_command)
def test_list_all_commands(self): cmd = Runner.list_all_commands() self.assertItemsEqual(cmd.keys(), ChaosMonkey.get_all_groups()) all_commands = [] for commands in cmd.values(): for command in commands: all_commands.extend(command) self._assert_from_list(ChaosMonkey.get_all_commands(), all_commands)
def list_all_commands(): """List all available 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 test_filter_command_include_command(self): include_command = 'deny-all' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(include_command=include_command) self.assertEqual(len(runner.chaos_monkey.chaos), 1) self.assertEqual(runner.chaos_monkey.chaos[0].command_str, 'deny-all')
def test_filter_commands_exclude_incorrect_group(self): exclude_group = 'net,killl' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with self.assertRaisesRegexp( BadRequest, "Invalid value given on command line: killl"): runner.filter_commands(exclude_group=exclude_group)
def test_exclude_commands(self): 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_exclude_group(self): group = ['net'] cm = ChaosMonkey.factory() cm.include_group('all') cm.exclude_group(group) self.assertGreaterEqual(len(cm.chaos), 1) self.assertTrue(all(c.group != 'net' for c in cm.chaos))
def test_include_command(self): command = ['deny-incoming'] cm = ChaosMonkey.factory() cm.include_command(command) self.assertGreaterEqual(len(cm.chaos), 1) self.assertTrue( all(c.command_str == 'deny-incoming' for c in cm.chaos))
def test_exclude_group_multiple_groups(self): group = ['net', 'kill'] cm = ChaosMonkey.factory() cm.include_group('all') cm.exclude_group(group) self.assertTrue(all(c.group != 'net' for c in cm.chaos)) self.assertTrue(all(c.group != 'kill' for c in cm.chaos))
def test_random_assert_run_command_method_called(self): with patch('utility.check_output', autospec=True): with patch('runner.Runner._run_command', autospec=True) as cm_mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.random_chaos(run_timeout=1, enablement_timeout=1) cm_mock.assert_called_with(runner, 1)
def test_random_enablement_zero(self): with patch('utility.check_output', autospec=True) as mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.random_chaos(run_timeout=1, enablement_timeout=0, exclude_command=Kill.restart_cmd) self.assertEqual(mock.called, True)
def test_filter_command_exclude_incorrect_command(self): exclude_command = 'deny-all,deny-net,{}'.format(Kill.jujud_cmd) with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with self.assertRaisesRegexp( BadRequest, "Invalid value given on command line: deny-net"): runner.filter_commands(exclude_command=exclude_command)
def test_filter_command_include_incorrect_command(self): include_command = 'deny-all,deny-net' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with self.assertRaisesRegexp( BadRequest, "Invalid value given on command line: deny-net"): runner.filter_commands(include_command=include_command)
def test_filter_command_exclude_command(self): exclude_command = Kill.jujud_cmd with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(exclude_command=exclude_command) self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 1) self.assertTrue(all(c.command_str != Kill.jujud_cmd for c in runner.chaos_monkey.chaos))
def test_filter_commands_exclude_group(self): exclude_group = 'net' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(exclude_group=exclude_group) self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 2) self.assertTrue(all(c.group != 'net' for c in runner.chaos_monkey.chaos))
def test_random_chaos_passes_timeout(self): with patch('utility.check_output', autospec=True): with patch('runner.Runner._run_command', autospec=True) as mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.random_chaos(run_timeout=3, enablement_timeout=2) self.assertEqual(mock.call_args_list[0][0][1], 2)
def test_exclude_and_include_group(self): group = ['kill'] cm = ChaosMonkey.factory() cm.include_group('all') cm.exclude_group(group) self.assertTrue(all(c.group != 'kill' for c in cm.chaos)) cm.include_group(['kill']) self.assertTrue(all(c.group == 'kill' for c in cm.chaos))
def test_filter_commands_include_groups(self): include_group = 'net,{}'.format(Kill.group) with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(include_group=include_group) self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 2) self.assertTrue( all(c.group == 'net' or c.group == Kill.group for c in runner.chaos_monkey.chaos))
def test_random_chaos_run_once(self): cm = ChaosMonkey.factory() with patch('runner.Runner._run_command', autospec=True) as mock: with temp_dir() as directory: runner = Runner(directory, cm) runner.random_chaos( run_timeout=2, enablement_timeout=1, run_once=True) mock.assert_called_once_with(runner, 1)
def test_get_groups_multiple_groups(self): cm = ChaosMonkey.factory() all_chaos, _ = cm.get_all_chaos() group = cm.get_groups(['net', 'kill'], all_chaos) self.assertGreaterEqual(len(group), 1) self.assertTrue( all(c.group == 'net' or c.group == 'kill' for c in group)) self.assertTrue(any(c.group == 'net' for c in group)) self.assertTrue(any(c.group == 'kill' for c in group))
def test_run_command(self): chaos = self._get_chaos_object(Net(), 'deny-state-server') with patch('utility.check_output', autospec=True) as mock: with patch( 'runner.random.choice', autospec=True, return_value=chaos): with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner._run_command(enablement_timeout=0) self.assertEqual(mock.mock_calls, self._deny_port_call_list())
def test_get_command_list(self): with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with NamedTemporaryFile() as temp_file: self._write_command_list_to_file(temp_file) args = Namespace(replay=temp_file.name, restart=False) commands = runner._get_command_list(args) expected = [['deny-state-server', 1], ['deny-api-server', 1]] self.assertItemsEqual(commands, expected)
def test_filter_command_include_command_exclude_group(self): include_command = 'deny-all,deny-incoming' exclude_group = 'net' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(exclude_group=exclude_group, include_command=include_command) self.assertEqual(len(runner.chaos_monkey.chaos), 2) self.assertEqual(runner.chaos_monkey.chaos[0].command_str, 'deny-all') self.assertEqual(runner.chaos_monkey.chaos[1].command_str, 'deny-incoming')
def test_include_group_and_exclude_commands(self): groups = ['net'] commands = ['deny-all', 'deny-incoming'] 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.exclude_command(commands) self.assertGreaterEqual(len(cm.chaos), 1) self.assertTrue(all(c.group == 'net' for c in cm.chaos)) self.assertTrue(all(c.command_str not in commands for c in cm.chaos))
def test_replay_commands(self): with patch('utility.check_output', autospec=True) as mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with NamedTemporaryFile() as temp_file: self._write_command_list_to_file(temp_file) args = Namespace(replay=temp_file.name, restart=False) runner.replay_commands(args) expected = self._deny_port_call_list() expected.extend(self._deny_port_call_list('17017')) self.assertEqual(mock.mock_calls, expected)
def test_run_command_select_restart_unit(self): chaos = self._get_chaos_object(Kill(), Kill.restart_cmd) with patch('utility.check_output', autospec=True) as mock: with patch( 'runner.random.choice', autospec=True, return_value=chaos): with patch('runner.Init', autospec=True) as ri_mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner._run_command(enablement_timeout=0) self.assertEqual(mock.mock_calls, [call(['shutdown', '-r', 'now'])]) ri_mock.upstart.assert_called_once_with()
def test_exclude_group_and_include_command(self): groups = ['net'] commands = ['deny-all'] cm = ChaosMonkey.factory() cm.include_group('all') cm.exclude_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 == 'deny-all' for c in cm.chaos)) self.assertTrue(any(c.group == 'net' for c in cm.chaos))
def test_include_group_and_include_command(self): groups = ['net'] 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 == 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))
def test_filter_commands_include_group_and_exclude_command(self): include_group = 'net' exclude_command = 'deny-all' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(include_group=include_group, exclude_command=exclude_command) self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 1) self.assertTrue(all(c.group == 'net' for c in runner.chaos_monkey.chaos)) self.assertTrue(all(c.command_str != 'deny-all' for c in runner.chaos_monkey.chaos))
def test_save_replay_command_list(self): commands = [['deny-state-server', 1], ['deny-api-server', 1]] with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with NamedTemporaryFile( suffix=runner.replay_filename_ext) as temp_file: args = Namespace(replay=temp_file.name.split('.')[0], restart=False) runner._save_command_list(commands, args) file_content = temp_file.read() expected = yaml.dump(commands) self.assertItemsEqual(file_content, expected)