| 1 |
# Copyright (c) 2020 Justin Bronn <[email protected]> |
| 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 platform |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ansible.module_utils.basic import AnsibleModule |
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 13 |
set_module_args, |
| 14 |
) |
| 15 |
|
| 16 |
from ansible_collections.community.general.plugins.modules import solaris_zone |
| 17 |
|
| 18 |
ZONEADM = "/usr/sbin/zoneadm" |
| 19 |
|
| 20 |
|
| 21 |
def mocker_zone_set(mocker, rc=0, out="", err="", zone_exists=False, zone_status=None): |
| 22 |
""" |
| 23 |
Configure common mocker object for Solaris Zone tests |
| 24 |
""" |
| 25 |
exists = mocker.patch.object(solaris_zone.Zone, "exists") |
| 26 |
exists.return_value = zone_exists |
| 27 |
get_bin_path = mocker.patch.object(AnsibleModule, "get_bin_path") |
| 28 |
get_bin_path.return_value = ZONEADM |
| 29 |
run_command = mocker.patch.object(AnsibleModule, "run_command") |
| 30 |
run_command.return_value = (rc, out, err) |
| 31 |
platform_release = mocker.patch.object(platform, "release") |
| 32 |
platform_release.return_value = "5.11" |
| 33 |
platform_system = mocker.patch.object(platform, "system") |
| 34 |
platform_system.return_value = "SunOS" |
| 35 |
if zone_status is not None: |
| 36 |
status = mocker.patch.object(solaris_zone.Zone, "status") |
| 37 |
status.return_value = zone_status |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def mocked_zone_create(mocker): |
| 42 |
mocker_zone_set(mocker) |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture |
| 46 |
def mocked_zone_delete(mocker): |
| 47 |
mocker_zone_set(mocker, zone_exists=True, zone_status="running") |
| 48 |
|
| 49 |
|
| 50 |
def test_zone_create(mocked_zone_create, capfd): |
| 51 |
""" |
| 52 |
test zone creation |
| 53 |
""" |
| 54 |
with set_module_args( |
| 55 |
{ |
| 56 |
"name": "z1", |
| 57 |
"state": "installed", |
| 58 |
"path": "/zones/z1", |
| 59 |
"_ansible_check_mode": False, |
| 60 |
} |
| 61 |
): |
| 62 |
with pytest.raises(SystemExit): |
| 63 |
solaris_zone.main() |
| 64 |
|
| 65 |
out, err = capfd.readouterr() |
| 66 |
results = json.loads(out) |
| 67 |
assert not results.get("failed") |
| 68 |
assert results["changed"] |
| 69 |
|
| 70 |
|
| 71 |
def test_zone_delete(mocked_zone_delete, capfd): |
| 72 |
""" |
| 73 |
test zone deletion |
| 74 |
""" |
| 75 |
with set_module_args( |
| 76 |
{ |
| 77 |
"name": "z1", |
| 78 |
"state": "absent", |
| 79 |
"path": "/zones/z1", |
| 80 |
"_ansible_check_mode": False, |
| 81 |
} |
| 82 |
): |
| 83 |
with pytest.raises(SystemExit): |
| 84 |
solaris_zone.main() |
| 85 |
|
| 86 |
out, err = capfd.readouterr() |
| 87 |
results = json.loads(out) |
| 88 |
assert not results.get("failed") |
| 89 |
assert results["changed"] |
| 90 |
|
| 91 |
|
| 92 |
def test_zone_create_invalid_names(mocked_zone_create, capfd): |
| 93 |
""" |
| 94 |
test zone creation with invalid names |
| 95 |
""" |
| 96 |
# 1. Invalid character ('!'). |
| 97 |
# 2. Zone name > 64 characters. |
| 98 |
# 3. Zone name beginning with non-alphanumeric character. |
| 99 |
for invalid_name in ("foo!bar", "z" * 65, "_zone"): |
| 100 |
with set_module_args( |
| 101 |
{ |
| 102 |
"name": invalid_name, |
| 103 |
"state": "installed", |
| 104 |
"path": f"/zones/{invalid_name}", |
| 105 |
"_ansible_check_mode": False, |
| 106 |
} |
| 107 |
): |
| 108 |
with pytest.raises(SystemExit): |
| 109 |
solaris_zone.main() |
| 110 |
|
| 111 |
out, err = capfd.readouterr() |
| 112 |
results = json.loads(out) |
| 113 |
assert results.get("failed") |