| 1 |
# Copyright (c) 2020 Red Hat Inc. |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Make coding more python3-ish |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import sys |
| 9 |
from io import StringIO |
| 10 |
from unittest import mock |
| 11 |
|
| 12 |
import pytest |
| 13 |
from ansible.errors import AnsibleError |
| 14 |
from ansible.playbook.play_context import PlayContext |
| 15 |
from ansible.plugins.loader import connection_loader |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture(autouse=True) |
| 19 |
def lxc(request): |
| 20 |
"""Fixture to import/load the lxc plugin module. |
| 21 |
|
| 22 |
The fixture parameter is used to determine the presence of liblxc. |
| 23 |
When true (default), a mocked liblxc module is injected. If False, |
| 24 |
no liblxc will be present. |
| 25 |
""" |
| 26 |
liblxc_present = getattr(request, "param", True) |
| 27 |
|
| 28 |
class ContainerMock: |
| 29 |
# dict of container name to its state |
| 30 |
_container_states = {} |
| 31 |
|
| 32 |
def __init__(self, name): |
| 33 |
super().__init__() |
| 34 |
self.name = name |
| 35 |
|
| 36 |
@property |
| 37 |
def state(self): |
| 38 |
return ContainerMock._container_states.get(self.name, "STARTED") |
| 39 |
|
| 40 |
liblxc_module_mock = mock.MagicMock() |
| 41 |
liblxc_module_mock.Container = ContainerMock |
| 42 |
|
| 43 |
with mock.patch.dict("sys.modules"): |
| 44 |
if liblxc_present: |
| 45 |
sys.modules["lxc"] = liblxc_module_mock |
| 46 |
elif "lxc" in sys.modules: |
| 47 |
del sys.modules["lxc"] |
| 48 |
|
| 49 |
from ansible_collections.community.general.plugins.connection import lxc as lxc_plugin_module |
| 50 |
|
| 51 |
assert liblxc_present == lxc_plugin_module.HAS_LIBLXC |
| 52 |
assert bool(getattr(lxc_plugin_module, "_lxc", None)) == liblxc_present |
| 53 |
|
| 54 |
yield lxc_plugin_module |
| 55 |
|
| 56 |
|
| 57 |
class TestLXCConnectionClass: |
| 58 |
@pytest.mark.parametrize("lxc", [True, False], indirect=True) |
| 59 |
def test_lxc_connection_module(self, lxc): |
| 60 |
"""Test that a connection can be created with the plugin.""" |
| 61 |
play_context = PlayContext() |
| 62 |
in_stream = StringIO() |
| 63 |
|
| 64 |
conn = connection_loader.get("lxc", play_context, in_stream) |
| 65 |
assert conn |
| 66 |
assert isinstance(conn, lxc.Connection) |
| 67 |
|
| 68 |
@pytest.mark.parametrize("lxc", [False], indirect=True) |
| 69 |
def test_lxc_connection_liblxc_error(self, lxc): |
| 70 |
"""Test that on connect an error is thrown if liblxc is not present.""" |
| 71 |
play_context = PlayContext() |
| 72 |
in_stream = StringIO() |
| 73 |
conn = connection_loader.get("lxc", play_context, in_stream) |
| 74 |
|
| 75 |
with pytest.raises(AnsibleError, match="lxc python bindings are not installed"): |
| 76 |
conn._connect() |
| 77 |
|
| 78 |
def test_remote_addr_option(self): |
| 79 |
"""Test that the remote_addr option is used""" |
| 80 |
play_context = PlayContext() |
| 81 |
in_stream = StringIO() |
| 82 |
conn = connection_loader.get("lxc", play_context, in_stream) |
| 83 |
|
| 84 |
container_name = "my-container" |
| 85 |
conn.set_option("remote_addr", container_name) |
| 86 |
assert conn.get_option("remote_addr") == container_name |
| 87 |
|
| 88 |
conn._connect() |
| 89 |
assert conn.container_name == container_name |
| 90 |
|
| 91 |
def test_error_when_stopped(self, lxc): |
| 92 |
"""Test that on connect an error is thrown if the container is stopped.""" |
| 93 |
play_context = PlayContext() |
| 94 |
in_stream = StringIO() |
| 95 |
conn = connection_loader.get("lxc", play_context, in_stream) |
| 96 |
conn.set_option("remote_addr", "my-container") |
| 97 |
|
| 98 |
lxc._lxc.Container._container_states["my-container"] = "STOPPED" |
| 99 |
|
| 100 |
with pytest.raises(AnsibleError, match="my-container is not running"): |
| 101 |
conn._connect() |
| 102 |
|
| 103 |
def test_container_name_change(self): |
| 104 |
"""Test connect method reconnects when remote_addr changes""" |
| 105 |
play_context = PlayContext() |
| 106 |
in_stream = StringIO() |
| 107 |
conn = connection_loader.get("lxc", play_context, in_stream) |
| 108 |
|
| 109 |
# setting the option does nothing |
| 110 |
container1_name = "my-container" |
| 111 |
conn.set_option("remote_addr", container1_name) |
| 112 |
assert conn.container_name is None |
| 113 |
assert conn.container is None |
| 114 |
|
| 115 |
# first call initializes the connection |
| 116 |
conn._connect() |
| 117 |
assert conn.container_name == container1_name |
| 118 |
assert conn.container is not None |
| 119 |
assert conn.container.name == container1_name |
| 120 |
container1 = conn.container |
| 121 |
|
| 122 |
# second call is basically a no-op |
| 123 |
conn._connect() |
| 124 |
assert conn.container_name == container1_name |
| 125 |
assert conn.container is container1 |
| 126 |
assert conn.container.name == container1_name |
| 127 |
|
| 128 |
# setting the option does again nothing |
| 129 |
container2_name = "my-other-container" |
| 130 |
conn.set_option("remote_addr", container2_name) |
| 131 |
assert conn.container_name == container1_name |
| 132 |
assert conn.container is container1 |
| 133 |
assert conn.container.name == container1_name |
| 134 |
|
| 135 |
# first call with a different remote_addr changes the connection |
| 136 |
conn._connect() |
| 137 |
assert conn.container_name == container2_name |
| 138 |
assert conn.container is not None |
| 139 |
assert conn.container is not container1 |
| 140 |
assert conn.container.name == container2_name |