collection 교체

This commit is contained in:
정훈 변
2024-02-23 16:37:40 +09:00
parent b494779b5b
commit 3fd554eee9
38862 changed files with 220204 additions and 6600073 deletions

View File

@@ -1,138 +1,131 @@
# (c) 2020 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Make coding more python3-ish
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from unittest.mock import MagicMock, patch
import pytest
from ansible.module_utils._text import to_bytes
from ansible.errors import AnsibleError, AnsibleFileNotFound
from ansible.module_utils._text import to_bytes
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import connection_loader
from ansible_collections.ansible.netcommon.tests.unit.compat import unittest
from ansible_collections.ansible.netcommon.tests.unit.compat.mock import (
patch,
MagicMock,
)
from ansible_collections.ansible.netcommon.plugins.connection import libssh
pylibsshext = pytest.importorskip("pylibsshext")
class TestConnectionClass(unittest.TestCase):
@patch("pylibsshext.session.Session")
@patch("ansible.plugins.connection.ConnectionBase._connect")
def test_libssh_connect(self, mocked_super, mock_session):
pc = PlayContext()
pc.remote_addr = "localhost"
pc.password = "test"
pc.port = 8080
pc.timeout = 60
pc.remote_user = "user1"
@pytest.fixture(name="conn")
def plugin_fixture():
pc = PlayContext()
pc.port = 8080
pc.timeout = 60
conn = connection_loader.get(
"ansible.netcommon.libssh", pc, "/dev/null"
)
conn = connection_loader.get("ansible.netcommon.libssh", pc, "/dev/null")
return conn
conn.ssh = mock_session
mock_connect = MagicMock()
conn.ssh.connect = mock_connect
conn._connect()
conn.ssh.connect.assert_called_with(
host="localhost",
host_key_checking=False,
look_for_keys=True,
password="test",
port=8080,
timeout=60,
user="user1",
private_key=None,
)
def test_libssh_close(self):
pc = PlayContext()
conn = connection_loader.get(
"ansible.netcommon.libssh", pc, "/dev/null"
)
conn.ssh = MagicMock()
conn.sftp = MagicMock()
conn.chan = MagicMock()
def test_libssh_connect(conn, monkeypatch):
"""Test the libssh connection plugin.
conn.close()
:param monkeypatch: pytest fixture
"""
conn.set_options(
direct={
"remote_addr": "localhost",
"remote_user": "user1",
"password": "test",
"host_key_checking": False,
}
)
conn.sftp.close.assert_called_with()
conn.chan.close.assert_called_with()
conn.sftp.close.assert_called_with()
mock_session = MagicMock()
monkeypatch.setattr(libssh, "Session", mock_session)
mock_ssh = MagicMock()
mock_session.return_value = mock_ssh
conn._connect()
mock_ssh.connect.assert_called_with(
host="localhost",
host_key_checking=False,
look_for_keys=True,
password="test",
password_prompt=None,
port=8080,
timeout=60,
user="user1",
private_key=None,
)
@patch("ansible.plugins.connection.ConnectionBase.exec_command")
def test_libssh_exec_command(self, mocked_super):
pc = PlayContext()
conn = connection_loader.get(
"ansible.netcommon.libssh", pc, "/dev/null"
)
with self.assertRaises(AnsibleError):
conn.exec_command(cmd="ls", in_data=True)
mock_chan = MagicMock()
mock_chan.request_shell = MagicMock()
mock_chan.exec_command = MagicMock()
mock_chan.exec_command.return_value = MagicMock(
returncode=0, stdout="echo hello", stderr=""
)
def test_libssh_close(conn):
conn.ssh = MagicMock()
conn.sftp = MagicMock()
conn.chan = MagicMock()
attr = {"new_channel.return_value": mock_chan}
mock_ssh = MagicMock(**attr)
conn.ssh = mock_ssh
conn.close()
rc, out, err = conn.exec_command(cmd="echo hello")
conn.sftp.close.assert_called_with()
conn.chan.close.assert_called_with()
conn.ssh.close.assert_called_with()
self.assertEqual((rc, out, err), (0, "echo hello", ""))
@patch("ansible.plugins.connection.ConnectionBase.put_file")
def test_libssh_put_file_not_exist(self, mocked_super):
pc = PlayContext()
conn = connection_loader.get(
"ansible.netcommon.libssh", pc, "/dev/null"
)
with self.assertRaises(AnsibleFileNotFound):
conn.put_file(in_path="", out_path="")
@patch("ansible.plugins.connection.ConnectionBase.exec_command")
def test_libssh_exec_command(mocked_super, conn):
with pytest.raises(AnsibleError):
conn.exec_command(cmd="ls", in_data=True)
@patch("os.path.exists")
@patch("ansible.plugins.connection.ConnectionBase.put_file")
def test_libssh_put_file(self, mocked_super, mock_exists):
pc = PlayContext()
conn = connection_loader.get(
"ansible.netcommon.libssh", pc, "/dev/null"
)
mock_chan = MagicMock()
mock_chan.request_shell = MagicMock()
mock_chan.exec_command = MagicMock()
mock_chan.exec_command.return_value = MagicMock(returncode=0, stdout="echo hello", stderr="")
mock_sftp = MagicMock()
attr = {"sftp.return_value": mock_sftp}
mock_ssh = MagicMock(**attr)
conn.ssh = mock_ssh
attr = {"new_channel.return_value": mock_chan}
mock_ssh = MagicMock(**attr)
conn.ssh = mock_ssh
file_path = "test_libssh.py"
conn.put_file(in_path=file_path, out_path=file_path)
mock_sftp.put.assert_called_with(
to_bytes(file_path), to_bytes(file_path)
)
rc, out, err = conn.exec_command(cmd="echo hello")
@patch("pylibsshext.session.Session")
@patch("ansible.plugins.connection.ConnectionBase.fetch_file")
def test_libssh_fetch_file(self, mocked_super, mock_session):
pc = PlayContext()
pc.remote_addr = "localhost"
conn = connection_loader.get(
"ansible.netcommon.libssh", pc, "/dev/null"
)
assert (rc, out, err) == (0, "echo hello", "")
conn.ssh = mock_session
mock_connect = MagicMock()
conn.ssh.connect = mock_connect
file_path = "test_libssh.py"
conn.fetch_file(in_path=file_path, out_path=file_path)
conn.sftp.get.assert_called_with(
to_bytes(file_path), to_bytes(file_path)
)
@patch("ansible.plugins.connection.ConnectionBase.put_file")
def test_libssh_put_file_not_exist(mocked_super, conn):
with pytest.raises(AnsibleFileNotFound):
conn.put_file(in_path="", out_path="")
@patch("os.path.exists")
@patch("ansible.plugins.connection.ConnectionBase.put_file")
def test_libssh_put_file(mocked_super, mock_exists, conn):
mock_sftp = MagicMock()
attr = {"sftp.return_value": mock_sftp}
mock_ssh = MagicMock(**attr)
conn.ssh = mock_ssh
file_path = "test_libssh.py"
conn.put_file(in_path=file_path, out_path=file_path)
mock_sftp.put.assert_called_with(to_bytes(file_path), to_bytes(file_path))
@patch("ansible.plugins.connection.ConnectionBase.fetch_file")
def test_libssh_fetch_file(mocked_super, conn, monkeypatch):
mock_session = MagicMock()
monkeypatch.setattr(libssh, "Session", mock_session)
mock_ssh = MagicMock()
mock_session.return_value = mock_ssh
file_path = "test_libssh.py"
conn.fetch_file(in_path=file_path, out_path=file_path)
conn.sftp.get.assert_called_with(
to_bytes(file_path),
to_bytes(file_path),
)

