Skip to content

Commit 555eb62

Browse files
jbronnfelixfontein
andauthored
solaris_zone: fix zone configuration with python3 (ansible-collections#1082)
* * Explicitly open up temporary file in text mode. * Add test for `solaris_zone` creation. * Update changelog fragment. Co-authored-by: Felix Fontein <[email protected]> * Add tests for zone deletion and invalid zone names. Co-authored-by: Felix Fontein <[email protected]>
1 parent da7f9ff commit 555eb62

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- solaris_zone - fixed issue trying to configure zone in Python 3 (https://github.com/ansible-collections/community.general/issues/1081).

plugins/modules/system/solaris_zone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def configure(self):
193193
self.module.fail_json(msg='Missing required argument: path')
194194

195195
if not self.module.check_mode:
196-
t = tempfile.NamedTemporaryFile(delete=False)
196+
t = tempfile.NamedTemporaryFile(delete=False, mode='wt')
197197

198198
if self.sparse:
199199
t.write('create %s\n' % self.create_options)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)