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_query
23from . import nso_module
24from .nso_module import MockResponse
25
26from units.modules.utils import set_module_args
27
28
29class TestNsoQuery(nso_module.TestNsoModule):
30    module = nso_query
31
32    @patch('ansible.module_utils.network.nso.nso.open_url')
33    def test_nso_query(self, open_url_mock):
34        xpath = '/packages/package'
35        fields = ['name', 'package-version']
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('query',
41                         {'xpath_expr': xpath, 'selection': fields}, 200,
42                         '{"result": {"results": [["test", "1.0"]]}}'),
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            'xpath': xpath,
51            'fields': fields,
52            'validate_certs': False
53        })
54        self.execute_module(changed=False, output=[["test", "1.0"]])
55
56        self.assertEqual(0, len(calls))
57