def __wait_for_healthy_mocks(self): for mock in self.__runnable_docker_mocks: blocking_health_check = BlockingHealthCheck(5, 10, mock) is_healthy = blocking_health_check.wait_until_healthy() if not is_healthy: raise exceptions.MockException( f'Mock {mock.get_pretty_name()} never became healthy and timed out on initialization.' ) for mock in self.__runnable_local_mocks: blocking_health_check = BlockingHealthCheck(5, 10, mock) is_healthy = blocking_health_check.wait_until_healthy() if not is_healthy: raise exceptions.MockException( f'Mock {mock.get_pretty_name()} never became healthy and timed out on initialization.' )
def is_healthy(self) -> bool: if not self.__container_id: raise exceptions.MockException( 'Container ID must be set before checking health.') try: common.logger.debug( f'Executing Docker health check for container ID: {self.__container_id}' ) passed = 'healthy' in str(subprocess.run( [ 'docker', 'inspect', "--format='{{.State.Health.Status}}'", self.__container_id ], stdout=subprocess.PIPE).stdout, encoding='utf-8') if passed: common.logger.debug( f'Docker health check passed for container ID: {self.__container_id}' ) else: common.logger.debug( f'Docker health check failed for container ID: {self.__container_id}' ) return passed except (urllib.error.URLError, ConnectionResetError): return False
def register_mock(self, mock: IMockable): if isinstance(mock, RunnableLocalMock): self.__runnable_local_mocks.append(mock) elif isinstance(mock, RunnableDockerMock): self.__runnable_docker_mocks.append(mock) else: raise exceptions.MockException(f'Unsupported mock type: {mock}')
def is_healthy(self) -> bool: if not self.__url: raise exceptions.MockException('URL must be set before checking health.') try: common.logger.debug(f'Executing HTTP health check for: {self.__url}') code = urllib.request.urlopen(self.__url).getcode() passed = code % 200 < 100 if passed: common.logger.debug(f'HTTP health check passed for: {self.__url}') else: common.logger.debug(f'HTTP health check failed for: {self.__url}') return passed except (urllib.error.URLError, ConnectionResetError): return False
def verify(self) -> IRedisVerify: if not self.__verify: raise exceptions.MockException( 'Verify unavailable. Mock is still starting.') return self.__verify
def setup(self) -> IRedisSetup: if not self.__setup: raise exceptions.MockException( 'Setup unavailable. Mock is still starting.') return self.__setup
def set(self, key: str, value: str): if not isinstance(value, str): raise exceptions.MockException('Value must be a str.') return self.__redis_client.set(key, value)
def __check_mock_response_type(self, response): if type(response) is not str: raise exceptions.MockException('Response must be of type str.')