Skip to content

Commit 1d82eb9

Browse files
Merge pull request #123 from wfcommons/testing
Added WfChef testing
2 parents dbbef0f + 902b586 commit 1d82eb9

2 files changed

Lines changed: 136 additions & 7 deletions

File tree

tests/wfchef/test_wfchef.py

Lines changed: 135 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
from wfcommons.wfchef.chef import install_recipe
2121
from wfcommons.wfchef.chef import uninstall_recipe
2222
from wfcommons.wfchef.chef import ls_recipe
23+
from wfcommons import WorkflowGenerator
2324

2425

2526
class TestWfChef:
2627

2728
@pytest.mark.unit
28-
def test_create_recipe(self) -> None:
29+
# @pytest.mark.skip
30+
def test_recipe_management_functions(self) -> None:
2931
"""
3032
Just calling the create_recipe function from chef.py directly (i.e., bypassing main())
3133
"""
@@ -84,7 +86,7 @@ def test_create_recipe(self) -> None:
8486

8587
try:
8688
install_recipe("/bogus/bogus/whatever", verbose=True)
87-
raise Exception("Should not be able to install a recipe given a bogus path")
89+
raise RuntimeError("Should not be able to install a recipe given a bogus path")
8890
except FileNotFoundError as e:
8991
pass
9092

@@ -110,6 +112,18 @@ def test_create_recipe(self) -> None:
110112
sys.stderr.write(f"✗ Failed to import recipe: {e}\n")
111113
raise
112114

115+
# Verify the recipe can be used
116+
sys.stderr.write("\n" + "=" * 60 + "\n")
117+
sys.stderr.write("Testing the recipe usage...\n")
118+
sys.stderr.write("=" * 60 + "\n")
119+
try:
120+
generator = WorkflowGenerator(SomenameRecipe.from_num_tasks(250))
121+
generator.build_workflow()
122+
except Exception as e:
123+
sys.stderr.write(f"✗ Failed to use installed recipe: {e}\n")
124+
raise
125+
126+
113127
# Uninstall the recipe
114128
sys.stderr.write("\n" + "=" * 60 + "\n")
115129
sys.stderr.write("Uninstalling the recipe...\n")
@@ -123,15 +137,130 @@ def test_create_recipe(self) -> None:
123137
sys.stderr.write("=" * 60 + "\n")
124138
ls_recipe()
125139

140+
# Verify the recipe can be used
126141
sys.stderr.write("\n" + "=" * 60 + "\n")
127-
sys.stderr.write("TEST COMPLETED SUCCESSFULLY\n")
142+
sys.stderr.write("Testing the recipe usage after an uninstall...\n")
128143
sys.stderr.write("=" * 60 + "\n")
144+
try:
145+
generator = WorkflowGenerator(SomenameRecipe.from_num_tasks(250))
146+
generator.build_workflow()
147+
raise Exception("Should not be able to use a recipe after an uninstall")
148+
except Exception as e:
149+
pass
129150

151+
@pytest.mark.unit
152+
def test_recipe_management_wfchef_main(self, monkeypatch, capsys) -> None:
130153

131-
# TODO: Do more extensive tests
132-
# - Install/Uninstall the recipe
133-
# - Use the recipe
154+
# Importing main for easier testing/coverage (compared to subprocessing the executable)
155+
from wfcommons.wfchef.chef import main
134156

157+
dirpath = _create_fresh_local_dir("/tmp/recipe/")
135158

