Skip to content

Commit d7ea6c2

Browse files
committed
enhancement in test-cases
1 parent 284494e commit d7ea6c2

19 files changed

Lines changed: 588 additions & 85 deletions

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77

88
ncs-netsim is a great tool, but it lack of following features which are developed as part of netsim-wrapper
99

10-
- netsim-wrapper features
11-
- delete-devices \<device-names>
12-
- create-network-from [ yaml | json ] \<filename>
13-
- create-device-from [ yaml | json ] \<filename>
14-
- create-network-template [ yaml | json ]
15-
- create-device-template [ yaml | json ]
10+
```python
11+
Usage nwrap template create [network | device] [yaml | json] [<fileName>]
12+
template load <fileName> [<username> <password>]
13+
download <username> <password> link
14+
delete-devices <DeviceNames>
15+
-v | --version
16+
-vv | --verbose
17+
-h | --help
18+
19+
nwrap is an alias of `netsim-wrapper` and you can pass
20+
multiple devnames instead of single devname, eg. start, stop, ...
21+
22+
Additionally we support all `ncs-netsim` commands
23+
```
1624

1725
netsim-wrapper is a wrapper on top of ncs-netsim with added features. It's written in python and we opened the space to add more features to it.
1826

netsim_wrapper/netsim.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def status(self, devices=None, _async=False):
103103
out = self.device('status', devices, False)
104104
return out
105105

106-
def list(self):
106+
def list(self, raiseError=True):
107107
self.log.debug("fetching netsim list")
108-
out = self.utils.cmd.run(self.cmd + ['list'])
108+
out = self.utils.cmd.run(self.cmd + ['list'], raiseError)
109109
self.log.debug("netsim list done")
110110
return out
111111

@@ -185,6 +185,7 @@ def basic_template(self):
185185
template['neds'].append('<ned1-link>')
186186
template['neds'].append('<ned2-link>')
187187
template['compile-neds'] = True # True/False
188+
template['packages-reload'] = True # True/False
188189
template['start-devices'] = True # True/False
189190
template['add-to-nso'] = True # True/False
190191
template['add-authgroup-to-nso'] = True # True/False

netsim_wrapper/nso.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
3+
from pathlib import Path
14
from collections import defaultdict
25

36
from netsim_wrapper.utils import Singleton, Download
@@ -178,6 +181,8 @@ def compile_neds(self):
178181
self.log.info("$ make clean all -> in {}/src".format(path))
179182
cmd = "cd {}/src; make clean all;".format(path)
180183
self.utils.cmd.call(cmd)
184+
## kkotari: new change
185+
self.utils.change_dir(self.default['cwd'])
181186

182187
def load_authgroup(self, type, path=None): # type -> local/system/custom
183188
path = path or '/tmp/authgroup.xml'
@@ -191,8 +196,8 @@ def load_aaa(self, type): # type -> add.admin/add.default
191196
return path
192197

193198
def load_merge(self, fpath):
194-
self.log.info("{}# load merge {}".format(self.default['user'], fpath))
195-
cmd = "echo 'load merge {}' | ncs_cli -u {} -C".format(fpath, self.default['user'])
199+
self.log.info("{}(config)# load merge {}".format(self.default['user'], fpath))
200+
cmd = "echo 'config ; load merge {} ; commit ;' | ncs_cli -u {} -C".format(fpath, self.default['user'])
196201
self.utils.cmd.call(cmd)
197202

198203
def fetch_neds(self):
@@ -227,12 +232,16 @@ def extract_neds(self, signed_bin=True, extract_tar=True):
227232
if extract_tar:
228233
tar_file = self.utils.get_tar(ned)
229234
self.utils.cmd.run(['tar', '-xvf', tar_file])
230-
self.utils.cmd.run(['rm', '-rf', tar_file, ned, '*.signature', '*.py', '*.py3', 'tailf.cer'])
235+
self.utils.cmd.run(['rm', '-rf', tar_file, ned, 'tailf.cer', f'{tar_file}.signature'])
236+
self.utils.cmd.run(['rm', '-rf', 'README.signature', 'cisco_x509_verify_release.py'])
237+
self.utils.cmd.run(['rm', '-rf', 'cisco_x509_verify_release.py3'])
238+
## kkotari: new change
239+
self.utils.change_dir(self.default['cwd'])
231240
self.log.debug('extraction done')
232241

