-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reverseproxy: Correct alternate port for active health checks (#3693)
* reverseproxy: construct active health-check transport from scratch (Fixes #3691) * reverseproxy: do upstream health-check on the correct alternative port * reverseproxy: add integration test for health-check on alternative port * reverseproxy: put back the custom transport for health-check http client * reverseproxy: cleanup health-check integration test * reverseproxy: fix health-check of unix socket upstreams * reverseproxy: skip unix socket tests on Windows * tabs > spaces Co-authored-by: Francis Lavoie <[email protected]> * make the linter (and @francislavoie) happy Co-authored-by: Francis Lavoie <[email protected]> * One more lint fix Co-authored-by: Francis Lavoie <[email protected]> Co-authored-by: Francis Lavoie <[email protected]>
- Loading branch information
1 parent
e3324aa
commit bc453fa
Showing
5 changed files
with
132 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"os" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/caddyserver/caddy/v2/caddytest" | ||
) | ||
|
||
func TestReverseProxyHealthCheck(t *testing.T) { | ||
tester := caddytest.NewTester(t) | ||
tester.InitServer(` | ||
{ | ||
http_port 9080 | ||
https_port 9443 | ||
} | ||
http://localhost:2020 { | ||
respond "Hello, World!" | ||
} | ||
http://localhost:2021 { | ||
respond "ok" | ||
} | ||
http://localhost:9080 { | ||
reverse_proxy { | ||
to localhost:2020 | ||
health_path /health | ||
health_port 2021 | ||
health_interval 2s | ||
health_timeout 5s | ||
} | ||
} | ||
`, "caddyfile") | ||
|
||
tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!") | ||
} | ||
|
||
func TestReverseProxyHealthCheckUnixSocket(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.SkipNow() | ||
} | ||
tester := caddytest.NewTester(t) | ||
f, err := ioutil.TempFile("", "*.sock") | ||
if err != nil { | ||
t.Errorf("failed to create TempFile: %s", err) | ||
return | ||
} | ||
// a hack to get a file name within a valid path to use as socket | ||
socketName := f.Name() | ||
os.Remove(f.Name()) | ||
|
||
server := http.Server{ | ||
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
if strings.HasPrefix(req.URL.Path, "/health") { | ||
w.Write([]byte("ok")) | ||
return | ||
} | ||
w.Write([]byte("Hello, World!")) | ||
}), | ||
} | ||
|
||
unixListener, err := net.Listen("unix", socketName) | ||
if err != nil { | ||
t.Errorf("failed to listen on the socket: %s", err) | ||
return | ||
} | ||
go server.Serve(unixListener) | ||
t.Cleanup(func() { | ||
server.Close() | ||
}) | ||
runtime.Gosched() // Allow other goroutines to run | ||
|
||
tester.InitServer(fmt.Sprintf(` | ||
{ | ||
http_port 9080 | ||
https_port 9443 | ||
} | ||
http://localhost:9080 { | ||
reverse_proxy { | ||
to unix/%s | ||
health_path /health | ||
health_port 2021 | ||
health_interval 2s | ||
health_timeout 5s | ||
} | ||
} | ||
`, socketName), "caddyfile") | ||
|
||
tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters