Skip to content

Commit ed389fe

Browse files
committed
Rename EnvType to UserConfig
1 parent 43c7ae4 commit ed389fe

5 files changed

Lines changed: 63 additions & 132 deletions

File tree

node_cli/configs/env.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ValidationResult(NamedTuple):
4343

4444

4545
@dataclass(kw_only=True)
46-
class BaseEnvConfig(ABC):
46+
class BaseUserConfig(ABC):
4747
container_configs_stream: str
4848
endpoint: str
4949
sgx_server_url: str
@@ -84,20 +84,20 @@ def validate_params(cls, params: Dict) -> ValidationResult:
8484

8585

8686
@dataclass
87-
class MirageEnvConfig(BaseEnvConfig):
87+
class MirageUserConfig(BaseUserConfig):
8888
mirage_contracts: str
8989
enforce_btrfs: str = ''
9090

9191

9292
@dataclass
93-
class MirageBootEnvConfig(BaseEnvConfig):
93+
class MirageBootUserConfig(BaseUserConfig):
9494
manager_contracts: str
9595
ima_contracts: str
9696
enforce_btrfs: str = ''
9797

9898

9999
@dataclass
100-
class SkaleEnvConfig(BaseEnvConfig):
100+
class SkaleUserConfig(BaseUserConfig):
101101
manager_contracts: str
102102
ima_contracts: str
103103
docker_lvmpy_stream: str
@@ -113,7 +113,7 @@ class SkaleEnvConfig(BaseEnvConfig):
113113

114114

115115
@dataclass
116-
class SyncEnvConfig(BaseEnvConfig):
116+
class SyncUserConfig(BaseUserConfig):
117117
manager_contracts: str
118118
schain_name: str = ''
119119
ima_contracts: str = ''
@@ -174,14 +174,14 @@ def absent_required_params(params: Dict[str, str]) -> List[str]:
174174
return [key for key in params if key not in OPTIONAL_PARAMS and not params[key]]
175175

176176

177-
def get_validated_env_config(
177+
def get_validated_user_config(
178178
node_type: NodeType,
179179
env_filepath: str = SKALE_DIR_ENV_FILEPATH,
180180
is_mirage_boot: bool = False,
181-
) -> BaseEnvConfig:
181+
) -> BaseUserConfig:
182182
params = parse_env_file(env_filepath)
183-
EnvType = get_env_class(node_type, is_mirage_boot)
184-
_, missing_params, extra_params = EnvType.validate_params(params)
183+
UserConfigType = get_user_config_type(node_type, is_mirage_boot)
184+
_, missing_params, extra_params = UserConfigType.validate_params(params)
185185

186186
if len(missing_params) > 0:
187187
error_exit(f'Missing required parameters: {missing_params}')
@@ -191,19 +191,19 @@ def get_validated_env_config(
191191

192192
validate_env_type(env_type=params['ENV_TYPE'])
193193
params = to_lower_keys(params)
194-
env = EnvType(**params)
194+
user_config = UserConfigType(**params)
195195

196196
if node_type == NodeType.MIRAGE and not is_mirage_boot:
197-
contract_alias_or_address = env.mirage_contracts
197+
contract_alias_or_address = user_config.mirage_contracts
198198
else:
199199
contract_alias_or_address = params.get('MANAGER_CONTRACTS', '')
200-
contract_alias_or_address = env.manager_contracts
201-
validate_env_alias_or_address(contract_alias_or_address, ContractType.MANAGER, env.endpoint)
200+
contract_alias_or_address = user_config.manager_contracts
201+
validate_env_alias_or_address(contract_alias_or_address, ContractType.MANAGER, user_config.endpoint)
202202

203203
if 'IMA_CONTRACTS' in params:
204-
validate_env_alias_or_address(env.ima_contracts, ContractType.IMA, env.endpoint)
204+
validate_env_alias_or_address(user_config.ima_contracts, ContractType.IMA, user_config.endpoint)
205205

206-
return env
206+
return user_config
207207

208208

209209
def to_lower_keys(params: Dict[str, str]) -> Dict[str, str]:
@@ -216,26 +216,19 @@ def parse_env_file(env_filepath: str) -> Dict:
216216
return DotEnv(env_filepath).dict()
217217

218218

219-
def get_env_class(
219+
def get_user_config_type(
220220
node_type: NodeType,
221221
is_mirage_boot: bool = False,
222-
) -> type[BaseEnvConfig]:
222+
) -> type[BaseUserConfig]:
223223
if node_type == NodeType.MIRAGE and is_mirage_boot:
224-
env_type = MirageBootEnvConfig
224+
user_config_type = MirageBootUserConfig
225225
elif node_type == NodeType.MIRAGE:
226-
env_type = MirageEnvConfig
226+
user_config_type = MirageUserConfig
227227
elif node_type == NodeType.SYNC:
228-
env_type = SyncEnvConfig
228+
user_config_type = SyncUserConfig
229229
else:
230-
env_type = SkaleEnvConfig
231-
return env_type
232-
233-
234-
def populate_env_params(params: Dict[str, str]) -> None:
235-
for key in params:
236-
env_value = os.getenv(key)
237-
if env_value is not None:
238-
params[key] = str(env_value)
230+
user_config_type = SkaleUserConfig
231+
return user_config_type
239232

240233

241234
def validate_env_type(env_type: str) -> None:

node_cli/core/node.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
TM_INIT_TIMEOUT,
4343
)
4444
from node_cli.cli import __version__
45-
from node_cli.configs.env import get_validated_env_config, SKALE_DIR_ENV_FILEPATH
45+
from node_cli.configs.env import get_validated_user_config, SKALE_DIR_ENV_FILEPATH
4646
from node_cli.configs.cli_logger import LOG_DATA_PATH as CLI_LOG_DATA_PATH
4747

