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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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)
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))
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)
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)
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_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()