1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-05 19:47:12 +00:00

Move modules and module_utils unit tests to correct place (#81)

* Move modules and module_utils unit tests to correct place.

* Update ignore.txt

* Fix imports.

* Fix typos.

* Fix more typos.
This commit is contained in:
Felix Fontein 2020-03-31 10:42:38 +02:00 committed by GitHub
parent ab3c2120fb
commit be191cce6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1170 changed files with 732 additions and 751 deletions

View file

@ -0,0 +1,212 @@
{
"meta": {
"prefix": "ansible",
"namespace": "http://example.com/ansible",
"types": {
},
"keypath": "/ansible:action/complex"
},
"data": {
"kind": "action",
"mandatory": true,
"name": "complex",
"qname": "ansible:complex",
"access": {
"read": false,
"create": false,
"execute": true,
"update": false,
"delete": false
},
"children": [
{
"kind": "leaf",
"is_action_input": true,
"name": "number",
"qname": "ansible:number",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"type": {
"primitive": true,
"name": "uint8"
}
},
{
"kind": "container",
"is_action_input": true,
"mandatory": true,
"name": "ansible",
"qname": "ansible:ansible",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"children": [
{
"kind": "choice",
"cases": [
{
"kind": "case",
"name": "version",
"children": [
{
"kind": "leaf",
"is_action_input": true,
"name": "version",
"qname": "ansible:version",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"type": {
"primitive": true,
"name": "string"
}
}
]
},
{
"kind": "case",
"name": "release",
"children": [
{
"kind": "container",
"is_action_input": true,
"mandatory": true,
"name": "release",
"qname": "ansible:release",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"children": [
{
"kind": "leaf",
"is_action_input": true,
"name": "major",
"qname": "ansible:major",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"type": {
"primitive": true,
"name": "uint8"
}
},
{
"kind": "leaf",
"is_action_input": true,
"name": "minor",
"qname": "ansible:minor",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"type": {
"primitive": true,
"name": "uint8"
}
}
]
}
]
}
],
"name": "version-releae-choice"
}
]
},
{
"kind": "choice",
"cases": [
{
"kind": "case",
"name": "version",
"children": [
{
"kind": "list",
"min_elements": 0,
"name": "version",
"max_elements": "unbounded",
"qname": "ansible:version",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"mandatory": true,
"children": [
{
"kind": "leaf",
"name": "name",
"qname": "ansible:name",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"type": {
"primitive": true,
"name": "string"
},
"is_action_output": true
}
],
"is_action_output": true
}
]
},
{
"kind": "case",
"name": "release",
"children": [
{
"kind": "leaf",
"name": "release",
"qname": "ansible:release",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"type": {
"primitive": true,
"name": "string"
},
"is_action_output": true
}
]
}
],
"name": "version-release-choice"
}
]
}
}

View file

@ -0,0 +1,20 @@
{
"l3vpn:vpn": {
"l3vpn": [
{
"name": "company",
"route-distinguisher": 999,
"endpoint": [
{
"id": "branch-office1",
"ce-device": "ce6",
"ce-interface": "GigabitEthernet0/12",
"ip-network": "10.10.1.0/24",
"bandwidth": 12000000,
"as-number": 65101
}
]
}
]
}
}

View file

@ -0,0 +1,46 @@
{
"changes": [
{
"path": "/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ce-device",
"old": "",
"value": "ce6",
"op": "value_set"
},
{
"path": "/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ip-network",
"old": "",
"value": "10.10.1.0/24",
"op": "value_set"
},
{
"path": "/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/as-number",
"old": "",
"value": "65101",
"op": "value_set"
},
{
"path": "/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/ce-interface",
"old": "",
"value": "GigabitEthernet0/12",
"op": "value_set"
},
{
"path": "/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}/bandwidth",
"old": "",
"value": "12000000",
"op": "value_set"
},
{
"path": "/l3vpn:vpn/l3vpn{company}/endpoint{branch-office1}",
"old": "",
"value": "",
"op": "created"
},
{
"path": "/l3vpn:vpn/l3vpn{company}",
"old": "",
"value": "",
"op": "modified"
}
]
}

View file

@ -0,0 +1,28 @@
{
"meta": {
"prefix": "ncs",
"namespace": "http://tail-f.com/ns/ncs",
"types": {
},
"keypath": "/ncs:devices/device{ce0}/description"
},
"data": {
"info": {
"string": "Free form textual description"
},
"kind": "leaf",
"name": "description",
"qname": "ncs:description",
"access": {
"read": true,
"create": true,
"execute": false,
"update": true,
"delete": true
},
"type": {
"primitive": true,
"name": "string"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
{"meta": {"prefix": "l3vpn", "namespace": "http://com/example/l3vpn", "types": {}, "keypath": "/l3vpn:vpn/l3vpn/endpoint"}, "data": {"kind": "list", "leafref_groups": [["ce-device"]], "min_elements": 0, "name": "endpoint", "max_elements": "unbounded", "qname": "l3vpn:endpoint", "children": [{"info": {"string": "Endpoint identifier"}, "kind": "key", "mandatory": true, "name": "id", "qname": "l3vpn:id", "access": {"read": true, "create": false, "execute": false, "update": true, "delete": false}, "type": {"primitive": true, "name": "string"}}, {"kind": "leaf", "mandatory": true, "name": "ce-device", "type": {"primitive": true, "name": "string"}, "qname": "l3vpn:ce-device", "access": {"read": true, "create": false, "execute": false, "update": true, "delete": false}, "leafref_target": "/ncs:devices/device/name", "is_leafref": true}, {"kind": "leaf", "mandatory": true, "name": "ce-interface", "qname": "l3vpn:ce-interface", "access": {"read": true, "create": false, "execute": false, "update": true, "delete": false}, "type": {"primitive": true, "name": "string"}}, {"kind": "leaf", "mandatory": true, "name": "ip-network", "qname": "l3vpn:ip-network", "access": {"read": true, "create": false, "execute": false, "update": true, "delete": false}, "type": {"primitive": true, "name": "ip-prefix"}}, {"info": {"string": "Bandwidth in bps"}, "kind": "leaf", "mandatory": true, "name": "bandwidth", "qname": "l3vpn:bandwidth", "access": {"read": true, "create": false, "execute": false, "update": true, "delete": false}, "type": {"primitive": true, "name": "uint32"}}, {"info": {"string": "CE Router as-number"}, "kind": "leaf", "name": "as-number", "qname": "l3vpn:as-number", "access": {"read": true, "create": true, "execute": false, "update": true, "delete": true}, "type": {"primitive": true, "name": "uint32"}}], "access": {"read": true, "create": true, "execute": false, "update": true, "delete": true}, "key": ["id"], "mandatory": true, "leafrefGroups": [["ce-device"]]}}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,178 @@
{
"meta": {
"prefix": "ncs",
"namespace": "http://tail-f.com/ns/ncs",
"types": {
"http://tail-f.com/ns/ncs:outformat2": [
{
"name": "http://tail-f.com/ns/ncs:outformat2",
"enumeration": [
{
"info": "NCS CLI curly bracket format.",
"label": "cli"
},
{
"info": "NETCONF XML edit-config format, i.e., the edit-config that\nwould be applied locally (at NCS) to get a config\nthat is equal to that of the managed device.",
"label": "xml"
}
]
},
{
"name": "string"
}
]
},
"keypath": "/ncs:devices/device{ce0}/sync-from"
},
"data": {
"info": {
"string": "Synchronize the config by pulling from the device"
},
"kind": "action",
"mandatory": true,
"name": "sync-from",
"qname": "ncs:sync-from",
"access": {
"read": false,
"create": false,
"execute": true,
"update": false,
"delete": false
},
"children": [
{
"kind": "container",
"is_action_input": true,
"name": "dry-run",
"presence": true,
"qname": "ncs:dry-run",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"children": [
{
"info": {
"string": "Report what would be done towards CDB, without\nactually doing anything."
},
"kind": "leaf",
"is_action_input": true,
"name": "outformat",
"qname": "ncs:outformat",
"access": {
"read": false,
"create": false,
"execute": false,
"update": true,
"delete": false
},
"type": {
"namespace": "http://tail-f.com/ns/ncs",
"name": "outformat2"
}
}
]
},
{
"kind": "choice",
"cases": [
{
"kind": "case",
"name": "result",
"children": [
{
"kind": "leaf",
"name": "result",
"qname": "ncs:result",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"type": {
"primitive": true,
"name": "boolean"
},
"is_action_output": true
}
]
},
{
"kind": "case",
"name": "result-xml",
"children": [
{
"kind": "leaf",
"name": "result-xml",
"is_cli_preformatted": true,
"qname": "ncs:result-xml",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"type": {
"primitive": true,
"name": "string"
},
"is_action_output": true
}
]
},
{
"kind": "case",
"name": "cli",
"children": [
{
"kind": "leaf",
"name": "cli",
"is_cli_preformatted": true,
"qname": "ncs:cli",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"type": {
"primitive": true,
"name": "string"
},
"is_action_output": true
}
]
}
],
"name": "outformat"
},
{
"info": {
"string": "If present, contains additional information about the result."
},
"kind": "leaf",
"name": "info",
"qname": "ncs:info",
"access": {
"read": false,
"create": false,
"execute": false,
"update": false,
"delete": false
},
"type": {
"primitive": true,
"name": "string"
},
"is_action_output": true
}
]
}
}

View file

@ -0,0 +1,10 @@
{
"tailf-ncs:devices": {
"device": [
{
"name": "ce0",
"description": "Example Device"
}
]
}
}

View file

@ -0,0 +1,131 @@
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import os
import json
from ansible_collections.community.general.tests.unit.compat import unittest
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible.module_utils import basic
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
if path not in fixture_data:
with open(path) as f:
data = json.load(f)
fixture_data[path] = data
return fixture_data[path]
class MockResponse(object):
def __init__(self, method, params, code, body, headers=None):
if headers is None:
headers = {}
self.method = method
self.params = params
self.code = code
self.body = body
self.headers = dict(headers)
def read(self):
return self.body
def mock_call(calls, url, timeout, validate_certs, data=None, headers=None, method=None):
if len(calls) == 0:
raise ValueError('no call mock for method {0}({1})'.format(
url, data))
result = calls[0]
del calls[0]
request = json.loads(data)
if result.method != request['method']:
raise ValueError('expected method {0}({1}), got {2}({3})'.format(
result.method, result.params,
request['method'], request['params']))
for key, value in result.params.items():
if key not in request['params']:
raise ValueError('{0} not in parameters'.format(key))
if value != request['params'][key]:
raise ValueError('expected {0} to be {1}, got {2}'.format(
key, value, request['params'][key]))
return result
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestNsoModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, **kwargs):
if failed:
result = self.failed()
self.assertTrue(result['failed'], result)
else:
result = self.changed(changed)
self.assertEqual(result['changed'], changed, result)
for key, value in kwargs.items():
if key not in result:
self.fail("{0} not in result {1}".format(key, result))
self.assertEqual(value, result[key])
return result
def failed(self):
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
with patch.object(basic.AnsibleModule, 'fail_json', fail_json):
with self.assertRaises(AnsibleFailJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertTrue(result['failed'], result)
return result
def changed(self, changed=False):
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
with patch.object(basic.AnsibleModule, 'exit_json', exit_json):
with self.assertRaises(AnsibleExitJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertEqual(result['changed'], changed, result)
return result

View file

@ -0,0 +1,166 @@
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.nso import nso_action
from . import nso_module
from .nso_module import MockResponse
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
class TestNsoAction(nso_module.TestNsoModule):
module = nso_action
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': '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))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': '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))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_action_ok(self, open_url_mock):
action_input = {}
path = '/ncs:devices/device{ce0}/sync-from'
output = {"result": True}
schema = nso_module.load_fixture('sync_from_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": {"result": true}}'),
MockResponse('logout', {}, 200, '{"result": {}}'),
]
open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
set_module_args({
'username': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc',
'path': path,
'input': action_input,
'validate_certs': False
})
self.execute_module(changed=True, output=output)
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_action_validate_ok(self, open_url_mock):
action_input = {}
path = '/test:action'
output = {'version': [{'name': 'v1'}, {'name': 'v2'}]}
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc',
'path': path,
'input': action_input,
'output_required': output,
'validate_certs': False
})
self.execute_module(changed=True, output=output)
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': '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))

