Skip to content

Commit ab7b452

Browse files
committed
CH-188 test: Add new test on exit notify
1 parent 152cec1 commit ab7b452

1 file changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
"""Test on_exit_notify functionality with optional queue and payload parameters"""
2+
import sys
3+
import os
4+
import yaml
5+
6+
# Add the cloudharness-common library to the path
7+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
8+
9+
from cloudharness.utils.config import CloudharnessConfig
10+
from cloudharness.workflows import operations, tasks, utils
11+
from test_env import set_test_environment
12+
13+
# Set up test environment
14+
set_test_environment()
15+
16+
def test_on_exit_notify_custom_image_only():
17+
"""Test that on_exit_notify works with just a custom image, no queue/payload"""
18+
print("Testing on_exit_notify with custom image only...")
19+
20+
def test_function():
21+
return "test"
22+
23+
task = tasks.PythonTask('test-task', test_function)
24+
25+
# Test with custom image only (no queue/payload)
26+
on_exit_notify = {
27+
'image': 'my-custom-exit-handler'
28+
}
29+
30+
op = operations.PipelineOperation('test-custom-exit', [task], on_exit_notify=on_exit_notify)
31+
wf = op.to_workflow()
32+
33+
# Verify the exit handler was added
34+
assert 'onExit' in wf['spec'], "onExit should be present in spec"
35+
assert wf['spec']['onExit'] == 'exit-handler', "onExit should reference exit-handler template"
36+
37+
# Find the exit-handler template
38+
exit_template = None
39+
for template in wf['spec']['templates']:
40+
if template['name'] == 'exit-handler':
41+
exit_template = template
42+
break
43+
44+
assert exit_template is not None, "exit-handler template should exist"
45+
assert 'my-custom-exit-handler' in exit_template['container']['image'], f"Image should contain my-custom-exit-handler, got {exit_template['container']['image']}"
46+
47+
# Check environment variables - should only have workflow_result
48+
env_vars = {env['name']: env['value'] for env in exit_template['container']['env']}
49+
assert 'workflow_result' in env_vars, "workflow_result should be present"
50+
assert env_vars['workflow_result'] == '{{workflow.status}}', "workflow_result should have correct value"
51+
52+
# queue_name and payload should NOT be present since they weren't specified
53+
assert 'queue_name' not in env_vars, "queue_name should not be present when not specified"
54+
assert 'payload' not in env_vars, "payload should not be present when not specified"
55+
56+
print("✓ Test passed: Custom image without queue/payload works correctly")
57+
return True
58+
59+
def test_on_exit_notify_with_queue_and_payload():
60+
"""Test that the traditional usage still works (backward compatibility)"""
61+
print("Testing on_exit_notify with queue and payload (backward compatibility)...")
62+
63+
def test_function():
64+
return "test"
65+
66+
task = tasks.PythonTask('test-task', test_function)
67+
68+
# Traditional usage with queue and payload
69+
on_exit_notify = {
70+
'queue': 'my_queue',
71+
'payload': '{"test": true}',
72+
'image': 'custom-image'
73+
}
74+
75+
op = operations.PipelineOperation('test-traditional-exit', [task], on_exit_notify=on_exit_notify)
76+
wf = op.to_workflow()
77+
78+
# Find the exit-handler template
79+
exit_template = None
80+
for template in wf['spec']['templates']:
81+
if template['name'] == 'exit-handler':
82+
exit_template = template
83+
break
84+
85+
assert exit_template is not None, "exit-handler template should exist"
86+
87+
# Check environment variables - should have all three
88+
env_vars = {env['name']: env['value'] for env in exit_template['container']['env']}
89+
assert 'workflow_result' in env_vars, "workflow_result should be present"
90+
assert 'queue_name' in env_vars, "queue_name should be present"
91+
assert 'payload' in env_vars, "payload should be present"
92+
93+
assert env_vars['queue_name'] == 'my_queue', f"queue_name should be 'my_queue', got {env_vars['queue_name']}"
94+
assert env_vars['payload'] == '{"test": true}', f"payload should be correct, got {env_vars['payload']}"
95+
96+
print("✓ Test passed: Backward compatibility works correctly")
97+
return True
98+
99+
def test_on_exit_notify_with_additional_env():
100+
"""Test that additional environment variables can be passed"""
101+
print("Testing on_exit_notify with additional environment variables...")
102+
103+
def test_function():
104+
return "test"
105+
106+
task = tasks.PythonTask('test-task', test_function)
107+
108+
# Custom usage with additional env vars
109+
on_exit_notify = {
110+
'image': 'my-custom-image',
111+
'env': {
112+
'CUSTOM_VAR': 'custom_value',
113+
'ANOTHER_VAR': 'another_value'
114+
}
115+
}
116+
117+
op = operations.PipelineOperation('test-env-exit', [task], on_exit_notify=on_exit_notify)
118+
wf = op.to_workflow()
119+
120+
# Find the exit-handler template
121+
exit_template = None
122+
for template in wf['spec']['templates']:
123+
if template['name'] == 'exit-handler':
124+
exit_template = template
125+
break
126+
127+
assert exit_template is not None, "exit-handler template should exist"
128+
129+
# Check environment variables
130+
env_vars = {env['name']: env['value'] for env in exit_template['container']['env']}
131+
assert 'workflow_result' in env_vars, "workflow_result should be present"
132+
assert 'CUSTOM_VAR' in env_vars, "CUSTOM_VAR should be present"
133+
assert 'ANOTHER_VAR' in env_vars, "ANOTHER_VAR should be present"
134+
135+
assert env_vars['CUSTOM_VAR'] == 'custom_value', f"CUSTOM_VAR should be 'custom_value', got {env_vars['CUSTOM_VAR']}"
136+
assert env_vars['ANOTHER_VAR'] == 'another_value', f"ANOTHER_VAR should be 'another_value', got {env_vars['ANOTHER_VAR']}"
137+
138+
print("✓ Test passed: Additional environment variables work correctly")
139+
return True
140+
141+
def test_on_exit_notify_mixed_usage():
142+
"""Test mixed usage with some optional parameters"""
143+
print("Testing on_exit_notify with mixed usage (queue but no payload)...")
144+
145+
def test_function():
146+
return "test"
147+
148+
task = tasks.PythonTask('test-task', test_function)
149+
150+
# Mixed usage - queue but no payload
151+
on_exit_notify = {
152+
'image': 'my-custom-image',
153+
'queue': 'my_queue'
154+
# No payload specified
155+
}
156+
157+
op = operations.PipelineOperation('test-mixed-exit', [task], on_exit_notify=on_exit_notify)
158+
wf = op.to_workflow()
159+
160+
# Find the exit-handler template
161+
exit_template = None
162+
for template in wf['spec']['templates']:
163+
if template['name'] == 'exit-handler':
164+
exit_template = template
165+
break
166+
167+
assert exit_template is not None, "exit-handler template should exist"
168+
169+
# Check environment variables
170+
env_vars = {env['name']: env['value'] for env in exit_template['container']['env']}
171+
assert 'workflow_result' in env_vars, "workflow_result should be present"
172+
assert 'queue_name' in env_vars, "queue_name should be present"
173+
assert 'payload' not in env_vars, "payload should not be present when not specified"
174+
175+
assert env_vars['queue_name'] == 'my_queue', f"queue_name should be 'my_queue', got {env_vars['queue_name']}"
176+
177+
print("✓ Test passed: Mixed usage works correctly")
178+
return True
179+
180+
def test_on_exit_notify_default_image():
181+
"""Test that default image still works when no custom image is specified"""
182+
print("Testing on_exit_notify with default image...")
183+
184+
def test_function():
185+
return "test"
186+
187+
task = tasks.PythonTask('test-task', test_function)
188+
189+
# Traditional usage without custom image
190+
on_exit_notify = {
191+
'queue': 'my_queue',
192+
'payload': '{"test": true}'
193+
# No image specified - should use default
194+
}
195+
196+
op = operations.PipelineOperation('test-default-exit', [task], on_exit_notify=on_exit_notify)
197+
wf = op.to_workflow()
198+
199+
# Find the exit-handler template
200+
exit_template = None
201+
for template in wf['spec']['templates']:
202+
if template['name'] == 'exit-handler':
203+
exit_template = template
204+
break
205+
206+
assert exit_template is not None, "exit-handler template should exist"
207+
208+
# Should use default image
209+
assert 'workflows-notify-queue' in exit_template['container']['image'], f"Should use default image, got {exit_template['container']['image']}"
210+
211+
print("✓ Test passed: Default image works correctly")
212+
return True

0 commit comments

Comments
 (0)