1# (c) 2017 Red Hat Inc. 2# 3# This file is part of Ansible 4# 5# Ansible is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 3 of the License, or 8# (at your option) any later version. 9# 10# Ansible is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 17 18# Make coding more python3-ish 19from __future__ import absolute_import, division, print_function 20 21__metaclass__ = type 22 23from ansible_collections.junipernetworks.junos.tests.unit.compat.mock import ( 24 patch, 25) 26from ansible_collections.junipernetworks.junos.plugins.modules import ( 27 junos_netconf, 28) 29from ansible_collections.junipernetworks.junos.tests.unit.modules.utils import ( 30 set_module_args, 31) 32from .junos_module import TestJunosModule 33 34 35class TestJunosCommandModule(TestJunosModule): 36 37 module = junos_netconf 38 39 def setUp(self): 40 super(TestJunosCommandModule, self).setUp() 41 42 self.mock_lock_configuration = patch( 43 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.lock_configuration" 44 ) 45 self.lock_configuration = self.mock_lock_configuration.start() 46 47 self.mock_unlock_configuration = patch( 48 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.unlock_configuration" 49 ) 50 self.unlock_configuration = self.mock_unlock_configuration.start() 51 52 self.mock_conn = patch("ansible.module_utils.connection.Connection") 53 self.conn = self.mock_conn.start() 54 55 self.mock_netconf = patch( 56 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.NetconfConnection" 57 ) 58 self.netconf_conn = self.mock_netconf.start() 59 60 self.mock_netconf_rpc = patch( 61 "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.netconf.NetconfConnection" 62 ) 63 self.netconf_rpc = self.mock_netconf_rpc.start() 64 65 self.mock_get_capabilities = patch( 66 "ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos.get_capabilities" 67 ) 68 self.get_capabilities = self.mock_get_capabilities.start() 69 self.get_capabilities.return_value = {"network_api": "netconf"} 70 71 def tearDown(self): 72 super(TestJunosCommandModule, self).tearDown() 73 self.mock_lock_configuration.stop() 74 self.mock_unlock_configuration.stop() 75 self.mock_conn.stop() 76 self.mock_netconf.stop() 77 self.mock_netconf_rpc.stop() 78 self.mock_get_capabilities.stop() 79 80 def test_junos_netconf_enable(self): 81 self.netconf_conn().get.return_value = "" 82 set_module_args(dict(state="present")) 83 result = self.execute_module(changed=True) 84 self.assertEqual( 85 result["commands"], ["set system services netconf ssh port 830"] 86 ) 87 88 def test_junos_netconf_disable(self): 89 out = """ 90 ssh { 91 port 830; 92 } 93 """ 94 self.netconf_conn().get.return_value = out 95 set_module_args(dict(state="absent")) 96 result = self.execute_module(changed=True) 97 self.assertEqual( 98 result["commands"], ["delete system services netconf"] 99 ) 100 101 def test_junos_netconf_port_change(self): 102 out = """ 103 ssh { 104 port 830; 105 } 106 """ 107 self.netconf_conn().get.return_value = out 108 set_module_args(dict(state="present", netconf_port=22)) 109 result = self.execute_module(changed=True) 110 self.assertEqual( 111 result["commands"], ["set system services netconf ssh port 22"] 112 ) 113 114 def test_junos_netconf_port_error(self): 115 out = """ 116 ssh { 117 port 22; 118 } 119 """ 120 self.netconf_conn().get.return_value = out 121 set_module_args(dict(state="present", netconf_port=0)) 122 result = self.execute_module(changed=True, failed=True) 123 self.assertEqual( 124 result["msg"], "netconf_port must be between 1 and 65535" 125 ) 126 127 def test_junos_netconf_config_error(self): 128 self.netconf_conn().get.return_value = None 129 set_module_args(dict(state="present")) 130 result = self.execute_module(failed=True) 131 self.assertEqual(result["msg"], "unable to retrieve current config") 132