This commit is contained in:
Hans Kristian Moen 2025-02-17 09:50:35 -08:00 committed by GitHub
commit 0d526fdc25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 1 deletions

View file

@ -69,5 +69,14 @@ func (n *NGINXController) Check(_ *http.Request) error {
return fmt.Errorf("dynamic load balancer not started") return fmt.Errorf("dynamic load balancer not started")
} }
statusCode, _, err = nginx.NewGetHealthzRequest(n.cfg.ListenPorts.HTTP, "/healthz")
if err != nil {
return fmt.Errorf("checking if default http port is responding: %w", err)
}
if statusCode != 200 {
return fmt.Errorf("default nginx http port not responding")
}
return nil return nil
} }

View file

@ -44,6 +44,7 @@ func TestNginxCheck(t *testing.T) {
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
mux := http.NewServeMux() mux := http.NewServeMux()
// Status server
listener, err := tryListen("tcp", fmt.Sprintf(":%v", nginx.StatusPort)) listener, err := tryListen("tcp", fmt.Sprintf(":%v", nginx.StatusPort))
if err != nil { if err != nil {
t.Fatalf("creating tcp listener: %s", err) t.Fatalf("creating tcp listener: %s", err)
@ -64,10 +65,31 @@ func TestNginxCheck(t *testing.T) {
n := &NGINXController{ n := &NGINXController{
cfg: &Configuration{ cfg: &Configuration{
ListenPorts: &ngx_config.ListenPorts{}, ListenPorts: &ngx_config.ListenPorts{
HTTP: 80,
},
}, },
} }
// http server
httpListener, err := tryListen("tcp", fmt.Sprintf(":%v", n.cfg.ListenPorts.HTTP))
if err != nil {
t.Fatalf("creating tcp listener: %s", err)
}
defer httpListener.Close()
httpServer := &httptest.Server{
Listener: httpListener,
Config: &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
}),
},
}
defer httpServer.Close()
httpServer.Start()
t.Run("no pid or process", func(t *testing.T) { t.Run("no pid or process", func(t *testing.T) {
if err := callHealthz(true, tt.healthzPath, mux); err == nil { if err := callHealthz(true, tt.healthzPath, mux); err == nil {
t.Error("expected an error but none returned") t.Error("expected an error but none returned")

View file

@ -80,6 +80,24 @@ func NewGetStatusRequest(path string) (statusCode int, data []byte, err error) {
return res.StatusCode, data, nil return res.StatusCode, data, nil
} }
func NewGetHealthzRequest(port int, path string) (int, []byte, error) {
url := fmt.Sprintf("http://127.0.0.1:%v%v", port, path)
client := http.Client{}
res, err := client.Get(url)
if err != nil {
return 0, nil, err
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return 0, nil, err
}
return res.StatusCode, data, nil
}
// NewPostStatusRequest creates a new POST request to the internal NGINX status server // NewPostStatusRequest creates a new POST request to the internal NGINX status server
func NewPostStatusRequest(path, contentType string, data interface{}) (statusCode int, body []byte, err error) { func NewPostStatusRequest(path, contentType string, data interface{}) (statusCode int, body []byte, err error) {
url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path) url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path)