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_mixed_usage ():
100+ """Test mixed usage with some optional parameters"""
101+ print ("Testing on_exit_notify with mixed usage (queue but no payload)..." )
102+
103+ def test_function ():
104+ return "test"
105+
106+ task = tasks .PythonTask ('test-task' , test_function )
107+
108+ # Mixed usage - queue but no payload
109+ on_exit_notify = {
110+ 'image' : 'my-custom-image' ,
111+ 'queue' : 'my_queue'
112+ # No payload specified
113+ }
114+
115+ op = operations .PipelineOperation ('test-mixed-exit' , [task ], on_exit_notify = on_exit_notify )
116+ wf = op .to_workflow ()
117+
118+ # Find the exit-handler template
119+ exit_template = None
120+ for template in wf ['spec' ]['templates' ]:
121+ if template ['name' ] == 'exit-handler' :
122+ exit_template = template
123+ break
124+
125+ assert exit_template is not None , "exit-handler template should exist"
126+
127+ # Check environment variables
128+ env_vars = {env ['name' ]: env ['value' ] for env in exit_template ['container' ]['env' ]}
129+ assert 'workflow_result' in env_vars , "workflow_result should be present"
130+ assert 'queue_name' in env_vars , "queue_name should be present"
131+ assert 'payload' not in env_vars , "payload should not be present when not specified"
132+
133+ assert env_vars ['queue_name' ] == 'my_queue' , f"queue_name should be 'my_queue', got { env_vars ['queue_name' ]} "
134+
135+ print ("✓ Test passed: Mixed usage works correctly" )
136+ return True
137+
138+ def test_on_exit_notify_default_image ():
139+ """Test that default image still works when no custom image is specified"""
140+ print ("Testing on_exit_notify with default image..." )
141+
142+ def test_function ():
143+ return "test"
144+
145+ task = tasks .PythonTask ('test-task' , test_function )
146+
147+ # Traditional usage without custom image
148+ on_exit_notify = {
149+ 'queue' : 'my_queue' ,
150+ 'payload' : '{"test": true}'
151+ # No image specified - should use default
152+ }
153+
154+ op = operations .PipelineOperation ('test-default-exit' , [task ], on_exit_notify = on_exit_notify )
155+ wf = op .to_workflow ()
156+
157+ # Find the exit-handler template
158+ exit_template = None
159+ for template in wf ['spec' ]['templates' ]:
160+ if template ['name' ] == 'exit-handler' :
161+ exit_template = template
162+ break
163+
164+ assert exit_template is not None , "exit-handler template should exist"
165+
166+ # Should use default image
167+ assert 'workflows-notify-queue' in exit_template ['container' ]['image' ], f"Should use default image, got { exit_template ['container' ]['image' ]} "
168+
169+ print ("✓ Test passed: Default image works correctly" )
170+ return True
0 commit comments