Skip to content

Commit

Permalink
Rename health-endpoint and variables;
Browse files Browse the repository at this point in the history
  • Loading branch information
joergdw committed Mar 8, 2023
1 parent 6c251f3 commit 04bc7db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/autoscaler/api/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ var _ = Describe("Api", func() {
basicAuthConfig.Health.HealthCheckUsername = ""
basicAuthConfig.Health.HealthCheckPassword = ""
basicAuthConfig.Health.ReadinessCheckEnabled = true
basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", healthendpoint.LIVELINESS_PATH,
healthendpoint.READINESS_PATH, healthendpoint.PPROF_PATH, healthendpoint.PROMETHEUS_PATH}
basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", healthendpoint.LivenessPath,
healthendpoint.ReadinessPath, healthendpoint.PprofPath, healthendpoint.PrometheusPath}
runner.configPath = writeConfig(&basicAuthConfig).Name()
runner.Start()
})
Expand All @@ -219,7 +219,7 @@ var _ = Describe("Api", func() {
})
Context("when a request to query health comes", func() {
It("returns with a 200", func() {
url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.PROMETHEUS_PATH)
url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.PrometheusPath)
rsp, err := healthHttpClient.Get(url)
Expect(err).NotTo(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
Expand Down Expand Up @@ -252,7 +252,7 @@ var _ = Describe("Api", func() {
})
Context("when username and password are incorrect for basic authentication during health check", func() {
It("should return 401", func() {
url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LIVELINESS_PATH)
url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LivenessPath)
req, err := http.NewRequest(http.MethodGet, url, nil)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -266,7 +266,7 @@ var _ = Describe("Api", func() {

Context("when username and password are correct for basic authentication during health check", func() {
It("should return 200", func() {
url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LIVELINESS_PATH)
url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LivenessPath)
req, err := http.NewRequest(http.MethodGet, url, nil)
Expect(err).NotTo(HaveOccurred())

Expand Down
60 changes: 30 additions & 30 deletions src/autoscaler/healthendpoint/health_readiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var _ = Describe("Health Readiness", func() {
It("should require basic auth", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.PROMETHEUS_PATH).
Get(healthendpoint.PrometheusPath).
Expect(t).
Status(http.StatusUnauthorized).
End()
Expand All @@ -100,7 +100,7 @@ var _ = Describe("Health Readiness", func() {
It("should require basic auth", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.PROMETHEUS_PATH).
Get(healthendpoint.PrometheusPath).
Expect(t).
Status(http.StatusUnauthorized).
End()
Expand All @@ -113,14 +113,14 @@ var _ = Describe("Health Readiness", func() {
BeforeEach(func() {
config.HealthCheckUsername = ""
config.HealthCheckPassword = ""
config.UnprotectedEndpoints = []string{"", healthendpoint.LIVELINESS_PATH,
healthendpoint.READINESS_PATH, healthendpoint.PROMETHEUS_PATH, healthendpoint.PPROF_PATH}
config.UnprotectedEndpoints = []string{"", healthendpoint.LivenessPath,
healthendpoint.ReadinessPath, healthendpoint.PrometheusPath, healthendpoint.PprofPath}
})
When("Prometheus Health or / endpoint is called", func() {
It("should respond OK", func() {
apitest.New().Debug().
Handler(healthRoute).
Get(healthendpoint.PROMETHEUS_PATH).
Get(healthendpoint.PrometheusPath).
Expect(t).
Status(http.StatusOK).
Header("Content-Type", "text/plain; version=0.0.4; charset=utf-8").
Expand All @@ -136,22 +136,22 @@ var _ = Describe("Health Readiness", func() {
})
})
When("/health/liveness endpoint is called", func() {
It("should response OK", func(){
It("should response OK", func() {
apitest.New().Debug().
Handler(healthRoute).
Get(healthendpoint.LIVELINESS_PATH).
Expect(t).
Status(http.StatusOK).
Header("Content-Type", "application/json").
Body(`{"overall_status" : "UP", "checks" : [] }`).
End()
Handler(healthRoute).
Get(healthendpoint.LivenessPath).
Expect(t).
Status(http.StatusOK).
Header("Content-Type", "application/json").
Body(`{"overall_status" : "UP", "checks" : [] }`).
End()
})
})
When("/health/readiness endpoint is called", func() {
It("should response OK", func() {
apitest.New().Debug().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
Expect(t).
Status(http.StatusOK).
Header("Content-Type", "application/json").
Expand All @@ -164,7 +164,7 @@ var _ = Describe("Health Readiness", func() {
It("should respond with 404", func() {
apitest.New().Debug().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
Expect(t).
Status(http.StatusNotFound).
End()
Expand All @@ -177,7 +177,7 @@ var _ = Describe("Health Readiness", func() {
It("should return 401", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
Expect(t).
Status(http.StatusUnauthorized).
End()
Expand All @@ -188,7 +188,7 @@ var _ = Describe("Health Readiness", func() {
It("should have json response", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -208,7 +208,7 @@ var _ = Describe("Health Readiness", func() {
It("should have database check passing", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -222,7 +222,7 @@ var _ = Describe("Health Readiness", func() {
It("should cache health result", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -232,7 +232,7 @@ var _ = Describe("Health Readiness", func() {
timesetter = &(tmsttr)
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -242,7 +242,7 @@ var _ = Describe("Health Readiness", func() {
It("should expire the cache entry after 30 seconds", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -252,7 +252,7 @@ var _ = Describe("Health Readiness", func() {
timesetter = &(tmsttr)
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -269,7 +269,7 @@ var _ = Describe("Health Readiness", func() {
It("should respond with 404", func() {
apitest.New().Debug().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
Expect(t).
Status(http.StatusNotFound).
End()
Expand All @@ -288,7 +288,7 @@ var _ = Describe("Health Readiness", func() {
It("should have overall status down", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand Down Expand Up @@ -323,7 +323,7 @@ var _ = Describe("Health Readiness", func() {
mu.RLock()
apitest.New().
Handler(healthRoute).
Get(healthendpoint.READINESS_PATH).
Get(healthendpoint.ReadinessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -341,7 +341,7 @@ var _ = Describe("Health Readiness", func() {
It("should return 401", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.LIVELINESS_PATH).
Get(healthendpoint.LivenessPath).
Expect(t).
Status(http.StatusUnauthorized).
End()
Expand All @@ -352,7 +352,7 @@ var _ = Describe("Health Readiness", func() {
It("should have json response", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.LIVELINESS_PATH).
Get(healthendpoint.LivenessPath).
BasicAuth("test-user-name", "test-user-password").
Expect(t).
Status(http.StatusOK).
Expand All @@ -367,7 +367,7 @@ var _ = Describe("Health Readiness", func() {
It("should require basic auth", func() {
apitest.New().
Handler(healthRoute).
Get(healthendpoint.PROMETHEUS_PATH).
Get(healthendpoint.PrometheusPath).
Expect(t).
Status(http.StatusUnauthorized).
End()
Expand Down Expand Up @@ -402,8 +402,8 @@ var _ = Describe("Health Readiness", func() {
BeforeEach(func() {
config.HealthCheckUsername = ""
config.HealthCheckPassword = ""
config.UnprotectedEndpoints = []string{"", healthendpoint.LIVELINESS_PATH,
healthendpoint.READINESS_PATH, healthendpoint.PROMETHEUS_PATH, healthendpoint.PPROF_PATH}
config.UnprotectedEndpoints = []string{"", healthendpoint.LivenessPath,
healthendpoint.ReadinessPath, healthendpoint.PrometheusPath, healthendpoint.PprofPath}
})
It("should work", func() {
apitest.New().
Expand Down
30 changes: 15 additions & 15 deletions src/autoscaler/healthendpoint/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
)

const (
LIVELINESS_PATH string = "/health/liveliness"
READINESS_PATH string = "/health/readiness"
PPROF_PATH string = "/debug/pprof"
PROMETHEUS_PATH string = "/health/prometheus"
LivenessPath string = "/health/liveness"
ReadinessPath string = "/health/readiness"
PprofPath string = "/debug/pprof"
PrometheusPath string = "/health/prometheus"
)

// basic authentication credentials struct
Expand Down Expand Up @@ -114,12 +114,12 @@ func addLivelinessHandlers(conf models.HealthConfig, mainRouter *mux.Router, tim
authMiddleware *basicAuthenticationMiddleware) error {

livenessHandler := common.VarsFunc(readiness([]Checker{}, time))
livenessRouter := mainRouter.PathPrefix(LIVELINESS_PATH).Subrouter()
livenessRouter := mainRouter.PathPrefix(LivenessPath).Subrouter()

if endpointsNeedsProtection(LIVELINESS_PATH, conf) {
if endpointsNeedsProtection(LivenessPath, conf) {
if !conf.BasicAuthPossible() {
msg := "Basic authentication required for endpoint %s, but credentials not set up properly."
return fmt.Errorf(msg, LIVELINESS_PATH)
return fmt.Errorf(msg, LivenessPath)
}
livenessRouter.Use(authMiddleware.middleware)
}
Expand All @@ -137,11 +137,11 @@ func addReadinessHandler(conf models.HealthConfig, mainRouter *mux.Router,
) error {

readinessRouter := mainRouter.PathPrefix("/health").Subrouter()
if endpointsNeedsProtection(READINESS_PATH, conf) {
if endpointsNeedsProtection(ReadinessPath, conf) {
readinessRouter.Use(authMiddleware.middleware)
if !conf.BasicAuthPossible() {
msg := "Basic authentication required for endpoint %s, but credentials not set up properly."
return fmt.Errorf(msg, READINESS_PATH)
return fmt.Errorf(msg, ReadinessPath)
}
}
// unauthenticated route
Expand All @@ -157,12 +157,12 @@ func addReadinessHandler(conf models.HealthConfig, mainRouter *mux.Router,
func addPprofHandlers(conf models.HealthConfig, mainRouter *mux.Router,
authMiddleware *basicAuthenticationMiddleware) error {

pprofRouter := mainRouter.PathPrefix(PPROF_PATH).Subrouter()
pprofRouter := mainRouter.PathPrefix(PprofPath).Subrouter()

if endpointsNeedsProtection(PPROF_PATH, conf) {
if endpointsNeedsProtection(PprofPath, conf) {
if !conf.BasicAuthPossible() {
msg := "Basic authentication required for endpoint %s, but credentials not set up properly."
return fmt.Errorf(msg, PPROF_PATH)
return fmt.Errorf(msg, PprofPath)
}
pprofRouter.Use(authMiddleware.middleware)
}
Expand All @@ -185,12 +185,12 @@ func addPrometheusHandler(mainRouter *mux.Router, conf models.HealthConfig,

promHandler := promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{})

prometheusRouter := mainRouter.PathPrefix(PROMETHEUS_PATH).Subrouter()
if endpointsNeedsProtection(PROMETHEUS_PATH, conf) {
prometheusRouter := mainRouter.PathPrefix(PrometheusPath).Subrouter()
if endpointsNeedsProtection(PrometheusPath, conf) {
prometheusRouter.Use(authMiddleware.middleware)
if !conf.BasicAuthPossible() {
msg := "Basic authentication required for endpoint %s, but credentials not set up properly."
return fmt.Errorf(msg, PROMETHEUS_PATH)
return fmt.Errorf(msg, PrometheusPath)
}
}
// unauthenticated routes
Expand Down

0 comments on commit 04bc7db

Please sign in to comment.