validateTestPrerequisites ensures the certificates are available in the designated path and Caddy sub-process is running.
(tc *Tester)
| 265 | // validateTestPrerequisites ensures the certificates are available in the |
| 266 | // designated path and Caddy sub-process is running. |
| 267 | func validateTestPrerequisites(tc *Tester) error { |
| 268 | // check certificates are found |
| 269 | for _, certName := range tc.config.Certificates { |
| 270 | if _, err := os.Stat(getIntegrationDir() + certName); errors.Is(err, fs.ErrNotExist) { |
| 271 | return fmt.Errorf("caddy integration test certificates (%s) not found", certName) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if isCaddyAdminRunning(tc) != nil { |
| 276 | // setup the init config file, and set the cleanup afterwards |
| 277 | f, err := os.CreateTemp("", "") |
| 278 | if err != nil { |
| 279 | return err |
| 280 | } |
| 281 | tc.t.Cleanup(func() { |
| 282 | os.Remove(f.Name()) //nolint:gosec // false positive, filename comes from std lib, no path traversal |
| 283 | }) |
| 284 | if _, err := fmt.Fprintf(f, initConfig, tc.config.AdminPort); err != nil { |
| 285 | return err |
| 286 | } |
| 287 | |
| 288 | // start inprocess caddy server |
| 289 | os.Args = []string{"caddy", "run", "--config", f.Name(), "--adapter", "caddyfile"} |
| 290 | go func() { |
| 291 | caddycmd.Main() |
| 292 | }() |
| 293 | |
| 294 | // wait for caddy to start serving the initial config |
| 295 | for retries := 10; retries > 0 && isCaddyAdminRunning(tc) != nil; retries-- { |
| 296 | time.Sleep(1 * time.Second) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // one more time to return the error |
| 301 | return isCaddyAdminRunning(tc) |
| 302 | } |
| 303 | |
| 304 | func isCaddyAdminRunning(tc *Tester) error { |
| 305 | // assert that caddy is running |
no test coverage detected