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