Skip to content

Commit

Permalink
test_: unskip test initialize logging (#6229)
Browse files Browse the repository at this point in the history
* test_: add option to get file from status-backend container

* feat_: automatically create dataDir and logsDir directories

* test_: unskip and update TestInitializeLogging

* fix_: parametrize test

* ci_: use ms precision for func tests docker project_name to

* chore_: rename to extract_data

* fix_: linter

* fix_: set timestamp in python as well
  • Loading branch information
igor-sirotin authored Dec 19, 2024
1 parent b803918 commit 0cf556b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 53 deletions.
3 changes: 2 additions & 1 deletion _assets/scripts/run_functional_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ mkdir -p "${merged_coverage_reports_path}"
mkdir -p "${test_results_path}"

all_compose_files="-f ${root_path}/docker-compose.anvil.yml -f ${root_path}/docker-compose.test.status-go.yml"
project_name="status-go-func-tests-$(date +%s)"
timestamp=$(python3 -c "import time; print(int(time.time() * 1000))") # Keep in sync with status_backend.py
project_name="status-go-func-tests-${timestamp}"

export STATUS_BACKEND_URLS=$(eval echo http://${project_name}-status-backend-{1..${STATUS_BACKEND_COUNT}}:3333 | tr ' ' ,)

Expand Down
21 changes: 15 additions & 6 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"runtime"
"time"
"unsafe"

"go.uber.org/zap"
Expand All @@ -29,6 +32,7 @@ import (
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/logutils/requestlog"
"github.com/status-im/status-go/mobile/callog"
m_requests "github.com/status-im/status-go/mobile/requests"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/accounts"
Expand All @@ -49,11 +53,6 @@ import (
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/services/wallet/wallettypes"
"github.com/status-im/status-go/signal"

"path"
"time"

"github.com/status-im/status-go/mobile/callog"
)

func call(fn any, params ...any) any {
Expand Down Expand Up @@ -106,6 +105,11 @@ func initializeApplication(requestJSON string) string {
providers.MixpanelAppID = request.MixpanelAppID
providers.MixpanelToken = request.MixpanelToken

err = os.MkdirAll(request.DataDir, 0700)
if err != nil {
return makeJSONResponse(err)
}

statusBackend.StatusNode().SetMediaServerEnableTLS(request.MediaServerEnableTLS)
statusBackend.UpdateRootDataDir(request.DataDir)

Expand Down Expand Up @@ -153,7 +157,12 @@ func initializeLogging(request *requests.InitializeApplication) error {
File: path.Join(request.LogDir, api.DefaultLogFile),
}

err := logutils.OverrideRootLoggerWithConfig(logSettings)
err := os.MkdirAll(request.LogDir, 0700)
if err != nil {
return err
}

err = logutils.OverrideRootLoggerWithConfig(logSettings)
if err != nil {
return err
}
Expand Down
30 changes: 28 additions & 2 deletions tests-functional/clients/status_backend.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import io
import json
import logging
import tarfile
import tempfile
import time
import random
import threading
import requests
import docker
import docker.errors
import os

from tenacity import retry, stop_after_delay, wait_fixed
from clients.signals import SignalClient
from clients.rpc import RpcClient
from datetime import datetime
from conftest import option
from resources.constants import user_1, DEFAULT_DISPLAY_NAME, USER_DIR

Expand All @@ -19,6 +22,8 @@

class StatusBackend(RpcClient, SignalClient):

container = None

def __init__(self, await_signals=[]):

if option.status_backend_url:
Expand Down Expand Up @@ -48,7 +53,7 @@ def __init__(self, await_signals=[]):
def _start_container(self, host_port):
docker_project_name = option.docker_project_name

timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
timestamp = int(time.time() * 1000) # Keep in sync with run_functional_tests.sh
image_name = f"{docker_project_name}-status-backend:latest"
container_name = f"{docker_project_name}-status-backend-{timestamp}"

Expand Down Expand Up @@ -153,6 +158,27 @@ def _set_proxy_credentials(self, data):
data["StatusProxyStageName"] = "test"
return data

def extract_data(self, path: str):
if not self.container:
return path

try:
stream, _ = self.container.get_archive(path)
except docker.errors.NotFound:
return None

temp_dir = tempfile.mkdtemp()
tar_bytes = io.BytesIO(b"".join(stream))

with tarfile.open(fileobj=tar_bytes) as tar:
tar.extractall(path=temp_dir)
# If the tar contains a single file, return the path to that file
# Otherwise it's a directory, just return temp_dir.
if len(tar.getmembers()) == 1:
return os.path.join(temp_dir, tar.getmembers()[0].name)

return temp_dir

def create_account_and_login(
self,
data_dir=USER_DIR,
Expand Down
82 changes: 38 additions & 44 deletions tests-functional/tests/test_init_status_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from resources.constants import USER_DIR
from test_cases import StatusBackend
import pytest
from clients.signals import SignalType
Expand Down Expand Up @@ -41,49 +42,42 @@ def test_init_app(self):
)


@pytest.mark.rpc
@pytest.mark.skip("waiting for status-backend to be executed on the same host/container")
class TestInitializeLogging:

@pytest.mark.init
def test_init_logging(self, tmp_path):
self.check_logs(tmp_path, log_enabled=True, api_logging_enabled=True)

@pytest.mark.init
def test_no_logging(self, tmp_path):
self.check_logs(tmp_path, log_enabled=False, api_logging_enabled=False)

def assert_file_first_line(self, path, pattern: str, expected: bool):
assert os.path.exists(path) == expected
if not expected:
return
with open(path) as file:
line = file.readline()
line_found = line.find(pattern) >= 0
assert line_found == expected
def assert_file_first_line(path, pattern: str, expected: bool):
if not expected:
assert path is None
return
assert os.path.exists(path)
with open(path) as file:
line = file.readline()
line_found = line.find(pattern) >= 0
assert line_found == expected

def check_logs(self, path, log_enabled: bool, api_logging_enabled: bool):
data_dir = path / "data"
logs_dir = path / "logs"

data_dir.mkdir()
logs_dir.mkdir()

backend = StatusBackend()
backend.api_valid_request(
"InitializeApplication",
{
"dataDir": str(data_dir),
"logDir": str(logs_dir),
"logEnabled": log_enabled,
"apiLoggingEnabled": api_logging_enabled,
},
)

self.assert_file_first_line(logs_dir / "geth.log", pattern="logging initialised", expected=log_enabled)

self.assert_file_first_line(
logs_dir / "api.log",
pattern='"method": "InitializeApplication"',
expected=api_logging_enabled,
)
@pytest.mark.rpc
@pytest.mark.init
@pytest.mark.parametrize("log_enabled,api_logging_enabled", [(True, True), (False, False)])
def test_check_logs(log_enabled: bool, api_logging_enabled: bool):
data_dir = os.path.join(USER_DIR, "data")
logs_dir = os.path.join(USER_DIR, "logs")

backend = StatusBackend()
backend.api_valid_request(
"InitializeApplication",
{
"dataDir": str(data_dir),
"logDir": str(logs_dir),
"logEnabled": log_enabled,
"apiLoggingEnabled": api_logging_enabled,
},
)

local_geth_log = backend.extract_data(os.path.join(logs_dir, "geth.log"))
local_api_log = backend.extract_data(os.path.join(logs_dir, "api.log"))

assert_file_first_line(path=local_geth_log, pattern="logging initialised", expected=log_enabled)

assert_file_first_line(
path=local_api_log,
pattern='"method": "InitializeApplication"',
expected=api_logging_enabled,
)

0 comments on commit 0cf556b

Please sign in to comment.