def test_long_running_plugin_verbose(): '''Confirm long running check does show up in report with verbose on''' f = Fossor() f.check_plugins = set() f.check_plugins = [LongCheck] f.variable_plugins = set() f.add_variable('timeout', 1) f.add_variable('verbose', 'True') result = f.run(report='DictObject') log.debug(f"result is: {result}") assert 'Timed out' in result['LongCheck']
def test_long_running_plugin(): '''Confirm long running check will not show up in report''' f = Fossor() f.check_plugins = set() f.check_plugins = [LongCheck] f.variable_plugins = set() f.add_variable('timeout', 1) f.add_variable('verbose', False) result = f.run(report='DictObject') log.debug(f"result is: {result}") assert 'LongCheck' not in result.keys()
def main(context, **kwargs): """Fossor is a plugin oriented tool for automating the investigation of broken hosts and services. \b Advanced: Multiple additional internal variables may be passed on the command line in this format: name="value". This is intended for testing or automation. """ # \b makes click library honor paragraphs f = Fossor() f.run(**kwargs)
def test_one_fail_both_short_and_long_running_plugin(): '''Confirm long running check will not show up in report''' f = Fossor() f.check_plugins = set() f.check_plugins = [LongCheck, ShortCheck] f.variable_plugins = set() f.add_variable('timeout', 1) f.add_variable('verbose', 'True') result = f.run(report='DictObject') assert 'Timed out' in result['LongCheck'] assert 'This plugin slept' in result['ShortCheck']
def test_no_popen_usage(): f = Fossor() for plugin in f.list_plugins(): for line in inspect.getsourcelines(plugin)[0]: print(line) if 'popen' in line: log.error( "os.popen is deprecated as of python 2.6, please use the plugin.py shell_call method instead." ) log.error('os.popen usage located in {plugin} for line {line}'. format(plugin=plugin, line=line)) assert 'popen' not in line
def test_timeout_exception_and_success_for_plugins(): '''Confirm long running check will not show up in report''' f = Fossor() f.check_plugins = set() f.check_plugins = [LongCheck, FailCheck, ShortCheck] f.variable_plugins = set() f.add_variable('timeout', 5) f.add_variable('verbose', 'True') result = f.run(report='DictObject') assert 'Timed out' in result['LongCheck'] assert 'This plugin slept for' in result['ShortCheck'] assert 'Failed' in result['FailCheck']
def test_short_running_plugin(): '''Confirm short running check will show up in report''' f = Fossor() f.check_plugins = set() f.check_plugins = [ShortCheck] f.variable_plugins = set() f.add_variable('verbose', True) result = f.run(report='DictObject') assert 'ShortCheck' in result.keys()
def test_shell_call_properly_kills(): f = Fossor() f.check_plugins = set() f.check_plugins = [LongCheck] f.variable_plugins = set() f.add_variable('timeout', 5) f.add_variable('verbose', 'True') result = f.run(report='DictObject') log.debug(f"result is: {result}") assert 'Timed out' in result['LongCheck'] # Confirm that all processes were properly killed before Fossor exited for p in psutil.process_iter(): try: assert 'sleep 31.4' != ' '.join(p.cmdline()) except process_exceptions: continue
def test_crash_not_verbose(): '''Ensure crash report does not print when result should not be verbose.''' f = Fossor() f.check_plugins = set() f.check_plugins = [FailCheck] f.variable_plugins = set() f.add_variable('verbose', False) result = f.run(report='DictObject') result.pop('Stats') assert len(result) == 0
def test_variable_plugin_whitelist(): f = Fossor() f.add_variable('timeout', 1) assert len(f.variable_plugins) > 2 whitelist = ['Hostname', 'PidExe'] f.run(variable_plugin_whitelist=whitelist) assert len(f.variable_plugins) == 2
def test_variable_plugin_blacklist(): f = Fossor() f.add_variable('timeout', 1) assert fossor.variables.hostname.Hostname in f.variable_plugins blacklist = ['Hostname'] f.run(variable_plugin_blacklist=blacklist) assert 'Hostname' not in f.variable_plugins assert len(f.variable_plugins) > 0
def test_crash_verbose(): '''Check that the variable from the above class did show up in the report''' f = Fossor() f.check_plugins = set() f.check_plugins = [FailCheck] f.variable_plugins = set() f.add_variable('verbose', True) result = f.run(report='DictObject') output = result['FailCheck'] assert 'Fail' in output # check that the variable foo and it's value of bar showed up in the output assert 'foo' in output assert 'bar' in output
def test_check_plugin_whitelist(): f = Fossor() f.add_variable('timeout', 1) assert len(f.check_plugins) > 2 whitelist = ['BuddyInfo', 'LoadAvg'] f.run(check_whitelist=whitelist) assert len(f.check_plugins) == 2 assert fossor.checks.buddyinfo.BuddyInfo in f.check_plugins
def test_plugin_whitelist(): f = Fossor() f.add_variable('timeout', 1) assert len(f.variable_plugins) > 2 assert len(f.check_plugins) > 2 whitelist = ['BuddyInfo', 'LoadAvg', 'hostname', 'pidexe'] # Should be case insensitive f.run(whitelist=whitelist) assert len(f.variable_plugins) == 2 assert len(f.check_plugins) == 2
def test_check_plugin_blacklist(): f = Fossor() f.add_variable('timeout', 1) assert len(f.check_plugins) > 2 assert fossor.checks.buddyinfo.BuddyInfo in f.check_plugins blacklist = ['BuddyInfo', 'LoadAvg'] f.run(check_blacklist=blacklist) assert fossor.checks.buddyinfo.BuddyInfo not in f.check_plugins assert len(f.check_plugins) > 1
def test_shell_call_is_needed_to_kill(): f = Fossor() f.check_plugins = set() f.check_plugins = [LongCheck] f.variable_plugins = set() f.add_variable('timeout', 5) f.add_variable('verbose', 'True') with patch('fossor.engine.Fossor._terminate_process_group'): result = f.run(report='DictObject') log.debug(f"result is: {result}") # Confirm fossor thinks the plugin ran too long assert 'Timed out' in result['LongCheck'] # Confirm that the sleeping process outlasted lipy-fossor since we patched process group termination to do nothing sleeping_pids = [] for p in psutil.process_iter(): try: if 'sleep 31.4' == ' '.join(p.cmdline()): sleeping_pids.append(p) except process_exceptions: continue assert len(sleeping_pids) == 1 # Clean-up pid by killing it and making sure it is gone pid = sleeping_pids[0] pid.kill() sleep(5) sleeping_pids = [] for p in psutil.process_iter(): try: if 'sleep 31.4' == ' '.join(p.cmdline()): sleeping_pids.append(p) except process_exceptions: continue assert len(sleeping_pids) == 0
def test_plugin_import_by_path(): f = Fossor() f.clear_plugins() f.add_plugins(fossor.__path__[0]) assert 'BuddyInfo' in [p.get_name() for p in f.check_plugins] assert 'fossor.local.checks.buddyinfo.BuddyInfo' in [ p.get_full_name() for p in f.check_plugins ]
def test_plugin_blacklist(): f = Fossor() f.add_variable('timeout', 1) assert fossor.variables.hostname.Hostname in f.variable_plugins blacklist = ['hostname', 'BuddyInfo', 'LoadAvg'] # Should be case insensitive f.run(blacklist=blacklist) assert 'Hostname' not in f.variable_plugins assert 'BuddyInfo' not in f.check_plugins assert 'LoadAvg' not in f.check_plugins assert len(f.variable_plugins) > 0 assert len(f.check_plugins) > 0
def test_plugin_import_by_module(): f = Fossor() f.clear_plugins() f.add_plugins(fossor) assert fossor.checks.buddyinfo.BuddyInfo in f.check_plugins assert fossor.variables.examplevariable.ExampleVariable in f.variable_plugins assert 'BuddyInfo' in [p.get_name() for p in f.check_plugins] assert 'fossor.checks.buddyinfo.BuddyInfo' in [ p.get_full_name() for p in f.check_plugins ]
def test_json_report(): f = Fossor() f.check_plugins = set() f.check_plugins = [TestCheck1, TestCheck2] f.variable_plugins = set() f.add_variable('verbose', True) json_result = f.run(report='Json') log.debug(f"json_result: {json_result}") object_result = json.loads(json_result) log.debug(f"object_result: {object_result}") assert 'TestCheck1' in object_result new_json_result = json.dumps(object_result) log.debug(f"new_json_result: {new_json_result}") assert 'TestCheck1' in new_json_result assert 'TestCheck1' in json_result assert 'TestCheck2' in new_json_result assert 'TestCheck2' in json_result
def main(context, pid, product, plugin_dir, hours, report, start_time, end_time, time_out, debug, verbose, no_truncate): """Fossor is a plugin oriented tool for automating the investigation of broken hosts and services. \b Advanced: Multiple additional internal variables may be passed on the command line in this format: name="value". This is intended for testing or automation. """ # \b makes click library honor paragraphs setproctitle.setproctitle('fossor ' + ' '.join(sys.argv[1:])) if debug: log_level = logging.DEBUG verbose = True else: log_level = logging.CRITICAL logging.getLogger("requests").setLevel(logging.WARNING) logging.basicConfig(stream=sys.stdout, level=log_level) log = logging.getLogger(__name__) f = Fossor() # Add pre-defined args f.add_variable('timeout', time_out) if product: f.add_variable('product', product) if pid: f.add_variable('pid', pid) if no_truncate: f.add_variable('truncate', False) f.add_variable('verbose', verbose) # Add dynamic args log.debug(f"Remaining Arguments that will be used for dynamic args now that hardcoded cli flags have been removed: {context.args}") format_help_message = "Please use --help to see valid flags. Or use the name=value method for setting internal variables." for arg in context.args: a = arg.strip() if a.startswith('-'): raise ValueError(f"Argument: {arg} is invalid {format_help_message}.") try: name, value = a.split('=') f.add_variable(name, value) log.debug(f"Using dynamic variable {name}={value}") except Exception as e: log.exception(f"Failed to add argument: \"{arg}\". {format_help_message} Exception was: {e}") raise e # Get start/end times cal = pdt.Calendar() if start_time: start_time = (cal.parseDT(start_time)[0]).timestamp() else: start_time = (datetime.now() - timedelta(hours=hours)).timestamp() f.add_variable('start_time', str(start_time)) if end_time: end_time = (cal.parseDT(end_time)[0]).timestamp() else: end_time = datetime.now().timestamp() f.add_variable('end_time', str(end_time)) # Import plugin dir if it exists f.add_plugins(plugin_dir) log.debug("Starting fossor") return f.run(report=report)
def test_convert_simple_type(): f = Fossor() assert f._convert_simple_type('false') is False assert f._convert_simple_type('False') is False assert f._convert_simple_type(False) is False assert f._convert_simple_type('true') is True assert f._convert_simple_type('True') is True assert f._convert_simple_type(True) is True assert f._convert_simple_type('1.0') == 1.0 assert type(f._convert_simple_type('1.0')) == float assert f._convert_simple_type('1') == 1 assert type(f._convert_simple_type('1')) == int assert f._convert_simple_type('foo') == 'foo' assert type(f._convert_simple_type('foo')) == str