1# (c) 2018 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 23import os 24 25from ansible_collections.junipernetworks.junos.tests.unit.compat.mock import ( 26 patch, 27 MagicMock, 28) 29from ansible_collections.junipernetworks.junos.tests.unit.modules.utils import ( 30 set_module_args, 31) 32from .junos_module import TestJunosModule 33 34jnpr_mock = MagicMock() 35modules = { 36 "jnpr": jnpr_mock, 37 "jnpr.junos": jnpr_mock.junos, 38 "jnpr.junos.utils": jnpr_mock.junos.utils, 39 "jnpr.junos.utils.scp": jnpr_mock.junos.utils.scp, 40} 41module_patcher = patch.dict("sys.modules", modules) 42module_patcher.start() 43 44from ansible_collections.junipernetworks.junos.plugins.modules import junos_scp 45 46 47class TestJunosScpModule(TestJunosModule): 48 49 module = junos_scp 50 51 def setUp(self): 52 super(TestJunosScpModule, self).setUp() 53 self.mock_get_device = patch( 54 "ansible_collections.junipernetworks.junos.plugins.modules.junos_scp.get_device" 55 ) 56 self.get_device = self.mock_get_device.start() 57 58 self.mock_scp = patch( 59 "ansible_collections.junipernetworks.junos.plugins.modules.junos_scp.SCP" 60 ) 61 self.scp = self.mock_scp.start() 62 63 self.scp_mock = MagicMock() 64 self.scp().__enter__.return_value = self.scp_mock 65 66 def tearDown(self): 67 super(TestJunosScpModule, self).tearDown() 68 self.mock_get_device.stop() 69 self.mock_scp.stop() 70 71 def test_junos_scp_src(self): 72 set_module_args(dict(src="test.txt")) 73 self.execute_module(changed=True) 74 75 self.scp_mock.put.assert_called_once_with( 76 "test.txt", remote_path=".", recursive=False 77 ) 78 79 def test_junos_scp_src_expand_tilde(self): 80 set_module_args(dict(src="~/test.txt")) 81 self.execute_module(changed=True) 82 83 self.scp_mock.put.assert_called_once_with( 84 os.path.expanduser("~/test.txt"), remote_path=".", recursive=False 85 ) 86 87 def test_junos_scp_src_fail(self): 88 self.scp_mock.put.side_effect = OSError( 89 "[Errno 2] No such file or directory: 'text.txt'" 90 ) 91 set_module_args(dict(src="test.txt")) 92 result = self.execute_module(changed=True, failed=True) 93 94 self.assertEqual( 95 result["msg"], "[Errno 2] No such file or directory: 'text.txt'" 96 ) 97 98 def test_junos_scp_remote_src(self): 99 set_module_args(dict(src="test.txt", remote_src=True)) 100 self.execute_module(changed=True) 101 102 self.scp_mock.get.assert_called_once_with( 103 "test.txt", local_path=".", recursive=False 104 ) 105 106 def test_junos_scp_all(self): 107 set_module_args( 108 dict(src="test", remote_src=True, dest="tmp", recursive=True) 109 ) 110 self.execute_module(changed=True) 111 112 self.scp_mock.get.assert_called_once_with( 113 "test", local_path="tmp", recursive=True 114 ) 115