233242
def create_devices(self):
234243
self.log.info("creating netsim devices")
235-
create = True if not self.nwrap.netsim.list() else False
244+
create = True if not self.nwrap.netsim.list(raiseError=False) else False
236245
if self.default['ttype'] == 'network':
237246
for ned, val in self.default['device-mode']['prefix-based'].items():
238247
path = "{}/{}".format(self.default['ppath'], ned)
@@ -256,8 +265,19 @@ def analyse_params(self):
256265
self.log.debug("analysing template parameters")
257266

258267
self.default.update(self.nwrap.template.data)
268+
# fetching full paths
269+
self.default['nso-packages-path'] = Path(self.default.get('nso-packages-path', '')).absolute().as_posix()
270+
self.default['config-path'] = Path(self.default.get('config-path', '')).absolute().as_posix()
271+
if 'path' in self.default['authgroup']:
272+
self.default['authgroup']['path'] = Path(self.default['authgroup']['path']).absolute().as_posix()
273+
else:
274+
self.default['authgroup']['path'] = None
275+
276+
# mode, package path
259277
self.default['ttype'] = self.nwrap.template.defaults['current']
260278
self.default['ppath'] = self.default['nso-packages-path']
279+
self.default['runpath'] = Path(self.default['nso-packages-path']).parent.as_posix()
280+
self.default['devices-path'] = '/tmp/devices.xml'
261281

262282
# args compile neds
263283
if self.default['compile-neds']:
@@ -267,7 +287,7 @@ def analyse_params(self):
267287
if self.default['add-authgroup-to-nso']:
268288
self.default['authgroup-path'] = self.load_authgroup(
269289
type=self.default['authgroup']['type'],
270-
path=self.default['authgroup'].get('path', None)
290+
path=self.default['authgroup']['path']
271291
)
272292

273293
# args day0 config
@@ -283,54 +303,59 @@ def analyse_params(self):
283303

284304
self.log.debug("analysing template done")
285305

286-
def apply(self, username, password):
306+
def apply(self, cwd, username, password):
287307
self.analyse_params()
308+
self.default['cwd'] = cwd
288309
self.default['username'] = username
289310
self.default['password'] = password
290311

291312
self.log.debug("per-netsim steps")
292313
if self.default['download-neds']:
293314
self.download_neds()
294315
self.extract_neds()
295-
316+
296317
if self.default['compile-neds']:
297318
self.compile_neds()
298319

299320
# feature pre netsim create
300321
@self.log_ncs_cli
301322
def pre_create():
302-
if self.default['compile-neds']:
323+
if self.default['packages-reload']:
303324
self.packages_reload()
304325

305326
if self.default['add-authgroup-to-nso']:
306327
self.load_merge(self.load_aaa(type='admin'))
307328
self.load_merge(self.default['authgroup-path'])
308329
return pre_create
330+
pre_create()
309331

310332
# feature create devices
311333
self.log.debug("netsim steps")
334+
335+
self.utils.change_dir(self.default['runpath'])
312336
self.create_devices()
313337

314338
if self.default['start-devices']:
315339
self.nwrap.netsim.start()
316340

317341
if self.default['add-to-nso']:
318-
path = '/tmp/devices.xml'
319342
data = self.nwrap.netsim.ncs_xml_init(sysout=False)
320-
self.utils.xml.dump(data, path)
343+
if data is not None: self.utils.xml.dump(data, self.default['devices-path'])
321344

322345
# feature post netsim create
323346
self.log.debug("post-netsim steps")
324347
@self.log_ncs_cli
325348
def post_create():
326349
if self.default['add-to-nso']:
327-
self.load_merge(path)
350+
self.load_merge(self.default['devices-path'])
328351
self.fetch_ssh_keys()
329352
self.sync_from()
330353

