def test_nso_action_not_action(self, open_url_mock): action_input = {} path = '/ncs:devices/device{ce0}/description' schema = nso_module.load_fixture('description_schema.json') calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5.0"}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse('get_schema', {'path': path}, 200, '{"result": %s}' % (json.dumps(schema, ))), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'path': path, 'input': action_input, 'validate_certs': False }) self.execute_module( failed=True, msg='/ncs:devices/device{ce0}/description is not an action') self.assertEqual(0, len(calls))
def _test_valid_version(self, open_url_mock, version): calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "%s"}' % (version, )), MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'), MockResponse('get_trans_changes', {}, 200, '{"result": {"changes": []}}'), MockResponse('delete_trans', {}, 200, '{"result": {}}'), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) data = nso_module.load_fixture('config_empty_data.json') set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'data': data, 'validate_certs': False }) self.execute_module(changed=False, changes=[], diffs=[]) self.assertEqual(0, len(calls))
def test_nso_action_missing(self, open_url_mock): action_input = {} path = '/ncs:devices/device{ce0}/missing' calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5.0"}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse( 'get_schema', {'path': path}, 200, '{"error": {"data": {"param": "path"}, "type": "rpc.method.invalid_params"}}' ), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'path': path, 'input': action_input, 'validate_certs': False }) self.execute_module( failed=True, msg= 'NSO get_schema invalid params. path = /ncs:devices/device{ce0}/missing' ) self.assertEqual(0, len(calls))
def test_nso_show_config(self, open_url_mock): path = '/ncs:devices/device{ce0}' calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse('show_config', { 'path': path, 'result_as': 'json' }, 200, '{"result": {"data": {}}}'), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'path': path, 'operational': False }) self.execute_module(changed=False, output={"data": {}}) self.assertEqual(0, len(calls))
def test_nso_query(self, open_url_mock): xpath = '/packages/package' fields = ['name', 'package-version'] calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse('query', { 'xpath_expr': xpath, 'selection': fields }, 200, '{"result": {"results": [["test", "1.0"]]}}'), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'xpath': xpath, 'fields': fields, 'validate_certs': False }) self.execute_module(changed=False, output=[["test", "1.0"]]) self.assertEqual(0, len(calls))
def test_nso_verify_violation(self, open_url_mock): devices_schema = nso_module.load_fixture('devices_schema.json') device_schema = nso_module.load_fixture('device_schema.json') description_schema = nso_module.load_fixture('description_schema.json') calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5.0"}'), MockResponse('get_module_prefix_map', {}, 200, '{"result": {"tailf-ncs": "ncs"}}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse('get_schema', {'path': '/ncs:devices'}, 200, '{"result": %s}' % (json.dumps(devices_schema, ))), MockResponse('get_schema', {'path': '/ncs:devices/device'}, 200, '{"result": %s}' % (json.dumps(device_schema, ))), MockResponse('exists', {'path': '/ncs:devices/device{ce0}'}, 200, '{"result": {"exists": true}}'), MockResponse('get_value', {'path': '/ncs:devices/device{ce0}/description'}, 200, '{"result": {"value": "In Violation"}}'), MockResponse( 'get_schema', {'path': '/ncs:devices/device/description'}, 200, '{"result": %s}' % (json.dumps(description_schema, ))), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) data = nso_module.load_fixture('verify_violation_data.json') set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'data': data }) self.execute_module(failed=True, violations=[ { 'path': '/ncs:devices/device{ce0}/description', 'expected-value': 'Example Device', 'value': 'In Violation' }, ]) self.assertEqual(0, len(calls))
def test_nso_verify_empty_data(self, open_url_mock): calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.4.3"}'), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) data = {} set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'data': data }) self.execute_module(changed=False) self.assertEqual(0, len(calls))
def _test_invalid_version(self, open_url_mock, version): calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "%s"}' % (version, )), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) data = nso_module.load_fixture('config_config.json') set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'data': data, 'validate_certs': False }) self.execute_module(failed=True) self.assertEqual(0, len(calls))
def test_nso_action_validate_failed(self, open_url_mock): action_input = {} path = '/test:action' output_mismatch = {'version': [{'name': 'v1'}, {'name': 'v3'}]} schema = nso_module.load_fixture('complex_schema.json') calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5.0"}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse('get_schema', {'path': path}, 200, '{"result": %s}' % (json.dumps(schema, ))), MockResponse( 'run_action', { 'path': path, 'params': action_input }, 200, '{"result": {"version": [{"name": "v1"}, {"name": "v2"}]}}'), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'path': path, 'input': action_input, 'output_required': output_mismatch, 'validate_certs': False }) self.execute_module( failed=True, msg= "version value mismatch. expected [{'name': 'v1'}, {'name': 'v3'}] got [{'name': 'v1'}, {'name': 'v2'}]" ) self.assertEqual(0, len(calls))
def test_nso_config_changed(self, open_url_mock): vpn_schema = nso_module.load_fixture('l3vpn_schema.json') l3vpn_schema = nso_module.load_fixture('l3vpn_l3vpn_schema.json') endpoint_schema = nso_module.load_fixture( 'l3vpn_l3vpn_endpoint_schema.json') changes = nso_module.load_fixture('config_config_changes.json') calls = [ MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}), MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5.1"}'), MockResponse('get_module_prefix_map', {}, 200, '{"result": {"l3vpn": "l3vpn"}}'), MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'), MockResponse('get_schema', {'path': '/l3vpn:vpn'}, 200, '{"result": %s}' % (json.dumps(vpn_schema, ))), MockResponse('get_schema', {'path': '/l3vpn:vpn/l3vpn'}, 200, '{"result": %s}' % (json.dumps(l3vpn_schema, ))), MockResponse('exists', {'path': '/l3vpn:vpn/l3vpn{company}'}, 200, '{"result": {"exists": true}}'), MockResponse('get_schema', {'path': '/l3vpn:vpn/l3vpn/endpoint'}, 200, '{"result": %s}' % (json.dumps(endpoint_schema, ))), MockResponse( 'exists', {'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}'}, 200, '{"result": {"exists": false}}'), MockResponse('new_trans', {'mode': 'read_write'}, 200, '{"result": {"th": 2}}'), MockResponse( 'create', {'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}'}, 200, '{"result": {}}'), MockResponse( 'set_value', { 'path': '/l3vpn:vpn/l3vpn{company}/route-distinguisher', 'value': 999 }, 200, '{"result": {}}'), MockResponse( 'set_value', { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/as-number', 'value': 65101 }, 200, '{"result": {}}'), MockResponse( 'set_value', { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/bandwidth', 'value': 12000000 }, 200, '{"result": {}}'), MockResponse( 'set_value', { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ce-device', 'value': 'ce6' }, 200, '{"result": {}}'), MockResponse( 'set_value', { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ce-interface', 'value': 'GigabitEthernet0/12' }, 200, '{"result": {}}'), MockResponse( 'set_value', { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ip-network', 'value': '10.10.1.0/24' }, 200, '{"result": {}}'), MockResponse('get_trans_changes', {}, 200, '{"result": %s}' % (json.dumps(changes), )), MockResponse('validate_commit', {}, 200, '{"result": {}}'), MockResponse('commit', {}, 200, '{"result": {}}'), MockResponse('logout', {}, 200, '{"result": {}}'), ] open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call( calls, *args, **kwargs) data = nso_module.load_fixture('config_config.json') set_module_args({ 'username': '******', 'password': '******', 'url': 'http://localhost:8080/jsonrpc', 'data': data, 'validate_certs': False }) self.execute_module( changed=True, changes=[ { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ce-device', 'type': 'set', 'from': None, 'to': 'ce6' }, { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ip-network', 'type': 'set', 'from': None, 'to': '10.10.1.0/24' }, { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/as-number', 'type': 'set', 'from': None, 'to': '65101' }, { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ce-interface', 'type': 'set', 'from': None, 'to': 'GigabitEthernet0/12' }, { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/bandwidth', 'type': 'set', 'from': None, 'to': '12000000' }, { 'path': '/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}', 'type': 'create' }, ], diffs=[]) self.assertEqual(0, len(calls))