|
1 | 1 | package caddy_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/dunglas/frankenphp/internal/fastabs" |
5 | 8 | "io" |
6 | 9 | "net/http" |
7 | 10 | "sync" |
@@ -213,3 +216,82 @@ func getDebugState(t *testing.T, tester *caddytest.Tester) frankenphp.FrankenPHP |
213 | 216 |
|
214 | 217 | return debugStates |
215 | 218 | } |
| 219 | + |
| 220 | +func TestAddModuleWorkerViaAdminApi(t *testing.T) { |
| 221 | + // Initialize a server with admin API enabled |
| 222 | + tester := caddytest.NewTester(t) |
| 223 | + tester.InitServer(` |
| 224 | + { |
| 225 | + skip_install_trust |
| 226 | + admin localhost:2999 |
| 227 | + http_port `+testPort+` |
| 228 | +
|
| 229 | + frankenphp |
| 230 | + } |
| 231 | +
|
| 232 | + localhost:`+testPort+` { |
| 233 | + route { |
| 234 | + root ../testdata |
| 235 | + php |
| 236 | + } |
| 237 | + } |
| 238 | + `, "caddyfile") |
| 239 | + |
| 240 | + // Get initial debug state to check number of workers |
| 241 | + initialDebugState := getDebugState(t, tester) |
| 242 | + initialWorkerCount := 0 |
| 243 | + for _, thread := range initialDebugState.ThreadDebugStates { |
| 244 | + if thread.Name != "" && thread.Name != "ready" { |
| 245 | + initialWorkerCount++ |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + // Create a Caddyfile configuration with a module worker |
| 250 | + workerConfig := ` |
| 251 | + { |
| 252 | + skip_install_trust |
| 253 | + admin localhost:2999 |
| 254 | + http_port ` + testPort + ` |
| 255 | +
|
| 256 | + frankenphp |
| 257 | + } |
| 258 | +
|
| 259 | + localhost:` + testPort + ` { |
| 260 | + route { |
| 261 | + root ../testdata |
| 262 | + php { |
| 263 | + worker ../testdata/worker-with-counter.php 1 |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + ` |
| 268 | + |
| 269 | + // Send the configuration to the admin API |
| 270 | + adminUrl := "http://localhost:2999/load" |
| 271 | + r, err := http.NewRequest("POST", adminUrl, bytes.NewBufferString(workerConfig)) |
| 272 | + assert.NoError(t, err) |
| 273 | + r.Header.Set("Content-Type", "text/caddyfile") |
| 274 | + resp := tester.AssertResponseCode(r, http.StatusOK) |
| 275 | + defer resp.Body.Close() |
| 276 | + |
| 277 | + // Get the updated debug state to check if the worker was added |
| 278 | + updatedDebugState := getDebugState(t, tester) |
| 279 | + updatedWorkerCount := 0 |
| 280 | + workerFound := false |
| 281 | + filename, _ := fastabs.FastAbs("../testdata/worker-with-counter.php") |
| 282 | + for _, thread := range updatedDebugState.ThreadDebugStates { |
| 283 | + if thread.Name != "" && thread.Name != "ready" { |
| 284 | + updatedWorkerCount++ |
| 285 | + if thread.Name == "Worker PHP Thread - "+filename { |
| 286 | + workerFound = true |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + // Assert that the worker was added |
| 292 | + assert.Greater(t, updatedWorkerCount, initialWorkerCount, "Worker count should have increased") |
| 293 | + assert.True(t, workerFound, fmt.Sprintf("Worker with name %q should be found", "Worker PHP Thread - "+filename)) |
| 294 | + |
| 295 | + // Make a request to the worker to verify it's working |
| 296 | + tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-counter.php", http.StatusOK, "requests:1") |
| 297 | +} |
0 commit comments