|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/useteploy/teploy/internal/config" |
| 10 | + "github.com/useteploy/teploy/internal/deploy" |
| 11 | + "github.com/useteploy/teploy/internal/ssh" |
| 12 | +) |
| 13 | + |
| 14 | +func TestHealthCommandHonorsHTTPContract(t *testing.T) { |
| 15 | + for _, stateOnly := range []bool{false, true} { |
| 16 | + mock := ssh.NewMockExecutor("host", |
| 17 | + ssh.MockCommand{Match: "if [ ! -e", Output: `present |
| 18 | +{"schema_version":1,"app":"demo","hash":"v1","health":{"mode":"http","path":"/ready","timeout_seconds":1,"interval_seconds":1}}`}, |
| 19 | + ssh.MockCommand{Match: "curl", Output: "404"}, |
| 20 | + ssh.MockCommand{Match: "bash -c", Output: ""}, |
| 21 | + ) |
| 22 | + cfg, err := healthConfigForCommand(context.Background(), mock, &config.AppConfig{App: "demo", Health: config.AppHealthConfig{Mode: "http", Path: "/ready", TimeoutSeconds: 1, IntervalSeconds: 1}}, "v1", stateOnly) |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + if cfg.Timeout != time.Second || cfg.Interval != time.Second { |
| 27 | + t.Fatalf("lost timing contract: %+v", cfg) |
| 28 | + } |
| 29 | + cfg.Timeout, cfg.Interval = 30*time.Millisecond, time.Millisecond |
| 30 | + err = deploy.NewDeployer(mock, nil).HealthCheckAtWithConfig(context.Background(), 8080, "", cfg) |
| 31 | + if err == nil { |
| 32 | + t.Fatal("HTTP 404 must fail even when TCP succeeds") |
| 33 | + } |
| 34 | + var sawPath bool |
| 35 | + for _, call := range mock.Calls { |
| 36 | + if strings.Contains(call, "curl") && strings.Contains(call, "/ready") { |
| 37 | + sawPath = true |
| 38 | + } |
| 39 | + if strings.HasPrefix(call, "bash -c") { |
| 40 | + t.Fatal("HTTP-only health used TCP fallback") |
| 41 | + } |
| 42 | + } |
| 43 | + if !sawPath { |
| 44 | + t.Fatal("configured readiness path was not probed") |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestHealthCommandRefusesUnknownRecordedContract(t *testing.T) { |
| 50 | + for _, output := range []string{"absent", "present\n{broken", `present |
| 51 | +{"schema_version":1,"app":"demo","hash":"v1"}`} { |
| 52 | + mock := ssh.NewMockExecutor("host", ssh.MockCommand{Match: "if [ ! -e", Output: output}) |
| 53 | + _, err := healthConfigForCommand(context.Background(), mock, &config.AppConfig{App: "demo"}, "v1", true) |
| 54 | + if err == nil { |
| 55 | + t.Fatalf("unknown contract accepted: %q", output) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestHealthCommandTCPDoesNotProbeHTTP(t *testing.T) { |
| 61 | + mock := ssh.NewMockExecutor("host", ssh.MockCommand{Match: "bash -c", Output: ""}) |
| 62 | + cfg, err := healthConfigForCommand(context.Background(), mock, &config.AppConfig{Health: config.AppHealthConfig{Mode: "tcp"}}, "v1", false) |
| 63 | + if err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + } |
| 66 | + if err := deploy.NewDeployer(mock, nil).HealthCheckAtWithConfig(context.Background(), 8080, "", cfg); err != nil { |
| 67 | + t.Fatal(err) |
| 68 | + } |
| 69 | + if len(mock.Calls) == 0 { |
| 70 | + t.Fatal("no probe ran") |
| 71 | + } |
| 72 | + for _, call := range mock.Calls { |
| 73 | + if strings.Contains(call, "curl") { |
| 74 | + t.Fatal("TCP health unexpectedly probed HTTP") |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments