Skip to content

Commit 284494e

Browse files
committed
adding more test-cases
1 parent 42ad95f commit 284494e

8 files changed

Lines changed: 306 additions & 8 deletions

File tree

netsim_wrapper/nso.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ def extract_neds(self, signed_bin=True, extract_tar=True):
223223
self.utils.change_dir(self.default['ppath'])
224224
for ned in files:
225225
if signed_bin:
226-
self.utils.cmd.run(['bash', ned])
226+
self.utils.cmd.run(['bash', ned, '--skip-verification'])
227227
if extract_tar:
228228
tar_file = self.utils.get_tar(ned)
229229
self.utils.cmd.run(['tar', '-xvf', tar_file])
230-
self.utils.cmd.run(['rm', '-rf', tar_file, ned])
230+
self.utils.cmd.run(['rm', '-rf', tar_file, ned, '*.signature', '*.py', '*.py3', 'tailf.cer'])
231231
self.log.debug('extraction done')
232232

233233
def create_devices(self):

tests/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# custom file ignore from tests folder
2+
nso-run/
3+
*cisco*

tests/custom_authgroup.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<devices xmlns="http://tail-f.com/ns/ncs">
2+
<authgroups>
3+
<group>
4+
<name>default</name>
5+
<default-map>
6+
<remote-name>admin</remote-name>
7+
<remote-password>admin</remote-password>
8+
<remote-secondary-password>admin</remote-secondary-password>
9+
</default-map>
10+
<umap>
11+
<local-user>admin</local-user>
12+
<remote-name>admin</remote-name>
13+
<remote-password>admin</remote-password>
14+
</umap>
15+
<umap>
16+
<local-user>oper</local-user>
17+
<remote-name>oper</remote-name>
18+
<remote-password>admin</remote-password>
19+
</umap>
20+
<umap>
21+
<local-user>cisco</local-user>
22+
<remote-name>cisco</remote-name>
23+
<remote-password>cisco</remote-password>
24+
</umap>
25+
<umap>
26+
<local-user>lab</local-user>
27+
<remote-name>lab</remote-name>
28+
<remote-password>lab</remote-password>
29+
</umap>
30+
</group>
31+
</authgroups>
32+
</devices>

tests/network.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"nso-packages-path": "./nso-run/packages",
3+
"download-neds": false,
4+
"neds": [
5+
"https://ned-link"
6+
],
7+
"compile-neds": true,
8+
"start-devices": true,
9+
"add-to-nso": true,
10+
"add-authgroup-to-nso": true,
11+
"authgroup": {
12+
"type": "system"
13+
},
14+
"device-mode": {
15+
"prefix-based": {
16+
"cisco-ios": {
17+
"count": 2,
18+
"prefix": "ios"
19+
}
20+
}
21+
},
22+
"load-day0-config": true,
23+
"config-path": "./day0/",
24+
"config-files": [
25+
"interface_loopback10.cfg",
26+
"interface_loopback100.cfg"
27+
]
28+
}

tests/test_nwrap_common.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ def test_nwrap_long_verbose(self):
142142
self.applyPatch
143143
nwrap, p = main.check_verbose(self.args)
144144
assert nwrap.log.level == INFO # DEBUG
145-
# TODO: We logger level is getting set to INFO due to singleton objects
145+
# TODO: We logger level is getting set to INFO due to singleton objects
146+
147+
def test_nwrap_dummy(self):
148+
self.args.append('--dummy')
149+
self.applyPatch
150+
try:
151+
main.nwrap()
152+
except SystemExit as e:
153+
assert type(e) == SystemExit
154+
finally:
155+
out, err = self._capfd.readouterr()
156+
out = out.strip()
157+
assert 'Usage nwrap template create' in out

