11package main
22
33import (
4- "bytes"
54 "context"
65 "database/sql"
76 _ "embed"
@@ -14,6 +13,7 @@ import (
1413 "os/exec"
1514 "path/filepath"
1615 "runtime"
16+ "syscall"
1717 "testing"
1818 "time"
1919
@@ -96,17 +96,28 @@ func TestCLIWorkflow(t *testing.T) {
9696 testProjectInitialization (t , cliPath )
9797 })
9898
99- t .Run ("ApplicationLifecycle" , func (t * testing.T ) {
100- cmd := testApplicationLifecycle (t , cliPath )
101- t .Cleanup (func () {
102- if cmd .Process != nil {
103- /*
104- fmt.Println(cmd.Stderr)
105- fmt.Println(cmd.Stdout)
106- */
107- cmd .Process .Kill ()
108- }
109- })
99+ // Start a test application using dbos start
100+ cmd := exec .CommandContext (context .Background (), cliPath , "start" )
101+ cmd .Env = append (os .Environ (), "DBOS_SYSTEM_DATABASE_URL=" + getDatabaseURL ())
102+ err = cmd .Start ()
103+ require .NoError (t , err , "Failed to start application" )
104+ // Wait for server to be ready
105+ require .Eventually (t , func () bool {
106+ resp , err := http .Get ("http://localhost:" + testServerPort )
107+ if err != nil {
108+ return false
109+ }
110+ resp .Body .Close ()
111+ return resp .StatusCode == http .StatusOK
112+ }, 10 * time .Second , 500 * time .Millisecond , "Server should start within 10 seconds" )
113+
114+ t .Cleanup (func () {
115+ fmt .Printf ("Cleaning up application process %d\n " , cmd .Process .Pid )
116+ // fmt.Println(cmd.Stderr)
117+ // fmt.Println(cmd.Stdout)
118+ err := syscall .Kill (cmd .Process .Pid , syscall .SIGTERM )
119+ require .NoError (t , err , "Failed to send interrupt signal to application process" )
120+ _ = cmd .Wait ()
110121 })
111122
112123 t .Run ("WorkflowCommands" , func (t * testing.T ) {
@@ -120,7 +131,6 @@ func TestCLIWorkflow(t *testing.T) {
120131
121132// testProjectInitialization verifies project initialization
122133func testProjectInitialization (t * testing.T , cliPath string ) {
123-
124134 // Initialize project
125135 cmd := exec .Command (cliPath , "init" , testProjectName )
126136 output , err := cmd .CombinedOutput ()
@@ -164,71 +174,6 @@ func testProjectInitialization(t *testing.T, cliPath string) {
164174 require .NoError (t , err , "go mod tidy failed: %s" , string (modOutput ))
165175}
166176
167- // testApplicationLifecycle starts the application and triggers workflows
168- func testApplicationLifecycle (t * testing.T , cliPath string ) * exec.Cmd {
169- // Should already be in project directory from previous test
170-
171- // Start the application in background
172- ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
173- defer cancel ()
174-
175- cmd := exec .CommandContext (ctx , cliPath , "start" )
176- cmd .Env = append (os .Environ (), "DBOS_SYSTEM_DATABASE_URL=" + getDatabaseURL ())
177-
178- // Capture output for debugging
179- var stdout , stderr bytes.Buffer
180- cmd .Stdout = & stdout
181- cmd .Stderr = & stderr
182-
183- err := cmd .Start ()
184- require .NoError (t , err , "Failed to start application" )
185-
186- // Wait for server to be ready
187- require .Eventually (t , func () bool {
188- resp , err := http .Get ("http://localhost:" + testServerPort )
189- if err != nil {
190- return false
191- }
192- resp .Body .Close ()
193- return resp .StatusCode == http .StatusOK
194- }, 10 * time .Second , 500 * time .Millisecond , "Server should start within 10 seconds" )
195-
196- // Trigger workflows via HTTP endpoints
197- t .Run ("TriggerExampleWorkflow" , func (t * testing.T ) {
198- resp , err := http .Get ("http://localhost:" + testServerPort + "/workflow" )
199- require .NoError (t , err , "Failed to trigger workflow" )
200- defer resp .Body .Close ()
201-
202- body , err := io .ReadAll (resp .Body )
203- require .NoError (t , err )
204-
205- assert .Equal (t , http .StatusOK , resp .StatusCode , "Workflow endpoint should return 200" )
206- assert .Contains (t , string (body ), "Workflow result" , "Should contain workflow result" )
207- })
208-
209- t .Run ("TriggerQueueWorkflow" , func (t * testing.T ) {
210- resp , err := http .Get ("http://localhost:" + testServerPort + "/queue" )
211- require .NoError (t , err , "Failed to trigger queue workflow" )
212- defer resp .Body .Close ()
213-
214- body , err := io .ReadAll (resp .Body )
215- require .NoError (t , err )
216-
217- assert .Equal (t , http .StatusOK , resp .StatusCode , "Queue endpoint should return 200" )
218-
219- // Parse JSON response to get workflow ID
220- var response map [string ]string
221- err = json .Unmarshal (body , & response )
222- require .NoError (t , err , "Should be valid JSON response" )
223-
224- workflowID , exists := response ["workflow_id" ]
225- assert .True (t , exists , "Response should contain workflow_id" )
226- assert .NotEmpty (t , workflowID , "Workflow ID should not be empty" )
227- })
228-
229- return cmd
230- }
231-
232177// testWorkflowCommands comprehensively tests all workflow CLI commands
233178func testWorkflowCommands (t * testing.T , cliPath string ) {
234179
@@ -256,8 +201,29 @@ func testWorkflowCommands(t *testing.T, cliPath string) {
256201// testListWorkflows tests various workflow listing scenarios
257202func testListWorkflows (t * testing.T , cliPath string ) {
258203 // Create some test workflows first to ensure we have data to filter
259- // The previous test functions have already created workflows that we can query
204+ resp , err := http .Get ("http://localhost:" + testServerPort + "/workflow" )
205+ require .NoError (t , err , "Failed to trigger workflow" )
206+ defer resp .Body .Close ()
207+ body , err := io .ReadAll (resp .Body )
208+ require .NoError (t , err )
209+ assert .Equal (t , http .StatusOK , resp .StatusCode , "Workflow endpoint should return 200" )
210+ assert .Contains (t , string (body ), "Workflow result" , "Should contain workflow result" )
211+
212+ resp , err = http .Get ("http://localhost:" + testServerPort + "/queue" )
213+ require .NoError (t , err , "Failed to trigger queue workflow" )
214+ defer resp .Body .Close ()
215+ body , err = io .ReadAll (resp .Body )
216+ require .NoError (t , err )
217+ assert .Equal (t , http .StatusOK , resp .StatusCode , "Queue endpoint should return 200" )
218+
219+ // Parse JSON response to get workflow ID
220+ var response map [string ]string
221+ err = json .Unmarshal (body , & response )
222+ require .NoError (t , err , "Should be valid JSON response" )
260223
224+ workflowID , exists := response ["workflow_id" ]
225+ assert .True (t , exists , "Response should contain workflow_id" )
226+ assert .NotEmpty (t , workflowID , "Workflow ID should not be empty" )
261227 // Get the current time for time-based filtering
262228 currentTime := time .Now ()
263229
@@ -724,14 +690,14 @@ func buildCLI(t *testing.T) string {
724690 // Build output path in the cmd directory
725691 cliPath := filepath .Join (cmdDir , "dbos-cli-test" )
726692
727- // Check if already built
728- if _ , err := os .Stat (cliPath ); os . IsNotExist ( err ) {
729- // Build the CLI from the cmd directory
730- buildCmd := exec . Command ( "go" , "build" , "-o" , "dbos-cli-test" , "." )
731- buildCmd . Dir = cmdDir
732- buildOutput , buildErr := buildCmd . CombinedOutput ()
733- require . NoError ( t , buildErr , "Failed to build CLI: %s" , string ( buildOutput ) )
734- }
693+ // Delete any existing binary before building
694+ os .Remove (cliPath )
695+
696+ // Build the CLI from the cmd directory
697+ buildCmd := exec . Command ( "go" , "build" , "-o" , "dbos-cli-test" , "." )
698+ buildCmd . Dir = cmdDir
699+ buildOutput , buildErr := buildCmd . CombinedOutput ( )
700+ require . NoError ( t , buildErr , "Failed to build CLI: %s" , string ( buildOutput ))
735701
736702 // Return absolute path
737703 absPath , err := filepath .Abs (cliPath )
0 commit comments