Skip to content

Commit

Permalink
operator -> fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
asalan316 committed Mar 13, 2023
1 parent 3a70a0b commit 478b82a
Showing 1 changed file with 11 additions and 47 deletions.
58 changes: 11 additions & 47 deletions src/autoscaler/operator/cmd/operator/operator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main_test

import (
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -340,13 +341,14 @@ var _ = Describe("Operator", Serial, func() {
})
})

Describe("when Health server is ready to serve RESTful API", func() {
Describe("when Health server is ready to serve RESTful API without basic Auth", func() {
BeforeEach(func() {
basicAuthConfig := cfg
basicAuthConfig.Health.HealthCheckUsername = ""
basicAuthConfig.Health.HealthCheckPassword = ""
basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", routes.LivenessPath,
routes.ReadinessPath, routes.PrometheusPath, routes.PprofPath}
runner.configPath = writeConfig(&basicAuthConfig).Name()

runner.Start()
Eventually(runner.Session.Buffer, 2*time.Second).Should(Say("operator.started"))
})
Expand All @@ -355,9 +357,10 @@ var _ = Describe("Operator", Serial, func() {
runner.ClearLockDatabase()
})

Context("when a request to query health comes", func() {
Context("when a request to query prometheus comes", func() {
It("returns with a 200", func() {
rsp, err := healthHttpClient.Get(fmt.Sprintf("http://127.0.0.1:%d", healthport))
prometheusUrl := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, routes.PrometheusPath)
rsp, err := healthHttpClient.Get(prometheusUrl)
Expect(err).NotTo(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
raw, _ := io.ReadAll(rsp.Body)
Expand Down Expand Up @@ -387,48 +390,8 @@ var _ = Describe("Operator", Serial, func() {

Context("when username and password are incorrect for basic authentication during health check", func() {
It("should return 401", func() {

req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/health", healthport), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth("wrongusername", "wrongpassword")

rsp, err := healthHttpClient.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusUnauthorized))
})
})

Context("when username and password are correct for basic authentication during health check", func() {
It("should return 200", func() {

req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/health", healthport), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(cfg.Health.HealthCheckUsername, cfg.Health.HealthCheckPassword)

rsp, err := healthHttpClient.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
})
})
})

Describe("when Health server is ready to serve RESTful API with basic Auth", func() {
BeforeEach(func() {
runner.Start()

Eventually(runner.Session.Buffer, 2*time.Second).Should(Say("operator.started"))
})

AfterEach(func() {
runner.ClearLockDatabase()
})

Context("when username and password are incorrect for basic authentication during health check", func() {
It("should return 401", func() {

req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/health", healthport), nil)
livenessUrl := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, routes.LivenessPath)
req, err := http.NewRequest(http.MethodGet, livenessUrl, nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth("wrongusername", "wrongpassword")
Expand All @@ -441,8 +404,9 @@ var _ = Describe("Operator", Serial, func() {

Context("when username and password are correct for basic authentication during health check", func() {
It("should return 200", func() {
livenessUrl := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, routes.LivenessPath)

req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/health", healthport), nil)
req, err := http.NewRequest(http.MethodGet, livenessUrl, nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(cfg.Health.HealthCheckUsername, cfg.Health.HealthCheckPassword)
Expand Down

0 comments on commit 478b82a

Please sign in to comment.