tests/test_nwrap_delete_device.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Usage nwrap template create [network | device] [yaml | json] [<fileName>]
3+
template load <fileName> [<username> <password>]
4+
download <username> <password> link
5+
delete-devices <DeviceNames>
6+
-v | --version
7+
-vv | --verbose
8+
-h | --help
9+
10+
nwrap is an alias of `netsim-wrapper` and you can pass
11+
multiple devnames instead of single devname, eg. start, stop, ...
12+
13+
Additionally we support all `ncs-netsim` commands
14+
"""
15+
16+
import gc
17+
import os
18+
import sys
19+
import pytest
20+
import shutil
21+
import unittest
22+
23+
from logging import DEBUG, INFO
24+
from netsim_wrapper import main
25+
from netsim_wrapper import utils
26+
27+
from _pytest.monkeypatch import MonkeyPatch
28+
29+
30+
# @pytest.mark.run
31+
class TestNwrapDeleteDevice(unittest.TestCase):
32+
33+
def setUp(self):
34+
self.monkeypatch = MonkeyPatch()
35+
self.args = ['nwrap']
36+
37+
def tearDown(self) -> None:
38+
gc.collect()
39+
self.monkeypatch.undo()
40+
return super().tearDown()
41+
42+
@property
43+
def applyPatch(self):
44+
self.monkeypatch.setattr(sys, 'argv', self.args)
45+
46+
@pytest.fixture(autouse=True)
47+
def inject_fixtures(self, capfd):
48+
self._capfd = capfd
49+
50+
@pytest.fixture(autouse=True)
51+
def inject_fixtures_log(self, caplog):
52+
self._caplog = caplog
53+
54+
def test_nwrap_delete_device(self):
55+
# command = 'template create network yaml'.split()
56+
# self.args.extend(command)
57+
# self.applyPatch
58+
# try:
59+
# main.nwrap()
60+
# except SystemExit as e:
61+
# assert type(e) == SystemExit
62+
# finally:
63+
# out, err = self._capfd.readouterr()
64+
# assert out.strip() == ''
65+
66+
# messages = [ x.message for x in self._caplog.get_records('call') if x.levelno == INFO ]
67+
# msg = 'update the base YAML template file: template.yaml'
68+
# assert msg in messages
69+
self._caplog.clear()
70+

tests/test_nwrap_download.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Usage nwrap template create [network | device] [yaml | json] [<fileName>]
3+
template load <fileName> [<username> <password>]
4+
download <username> <password> link
5+
delete-devices <DeviceNames>
6+
-v | --version
7+
-vv | --verbose
8+
-h | --help
9+
10+
nwrap is an alias of `netsim-wrapper` and you can pass
11+
multiple devnames instead of single devname, eg. start, stop, ...
12+
13+
Additionally we support all `ncs-netsim` commands
14+
"""
15+
16+
import gc
17+
import os
18+
import sys
19+
import pytest
20+
import shutil
21+
import unittest
22+
23+
from logging import DEBUG, INFO
24+
from netsim_wrapper import main
25+
from netsim_wrapper import utils
26+
27+
from _pytest.monkeypatch import MonkeyPatch
28+
29+
30+
# @pytest.mark.run
31+
class TestNwrapDownload(unittest.TestCase):
32+
33+
def setUp(self):
34+
self.monkeypatch = MonkeyPatch()
35+
self.args = ['nwrap']
36+
37+
def tearDown(self) -> None:
38+
gc.collect()
39+
self.monkeypatch.undo()
40+
return super().tearDown()
41+
42+
@property
43+
def applyPatch(self):
44+
self.monkeypatch.setattr(sys, 'argv', self.args)
45+
self.monkeypatch.setattr(utils.Command, 'run', lambda s, c, sensitive: 'download successful') # TODO: need to add mock for subprocessor
46+
47+
@pytest.fixture(autouse=True)
48+
def inject_fixtures(self, capfd):
49+
self._capfd = capfd
50+
51+
@pytest.fixture(autouse=True)
52+
def inject_fixtures_log(self, caplog):
53+
self._caplog = caplog
54+
55+
def test_nwrap_download(self):
56+
command = 'template download user_test password_test https://test.com/download.txt'.split()
57+
self.args.extend(command)
58+
self.applyPatch
59+
try:
60+
main.nwrap()
61+
except SystemExit as e:
62+
assert type(e) == SystemExit
63+
finally:
64+
out, err = self._capfd.readouterr()
65+
assert out.strip() == ''
66+
67+
messages = [ x.message for x in self._caplog.get_records('call') if x.levelno == INFO ]
68+
msg = 'download: https://test.com/download.txt'
69+
assert msg in messages
70+
self._caplog.clear()
71+
72+

tests/test_nwrap_template.py

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
import os
1818
import sys
1919
import pytest
20+
import shutil
2021
import unittest
2122

2223
from logging import DEBUG, INFO
2324
from netsim_wrapper import main
24-
from netsim_wrapper.nwrap import NWrap
25+
from netsim_wrapper import utils
2526

