예제 #1
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')
예제 #2
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)
예제 #3
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))
예제 #4
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))
예제 #5
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))
예제 #6
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)
예제 #7
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))
예제 #8
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)
예제 #9
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))
예제 #10
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))
예제 #11
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)
예제 #12
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)
예제 #13
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))
예제 #14
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)
예제 #15
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))
예제 #16
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)
예제 #17
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())
예제 #18
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)
예제 #19
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))
예제 #20
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))
예제 #21
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)
예제 #22
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()
예제 #23
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')
예제 #24
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))
예제 #25
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))
예제 #26
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)
예제 #27
0
 def test_filter_commands_exclude_groups(self):
     exclude_groups = 'net,{}'.format(Kill.group)
     with temp_dir() as directory:
         runner = Runner(directory, ChaosMonkey.factory())
         runner.filter_commands(exclude_group=exclude_groups)
     add_fake_group(runner.chaos_monkey.chaos)
     self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 1)
     self.assertTrue(
         all(c.group != 'net' and c.group != Kill.group
             for c in runner.chaos_monkey.chaos))
     self.assertTrue(any(c.group == 'fake_group'
                     for c in runner.chaos_monkey.chaos))
예제 #28
0
 def factory(cls, workspace, log_count=1, dry_run=False):
     log_dir_path = os.path.join(workspace, 'log')
     ensure_dir(log_dir_path)
     log_file = os.path.join(log_dir_path, 'results.log')
     cmd_log_file = os.path.join(log_dir_path, 'chaos_run_list.log')
     cmd_log_name = 'cmd_log'
     setup_logging(log_path=log_file, log_count=log_count)
     setup_logging(
         log_path=cmd_log_file, log_count=log_count,  name=cmd_log_name,
         add_stream=False, disable_formatter=True)
     chaos_monkey = ChaosMonkey.factory()
     return cls(workspace, chaos_monkey, log_count, dry_run, cmd_log_name)
예제 #29
0
 def test_random_verify_timeout(self):
     run_timeout = 6
     with patch('utility.check_output', autospec=True) as mock:
         current_time = time()
         with temp_dir() as directory:
             runner = Runner(directory, ChaosMonkey.factory())
             runner.random_chaos(run_timeout=run_timeout,
                                 enablement_timeout=2,
                                 exclude_command=Kill.restart_cmd)
         end_time = time()
     self.assertEqual(run_timeout, int(end_time-current_time))
     self.assertEqual(mock.called, True)
예제 #30
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))
예제 #31
0
 def test_random_assert_chaos_methods_called(self):
     net_ctx = patch('chaos.net.Net', autospec=True)
     kill_ctx = patch('chaos.kill.Kill', autospec=True)
     with patch('utility.check_output', autospec=True):
         with patch('runner.Runner._run_command', autospec=True):
             with net_ctx as net_mock:
                 with kill_ctx as kill_mock:
                     with temp_dir() as directory:
                         runner = Runner(directory, ChaosMonkey.factory())
                         runner.random_chaos(
                             run_timeout=1, enablement_timeout=1)
     net_mock.factory.return_value.get_chaos.assert_called_with()
     kill_mock.factory.return_value.get_chaos.assert_called_with()