Skip to content

Commit ae671aa

Browse files
adding tests
Signed-off-by: Lanuti_emanuele <[email protected]> Signed-off-by: Emanuele Lanuti <[email protected]>
1 parent b3d686f commit ae671aa

File tree

1 file changed

+132
-1
lines changed

1 file changed

+132
-1
lines changed

tests/unit/engine/rest/test_rest_engine.py

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@
66
import pytest
77
from requests.exceptions import ConnectionError
88
from suzieq.engines.rest.engineobj import SqRestEngine
9+
from time import sleep
10+
import subprocess
11+
import os
12+
13+
import yaml
14+
import pytest
15+
16+
from tests.conftest import suzieq_rest_server_path
17+
from suzieq.shared.utils import load_sq_config
18+
from tests.conftest import get_free_port
919

1020

1121
@dataclass
1222
class SqContextMock:
1323
""" SqContext rest parameters mock
1424
"""
25+
cfg: dict
1526
rest_api_key: str
1627
rest_transport: str
1728
rest_server_ip: str
@@ -94,7 +105,7 @@ def req_error(param: str, exp: str, got: str) -> str:
94105
def test_request_params():
95106
"""This test checks if parameters are set correctly in the request
96107
"""
97-
ctxt = SqContextMock('key', 'http', 'rest-ip', 80)
108+
ctxt = SqContextMock({}, 'key', 'http', 'rest-ip', 80)
98109
sqobj = SqObjMock(ctxt, 'default', 'default', 'default',
99110
'default', 'default', 'default', 'default')
100111
engine = SqRestEngine(sqobj)
@@ -109,3 +120,123 @@ def test_request_params():
109120
for sq_params in combinations(testing_params, n_sq_params):
110121
req_params = {p: 'override' for p in sq_params}
111122
validate_args(engine, req_params)
123+
124+
125+
126+
@pytest.mark.rest
127+
@pytest.mark.skipif(not os.environ.get('TEST_SERVER', None),
128+
reason='causes github action hang')
129+
def test_server_cert():
130+
'''Can we can get a valid response with & without certificate'''
131+
from tempfile import mkstemp
132+
133+
# We need to change the port used to avoid conflicts
134+
config = {'data-directory': './tests/data/parquet',
135+
'temp-directory': '/tmp/suzieq',
136+
'logging-level': 'WARNING',
137+
'test_set': 'basic_dual_bgp', # an extra field for testing
138+
'rest': {
139+
'rest-certfile': './tests/test_cert_CA/server-cert.pem',
140+
'rest-keyfile': './tests/test_cert_CA/server-key.pem',
141+
'API_KEY': '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f',
142+
'logging-level': 'WARNING',
143+
'address': '0.0.0.0',
144+
'port': get_free_port(),
145+
'no-https': True,
146+
'log-stdout': True
147+
},
148+
'analyzer': {'timezone': 'GMT'},
149+
}
150+
151+
def create_config(config):
152+
fd, tmpfname = mkstemp(suffix='.yml')
153+
f = os.fdopen(fd, 'w')
154+
f.write(yaml.dump(config))
155+
f.close()
156+
157+
cfgfile = tmpfname
158+
sqcfg = load_sq_config(config_file=cfgfile)
159+
160+
print(f'sqcfg: {sqcfg}')
161+
162+
with open(cfgfile, 'w') as f:
163+
f.write(yaml.safe_dump(sqcfg))
164+
return sqcfg, cfgfile
165+
166+
def open_rest_server(cfgfile):
167+
server_cmd_args = f'{suzieq_rest_server_path} -c {cfgfile}'.split()
168+
# pylint: disable=consider-using-with
169+
proc = subprocess.Popen(server_cmd_args)
170+
sleep(5)
171+
return proc
172+
173+
def make_get_response_request(sqcfg):
174+
ctxt = SqContextMock(
175+
rest_api_key=sqcfg['rest']['API_KEY'],
176+
rest_transport= \
177+
'http' if sqcfg['rest']['no-https'] == True else 'https',
178+
rest_server_ip=sqcfg['rest']['address'],
179+
rest_server_port=sqcfg['rest']['port'],
180+
cfg={'rest': sqcfg['rest']})
181+
182+
sqobj = SqObjMock(ctxt, '', '', 'default',
183+
'default', 'latest', 'device', 'default')
184+
185+
print(ctxt)
186+
print(sqobj)
187+
engine = SqRestEngine(sqobj)
188+
try:
189+
response = engine._get_response('show')
190+
print(f'responsein test: {response}')
191+
return 200
192+
except:
193+
return 400
194+
195+
196+
def close_session(proc, cfgfile):
197+
proc.kill()
198+
os.remove(cfgfile)
199+
200+
#test with http verify None
201+
sqcfg, cfgfile = create_config(config)
202+
proc = open_rest_server(cfgfile)
203+
response = make_get_response_request(sqcfg)
204+
assert response == 200
205+
close_session(proc, cfgfile)
206+
207+
208+
#test with https verify None
209+
config['rest']['no-https'] = False
210+
sqcfg, cfgfile = create_config(config)
211+
proc = open_rest_server(cfgfile)
212+
response = make_get_response_request(sqcfg)
213+
assert response == 400
214+
close_session(proc, cfgfile)
215+
216+
217+
#test with https verify False
218+
config['rest']['cert-verify'] = False
219+
sqcfg, cfgfile = create_config(config)
220+
proc = open_rest_server(cfgfile)
221+
response = make_get_response_request(sqcfg)
222+
assert response == 200
223+
close_session(proc, cfgfile)
224+
225+
226+
#test with https verify True
227+
config['rest']['cert-verify'] = True
228+
sqcfg, cfgfile = create_config(config)
229+
proc = open_rest_server(cfgfile)
230+
response = make_get_response_request(sqcfg)
231+
assert response == 400
232+
close_session(proc, cfgfile)
233+
234+
235+
#test with https verify CA
236+
config['rest']['cert-verify'] =\
237+
'./tests/test_cert_CA/ca-cert.pem'
238+
sqcfg, cfgfile = create_config(config)
239+
proc = open_rest_server(cfgfile)
240+
response = make_get_response_request(sqcfg)
241+
assert response == 200
242+
close_session(proc, cfgfile)

0 commit comments

Comments
 (0)