예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
 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)
예제 #4
0
 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
예제 #5
0
 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
예제 #6
0
 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')
예제 #7
0
 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)
예제 #8
0
 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))
예제 #9
0
 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))
예제 #10
0
 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))
예제 #11
0
 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))
예제 #12
0
 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)
예제 #13
0
 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)
예제 #14
0
 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)
예제 #15
0
 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)
예제 #16
0
 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))
예제 #17
0
 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))
예제 #18
0
 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)
예제 #19
0
 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))
예제 #20
0
 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))
예제 #21
0
 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)
예제 #22
0
 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))
예제 #23
0
 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())
예제 #24
0
 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)
예제 #25
0
 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')
예제 #26
0
 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))
예제 #27
0
 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)
예제 #28
0
 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()
예제 #29
0
 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))
예제 #30
0
 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))
예제 #31
0
 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))
예제 #32
0
 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)