1#
2# Copyright (c) 2017 Cisco and/or its affiliates.
3#
4# This file is part of Ansible
5#
6# Ansible is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Ansible is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
18
19from __future__ import absolute_import, division, print_function
20__metaclass__ = type
21
22from ansible_collections.cisco.nso.tests.unit.compat.mock import patch
23from ansible_collections.cisco.nso.plugins.modules import nso_show
24from . import nso_module
25from .nso_module import MockResponse
26
27from ansible_collections.cisco.nso.tests.unit.plugins.modules.utils import set_module_args
28
29
30class TestNsoShow(nso_module.TestNsoModule):
31    module = nso_show
32
33    @patch('ansible_collections.cisco.nso.plugins.module_utils.nso.open_url')
34    def test_nso_show_missing(self, open_url_mock):
35        path = '/ncs:devices/device{ce0}/missing'
36        calls = [
37            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
38            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
39            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
40            MockResponse('show_config',
41                         {'path': path, 'result_as': 'json'}, 200,
42                         '{"error": {"data": {"param": "path"}, "type": "rpc.method.invalid_params"}}'),
43            MockResponse('logout', {}, 200, '{"result": {}}'),
44        ]
45        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
46
47        set_module_args({
48            'username': 'user', 'password': 'password',
49            'url': 'http://localhost:8080/jsonrpc',
50            'path': path
51        })
52        self.execute_module(failed=True, msg='NSO show_config invalid params. path = /ncs:devices/device{ce0}/missing')
53
54        self.assertEqual(0, len(calls))
55
56    @patch('ansible_collections.cisco.nso.plugins.module_utils.nso.open_url')
57    def test_nso_show_config(self, open_url_mock):
58        path = '/ncs:devices/device{ce0}'
59        calls = [
60            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
61            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
62            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
63            MockResponse('show_config', {'path': path, 'result_as': 'json'}, 200, '{"result": {"data": {}}}'),
64            MockResponse('logout', {}, 200, '{"result": {}}'),
65        ]
66        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
67
68        set_module_args({
69            'username': 'user', 'password': 'password',
70            'url': 'http://localhost:8080/jsonrpc',
71            'path': path,
72            'operational': False
73        })
74        self.execute_module(changed=False, output={"data": {}})
75        self.assertEqual(0, len(calls))
76
77    @patch('ansible_collections.cisco.nso.plugins.module_utils.nso.open_url')
78    def test_nso_show_config_and_oper(self, open_url_mock):
79        path = '/ncs:devices/device{ce0}/sync-from'
80        calls = [
81            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
82            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
83            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
84            MockResponse('show_config', {'path': path, 'result_as': 'json'}, 200, '{"result": {"data": {}}}'),
85            MockResponse('logout', {}, 200, '{"result": {}}'),
86        ]
87        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
88
89        set_module_args({
90            'username': 'user', 'password': 'password',
91            'url': 'http://localhost:8080/jsonrpc',
92            'path': path,
93            'operational': True,
94            'validate_certs': False
95        })
96        self.execute_module(changed=False, output={"data": {}})
97
98        self.assertEqual(0, len(calls))
99