159+
# Put a few JSON workflows in /tmp
160+
urls = [
161+
"https://raw.githubusercontent.com/wfcommons/WfInstances/refs/heads/main/makeflow/blast/blast-chameleon-small-001.json",
162+
"https://raw.githubusercontent.com/wfcommons/WfInstances/refs/heads/main/makeflow/blast/blast-chameleon-small-002.json",
163+
"https://raw.githubusercontent.com/wfcommons/WfInstances/refs/heads/main/makeflow/blast/blast-chameleon-small-003.json",
164+
]
165+
for url in urls:
166+
response = requests.get(url)
167+
local_file_name = url.split("/")[-1]
168+
with open(dirpath / local_file_name, 'wb') as f:
169+
f.write(response.content)
170+
171+
# Calling main with --help
172+
with capsys.disabled():
173+
sys.stderr.write("\n" + "=" * 60 + "\n")
174+
sys.stderr.write("Calling wfchef script with '--help'...\n")
175+
sys.stderr.write("=" * 60 + "\n")
176+
monkeypatch.setattr(sys, 'argv', ["wfchef", "--help"])
177+
try:
178+
main()
179+
except SystemExit as e:
180+
assert e.code == 0
181+
captured = capsys.readouterr()
182+
if "usage" not in captured.out.lower():
183+
raise RuntimeError("wfchef script does not print usage information with -h flag")
184+
with capsys.disabled():
185+
sys.stderr.write("✓ Script called successfully\n")
186+
187+
# Calling main with 'ls' command
188+
with capsys.disabled():
189+
sys.stderr.write("\n" + "=" * 60 + "\n")
190+
sys.stderr.write("Calling wfchef script with 'ls' command...\n")
191+
sys.stderr.write("=" * 60 + "\n")
192+
monkeypatch.setattr(sys, 'argv', ["wfchef", "ls"])
193+
try:
194+
main()
195+
except SystemExit as e:
196+
assert e.code == 0
197+
captured = capsys.readouterr()
198+
if "_recipe" not in captured.out.lower():
199+
raise RuntimeError("wfchef script does not list recipes with 'ls' command")
200+
num_recipes = sum(["_recipe" in x for x in captured.out.splitlines()])
201+
with capsys.disabled():
202+
sys.stderr.write(f"✓ Script called successfully ({num_recipes} recipes listed)\n")
203+
204+
# Calling main with 'create' command
205+
with capsys.disabled():
206+
sys.stderr.write("\n" + "=" * 60 + "\n")
207+
sys.stderr.write("Calling wfchef script with 'create' command...\n")
208+
sys.stderr.write("=" * 60 + "\n")
209+
monkeypatch.setattr(sys, 'argv', ["wfchef", "create", str(dirpath), "-o", str(dirpath), "-n", "SomeRecipe"])
210+
try:
211+
main()
212+
except SystemExit as e:
213+
assert e.code == 0
214+
capsys.readouterr() # Clear output
136215

216+
# Calling main with 'ls' command
217+
with capsys.disabled():
218+
sys.stderr.write("\n" + "=" * 60 + "\n")
219+
sys.stderr.write("Calling wfchef script with 'ls' command...\n")
220+
sys.stderr.write("=" * 60 + "\n")
221+
monkeypatch.setattr(sys, 'argv', ["wfchef", "ls"])
222+
try:
223+
main()
224+
except SystemExit as e:
225+
assert e.code == 0
226+
captured = capsys.readouterr()
227+
if "_recipe" not in captured.out.lower():
228+
raise RuntimeError("wfchef script does not list recipes with 'ls' command")
229+
new_num_recipes = sum(["_recipe" in x for x in captured.out.splitlines()])
230+
with capsys.disabled():
231+
sys.stderr.write(f"✓ Script called successfully ({new_num_recipes} recipes listed)\n")
232+
233+
assert(num_recipes + 1 == new_num_recipes)
234+
235+
# Calling main with 'uninstall' command
236+
with capsys.disabled():
237+
sys.stderr.write("\n" + "=" * 60 + "\n")
238+
sys.stderr.write("Calling wfchef script with 'uninstall' command...\n")
239+
sys.stderr.write("=" * 60 + "\n")
240+
monkeypatch.setattr(sys, 'argv', ["wfchef", "uninstall", "-n", "SomeRecipe"])
241+
try:
242+
main()
243+
except SystemExit as e:
244+
assert e.code == 0
245+
capsys.readouterr() # Clear output
246+
247+
248+
249+
# Calling main with 'ls' command
250+
with capsys.disabled():
251+
sys.stderr.write("\n" + "=" * 60 + "\n")
252+
sys.stderr.write("Calling wfchef script with 'ls' command...\n")
253+
sys.stderr.write("=" * 60 + "\n")
254+
monkeypatch.setattr(sys, 'argv', ["wfchef", "ls"])
255+
try:
256+
main()
257+
except SystemExit as e:
258+
assert e.code == 0
259+
captured = capsys.readouterr()
260+
if "_recipe" not in captured.out.lower():
261+
raise RuntimeError("wfchef script does not list recipes with 'ls' command")
262+
new_new_num_recipes = sum(["_recipe" in x for x in captured.out.splitlines()])
263+
with capsys.disabled():
264+
sys.stderr.write(f"✓ Script called successfully ({new_new_num_recipes} recipes listed)\n")
137265

266+
assert(new_new_num_recipes == num_recipes)

wfcommons/wfchef/chef.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def main():
566566
if args.action == ls_recipe:
567567
ls_recipe()
568568
elif args.action == uninstall_recipe:
569-
uninstall_recipe(args.name, pathlib.Path(args.out))
569+
uninstall_recipe(args.name)
570570
elif args.action == create_recipe:
571571
create_recipe(args.path, args.out, args.name, cutoff=args.cutoff, verbose=True)
572572

0 commit comments

Comments
 (0)