def test_execute_not_authorized(protocol): with pytest.raises(arcomm.AuthorizationFailed): arcomm.execute(HOST, ['show restricted'], authorize=('bad', 'bad')) response = arcomm.execute(HOST, ['show restricted']) assert response.status == 'failed' and 'not authorized' in str(response)
def test_response_store_access(): responses = arcomm.execute(HOST, ['show version']) assert hasattr(responses, '__iter__'), "response must be an iterable" assert str(responses.last().command) == 'show version', \ "Last item should have been 'show version'" for r in responses: assert hasattr(r, 'command') assert hasattr(r, 'output')
def test_callback(protocol): def _cb(response): global called_back assert isinstance(response, arcomm.response.Response) called_back = 'test_callback' response = arcomm.execute(HOST, ['show bogus'], protocol=protocol, callback=_cb) assert called_back == 'test_callback'
def test_global_subscriber(protocol): import functools def cb(response, key): global called_back called_back = 'test_response_monitor:' + str(key) p1 = functools.partial(cb, key=1) p2 = functools.partial(cb, key=2) p3 = functools.partial(cb, key=3) arcomm.subscribe(p3) arcomm.subscribe(p1) arcomm.subscribe(p1) arcomm.subscribe(p2) # unsub two funcs arcomm.unsubscribe([p1, p2]) # bugus arcomm.unsubscribe("sdfsdf") arcomm.execute(HOST, ['show clock'], protocol=protocol) assert called_back == 'test_response_monitor:3'
#!/usr/bin/env python import os import sh import sys import arcomm _, local, host = sys.argv[:3] sh.scp(local, "{}:/tmp/".format(host)) package = os.path.basename(local) script = """ no extension {0} delete extension:{0} """.format(package) print arcomm.execute('eapi://{}'.format(host), script.splitlines()) script = """ copy file:/tmp/{0} extension: extension {0} copy installed-extensions boot-extensions """.format(package) print arcomm.execute('eapi://{}'.format(host), script.splitlines())
def test_authorize(protocol): response = arcomm.execute(HOST, ['show running-config'], creds=OPS_CREDS, authorize=ENABLE_SECRET, protocol=protocol)
def test_execute_not_authorized(protocol): response = arcomm.execute(HOST, ['show running-config'], creds=OPS_CREDS)[0] assert response.errored and 'privileged mode required' in response.output
def test_execute_invalid_command(protocol): response = arcomm.execute(HOST, ['show bogus'], protocol=protocol) assert response.status == 'failed'
def test_encoding_text(): arcomm.execute(HOST, ['show version'], encoding="text")
def test_execute_sess(): conn = arcomm.connect(HOST, creds=ARCOMM_CREDS) arcomm.execute(conn, 'show version')
def test_execute_ok(protocol): response = arcomm.execute(HOST, ['show clock'], protocol=protocol)
def test_execute_bad_command(protocol): response = arcomm.execute(HOST, ['show gloc'], protocol=protocol) assert response.status == 'failed'
def test_raise_for_error(protocol): response = arcomm.execute(HOST, ['show bogus'], protocol=protocol) with pytest.raises(arcomm.ExecuteFailed): response.raise_for_error()
def test_response_slice(protocol): response = arcomm.execute(HOST, ['show version'], protocol=protocol) response[0][0:5] response[0][0:5:2]
import os import sh import sys import arcomm _, local, host = sys.argv[:3] sh.scp(local, "{}:/tmp/".format(host)) package = os.path.basename(local) script = """ no extension {0} delete extension:{0} """.format(package) print arcomm.execute('eapi://{}'.format(host), script.splitlines()) script = """ copy file:/tmp/{0} extension: extension {0} copy installed-extensions boot-extensions configure no snmp-server extension .1.3.6.1.4.1.8072.1.3.1.5 snmp-server extension .1.3.6.1.4.1.8072.1.3.1.5 file:/var/tmp/snmpext end write """.format(package) print arcomm.execute('eapi://{}'.format(host), script.splitlines())
def test_execute_eapi_unconverted_command(): response = arcomm.execute(HOST, ['show clock'], encoding='json', protocol='eapi+http') assert response.status == 'failed'
def test_not_authorized(protocol): response = arcomm.execute(HOST, ['show running-config'], creds=OPS_CREDS, protocol=protocol) assert response.status == 'failed'