354+
self.utils.change_dir(self.default['cwd'])
331355
if self.default['load-day0-config']:
332356
for fpath in self.default['config-files']:
333357
self.load_merge(fpath)
334358
return post_create
359+
post_create()
335360

336361
self.log.debug("template done")

netsim_wrapper/nwrap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from netsim_wrapper.netsim import Netsim, NetsimTemplates
55
from netsim_wrapper.netsim import NetsimInfo, NetsimDelete
66
from netsim_wrapper.nso import NsoUtils
7-
7+
from os import getcwd
88

99
help = """
1010
Usage nwrap template create [network | device] [yaml | json] [<fileName>]
@@ -149,6 +149,7 @@ def analyse_params(self, params):
149149
pkeys = ('ncs-xml-init-remote', '--force-generic', 'packages', 'netconf-console', \
150150
'-w', '--window', 'cli', 'cli-c', 'cli-i', 'get-port', 'list')
151151

152+
res['cwd'] = getcwd()
152153
for each in params:
153154
p = str(each).lower()
154155

@@ -278,7 +279,7 @@ def apply(self, params):
278279
# feature template load and apply
279280
if params['load']:
280281
self.nwrap.template.load(params['filename'])
281-
self.nso.apply(params['username'], params['password'])
282+
self.nso.apply(params['cwd'], params['username'], params['password'])
282283

283284
# feature directly calling ncs-netsim commands
284285
if params['pass']:

netsim_wrapper/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def match(slef, data, regex):
255255

256256
def filter_not_alive(self, data):
257257
self.log.debug("applying not-alive filter")
258+
if data == None:
259+
return set()
258260
devices, data = set(), data.split('\n')
259261
regex = re.compile(r'DEVICE\s+(\S+)\s+(.*)')
260262
res = self.match(data, regex)
@@ -265,6 +267,8 @@ def filter_not_alive(self, data):
265267

266268
def filter_alive(self, data):
267269
self.log.debug("applying alive filter")
270+
if data == None:
271+
return set()
268272
devices, data = set(), data.split('\n')
269273
regex = re.compile(r'DEVICE\s+(\S+)\s+(.*)')
270274
res = self.match(data, regex)

tests/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# custom file ignore from tests folder
22
nso-run/
3-
*cisco*
3+
*cisco*

tests/configs/custom_authgroup.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
<devices xmlns="http://tail-f.com/ns/ncs">
3+
<authgroups>
4+
<group>
5+
<name>default</name>
6+
<default-map>
7+
<remote-name>admin</remote-name>
8+
<remote-password>admin</remote-password>
9+
</default-map>
10+
</group>
11+
</authgroups>
12+
</devices>

tests/configs/device.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
nso-packages-path: ./nso-run/packages
2+
download-neds: false
3+
neds:
4+
- https://earth.tail-f.com:8443/ncs-pkgs/cisco-ios/5.7.1/ncs-5.7.1-cisco-ios-6.79.signed.bin
5+
compile-neds: false
6+
packages-reload: false
7+
start-devices: true
8+
add-to-nso: true
9+
add-authgroup-to-nso: false
10+
authgroup:
11+
type: local
12+
path: ./custom_authgroup.xml
13+
device-mode:
14+
name-based:
15+
cisco-ios-cli-6.79:
16+
- device-ios0
17+
- device-ios1
18+
load-day0-config: false
19+
config-path: ../day0/
20+
config-files:
21+
- device_ios0_interface.cfg
22+
- device_ios1_interface.cfg

tests/custom_authgroup.xml

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
devices device device-ios0
2+
config
3+
interface Loopback10
4+
description day0 configuraiton of interface loopback 10
5+
ip address 10.0.10.0 255.255.255.0
6+
no shutdown
7+
exit
8+
interface Loopback100
9+
description day0 configuraiton of interface loopback 100
10+
ip address 10.0.100.0 255.255.255.0
11+
no shutdown
12+
exit
13+
!
14+
!

0 commit comments

Comments
 (0)