| 1 |
# Copyright (c) Ansible Project |
| 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 |
from __future__ import annotations |
| 6 |
|
| 7 |
import json |
| 8 |
import os |
| 9 |
from unittest import mock |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
linode_apiv4 = pytest.importorskip("linode_api4") |
| 14 |
|
| 15 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args |
| 16 |
from linode_api4 import LinodeClient |
| 17 |
from linode_api4.errors import ApiError as LinodeApiError |
| 18 |
|
| 19 |
from ansible_collections.community.general.plugins.module_utils._linode import get_user_agent |
| 20 |
from ansible_collections.community.general.plugins.modules import linode_v4 |
| 21 |
|
| 22 |
from .linode_conftest import ( # noqa: F401, pylint: disable=unused-import |
| 23 |
access_token, |
| 24 |
default_args, |
| 25 |
mock_linode, |
| 26 |
no_access_token_in_env, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def test_mandatory_state_is_validated(capfd): |
| 31 |
with pytest.raises(SystemExit): |
| 32 |
with set_module_args({"label": "foo"}): |
| 33 |
linode_v4.initialise_module() |
| 34 |
|
| 35 |
out, err = capfd.readouterr() |
| 36 |
results = json.loads(out) |
| 37 |
|
| 38 |
assert all(txt in results["msg"] for txt in ("state", "required")) |
| 39 |
assert results["failed"] is True |
| 40 |
|
| 41 |
|
| 42 |
def test_mandatory_label_is_validated(capfd): |
| 43 |
with pytest.raises(SystemExit): |
| 44 |
with set_module_args({"state": "present"}): |
| 45 |
linode_v4.initialise_module() |
| 46 |
|
| 47 |
out, err = capfd.readouterr() |
| 48 |
results = json.loads(out) |
| 49 |
|
| 50 |
assert all(txt in results["msg"] for txt in ("label", "required")) |
| 51 |
assert results["failed"] is True |
| 52 |
|
| 53 |
|
| 54 |
def test_mandatory_access_token_is_validated(default_args, no_access_token_in_env, capfd): |
| 55 |
with pytest.raises(SystemExit): |
| 56 |
with set_module_args(default_args): |
| 57 |
linode_v4.initialise_module() |
| 58 |
|
| 59 |
out, err = capfd.readouterr() |
| 60 |
results = json.loads(out) |
| 61 |
|
| 62 |
assert results["failed"] is True |
| 63 |
assert all( |
| 64 |
txt in results["msg"] |
| 65 |
for txt in ( |
| 66 |
"missing", |
| 67 |
"required", |
| 68 |
"access_token", |
| 69 |
) |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
def test_mandatory_access_token_passed_in_env(default_args, access_token): |
| 74 |
with set_module_args(default_args): |
| 75 |
try: |
| 76 |
module = linode_v4.initialise_module() |
| 77 |
except SystemExit: |
| 78 |
pytest.fail("'access_token' is passed in environment") |
| 79 |
|
| 80 |
now_set_token = module.params["access_token"] |
| 81 |
assert now_set_token == os.environ["LINODE_ACCESS_TOKEN"] |
| 82 |
|
| 83 |
|
| 84 |
def test_mandatory_access_token_passed_in_as_parameter(default_args, no_access_token_in_env): |
| 85 |
default_args.update({"access_token": "foo"}) |
| 86 |
with set_module_args(default_args): |
| 87 |
try: |
| 88 |
module = linode_v4.initialise_module() |
| 89 |
except SystemExit: |
| 90 |
pytest.fail("'access_token' is passed in as parameter") |
| 91 |
|
| 92 |
assert module.params["access_token"] == "foo" |
| 93 |
|
| 94 |
|
| 95 |
def test_instance_by_label_cannot_authenticate(capfd, access_token, default_args): |
| 96 |
with set_module_args(default_args): |
| 97 |
module = linode_v4.initialise_module() |
| 98 |
client = LinodeClient(module.params["access_token"]) |
| 99 |
|
| 100 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 101 |
with mock.patch(target, side_effect=LinodeApiError("foo")): |
| 102 |
with pytest.raises(SystemExit): |
| 103 |
linode_v4.maybe_instance_from_label(module, client) |
| 104 |
|
| 105 |
out, err = capfd.readouterr() |
| 106 |
results = json.loads(out) |
| 107 |
|
| 108 |
assert results["failed"] is True |
| 109 |
assert "Unable to query the Linode API" in results["msg"] |
| 110 |
|
| 111 |
|
| 112 |
def test_no_instances_found_with_label_gives_none(default_args, access_token): |
| 113 |
with set_module_args(default_args): |
| 114 |
module = linode_v4.initialise_module() |
| 115 |
client = LinodeClient(module.params["access_token"]) |
| 116 |
|
| 117 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 118 |
with mock.patch(target, return_value=[]): |
| 119 |
result = linode_v4.maybe_instance_from_label(module, client) |
| 120 |
|
| 121 |
assert result is None |
| 122 |
|
| 123 |
|
| 124 |
def test_optional_region_is_validated(default_args, capfd, access_token): |
| 125 |
default_args.update({"type": "foo", "image": "bar"}) |
| 126 |
with set_module_args(default_args): |
| 127 |
with pytest.raises(SystemExit): |
| 128 |
linode_v4.initialise_module() |
| 129 |
|
| 130 |
out, err = capfd.readouterr() |
| 131 |
results = json.loads(out) |
| 132 |
|
| 133 |
assert results["failed"] is True |
| 134 |
assert all(txt in results["msg"] for txt in ("required", "together", "region")) |
| 135 |
|
| 136 |
|
| 137 |
def test_optional_type_is_validated(default_args, capfd, access_token): |
| 138 |
default_args.update({"region": "foo", "image": "bar"}) |
| 139 |
with set_module_args(default_args): |
| 140 |
with pytest.raises(SystemExit): |
| 141 |
linode_v4.initialise_module() |
| 142 |
|
| 143 |
out, err = capfd.readouterr() |
| 144 |
results = json.loads(out) |
| 145 |
|
| 146 |
assert results["failed"] is True |
| 147 |
assert all(txt in results["msg"] for txt in ("required", "together", "type")) |
| 148 |
|
| 149 |
|
| 150 |
def test_optional_image_is_validated(default_args, capfd, access_token): |
| 151 |
default_args.update({"type": "foo", "region": "bar"}) |
| 152 |
with set_module_args(default_args): |
| 153 |
with pytest.raises(SystemExit): |
| 154 |
linode_v4.initialise_module() |
| 155 |
|
| 156 |
out, err = capfd.readouterr() |
| 157 |
results = json.loads(out) |
| 158 |
|
| 159 |
assert results["failed"] is True |
| 160 |
assert all(txt in results["msg"] for txt in ("required", "together", "image")) |
| 161 |
|
| 162 |
|
| 163 |
@pytest.mark.parametrize("value", [True, False]) |
| 164 |
def test_private_ip_valid_values(default_args, access_token, value): |
| 165 |
default_args.update({"private_ip": value}) |
| 166 |
with set_module_args(default_args): |
| 167 |
module = linode_v4.initialise_module() |
| 168 |
|
| 169 |
assert module.params["private_ip"] is value |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.parametrize("value", ["not-a-bool", 42]) |
| 173 |
def test_private_ip_invalid_values(default_args, capfd, access_token, value): |
| 174 |
default_args.update({"private_ip": value}) |
| 175 |
with set_module_args(default_args): |
| 176 |
with pytest.raises(SystemExit): |
| 177 |
linode_v4.initialise_module() |
| 178 |
|
| 179 |
out, err = capfd.readouterr() |
| 180 |
results = json.loads(out) |
| 181 |
|
| 182 |
assert results["failed"] is True |
| 183 |
assert "not a valid boolean" in results["msg"] |
| 184 |
|
| 185 |
|
| 186 |
def test_private_ip_default_value(default_args, access_token): |
| 187 |
default_args.pop("private_ip", None) |
| 188 |
with set_module_args(default_args): |
| 189 |
module = linode_v4.initialise_module() |
| 190 |
|
| 191 |
assert module.params["private_ip"] is False |
| 192 |
|
| 193 |
|
| 194 |
def test_private_ip_is_forwarded_to_linode(default_args, mock_linode, access_token): |
| 195 |
default_args.update({"private_ip": True}) |
| 196 |
with set_module_args(default_args): |
| 197 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 198 |
with mock.patch(target, return_value=[]): |
| 199 |
with pytest.raises(SystemExit): |
| 200 |
target = "linode_api4.linode_client.LinodeGroup.instance_create" |
| 201 |
with mock.patch(target, return_value=(mock_linode, "passw0rd")) as instance_create_mock: |
| 202 |
linode_v4.main() |
| 203 |
|
| 204 |
args, kwargs = instance_create_mock.call_args |
| 205 |
assert kwargs["private_ip"] is True |
| 206 |
|
| 207 |
|
| 208 |
def test_instance_already_created(default_args, mock_linode, capfd, access_token): |
| 209 |
default_args.update({"type": "foo", "region": "bar", "image": "baz"}) |
| 210 |
with set_module_args(default_args): |
| 211 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 212 |
with mock.patch(target, return_value=[mock_linode]): |
| 213 |
with pytest.raises(SystemExit) as sys_exit_exc: |
| 214 |
linode_v4.main() |
| 215 |
|
| 216 |
assert sys_exit_exc.value.code == 0 |
| 217 |
|
| 218 |
out, err = capfd.readouterr() |
| 219 |
results = json.loads(out) |
| 220 |
|
| 221 |
assert results["changed"] is False |
| 222 |
assert "root_password" not in results["instance"] |
| 223 |
assert results["instance"]["label"] == mock_linode._raw_json["label"] |
| 224 |
|
| 225 |
|
| 226 |
def test_instance_to_be_created_without_root_pass(default_args, mock_linode, capfd, access_token): |
| 227 |
default_args.update({"type": "foo", "region": "bar", "image": "baz"}) |
| 228 |
with set_module_args(default_args): |
| 229 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 230 |
with mock.patch(target, return_value=[]): |
| 231 |
with pytest.raises(SystemExit) as sys_exit_exc: |
| 232 |
target = "linode_api4.linode_client.LinodeGroup.instance_create" |
| 233 |
with mock.patch(target, return_value=(mock_linode, "passw0rd")): |
| 234 |
linode_v4.main() |
| 235 |
|
| 236 |
assert sys_exit_exc.value.code == 0 |
| 237 |
|
| 238 |
out, err = capfd.readouterr() |
| 239 |
results = json.loads(out) |
| 240 |
|
| 241 |
assert results["changed"] is True |
| 242 |
assert results["instance"]["label"] == mock_linode._raw_json["label"] |
| 243 |
assert results["instance"]["root_pass"] == "passw0rd" |
| 244 |
|
| 245 |
|
| 246 |
def test_instance_to_be_created_with_root_pass(default_args, mock_linode, capfd, access_token): |
| 247 |
default_args.update( |
| 248 |
{ |
| 249 |
"type": "foo", |
| 250 |
"region": "bar", |
| 251 |
"image": "baz", |
| 252 |
"root_pass": "passw0rd", |
| 253 |
} |
| 254 |
) |
| 255 |
with set_module_args(default_args): |
| 256 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 257 |
with mock.patch(target, return_value=[]): |
| 258 |
with pytest.raises(SystemExit) as sys_exit_exc: |
| 259 |
target = "linode_api4.linode_client.LinodeGroup.instance_create" |
| 260 |
with mock.patch(target, return_value=mock_linode): |
| 261 |
linode_v4.main() |
| 262 |
|
| 263 |
assert sys_exit_exc.value.code == 0 |
| 264 |
|
| 265 |
out, err = capfd.readouterr() |
| 266 |
results = json.loads(out) |
| 267 |
|
| 268 |
assert results["changed"] is True |
| 269 |
assert results["instance"]["label"] == mock_linode._raw_json["label"] |
| 270 |
assert "root_pass" not in results["instance"] |
| 271 |
|
| 272 |
|
| 273 |
def test_instance_to_be_deleted(default_args, mock_linode, capfd, access_token): |
| 274 |
default_args.update({"state": "absent"}) |
| 275 |
with set_module_args(default_args): |
| 276 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 277 |
with mock.patch(target, return_value=[mock_linode]): |
| 278 |
with pytest.raises(SystemExit) as sys_exit_exc: |
| 279 |
linode_v4.main() |
| 280 |
|
| 281 |
assert sys_exit_exc.value.code == 0 |
| 282 |
|
| 283 |
out, err = capfd.readouterr() |
| 284 |
results = json.loads(out) |
| 285 |
|
| 286 |
assert results["changed"] is True |
| 287 |
assert results["instance"]["label"] == mock_linode._raw_json["label"] |
| 288 |
|
| 289 |
|
| 290 |
def test_instance_already_deleted_no_change(default_args, mock_linode, capfd, access_token): |
| 291 |
default_args.update({"state": "absent"}) |
| 292 |
with set_module_args(default_args): |
| 293 |
target = "linode_api4.linode_client.LinodeGroup.instances" |
| 294 |
with mock.patch(target, return_value=[]): |
| 295 |
with pytest.raises(SystemExit) as sys_exit_exc: |
| 296 |
linode_v4.main() |
| 297 |
|
| 298 |
assert sys_exit_exc.value.code == 0 |
| 299 |
|
| 300 |
out, err = capfd.readouterr() |
| 301 |
results = json.loads(out) |
| 302 |
|
| 303 |
assert results["changed"] is False |
| 304 |
assert results["instance"] == {} |
| 305 |
|
| 306 |
|
| 307 |
def test_user_agent_created_properly(): |
| 308 |
try: |
| 309 |
from ansible.module_utils.ansible_release import __version__ as ansible_version |
| 310 |
except ImportError: |
| 311 |
ansible_version = "unknown" |
| 312 |
|
| 313 |
expected_user_agent = f"Ansible-linode_v4_module/{ansible_version}" |
| 314 |
assert expected_user_agent == get_user_agent("linode_v4_module") |