Skip to content

Commit 0cf556b

Browse files
authoredDec 19, 2024··
test_: unskip test initialize logging (#6229)
* 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
1 parent b803918 commit 0cf556b

File tree

4 files changed

+83
-53
lines changed

4 files changed

+83
-53
lines changed
 

‎_assets/scripts/run_functional_tests.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ mkdir -p "${merged_coverage_reports_path}"
2424
mkdir -p "${test_results_path}"
2525

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

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

‎mobile/status.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"os"
9+
"path"
810
"runtime"
11+
"time"
912
"unsafe"
1013

1114
"go.uber.org/zap"
@@ -29,6 +32,7 @@ import (
2932
"github.com/status-im/status-go/images"
3033
"github.com/status-im/status-go/logutils"
3134
"github.com/status-im/status-go/logutils/requestlog"
35+
"github.com/status-im/status-go/mobile/callog"
3236
m_requests "github.com/status-im/status-go/mobile/requests"
3337
"github.com/status-im/status-go/multiaccounts"
3438
"github.com/status-im/status-go/multiaccounts/accounts"
@@ -49,11 +53,6 @@ import (
4953
"github.com/status-im/status-go/services/typeddata"
5054
"github.com/status-im/status-go/services/wallet/wallettypes"
5155
"github.com/status-im/status-go/signal"
52-
53-
"path"
54-
"time"
55-
56-
"github.com/status-im/status-go/mobile/callog"
5756
)
5857

5958
func call(fn any, params ...any) any {
@@ -106,6 +105,11 @@ func initializeApplication(requestJSON string) string {
106105
providers.MixpanelAppID = request.MixpanelAppID
107106
providers.MixpanelToken = request.MixpanelToken
108107

108+
err = os.MkdirAll(request.DataDir, 0700)
109+
if err != nil {
110+
return makeJSONResponse(err)
111+
}
112+
109113
statusBackend.StatusNode().SetMediaServerEnableTLS(request.MediaServerEnableTLS)
110114
statusBackend.UpdateRootDataDir(request.DataDir)
111115

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

156-
err := logutils.OverrideRootLoggerWithConfig(logSettings)
160+
err := os.MkdirAll(request.LogDir, 0700)
161+
if err != nil {
162+
return err
163+
}
164+
165+
err = logutils.OverrideRootLoggerWithConfig(logSettings)
157166
if err != nil {
158167
return err
159168
}

‎tests-functional/clients/status_backend.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import io
12
import json
23
import logging
4+
import tarfile
5+
import tempfile
36
import time
47
import random
58
import threading
69
import requests
710
import docker
11+
import docker.errors
812
import os
913

1014
from tenacity import retry, stop_after_delay, wait_fixed
1115
from clients.signals import SignalClient
1216
from clients.rpc import RpcClient
13-
from datetime import datetime
1417
from conftest import option
1518
from resources.constants import user_1, DEFAULT_DISPLAY_NAME, USER_DIR
1619

@@ -19,6 +22,8 @@
1922

2023
class StatusBackend(RpcClient, SignalClient):
2124

25+
container = None
26+
2227
def __init__(self, await_signals=[]):
2328

2429
if option.status_backend_url:
@@ -48,7 +53,7 @@ def __init__(self, await_signals=[]):
4853
def _start_container(self, host_port):
4954
docker_project_name = option.docker_project_name
5055

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

@@ -153,6 +158,27 @@ def _set_proxy_credentials(self, data):
153158
data["StatusProxyStageName"] = "test"
154159
return data
155160

161+
def extract_data(self, path: str):
162+
if not self.container:
163+
return path
164+
165+
try:
166+
stream, _ = self.container.get_archive(path)
167+
except docker.errors.NotFound:
168+
return None
169+
170+
temp_dir = tempfile.mkdtemp()
171+
tar_bytes = io.BytesIO(b"".join(stream))
172+
173+
with tarfile.open(fileobj=tar_bytes) as tar:
174+
tar.extractall(path=temp_dir)
175+
# If the tar contains a single file, return the path to that file
176+
# Otherwise it's a directory, just return temp_dir.
177+
if len(tar.getmembers()) == 1:
178+
return os.path.join(temp_dir, tar.getmembers()[0].name)
179+
180+
return temp_dir
181+
156182
def create_account_and_login(
157183
self,
158184
data_dir=USER_DIR,
+38-44
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from resources.constants import USER_DIR
12
from test_cases import StatusBackend
23
import pytest
34
from clients.signals import SignalType
@@ -41,49 +42,42 @@ def test_init_app(self):
4142
)
4243

4344

44-
@pytest.mark.rpc
45-
@pytest.mark.skip("waiting for status-backend to be executed on the same host/container")
46-
class TestInitializeLogging:
47-
48-
@pytest.mark.init
49-
def test_init_logging(self, tmp_path):
50-
self.check_logs(tmp_path, log_enabled=True, api_logging_enabled=True)
51-
52-
@pytest.mark.init
53-
def test_no_logging(self, tmp_path):
54-
self.check_logs(tmp_path, log_enabled=False, api_logging_enabled=False)
55-
56-
def assert_file_first_line(self, path, pattern: str, expected: bool):
57-
assert os.path.exists(path) == expected
58-
if not expected:
59-
return
60-
with open(path) as file:
61-
line = file.readline()
62-
line_found = line.find(pattern) >= 0
63-
assert line_found == expected
45+
def assert_file_first_line(path, pattern: str, expected: bool):
46+
if not expected:
47+
assert path is None
48+
return
49+
assert os.path.exists(path)
50+
with open(path) as file:
51+
line = file.readline()
52+
line_found = line.find(pattern) >= 0
53+
assert line_found == expected
6454

65-
def check_logs(self, path, log_enabled: bool, api_logging_enabled: bool):
66-
data_dir = path / "data"
67-
logs_dir = path / "logs"
6855

69-
data_dir.mkdir()
70-
logs_dir.mkdir()
71-
72-
backend = StatusBackend()
73-
backend.api_valid_request(
74-
"InitializeApplication",
75-
{
76-
"dataDir": str(data_dir),
77-
"logDir": str(logs_dir),
78-
"logEnabled": log_enabled,
79-
"apiLoggingEnabled": api_logging_enabled,
80-
},
81-
)
82-
83-
self.assert_file_first_line(logs_dir / "geth.log", pattern="logging initialised", expected=log_enabled)
84-
85-
self.assert_file_first_line(
86-
logs_dir / "api.log",
87-
pattern='"method": "InitializeApplication"',
88-
expected=api_logging_enabled,
89-
)
56+
@pytest.mark.rpc
57+
@pytest.mark.init
58+
@pytest.mark.parametrize("log_enabled,api_logging_enabled", [(True, True), (False, False)])
59+
def test_check_logs(log_enabled: bool, api_logging_enabled: bool):
60+
data_dir = os.path.join(USER_DIR, "data")
61+
logs_dir = os.path.join(USER_DIR, "logs")
62+
63+
backend = StatusBackend()
64+
backend.api_valid_request(
65+
"InitializeApplication",
66+
{
67+
"dataDir": str(data_dir),
68+
"logDir": str(logs_dir),
69+
"logEnabled": log_enabled,
70+
"apiLoggingEnabled": api_logging_enabled,
71+
},
72+
)
73+
74+
local_geth_log = backend.extract_data(os.path.join(logs_dir, "geth.log"))
75+
local_api_log = backend.extract_data(os.path.join(logs_dir, "api.log"))
76+
77+
assert_file_first_line(path=local_geth_log, pattern="logging initialised", expected=log_enabled)
78+
79+
assert_file_first_line(
80+
path=local_api_log,
81+
pattern='"method": "InitializeApplication"',
82+
expected=api_logging_enabled,
83+
)

0 commit comments

Comments
 (0)
Please sign in to comment.