Skip to content

Commit 8ab720e

Browse files
Merge pull request #976 from netenglabs/fix/rest-engine-context
Fix the missing rest engine context
2 parents fc7a243 + bf843ca commit 8ab720e

File tree

4 files changed

+181
-16
lines changed

4 files changed

+181
-16
lines changed

suzieq/cli/sqcmds/context_commands.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import typing
21
import os
2+
import typing
3+
34
from nubia import command, context
4-
from nubia.internal.commands.help import HelpCommand
55
from nubia.internal.cmdbase import Command
6+
from nubia.internal.commands.help import HelpCommand
67
from prompt_toolkit.completion import Completion
7-
from termcolor import cprint, colored
8+
from termcolor import colored, cprint
89

910
from suzieq.cli.nubia_patch import argument
10-
from suzieq.shared.utils import SUPPORTED_ENGINES, print_version
11+
from suzieq.shared.utils import (SUPPORTED_ENGINES, print_version,
12+
set_rest_engine)
1113

1214

1315
@command("set")
@@ -86,7 +88,13 @@ def set_ctxt(
8688
ctxt.end_time = end_time
8789

8890
if engine:
89-
plugin_ctx.change_engine(engine)
91+
if ctxt.engine != engine:
92+
if engine == 'rest':
93+
ctxt.rest_server_ip, \
94+
ctxt.rest_server_port, \
95+
ctxt.rest_transport, \
96+
ctxt.rest_api_key = set_rest_engine(ctxt.cfg)
97+
plugin_ctx.change_engine(engine)
9098

9199
if debug:
92100
ctxt.debug = debug == 'True'

suzieq/shared/context.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict, List
22
from dataclasses import dataclass, field
33

4-
from suzieq.shared.utils import SUPPORTED_ENGINES
4+
from suzieq.shared.utils import SUPPORTED_ENGINES, set_rest_engine
55

66

77
@dataclass
@@ -34,15 +34,10 @@ def __post_init__(self):
3434
if not self.engine:
3535
self.engine = self.cfg.get('ux', {}).get('engine', 'pandas')
3636
if self.engine == 'rest':
37-
# See if we can extract the REST info from the REST part
38-
restcfg = self.cfg.get('rest', {})
39-
self.rest_server_ip = restcfg.get('address', '127.0.0.1')
40-
self.rest_server_port = restcfg.get('port', '80')
41-
if restcfg.get('no-https', False):
42-
self.rest_transport = 'http'
43-
else:
44-
self.rest_transport = 'https'
45-
self.rest_api_key = restcfg.get('API_KEY', '')
37+
self.rest_server_ip, \
38+
self.rest_server_port, \
39+
self.rest_transport, \
40+
self.rest_api_key = set_rest_engine(self.cfg)
4641

4742
if self.engine not in SUPPORTED_ENGINES:
4843
raise ValueError(f'Engine {self.engine} not supported')

suzieq/shared/utils.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from logging.handlers import RotatingFileHandler
1616
from os import getenv
1717
from time import time
18-
from typing import Any, Dict, List, Optional, Union
18+
from typing import Any, Dict, List, Optional, Union, Tuple
1919

2020
import pandas as pd
2121
import psutil
@@ -1229,3 +1229,29 @@ def log_suzieq_info(name: str, c_logger: logging.Logger = None,
12291229
'higher with 4 cores and 16 GB RAM')
12301230
if prev_level > logging.INFO:
12311231
c_logger.setLevel(prev_level)
1232+
1233+
1234+
def set_rest_engine(cfg: Dict[str, Any]) -> Tuple[str, str, str, str]:
1235+
"""Unpack the rest configuration from the cfg object. It is used to switch
1236+
to rest engine with the right config params
1237+
1238+
Args:
1239+
cfg (dict): the configuration object.
1240+
1241+
Returns:
1242+
tuple:
1243+
rest_server_ip
1244+
rest_server_port
1245+
rest_transport
1246+
rest_api_key
1247+
"""
1248+
restcfg = cfg.get('rest', {})
1249+
rest_server_ip = restcfg.get('address', '127.0.0.1')
1250+
rest_server_port = restcfg.get('port', '80')
1251+
if restcfg.get('no-https', False):
1252+
rest_transport = 'http'
1253+
else:
1254+
rest_transport = 'https'
1255+
rest_api_key = restcfg.get('API_KEY', '')
1256+
1257+
return rest_server_ip, rest_server_port, rest_transport, rest_api_key

tests/unit/test_sqconfig_load.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import re
2+
from dataclasses import dataclass
23
from tempfile import NamedTemporaryFile
4+
from typing import Dict
35
from unittest.mock import MagicMock, patch
46

57
import pytest
8+
9+
from suzieq.cli.sqcmds import context_commands
10+
from suzieq.shared.context import SqContext
611
from suzieq.shared.utils import (get_sq_install_dir, load_sq_config,
712
validate_sq_config)
813
from tests.conftest import create_dummy_config_file
@@ -90,3 +95,134 @@ def test_config_validation(monkeypatch):
9095
monkeypatch.setenv(env_var, env_key)
9196
error = validate_sq_config(cfg)
9297
assert error is None, error
98+
99+
100+
@pytest.mark.sq_config
101+
@pytest.mark.rest
102+
def test_config_rest() -> None:
103+
"""
104+
Test config file
105+
"""
106+
# with pandas engine
107+
cfg: Dict = {'rest': {}}
108+
109+
ctxt = SqContext(cfg=cfg)
110+
# check that the default value are the same
111+
assert ctxt.rest_transport == 'https'
112+
assert ctxt.rest_server_ip == '127.0.0.1'
113+
assert ctxt.rest_server_port == 8000
114+
assert ctxt.rest_api_key == ''
115+
assert ctxt.engine == 'pandas'
116+
117+
# defining rest engine params
118+
cfg['rest']['API_KEY'] = '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
119+
cfg['rest']['address'] = '0.0.0.0'
120+
cfg['rest']['port'] = 8000
121+
cfg['rest']['no-https'] = True
122+
cfg['ux'] = {'engine': 'rest'}
123+
124+
ctxt = SqContext(cfg=cfg)
125+
assert ctxt.rest_transport == 'http'
126+
assert ctxt.rest_server_ip == '0.0.0.0'
127+
assert ctxt.rest_server_port == 8000
128+
assert ctxt.rest_api_key == '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
129+
assert ctxt.engine == 'rest'
130+
131+
# https with rest engine
132+
cfg['rest']['no-https'] = False
133+
ctxt = SqContext(cfg=cfg)
134+
assert ctxt.rest_transport == 'https'
135+
assert ctxt.rest_server_ip == '0.0.0.0'
136+
assert ctxt.rest_server_port == 8000
137+
assert ctxt.rest_api_key == '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
138+
assert ctxt.engine == 'rest'
139+
140+
141+
@pytest.mark.sq_config
142+
@pytest.mark.rest
143+
def test_context_commands_context(monkeypatch):
144+
"""
145+
test context
146+
"""
147+
CTXT_REST_ATTRS = {
148+
'rest_server_ip': 'address',
149+
'rest_server_port': 'port',
150+
'rest_transport': 'no-https',
151+
'rest_api_key': 'API_KEY'}
152+
153+
@dataclass
154+
class fake_ctxt_class():
155+
engine = 'rest'
156+
cfg = {'rest':
157+
{
158+
'address': '0.0.0.0',
159+
'port': '8000',
160+
'no-https': True,
161+
'API_KEY': '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f'
162+
}}
163+
rest_server_ip: str = 'test_rest_server_ip'
164+
rest_server_port: int = 8080
165+
rest_api_key: str = 'test_rest_api_key'
166+
rest_transport: str = 'test_rest_transport'
167+
168+
@dataclass
169+
class fake_context_class():
170+
ctxt = fake_ctxt_class()
171+
172+
def change_engine(self, engine):
173+
self.ctxt.engine = engine
174+
175+
def fake_get_context():
176+
return fake_context_class()
177+
178+
# sending set engine: rest when is already selected, nothing should change
179+
monkeypatch.setattr(context_commands.context,
180+
'get_context',
181+
fake_get_context)
182+
context_commands.set_ctxt(engine='rest')
183+
assert context_commands.context.get_context().ctxt.engine == 'rest'
184+
assert getattr(context_commands.context.get_context().ctxt,
185+
'rest_server_ip') == 'test_rest_server_ip'
186+
assert getattr(context_commands.context.get_context().ctxt,
187+
'rest_server_port') == 8080
188+
assert getattr(context_commands.context.get_context().ctxt,
189+
'rest_api_key') == 'test_rest_api_key'
190+
assert getattr(context_commands.context.get_context().ctxt,
191+
'rest_transport') == 'test_rest_transport'
192+
193+
# sending set engine: rest when is selected engine: pandas with http
194+
fake_context_class.ctxt.engine = 'pandas'
195+
monkeypatch.setattr(context_commands.context,
196+
'get_context',
197+
fake_get_context)
198+
context_commands.set_ctxt(engine='rest')
199+
assert context_commands.context.get_context().ctxt.engine == 'rest'
200+
for attr in CTXT_REST_ATTRS:
201+
# get the expexted value of the rest param
202+
expected_value = fake_ctxt_class.cfg['rest'][CTXT_REST_ATTRS[attr]]
203+
if CTXT_REST_ATTRS[attr] == 'no-https':
204+
expected_value = 'http' if expected_value is True else 'https'
205+
# check if all the rest attr match
206+
assert getattr(
207+
context_commands.context.get_context().ctxt,
208+
attr) == expected_value, f'{attr} value shold be {expected_value},\
209+
not {getattr(context_commands.context.get_context().ctxt, attr)}'
210+
211+
# sending set engine: rest when is selected engine: pandas with https
212+
fake_context_class.ctxt.engine = 'pandas'
213+
fake_ctxt_class.cfg['rest']['no-https'] = False
214+
monkeypatch.setattr(context_commands.context,
215+
'get_context',
216+
fake_get_context)
217+
context_commands.set_ctxt(engine='rest')
218+
assert context_commands.context.get_context().ctxt.engine == 'rest'
219+
for attr in CTXT_REST_ATTRS:
220+
# get the expexted value of the rest param
221+
expected_value = fake_ctxt_class.cfg['rest'][CTXT_REST_ATTRS[attr]]
222+
if CTXT_REST_ATTRS[attr] == 'no-https':
223+
expected_value = 'http' if expected_value is True else 'https'
224+
# check if all the rest attr match
225+
assert getattr(
226+
context_commands.context.get_context().ctxt,
227+
attr) == expected_value, f'{attr} value shold be {expected_value},\
228+
not {getattr(context_commands.context.get_context().ctxt, attr)}'

0 commit comments

Comments
 (0)