4848
from node_cli.core.host import is_node_inited, save_env_params, get_flask_secret_key
@@ -233,15 +233,15 @@ def compose_node_env(
233233
is_mirage_boot: bool = False,
234234
) -> dict[str, str]:
235235
if env_filepath is not None:
236-
env_config = get_validated_env_config(
236+
user_config = get_validated_user_config(
237237
node_type=node_type,
238238
env_filepath=env_filepath,
239239
is_mirage_boot=is_mirage_boot,
240240
)
241241
if save:
242242
save_env_params(env_filepath)
243243
else:
244-
env_config = get_validated_env_config(
244+
user_config = get_validated_user_config(
245245
node_type=node_type,
246246
env_filepath=INIT_ENV_FILEPATH,
247247
is_mirage_boot=is_mirage_boot,
@@ -257,7 +257,7 @@ def compose_node_env(
257257
'SCHAINS_MNT_DIR': mnt_dir,
258258
'FILESTORAGE_MAPPING': FILESTORAGE_MAPPING,
259259
'SKALE_LIB_PATH': SKALE_STATE_DIR,
260-
**env_config.to_env(),
260+
**user_config.to_env(),
261261
}
262262

263263
if inited_node and not node_type == NodeType.SYNC:
@@ -502,7 +502,7 @@ def run_checks(
502502
return
503503

504504
if disk is None:
505-
env_config = get_validated_env_config(node_type=node_type)
505+
env_config = get_validated_user_config(node_type=node_type)
506506
disk = env_config.disk_mountpoint
507507
failed_checks = run_host_checks(disk, node_type, network, container_config_path)
508508
if not failed_checks:

node_cli/core/resources.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import psutil
2626

27-
from node_cli.configs.env import get_validated_env_config
27+
from node_cli.configs.env import get_validated_user_config
2828
from node_cli.utils.docker_utils import ensure_volume
2929
from node_cli.utils.schain_types import SchainTypes
3030
from node_cli.utils.helper import write_json, read_json, run_cmd, safe_load_yml
@@ -102,10 +102,10 @@ def generate_resource_allocation_config(
102102
logger.debug(msg)
103103
print(msg)
104104
return
105-
env_config = get_validated_env_config(node_type=node_type, env_filepath=env_file)
105+
user_config = get_validated_user_config(node_type=node_type, env_filepath=env_file)
106106
logger.info('Generating resource allocation file ...')
107107
try:
108-
update_resource_allocation(env_config.env_type)
108+
update_resource_allocation(user_config.env_type)
109109
except Exception as e:
110110
logger.exception(e)
111111
print("Can't generate resource allocation file, check out CLI logs")

node_cli/core/schains.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
SCHAIN_NODE_DATA_PATH,
1616
SCHAINS_MNT_DIR_SINGLE_CHAIN,
1717
)
18-
from node_cli.configs.env import get_validated_env_config
18+
from node_cli.configs.env import get_validated_user_config
1919

2020
from node_cli.utils.helper import get_request, error_exit, safe_load_yml
2121
from node_cli.utils.exit_codes import CLIExitCodes
@@ -190,8 +190,8 @@ def restore_schain_from_snapshot(
190190
schain_type: str = 'medium',
191191
) -> None:
192192
if env_type is None:
193-
env_config = get_validated_env_config(node_type=node_type)
194-
env_type = env_config.env_type
193+
user_config = get_validated_user_config(node_type=node_type)
194+
env_type = user_config.env_type
195195
ensure_schain_volume(schain, schain_type, env_type)
196196
block_number = get_block_number_from_path(snapshot_path)
197197
if block_number == -1:

tests/configs/configs_env_validate_test.py

Lines changed: 30 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
import os
22
from typing import Optional
3+
4+
import mock
35
import pytest
46
import requests
5-
import mock
67

7-
from node_cli.configs.env import (
8-
SkaleEnvConfig,
9-
SyncEnvConfig,
10-
MirageEnvConfig,
11-
absent_required_params,
12-
get_env_class,
13-
populate_env_params,
14-
get_validated_env_config,
15-
validate_env_params,
16-
validate_env_type,
17-
ALLOWED_ENV_TYPES,
18-
REQUIRED_PARAMS_MIRAGE_BOOT,
19-
REQUIRED_PARAMS_MIRAGE,
20-
OPTIONAL_PARAMS,
21-
)
228
from node_cli.configs.alias_address_validation import (
23-
validate_env_alias_or_address,
24-
validate_contract_address,
25-
validate_contract_alias,
9+
ContractType,
2610
get_chain_id,
2711
get_network_metadata,
28-
ContractType,
12+
validate_contract_address,
13+
validate_contract_alias,
14+
validate_env_alias_or_address,
15+
)
16+
from node_cli.configs.env import (
17+
ALLOWED_ENV_TYPES,
18+
OPTIONAL_PARAMS,
19+
REQUIRED_PARAMS_MIRAGE,
20+
REQUIRED_PARAMS_MIRAGE_BOOT,
21+
MirageBootUserConfig,
22+
MirageUserConfig,
23+
SkaleUserConfig,
24+
SyncUserConfig,
25+
absent_required_params,
26+
get_user_config_type,
27+
get_validated_user_config,
28+
validate_env_type,
2929
)
30-
from node_cli.utils.exit_codes import CLIExitCodes
3130
from node_cli.utils.node_type import NodeType
3231

3332
ENDPOINT = 'http://localhost:8545'
@@ -42,38 +41,18 @@ def json(self):
4241
return self._json_data
4342

4443

45-
def test_absent_required_params_returns_missing_keys():
46-
params = {
47-
'A': '',
48-
'B': 'value',
49-
'C': '',
50-
'MONITORING_CONTAINERS': 'optional',
51-
}
52-
missing = absent_required_params(params)
53-
assert 'A' in missing
54-
assert 'C' in missing
55-
assert 'MONITORING_CONTAINERS' not in missing
56-
57-
58-
def test_populate_env_params_updates_from_environ(monkeypatch):
59-
params = {'FOO': ''}
60-
monkeypatch.setenv('FOO', 'bar')
61-
populate_env_params(params)
62-
assert params['FOO'] == 'bar'
63-
64-
6544
@pytest.mark.parametrize(
6645
'node_type, is_mirage_boot, expected_type',
6746
[
68-
(NodeType.REGULAR, False, SkaleEnvConfig),
69-
(NodeType.SYNC, False, SyncEnvConfig),
70-
(NodeType.MIRAGE, True, SkaleEnvConfig),
71-
(NodeType.MIRAGE, False, MirageEnvConfig),
47+
(NodeType.REGULAR, False, SkaleUserConfig),
48+
(NodeType.SYNC, False, SyncUserConfig),
49+
(NodeType.MIRAGE, True, MirageBootUserConfig),
50+
(NodeType.MIRAGE, False, MirageUserConfig),
7251
],
7352
ids=['regular', 'sync', 'mirage_boot', 'mirage_regular'],
7453
)
7554
def test_build_env_params_keys(node_type, is_mirage_boot, expected_type):
76-
env_type = get_env_class(node_type=node_type, is_mirage_boot=is_mirage_boot)
55+
env_type = get_user_config_type(node_type=node_type, is_mirage_boot=is_mirage_boot)
7756
assert env_type == expected_type
7857

7958

@@ -190,47 +169,6 @@ def test_validate_env_alias_or_address_with_alias(requests_mock):
190169
validate_env_alias_or_address('test-alias', ContractType.IMA, ENDPOINT)
191170

192171

193-
@pytest.mark.parametrize('env_type', ALLOWED_ENV_TYPES)
194-
@pytest.mark.parametrize(
195-
'required_params, key_to_remove, should_fail',
196-
[
197-
(REQUIRED_PARAMS_MIRAGE_BOOT, None, False),
198-
(REQUIRED_PARAMS_MIRAGE, None, False),
199-
(REQUIRED_PARAMS_MIRAGE_BOOT, 'IMA_CONTRACTS', True),
200-
(REQUIRED_PARAMS_MIRAGE_BOOT, 'FILEBEAT_HOST', True),
201-
(REQUIRED_PARAMS_MIRAGE, 'FILEBEAT_HOST', True),
202-
],
203-
ids=[
204-
'mirage_boot',
205-
'mirage_regular',
206-
'mirage_boot_missing_ima',
207-
'mirage_boot_missing_filebeat',
208-
'mirage_regular_missing_filebeat',
209-
],
210-
)
211-
@mock.patch('node_cli.configs.env.validate_env_alias_or_address')
212-
@mock.patch('node_cli.configs.env.validate_env_type')
213-
def test_validate_env_params_mirage(
214-
mock_validate_type,
215-
mock_validate_alias,
216-
required_params,
217-
key_to_remove,
218-
should_fail,
219-
env_type,
220-
):
221-
params = {k: f'{k}_val' for k in required_params}
222-
params['ENV_TYPE'] = env_type
223-
224-
if key_to_remove:
225-
params[key_to_remove] = ''
226-
227-
if should_fail:
228-
with pytest.raises(SystemExit):
229-
validate_env_params(params=params)
230-
else:
231-
validate_env_params(params=params)
232-
233-
234172
@pytest.mark.parametrize(
235173
'node_type, is_boot, required_keys_dict',
236174
[
@@ -280,12 +218,12 @@ def test_get_validated_env_config_mirage_success(
280218
with mock.patch('node_cli.configs.alias_address_validation.requests.post') as mock_post:
281219
mock_post.return_value = FakeResponse(200, {'result': '0x123'})
282220

283-
config = get_validated_env_config(
221+
user_config = get_validated_user_config(
284222
node_type=node_type, env_filepath=str(env_file), is_mirage_boot=is_boot
285223
)
286224

287-
assert config is not None
288-
plain_config = config.to_env()
225+
assert user_config is not None
226+
plain_config = user_config.to_env()
289227
assert set(plain_config.keys()) == set(expected_config.keys())
290228
for key in expected_config:
291229
assert plain_config[key] == expected_config[key]
@@ -296,7 +234,7 @@ def test_get_validated_env_config_mirage_success(
296234

297235
def test_get_validated_env_config_missing_file():
298236
with pytest.raises(SystemExit):
299-
get_validated_env_config(env_filepath='nonexistent.env', node_type=NodeType.REGULAR)
237+
get_validated_user_config(env_filepath='nonexistent.env', node_type=NodeType.REGULAR)
300238

301239

302240
def test_get_validated_env_config_unreadable_file(tmp_path):
@@ -306,6 +244,6 @@ def test_get_validated_env_config_unreadable_file(tmp_path):
306244
try:
307245
os.chmod(env_file, 0o000)
308246
with pytest.raises(PermissionError):
309-
get_validated_env_config(env_filepath=str(env_file), node_type=NodeType.REGULAR)
247+
get_validated_user_config(env_filepath=str(env_file), node_type=NodeType.REGULAR)
310248
finally:
311249
os.chmod(env_file, original_mode)

0 commit comments

Comments
 (0)