|
| 1 | +package dbos |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAdminServer(t *testing.T) { |
| 11 | + // Skip if database is not available |
| 12 | + databaseURL := os.Getenv("DBOS_DATABASE_URL") |
| 13 | + if databaseURL == "" && os.Getenv("PGPASSWORD") == "" { |
| 14 | + t.Skip("Database not available (DBOS_DATABASE_URL and PGPASSWORD not set), skipping DBOS integration tests") |
| 15 | + } |
| 16 | + |
| 17 | + t.Run("Admin server starts through DBOS Launch with WithAdminServer", func(t *testing.T) { |
| 18 | + // Ensure clean state |
| 19 | + if dbos != nil { |
| 20 | + Shutdown() |
| 21 | + } |
| 22 | + |
| 23 | + // Launch DBOS with admin server |
| 24 | + err := Launch(WithAdminServer()) |
| 25 | + if err != nil { |
| 26 | + t.Skipf("Failed to launch DBOS with admin server (database likely not available): %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + // Ensure cleanup |
| 30 | + defer Shutdown() |
| 31 | + |
| 32 | + // Give the server a moment to start |
| 33 | + time.Sleep(100 * time.Millisecond) |
| 34 | + |
| 35 | + // Verify admin server is running by making a request |
| 36 | + client := &http.Client{Timeout: 5 * time.Second} |
| 37 | + resp, err := client.Get("http://localhost:3001/dbos-healthz") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Failed to make request to admin server: %v", err) |
| 40 | + } |
| 41 | + defer resp.Body.Close() |
| 42 | + |
| 43 | + if resp.StatusCode != http.StatusOK { |
| 44 | + t.Errorf("Expected status code 200, got %d", resp.StatusCode) |
| 45 | + } |
| 46 | + |
| 47 | + // Verify the DBOS executor has an admin server instance |
| 48 | + if dbos == nil { |
| 49 | + t.Fatal("Expected DBOS instance to be created") |
| 50 | + } |
| 51 | + |
| 52 | + if dbos.adminServer == nil { |
| 53 | + t.Fatal("Expected admin server to be created in DBOS instance") |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("Admin server is not started without WithAdminServer option", func(t *testing.T) { |
| 58 | + // Ensure clean state |
| 59 | + if dbos != nil { |
| 60 | + Shutdown() |
| 61 | + } |
| 62 | + |
| 63 | + // Launch DBOS without admin server option |
| 64 | + err := Launch() |
| 65 | + if err != nil { |
| 66 | + t.Skipf("Failed to launch DBOS (database likely not available): %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Ensure cleanup |
| 70 | + defer Shutdown() |
| 71 | + |
| 72 | + // Give time for any startup processes |
| 73 | + time.Sleep(100 * time.Millisecond) |
| 74 | + |
| 75 | + // Verify admin server is not running |
| 76 | + client := &http.Client{Timeout: 1 * time.Second} |
| 77 | + _, err = client.Get("http://localhost:3001/dbos-healthz") |
| 78 | + if err == nil { |
| 79 | + t.Error("Expected request to fail when admin server is not started, but it succeeded") |
| 80 | + } |
| 81 | + |
| 82 | + // Verify the DBOS executor doesn't have an admin server instance |
| 83 | + if dbos == nil { |
| 84 | + t.Fatal("Expected DBOS instance to be created") |
| 85 | + } |
| 86 | + |
| 87 | + if dbos.adminServer != nil { |
| 88 | + t.Error("Expected admin server to be nil when not configured") |
| 89 | + } |
| 90 | + }) |
| 91 | +} |
0 commit comments