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
21from units.compat.mock import patch
22from ansible.modules.network.nso import nso_show
23from . import nso_module
24from .nso_module import MockResponse
25
26from units.modules.utils import set_module_args
27
28
29class TestNsoShow(nso_module.TestNsoModule):
30    module = nso_show
31
32    @patch('ansible.module_utils.network.nso.nso.open_url')
33    def test_nso_show_missing(self, open_url_mock):
34        path = '/ncs:devices/device{ce0}/missing'
35        calls = [
36            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
37            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
38            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
39            MockResponse('show_config',
40                         {'path': path, 'result_as': 'json'}, 200,
41                         '{"error": {"data": {"param": "path"}, "type": "rpc.method.invalid_params"}}'),
42            MockResponse('logout', {}, 200, '{"result": {}}'),
43        ]
44        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
45
46        set_module_args({
47            'username': 'user', 'password': 'password',
48            'url': 'http://localhost:8080/jsonrpc',
49            'path': path
50        })
51        self.execute_module(failed=True, msg='NSO show_config invalid params. path = /ncs:devices/device{ce0}/missing')
52
53        self.assertEqual(0, len(calls))
54
55    @patch('ansible.module_utils.network.nso.nso.open_url')
56    def test_nso_show_config(self, open_url_mock):
57        path = '/ncs:devices/device{ce0}'
58        calls = [
59            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
60            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
61            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
62            MockResponse('show_config', {'path': path, 'result_as': 'json'}, 200, '{"result": {"data": {}}}'),
63            MockResponse('logout', {}, 200, '{"result": {}}'),
64        ]
65        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
66
67        set_module_args({
68            'username': 'user', 'password': 'password',
69            'url': 'http://localhost:8080/jsonrpc',
70            'path': path,
71            'operational': False
72        })
73        self.execute_module(changed=False, output={"data": {}})
74        self.assertEqual(0, len(calls))
75
76    @patch('ansible.module_utils.network.nso.nso.open_url')
77    def test_nso_show_config_and_oper(self, open_url_mock):
78        path = '/ncs:devices/device{ce0}/sync-from'
79        calls = [
80            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
81            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
82            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
83            MockResponse('show_config', {'path': path, 'result_as': 'json'}, 200, '{"result": {"data": {}}}'),
84            MockResponse('logout', {}, 200, '{"result": {}}'),
85        ]
86        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
87
88        set_module_args({
89            'username': 'user', 'password': 'password',
90            'url': 'http://localhost:8080/jsonrpc',
91            'path': path,
92            'operational': True,
93            'validate_certs': False
94        })
95        self.execute_module(changed=False, output={"data": {}})
96
97        self.assertEqual(0, len(calls))
98