def test_serialization(mocker): expected_service_datas = [ "test/helloworld", "hello", "microservice/hashes", "microservice/not_python", "npython", ] mocker.patch.object(GraphQL, "get_all", return_value=service_data_fixture) hub = ServiceWrapper(["hello", "microservice/hashes"]) hub.update_service(not_python_fixture, hub.services) temp_file = tempfile.mktemp(suffix=".json") hub.as_json_file(temp_file) assert hub.get_all_service_names() == expected_service_datas test_hub = ServiceWrapper.from_json_file(path=temp_file) assert test_hub.get_all_service_names() == expected_service_datas os.remove(temp_file)
def test_reload_services_with_list_dict(mocker): expected_service_datas = ["microservice/not_python", "npython"] mocker.patch.object(GraphQL, "get_all", return_value=service_data_fixture) hub = ServiceWrapper([not_python_fixture]) assert hub.get_all_service_names() == expected_service_datas
def test_dynamic_loading_with_deserialization(mocker): expected_service_datas = ['microservice/not_python', 'npython', 'test/helloworld', 'hello', 'microservice/hashes'] mocker.patch.object(GraphQL, 'get_all', return_value=service_data_fixture) hub = ServiceWrapper([not_python_fixture, "hello", "microservice/hashes" ]) assert hub.get_all_service_names() == expected_service_datas
def test_dynamic_loading_with_list_service_names(mocker): expected_service_datas = [ "test/helloworld", "hello", "microservice/hashes", ] mocker.patch.object(GraphQL, "get_all", return_value=service_data_fixture) hub = ServiceWrapper(["hello", "microservice/hashes"]) assert hub.get_all_service_names() == expected_service_datas
def test_serialization(mocker): expected_service_datas = ['microservice/not_python', 'npython', 'test/helloworld', 'hello', 'microservice/hashes'] mocker.patch.object(GraphQL, 'get_all', return_value=service_data_fixture) hub = ServiceWrapper([not_python_fixture, "hello", "microservice/hashes" ]) temp_file = tempfile.mktemp(suffix=".json") hub.as_json_file(temp_file) assert hub.get_all_service_names() == expected_service_datas test_hub = ServiceWrapper.from_json_file(path=temp_file) assert test_hub.get_all_service_names() == expected_service_datas os.remove(temp_file)
class ServiceHub: """ Contains a list of all available Story hub services """ def __init__(self, hub=None): if hub is not None: self.hub = hub return cache_dir = get_cache_dir() self.hub_path = path.join(cache_dir, "hub.json") if path.exists(self.hub_path): hub = self.read_hub_from_json() # local JSON storage doesn't exist or reading it failed if hub is None: makedirs(cache_dir, exist_ok=True) self.hub = ServiceWrapper() self.update_service_wrapper() self.hub = hub self.update_thread = AutoUpdateThread(self.update_service_wrapper, initial_update=False) def read_hub_from_json(self): """ Try loading an existing service JSON blob from storage. Returns an initialized ServiceWrapper if successful, None otherwise. """ try: return ServiceWrapper.from_json_file(self.hub_path) except JSONDecodeError: # local JSON blob might be invalid # see e.g. https://github.com/storyscript/sls/issues/191 return None except OSError: # reading local JSON blob might fail # see e.g. https://github.com/storyscript/sls/issues/195 return None def update_service_wrapper(self): """ Update the in-memory ServiceWrapper and save a snapshot into the cache_dir. """ services = self.hub.fetch_services() self.hub.reload_services(services) self.hub.as_json_file(self.hub_path) def find_services(self, keyword): for service_name in self.hub.get_all_service_names(): if service_name.startswith(keyword): try: service = Service(self.get_service_data(service_name)) service.set_name(service_name) yield service except BaseException: # ignore all invalid services log.warning("Service '%s' has an invalid config", service_name) def get_service_data(self, service_name): return self.hub.get(alias=service_name)