Skip to content

Commit

Permalink
operator -> fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
asalan316 authored and joergdw committed Mar 14, 2023
1 parent 3a70a0b commit 44d0f76
Showing 1 changed file with 14 additions and 50 deletions.
64 changes: 14 additions & 50 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 @@ -134,6 +135,8 @@ var _ = Describe("Operator", Serial, func() {
secondRunner.startCheck = ""
cfg.Health.HealthCheckUsername = ""
cfg.Health.HealthCheckPassword = ""
cfg.Health.UnprotectedEndpoints = []string{"/", routes.LivenessPath,
routes.ReadinessPath, routes.PrometheusPath, routes.PprofPath}
cfg.Health.Port = 9000 + GinkgoParallelProcess()
secondRunner.configPath = writeConfig(&cfg).Name()
secondRunner.Start()
Expand All @@ -149,15 +152,13 @@ var _ = Describe("Operator", Serial, func() {
Consistently(secondRunner.Session.Buffer, 5*time.Second).ShouldNot(Say("operator.successfully-acquired-lock"))

By("checking the health endpoint of the standing-by instance")
rsp, err := healthHttpClient.Get(fmt.Sprintf("http://127.0.0.1:%d/health", cfg.Health.Port))
rsp, err := healthHttpClient.Get(fmt.Sprintf("http://127.0.0.1:%d%s", cfg.Health.Port, routes.LivenessPath))
Expect(err).NotTo(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))

})
})

Context("When more than one instances of operator try to get the lock simultaneously", func() {

var runnerAcquiredLock bool

BeforeEach(func() {
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 44d0f76

Please sign in to comment.