def run(self): ''' Set up the event object, global settings and command line arguments. ''' # Parse the stdin into a global event object stdin = self.read_stdin() self.event = self.read_event(stdin) # Prepare global settings self.settings = get_settings() self.api_settings = self.get_api_settings() # Prepare command line arguments and self.parser = argparse.ArgumentParser() # set up the 2.x to 1.x event mapping argument self.parser.add_argument("--map-v2-event-into-v1", action="store_true", default=False, dest="v2event") if hasattr(self, 'setup'): self.setup() (self.options, self.remain) = self.parser.parse_known_args() # map the event if required if (self.options.v2event or os.environ.get("SENSU_MAP_V2_EVENT_INTO_V1")): self.event = map_v2_event_into_v1(self.event) # Filter (deprecated) and handle self.filter() self.handle()
def __init__(self, autorun=True): self.plugin_info = { 'check_name': None, 'message': None, 'status': None } # create a method for each of the exit codes # and register as exiy functions self._hook = ExitHook() self._hook.hook() self.settings = utils.get_settings() self.exit_code = ExitCode(0, 1, 2, 3) for field in self.exit_code._fields: self.__make_dynamic(field) atexit.register(self.__exitfunction) # Prepare command line arguments self.parser = argparse.ArgumentParser() if hasattr(self, 'setup'): self.setup() (self.options, self.remain) = self.parser.parse_known_args() if autorun: self.run()
def test_get_settings_no_files(): ''' Test the get settings method with no files. ''' with patch('os.listdir') as mocked_listdir: mocked_listdir.return_value = [] settings = get_settings() assert settings == {}
def test_get_settings_with_file(): ''' Test the get settings method with one file. ''' test_json = '{"test": { "key": "value"}}' mocked_open = mock_open(read_data=test_json) with patch('os.listdir') as mocked_listdir: try: with patch("builtins.open", mocked_open): mocked_listdir.return_value = ['/etc/sensu/conf.d/test.json'] settings = get_settings() assert settings == {'test': {'key': 'value'}} except ImportError: with patch("__builtin__.open", mocked_open): mocked_listdir.return_value = ['/etc/sensu/conf.d/test.json'] settings = get_settings() assert settings == {'test': {'key': 'value'}}
def test_get_settings_with_file(mock_isfile, mock_isdir): ''' Test the get settings method with one file. ''' test_json = '{"test": { "key": "value"}}' mocked_open = mock_open(read_data=test_json) mock_isfile.return_value = True mock_isdir.return_value = True with patch('os.listdir') as mocked_listdir: try: with patch("builtins.open", mocked_open): mocked_listdir.return_value = ['/etc/sensu/conf.d/test.json'] settings = get_settings() assert settings == {'test': {'key': 'value'}} except ImportError: with patch("__builtin__.open", mocked_open): mocked_listdir.return_value = ['/etc/sensu/conf.d/test.json'] settings = get_settings() assert settings == {'test': {'key': 'value'}}
def __init__(self): # Parse the stdin into a global event object stdin = sys.stdin.read() self.read_event(stdin) # Prepare global settings self.settings = get_settings() self.api_settings = self.get_api_settings() # Filter (deprecated) and handle self.filter() self.handle()
def read_config(): return utils.get_settings()['vault_config']