View File

@@ -1,36 +1,21 @@
#
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Make coding more python3-ish
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import sys
from unittest.mock import MagicMock, PropertyMock, patch
from ansible_collections.ansible.netcommon.tests.unit.compat.mock import (
patch,
MagicMock,
PropertyMock,
)
from ansible.playbook.play_context import PlayContext
import pytest
from ansible.playbook.play_context import PlayContext
pytest.importorskip("ncclient")
@@ -45,19 +30,10 @@ def import_mock(name, *args):
return builtin_import(name, *args)
PY3 = sys.version_info[0] == 3
if PY3:
with patch("builtins.__import__", side_effect=import_mock):
from ansible_collections.ansible.netcommon.plugins.connection import (
netconf,
)
from ansible.plugins.loader import connection_loader
else:
with patch("__builtin__.__import__", side_effect=import_mock):
from ansible_collections.ansible.netcommon.plugins.connection import (
netconf,
)
from ansible.plugins.loader import connection_loader
with patch("builtins.__import__", side_effect=import_mock):
from ansible.plugins.loader import connection_loader
from ansible_collections.ansible.netcommon.plugins.connection import netconf
def test_netconf_init():
@@ -69,9 +45,7 @@ def test_netconf_init():
assert conn._connected is False
@patch(
"ansible_collections.ansible.netcommon.plugins.connection.netconf.netconf_loader"
)
@patch("ansible_collections.ansible.netcommon.plugins.connection.netconf.netconf_loader")
def test_netconf__connect(mock_netconf_loader):
pc = PlayContext()
conn = connection_loader.get("ansible.netcommon.netconf", pc, "/dev/null")

View File