View file

@ -0,0 +1,137 @@
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.nso import nso_config
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from . import nso_module
from .nso_module import MockResponse
class TestNsoConfig(nso_module.TestNsoModule):
module = nso_config
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_config_invalid_version_short(self, open_url_mock):
self._test_invalid_version(open_url_mock, '3.3')
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_config_invalid_version_long(self, open_url_mock):
self._test_invalid_version(open_url_mock, '3.3.2')
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc', 'data': data,
'validate_certs': False
})
self.execute_module(failed=True)
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_config_valid_version_short(self, open_url_mock):
self._test_valid_version(open_url_mock, '4.5')
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_config_valid_version_long(self, open_url_mock):
self._test_valid_version(open_url_mock, '4.4.3')
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc', 'data': data,
'validate_certs': False
})
self.execute_module(changed=False, changes=[], diffs=[])
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': '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))

View file

@ -0,0 +1,56 @@
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.nso import nso_query
from . import nso_module
from .nso_module import MockResponse
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
class TestNsoQuery(nso_module.TestNsoModule):
module = nso_query
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': '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))

View file

@ -0,0 +1,97 @@
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.nso import nso_show
from . import nso_module
from .nso_module import MockResponse
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
class TestNsoShow(nso_module.TestNsoModule):
module = nso_show
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_show_missing(self, open_url_mock):
path = '/ncs:devices/device{ce0}/missing'
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,
'{"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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc',
'path': path
})
self.execute_module(failed=True, msg='NSO show_config invalid params. path = /ncs:devices/device{ce0}/missing')
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc',
'path': path,
'operational': False
})
self.execute_module(changed=False, output={"data": {}})
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_show_config_and_oper(self, open_url_mock):
path = '/ncs:devices/device{ce0}/sync-from'
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc',
'path': path,
'operational': True,
'validate_certs': False
})
self.execute_module(changed=False, output={"data": {}})
self.assertEqual(0, len(calls))

View file

@ -0,0 +1,109 @@
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.nso import nso_verify
from . import nso_module
from .nso_module import MockResponse
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
class TestNsoVerify(nso_module.TestNsoModule):
module = nso_verify
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc', 'data': data
})
self.execute_module(changed=False)
self.assertEqual(0, len(calls))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
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': 'user', 'password': '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))
@patch('ansible_collections.community.general.plugins.module_utils.network.nso.nso.open_url')
def test_nso_verify_ok(self, open_url_mock):
devices_schema = nso_module.load_fixture('devices_schema.json')
device_schema = nso_module.load_fixture('device_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": "Example Device"}}'),
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': 'user', 'password': 'password',
'url': 'http://localhost:8080/jsonrpc', 'data': data,
'validate_certs': False
})
self.execute_module(changed=False)
self.assertEqual(0, len(calls))