forked from netenglabs/suzieq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
169 lines (136 loc) · 5.52 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pytest
from _pytest.mark.structures import Mark, MarkDecorator
import os
import sys
from suzieq.cli.sq_nubia_context import NubiaSuzieqContext
from suzieq.poller.services import init_services
from unittest.mock import Mock
import yaml
import json
import pandas as pd
from tempfile import mkstemp
import shlex
from subprocess import check_output, CalledProcessError
suzieq_cli_path = './suzieq/cli/suzieq-cli'
commands = [('AddressCmd'), ('ArpndCmd'), ('BgpCmd'), ('DeviceCmd'),
('EvpnVniCmd'), ('InterfaceCmd'), ('LldpCmd'), ('MacCmd'),
('MlagCmd'), ('OspfCmd'), ('RouteCmd'),
('VlanCmd')]
cli_commands = [('arpnd'), ('address'), ('bgp'), ('device'), ('evpnVni'),
('fs'), ('interface'), ('lldp'), ('mac'),
('mlag'), ('ospf'), ('path'), ('route'), ('sqpoller'),
('topology'),
('vlan')]
tables = [('arpnd'), ('bgp'), ('evpnVni'), ('device'), ('fs'), ('ifCounters'),
('interfaces'), ('lldp'), ('macs'), ('mlag'),
('ospfIf'), ('ospfNbr'), ('path'), ('routes'), ('time'),
('topcpu'), ('topmem'), ('topology'), ('vlan')]
@pytest.fixture(scope='function')
def setup_nubia():
_setup_nubia()
def _setup_nubia():
from suzieq.cli.sq_nubia_plugin import NubiaSuzieqPlugin
from nubia import Nubia
# monkey patching -- there might be a better way
plugin = NubiaSuzieqPlugin()
plugin.create_context = create_context
# this is just so that context can be created
shell = Nubia(name='test', plugin=plugin)
def create_context():
config = _create_context_config()
context = NubiaSuzieqContext()
context.cfg = config
return context
@pytest.fixture()
def create_context_config():
return _create_context_config()
def _create_context_config():
config = {'schema-directory': './config/schema',
'service-directory': './config',
'data-directory': './tests/data/basic_dual_bgp/parquet-out',
'temp-directory': '/tmp/suzieq',
'logging-level': 'WARNING',
'test_set': 'basic_dual_bgp', # an extra field for testing
'API_KEY': '68986cfafc9d5a2dc15b20e3e9f289eda2c79f40',
'analyzer': {'timezone': 'GMT'},
}
return config
@pytest.fixture
@pytest.mark.asyncio
def init_services_default(event_loop):
configs = os.path.abspath(os.curdir) + '/config/'
schema = configs + 'schema/'
mock_queue = Mock()
services = event_loop.run_until_complete(
init_services(configs, schema, mock_queue, True))
return services
def load_up_the_tests(dir):
"""reads the files from the samples directory and parametrizes the test"""
tests = []
for i in dir:
if not i.path.endswith('.yml'):
continue
with open(i, 'r') as f:
out = yaml.load(f.read(), Loader=yaml.BaseLoader)
# The format of the YAML file assumed is as follows:
# description: <string>
# tests:
# - command: <sqcmd to execute in non-modal format
# data-directory: <where the data is present>, not used yet
# marks: <space separated string of marks to mark the test>
# output: |
# <json_output>
#
# - command:
# ....
if out and 'tests' in out:
for t in out['tests']:
# We use tags to dynamically mark the parametrized test
# the marks MUST be registered in pytest.ini
markers = []
if 'marks' in t:
markers = [MarkDecorator(Mark(x, [], {}))
for x in t['marks'].split()]
if 'xfail' in t:
except_err = None
if 'raises' in t['xfail']:
except_err = globals()['__builtins__'].get(
t['xfail']['raises'], None)
if except_err:
markers += [pytest.mark.xfail(
reason=t['xfail']['reason'],
raises=except_err)]
else:
if 'reason' in t['xfail']:
markers += [pytest.mark.xfail(
reason=t['xfail']['reason'])]
else:
markers += [pytest.mark.xfail()]
if markers:
tests += [pytest.param(t, marks=markers,
id=t['command'])]
else:
tests += [pytest.param(t, id=t['command'])]
return tests
def setup_sqcmds(testvar, context_config):
sqcmd_path = [sys.executable, suzieq_cli_path]
tmpfname = None
if 'data-directory' in testvar:
# We need to create a tempfile to hold the config
tmpconfig = context_config
tmpconfig['data-directory'] = testvar['data-directory']
fd, tmpfname = mkstemp(suffix='yml')
f = os.fdopen(fd, 'w')
f.write(yaml.dump(tmpconfig))
f.close()
sqcmd_path += ['--config={}'.format(tmpfname)]
exec_cmd = sqcmd_path + shlex.split(testvar['command'])
output = None
error = None
try:
output = check_output(exec_cmd)
except CalledProcessError as e:
error = e.output
if tmpfname:
os.remove(tmpfname)
return output, error