Skip to content

Commit f70ff01

Browse files
Merge pull request #342 from privateip/update-test-cases
updates test cases to clean up left behind files Reviewed-by: https://github.com/apps/ansible-zuul
2 parents 9af8423 + 86679ea commit f70ff01

File tree

2 files changed

+75
-60
lines changed

2 files changed

+75
-60
lines changed

test/integration/test_events.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pkg_resources
55
import json
66
import os
7+
import shutil
78

89
from ansible_runner import run, run_async
910

@@ -80,14 +81,17 @@ def test_playbook_on_stats_summary_fields(rc):
8081

8182

8283
def test_include_role_events():
83-
r = run(
84-
private_data_dir=os.path.abspath('test/integration'),
85-
playbook='use_role.yml'
86-
)
87-
role_events = [event for event in r.events if event.get('event_data', {}).get('role', '') == "benthomasson.hello_role"]
88-
assert 'runner_on_ok' in [event['event'] for event in role_events]
89-
for event in role_events:
90-
event_data = event['event_data']
91-
assert not event_data.get('warning', False) # role use should not contain warnings
92-
if event['event'] == 'runner_on_ok':
93-
assert event_data['res']['msg'] == 'Hello world!'
84+
try:
85+
r = run(
86+
private_data_dir=os.path.abspath('test/integration'),
87+
playbook='use_role.yml'
88+
)
89+
role_events = [event for event in r.events if event.get('event_data', {}).get('role', '') == "benthomasson.hello_role"]
90+
assert 'runner_on_ok' in [event['event'] for event in role_events]
91+
for event in role_events:
92+
event_data = event['event_data']
93+
assert not event_data.get('warning', False) # role use should not contain warnings
94+
if event['event'] == 'runner_on_ok':
95+
assert event_data['res']['msg'] == 'Hello world!'
96+
finally:
97+
shutil.rmtree('test/integration/artifacts')

test/integration/test_main.py

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ def ensure_directory(directory):
2323
os.makedirs(directory)
2424

2525

26-
def ensure_removed(file_path):
27-
28-
if os.path.exists(file_path):
29-
os.unlink(file_path)
26+
def ensure_removed(path):
27+
if os.path.exists(path):
28+
if os.path.isfile(path):
29+
os.unlink(path)
30+
elif os.path.isdir(path):
31+
shutil.rmtree(path)
3032

3133

3234
@contextmanager
3335
def temp_directory(files=None):
34-
3536
temp_dir = tempfile.mkdtemp()
3637
print(temp_dir)
3738
try:
@@ -77,26 +78,33 @@ def test_help():
7778

7879

7980
def test_module_run():
80-
81-
rc = main(['-m', 'ping',
82-
'--hosts', 'localhost',
83-
'run',
84-
'ping'])
85-
assert rc == 0
81+
try:
82+
rc = main(['-m', 'ping',
83+
'--hosts', 'localhost',
84+
'run',
85+
'ping'])
86+
assert os.path.exists('./ping')
87+
assert os.path.exists('./ping/artifacts')
88+
assert rc == 0
89+
finally:
90+
shutil.rmtree('./ping')
8691

8792

8893
def test_module_run_debug():
89-
90-
rc = main(['-m', 'ping',
91-
'--hosts', 'localhost',
92-
'--debug',
93-
'run',
94-
'ping'])
95-
assert rc == 0
94+
try:
95+
rc = main(['-m', 'ping',
96+
'--hosts', 'localhost',
97+
'--debug',
98+
'run',
99+
'ping'])
100+
assert os.path.exists('./ping')
101+
assert os.path.exists('./ping/artifacts')
102+
assert rc == 0
103+
finally:
104+
shutil.rmtree('./ping')
96105

97106

