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    MagicMock,
26)
27from ansible_collections.junipernetworks.junos.tests.unit.modules.utils import (
28    set_module_args,
29)
30from .junos_module import TestJunosModule
31
32jnpr_mock = MagicMock()
33modules = {
34    "jnpr": jnpr_mock,
35    "jnpr.junos": jnpr_mock.junos,
36    "jnpr.junos.utils": jnpr_mock.junos.utils,
37    "jnpr.junos.utils.sw": jnpr_mock.junos.utils.sw,
38}
39module_patcher = patch.dict("sys.modules", modules)
40module_patcher.start()
41
42from ansible_collections.junipernetworks.junos.plugins.modules import (
43    junos_package,
44)
45
46
47class TestJunosPackageModule(TestJunosModule):
48
49    module = junos_package
50
51    def setUp(self):
52        super(TestJunosPackageModule, self).setUp()
53        self.mock_get_device = patch(
54            "ansible_collections.junipernetworks.junos.plugins.modules.junos_package.get_device"
55        )
56        self.get_device = self.mock_get_device.start()
57
58    def tearDown(self):
59        super(TestJunosPackageModule, self).tearDown()
60        self.mock_get_device.stop()
61
62    def test_junos_package_src(self):
63        set_module_args(dict(src="junos-vsrx-12.1X46-D10.2-domestic.tgz"))
64        self.execute_module(changed=True)
65
66        args, _kwargs = jnpr_mock.junos.utils.sw.SW().install.call_args
67        self.assertEqual(args, ("junos-vsrx-12.1X46-D10.2-domestic.tgz",))
68
69    def test_junos_package_src_fail(self):
70        jnpr_mock.junos.utils.sw.SW().install.return_value = 0
71        set_module_args(dict(src="junos-vsrx-12.1X46-D10.2-domestic.tgz"))
72        result = self.execute_module(changed=True, failed=True)
73
74        self.assertEqual(result["msg"], "Unable to install package on device")
75
76    def test_junos_package_src_no_copy(self):
77        jnpr_mock.junos.utils.sw.SW().install.return_value = 1
78        set_module_args(
79            dict(src="junos-vsrx-12.1X46-D10.2-domestic.tgz", no_copy=True)
80        )
81        self.execute_module(changed=True)
82
83        args, kwargs = jnpr_mock.junos.utils.sw.SW().install.call_args
84        self.assertEqual(args, ("junos-vsrx-12.1X46-D10.2-domestic.tgz",))
85        self.assertEqual(kwargs["no_copy"], True)
86