@@ -1,45 +1,38 @@
#
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Make coding more python3-ish
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import json
from ansible_collections.ansible.netcommon.tests.unit.compat.mock import (
MagicMock,
)
from unittest.mock import MagicMock
import pytest
from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils._text import to_text
from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import connection_loader
import pytest
from ansible_collections.ansible.netcommon.plugins.connection.network_cli import terminal_loader
@pytest.fixture(name="conn")
def plugin_fixture():
def plugin_fixture(monkeypatch):
pc = PlayContext()
pc.network_os = "ios"
conn = connection_loader.get(
"ansible.netcommon.network_cli", pc, "/dev/null"
)
pc.network_os = "fakeos"
def get(*args, **kwargs):
return MagicMock()
monkeypatch.setattr(terminal_loader, "get", get)
conn = connection_loader.get("ansible.netcommon.network_cli", pc, "/dev/null")
return conn
@@ -55,10 +48,8 @@ def test_network_cli_invalid_os(network_os):
@pytest.mark.parametrize("look_for_keys", [True, False, None])
@pytest.mark.parametrize("password", ["password", None])
@pytest.mark.parametrize("private_key_file", ["/path/to/key/file", None])
@pytest.mark.parametrize("ssh_type", ["paramiko", "libssh"])
def test_look_for_keys(
conn, look_for_keys, password, private_key_file, ssh_type
):
@pytest.mark.parametrize("ssh_type", ["paramiko", "libssh", "auto"])
def test_look_for_keys(conn, look_for_keys, password, private_key_file, ssh_type):
conn.set_options(
direct={
"ssh_type": ssh_type,
@@ -81,7 +72,7 @@ def test_look_for_keys(
assert conn.ssh_type_conn.get_option("look_for_keys") is True
@pytest.mark.parametrize("ssh_type", ["paramiko", "libssh"])
@pytest.mark.parametrize("ssh_type", ["paramiko", "libssh", "auto"])
def test_options_pass_through(conn, ssh_type):
conn.set_options(
direct={
@@ -104,9 +95,25 @@ def test_options_pass_through(conn, ssh_type):
assert conn.ssh_type_conn.get_option("proxy_command") == "do a proxy"
@pytest.mark.parametrize(
"become_method,become_pass", [("enable", "password"), (None, None)]
)
@pytest.mark.parametrize("has_libssh", (True, False))
def test_network_cli_ssh_type_auto(conn, has_libssh):
"""Test that ssh_type: auto resolves to the correct option."""
from ansible_collections.ansible.netcommon.plugins.connection import network_cli
network_cli.HAS_PYLIBSSH = has_libssh
conn.set_options(
direct={
"ssh_type": "auto",
}
)
if has_libssh:
assert conn.ssh_type == "libssh"
else:
assert conn.ssh_type == "paramiko"
@pytest.mark.parametrize("become_method,become_pass", [("enable", "password"), (None, None)])
def test_network_cli__connect(conn, become_method, become_pass):
conn.ssh = MagicMock()
conn.receive = MagicMock()
@@ -127,9 +134,7 @@ def test_network_cli__connect(conn, become_method, become_pass):
assert conn._terminal.on_become.called is False
@pytest.mark.parametrize(
"command", ["command", json.dumps({"command": "command"})]
)
@pytest.mark.parametrize("command", ["command", json.dumps({"command": "command"})])
def test_network_cli_exec_command(conn, command):
mock_send = MagicMock(return_value=b"command response")
conn.send = mock_send
@@ -145,16 +150,19 @@ def test_network_cli_exec_command(conn, command):
@pytest.mark.parametrize(
"response",
[
b"device#command\ncommand response\n\ndevice#",
[b"device#command\ncommand response\n\ndevice#"],
[b"device#command\ncommand ", b"response\n\ndevice#"],
pytest.param(
b"ERROR: error message device#",
[b"ERROR: error message device#"],
marks=pytest.mark.xfail(raises=AnsibleConnectionFailure),
),
],
)
def test_network_cli_send(conn, response):
@pytest.mark.parametrize("ssh_type", ["paramiko", "libssh", "auto"])
def test_network_cli_send(conn, response, ssh_type):
conn.set_options(
direct={
"ssh_type": ssh_type,
"terminal_stderr_re": [{"pattern": "^ERROR"}],
"terminal_stdout_re": [{"pattern": "device#"}],
}
@@ -165,7 +173,10 @@ def test_network_cli_send(conn, response):
conn._ssh_shell = mock__shell
conn._connected = True
mock__shell.recv.side_effect = [response, None]
if conn.ssh_type == "paramiko":
mock__shell.recv.side_effect = [*response, None]
elif conn.ssh_type == "libssh":
mock__shell.read_bulk_response.side_effect = [*response, None]
conn.send(b"command")
mock__shell.sendall.assert_called_with(b"command\r")