2627
from _pytest.monkeypatch import MonkeyPatch
2728

@@ -188,25 +189,88 @@ def test_nwrap_create_device_json_with_file(self):
188189
assert msg in messages
189190
self._caplog.clear()
190191

192+
class TestNwrapTemplateData:
193+
@classmethod
194+
def network(cls):
195+
return {
196+
"nso-packages-path": "./nso-run/packages",
197+
"download-neds": True,
198+
"neds": [
199+
"https://earth.tail-f.com:8443/ncs-pkgs/cisco-ios/5.7.1/ncs-5.7.1-cisco-ios-6.79.signed.bin"
200+
],
201+
"compile-neds": True,
202+
"start-devices": True,
203+
"add-to-nso": True,
204+
"add-authgroup-to-nso": True,
205+
"authgroup": {
206+
"type": "system"
207+
},
208+
"device-mode": {
209+
"prefix-based": {
210+
"cisco-ios-6.56": {
211+
"count": 2,
212+
"prefix": "network-ios"
213+
}
214+
}
215+
},
216+
"load-day0-config": True,
217+
"config-path": "./day0/",
218+
"config-files": [
219+
"interface_loopback10.cfg",
220+
"interface_loopback100.cfg",
221+
]
222+
}
223+
224+
@classmethod
225+
def device(cls):
226+
return {
227+
"nso-packages-path": "./nso-run/packages",
228+
"download-neds": True,
229+
"neds": [
230+
"https://earth.tail-f.com:8443/ncs-pkgs/cisco-ios/5.7.1/ncs-5.7.1-cisco-ios-6.79.signed.bin"
231+
],
232+
"compile-neds": True,
233+
"start-devices": True,
234+
"add-to-nso": True,
235+
"add-authgroup-to-nso": True,
236+
"authgroup": {
237+
"type": "custom",
238+
"path": "./custom_authgroup.xml"
239+
},
240+
"device-mode": {
241+
"name-based": {
242+
"cisco-ios-6.56": [
243+
"device-ios0",
244+
"device-ios1"
245+
]
246+
}
247+
},
248+
"load-day0-config": True,
249+
"config-path": "./day0/",
250+
"config-files": [
251+
"interface_loopback10.cfg",
252+
"interface_loopback100.cfg",
253+
]
254+
}
191255

192256
# @pytest.mark.run
193-
class TestNwrapTemplateLoadNetwork(unittest.TestCase):
257+
class TestNwrapTemplateLoad(unittest.TestCase):
258+
yaml = utils.Yaml()
259+
json = utils.Json()
194260

195261
def setUp(self):
196262
self.monkeypatch = MonkeyPatch()
197263
self.args = ['nwrap']
198-
files = ['template.json', 'template.yaml', 'basic_template.json', 'basic_template.yaml']
199264

200265
def tearDown(self) -> None:
201266
gc.collect()
202-
files = ['template.json', 'template.yaml', 'basic_template.json', 'basic_template.yaml']
203-
[os.unlink(each) for each in files if os.path.exists(each)]
204267
self.monkeypatch.undo()
205268
return super().tearDown()
206269

207270
@property
208271
def applyPatch(self):
209272
self.monkeypatch.setattr(sys, 'argv', self.args)
273+
self.monkeypatch.setattr(utils.Download, 'get', lambda s, u, p, l: None)
210274

211275
@pytest.fixture(autouse=True)
212276
def inject_fixtures(self, capfd):
@@ -216,3 +280,20 @@ def inject_fixtures(self, capfd):
216280
def inject_fixtures_log(self, caplog):
217281
self._caplog = caplog
218282

283+
def test_nwrap_create_network_json(self):
284+
self.json.dump(TestNwrapTemplateData.network(), 'network.json')
285+
command = 'template load network.json'.split()
286+
self.args.extend(command)
287+
self.applyPatch
288+
# try:
289+
# main.nwrap()
290+
# except SystemExit as e:
291+
# assert type(e) == SystemExit
292+
# finally:
293+
# out, err = self._capfd.readouterr()
294+
# assert out.strip() == ''
295+
# # TODO: need to add some validations
296+
# messages = [ x.message for x in self._caplog.get_records('call') if x.levelno == INFO ]
297+
# msg = 'update the base YAML template file: template.yaml'
298+
# assert msg in messages
299+
# self._caplog.clear()

0 commit comments

Comments
 (0)