98107
def test_module_run_clean():
99-
100108
with temp_directory() as temp_dir:
101109
rc = main(['-m', 'ping',
102110
'--hosts', 'localhost',
@@ -106,13 +114,13 @@ def test_module_run_clean():
106114

107115

108116
def test_role_run():
109-
110117
rc = main(['-r', 'benthomasson.hello_role',
111118
'--hosts', 'localhost',
112119
'--roles-path', 'test/integration/roles',
113120
'run',
114121
"test/integration"])
115122
assert rc == 0
123+
ensure_removed("test/integration/artifacts")
116124

117125

118126
def test_role_run_abs():
@@ -126,25 +134,32 @@ def test_role_run_abs():
126134

127135

128136
def test_role_logfile():
129-
130-
rc = main(['-r', 'benthomasson.hello_role',
131-
'--hosts', 'localhost',
132-
'--roles-path', 'test/integration/project/roles',
133-
'--logfile', 'new_logfile',
134-
'run',
135-
'test/integration'])
136-
assert rc == 0
137-
138-
139-
def test_role_logfile_abs():
140-
with temp_directory() as temp_dir:
137+
try:
141138
rc = main(['-r', 'benthomasson.hello_role',
142139
'--hosts', 'localhost',
143-
'--roles-path', os.path.join(HERE, 'project/roles'),
140+
'--roles-path', 'test/integration/project/roles',
144141
'--logfile', 'new_logfile',
145142
'run',
146-
temp_dir])
147-
assert rc == 0
143+
'test/integration'])
144+
assert os.path.exists('new_logfile')
145+
assert rc == 0
146+
finally:
147+
ensure_removed("test/integration/artifacts")
148+
149+
150+
def test_role_logfile_abs():
151+
try:
152+
with temp_directory() as temp_dir:
153+
rc = main(['-r', 'benthomasson.hello_role',
154+
'--hosts', 'localhost',
155+
'--roles-path', os.path.join(HERE, 'project/roles'),
156+
'--logfile', 'new_logfile',
157+
'run',
158+
temp_dir])
159+
assert os.path.exists('new_logfile')
160+
assert rc == 0
161+
finally:
162+
ensure_removed("new_logfile")
148163

149164

150165
def test_role_bad_project_dir():
@@ -162,6 +177,7 @@ def test_role_bad_project_dir():
162177
'bad_project_dir'])
163178
finally:
164179
os.unlink('bad_project_dir')
180+
ensure_removed("new_logfile")
165181

166182

167183
def test_role_run_clean():
@@ -172,6 +188,7 @@ def test_role_run_clean():
172188
'run',
173189
"test/integration"])
174190
assert rc == 0
191+
ensure_removed("test/integration/artifacts")
175192

176193

177194
def test_role_run_cmd_line_abs():
@@ -185,19 +202,14 @@ def test_role_run_cmd_line_abs():
185202

186203

187204
def test_role_run_artifacts_dir():
188-
189-
try:
190-
tmpdir = tempfile.mkdtemp()
191-
rc = main(['-r', 'benthomasson.hello_role',
192-
'--hosts', 'localhost',
193-
'--roles-path', 'test/integration/roles',
194-
'--artifact-dir', os.path.join(tmpdir, 'otherartifacts'),
195-
'run',
196-
"test/integration"])
197-
assert os.path.exists(os.path.join(tmpdir, 'otherartifacts'))
198-
assert rc == 0
199-
finally:
200-
shutil.rmtree(tmpdir)
205+
rc = main(['-r', 'benthomasson.hello_role',
206+
'--hosts', 'localhost',
207+
'--roles-path', 'test/integration/roles',
208+
'--artifact-dir', 'otherartifacts',
209+
'run',
210+
"test/integration"])
211+
assert rc == 0
212+
ensure_removed("test/integration/artifacts")
201213

202214

203215
def test_role_run_artifacts_dir_abs():
@@ -331,4 +343,3 @@ def test_playbook_start():
331343

332344
rc = main(['stop', temp_dir])
333345
assert rc == 1
334-

0 commit comments

Comments
 (0)