Skip to content

Commit

Permalink
test_: health check
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Dec 16, 2024
1 parent 89f90e1 commit f508cb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
19 changes: 18 additions & 1 deletion tests-functional/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ Functional tests for status-go
- Every test has two types of verifications:
- `verify_is_valid_json_rpc_response()` checks for status code 200, non-empty response, JSON-RPC structure, presence of the `result` field, and expected ID.
- `jsonschema.validate()` is used to check that the response contains expected data, including required fields and types. Schemas are stored in `/schemas/wallet_MethodName`
- New schemas can be generated using `./tests-functional/utils/schema_builder.py` by passing a response to the `CustomSchemaBuilder(schema_name).create_schema(response.json())` method, should be used only on test creation phase, please search `how to create schema:` to see an example in a test
- New schemas can be generated using `./tests-functional/utils/schema_builder.py` by passing a response to the `CustomSchemaBuilder(schema_name).create_schema(response.json())` method, should be used only on test creation phase, please search `how to create schema:` to see an example in a test
# Known issues
## Docker permission denied
When running tests with auto-creating status-backend containers, you might face this:
```shell
sock.connect(self.unix_socket)
PermissionError: [Errno 13] Permission denied
```

Please follow this fix: https://github.com/docker/compose/issues/10299#issuecomment-1438247730

If you're on MacOS and `/var/run/docker.sock` doesn't exist, you need to create a symlink to the docker socket:
```shell
sudo ln -s $HOME/.docker/run/docker.sock /var/run/docker.sock
```
35 changes: 23 additions & 12 deletions tests-functional/clients/status_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from conftest import option
from resources.constants import user_1, DEFAULT_DISPLAY_NAME, USER_DIR

NANOSECONDS_PER_SECOND = 1_000_000_000


class StatusBackend(RpcClient, SignalClient):

Expand All @@ -26,6 +28,7 @@ def __init__(self, await_signals=[]):
host_port = random.choice(option.status_backend_port_range)

self.container = self._start_container(host_port)
assert self._wait_for_container_healthy()
url = f"http://127.0.0.1:{host_port}"
option.status_backend_port_range.remove(host_port)

Expand All @@ -36,8 +39,6 @@ def __init__(self, await_signals=[]):
RpcClient.__init__(self, self.rpc_url)
SignalClient.__init__(self, self.ws_url, await_signals)

self._health_check()

websocket_thread = threading.Thread(target=self._connect)
websocket_thread.daemon = True
websocket_thread.start()
Expand Down Expand Up @@ -71,8 +72,15 @@ def _start_container(self, host_port):
"mode": "rw",
}
},
# Add health check
"healthcheck": {
"Test": ["CMD-SHELL", "curl -f http://localhost:3333/health || exit 1"],
"Interval": 1 * NANOSECONDS_PER_SECOND,
"Timeout": 1 * NANOSECONDS_PER_SECOND,
"StartPeriod": 1 * NANOSECONDS_PER_SECOND,
"Retries": 0,
},
}

if "FUNCTIONAL_TESTS_DOCKER_UID" in os.environ:
container_args["user"] = os.environ["FUNCTIONAL_TESTS_DOCKER_UID"]

Expand All @@ -84,16 +92,19 @@ def _start_container(self, host_port):
option.status_backend_containers.append(container.id)
return container

def _health_check(self):
def _wait_for_container_healthy(self, timeout=5):
start_time = time.time()
while True:
try:
self.api_valid_request(method="Fleets", data=[])
break
except Exception as e:
if time.time() - start_time > 20:
raise Exception(e)
time.sleep(1)
while time.time() - start_time < timeout:
self.container.reload() # Reload container attributes
status = self.container.attrs['State'].get('Health', {}).get('Status')
if status == 'healthy':
logging.debug("Container is healthy!")
return True
if status in ['unhealthy', 'exited', 'dead']:
return False
time.sleep(0.1) # Wait before checking again
logging.error("Timeout reached while waiting for container health.")
return False

def api_request(self, method, data, url=None):
url = url if url else self.api_url
Expand Down

0 comments on commit f508cb7

Please sign in to comment.