Skip to content

Commit

Permalink
Refactor routes
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Nov 29, 2024
1 parent 53fbafc commit 9532689
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/autoscaler/eventgenerator/generator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ func (e *Evaluator) sendTriggerAlarm(trigger *models.Trigger) error {
return nil
}

path, err := routes.ScalingEngineRoutes().Get(routes.ScaleRouteName).URLPath("appid", trigger.AppId)
r := routes.NewRouter()
scalingEngineRouter := r.CreateScalingEngineRoutes()

path, err := scalingEngineRouter.Get(routes.ScaleRouteName).URLPath("appid", trigger.AppId)
if err != nil {
return fmt.Errorf("failed to create url ScaleRouteName, %s: %w", trigger.AppId, err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/autoscaler/eventgenerator/generator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ var _ = Describe("Evaluator", func() {
httpClient = cfhttp.NewClient()
triggerChan = make(chan []*models.Trigger, 1)

path, err := routes.ScalingEngineRoutes().Get(routes.ScaleRouteName).URLPath("appid", testAppId)
r := routes.NewRouter()
path, err := r.CreateScalingEngineRoutes().Get(routes.ScaleRouteName).URLPath("appid", testAppId)
Expect(err).NotTo(HaveOccurred())
urlPath = path.Path

Expand Down
9 changes: 0 additions & 9 deletions src/autoscaler/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,10 @@ func MetricsCollectorRoutes() *mux.Router {
return autoScalerRouteInstance.GetRouter()
}

<<<<<<< HEAD
func (r *Router) CreateEventGeneratorRoutes() *mux.Router {
r.router.Path(AggregatedMetricHistoriesPath).Methods(http.MethodGet).Name(GetAggregatedMetricHistoriesRouteName)
r.router.Path(LivenessPath).Methods(http.MethodGet).Name(LivenessRouteName)
return r.router
=======
func EventGeneratorRoutes() *mux.Router {
return autoScalerRouteInstance.GetRouter()
>>>>>>> ca4050ddd (Adds xfcc cf endpoint support to scaling engine)
}

func ScalingEngineRoutes() *mux.Router {
return autoScalerRouteInstance.GetRouter()
}

func MetricsForwarderRoutes() *mux.Router {
Expand Down
39 changes: 21 additions & 18 deletions src/autoscaler/routes/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var _ = Describe("Routes", func() {
JustBeforeEach(func() {
router = autoscalerRouter.GetRouter()
})

Describe("MetricsCollectorRoutes", func() {
Context("GetMetricHistoriesRoute", func() {
Context("when provide correct route variable", func() {
Expand Down Expand Up @@ -210,7 +211,7 @@ var _ = Describe("Routes", func() {
})
})

Describe("EventGeneratorRoutes", func() {
Describe("CreateEventGeneratorRoutes", func() {
JustBeforeEach(func() {
autoscalerRouter.CreateEventGeneratorRoutes()
})
Expand Down Expand Up @@ -243,27 +244,30 @@ var _ = Describe("Routes", func() {

})

Describe("ScalingEngineRoutes", func() {
Describe("CreateScalingEngineRoutes", func() {
JustBeforeEach(func() {
autoscalerRouter.CreateScalingEngineRoutes()
})
Context("ScaleRoute", func() {
Context("when provide correct route variable", func() {
It("should return the correct path", func() {
path, err := routes.ScalingEngineRoutes().Get(routes.ScaleRouteName).URLPath("appid", testAppId)
path, err := router.Get(routes.ScaleRouteName).URLPath("appid", testAppId)
Expect(err).NotTo(HaveOccurred())
Expect(path.Path).To(Equal("/v1/apps/" + testAppId + "/scale"))
})
})

Context("when provide wrong route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.ScaleRouteName).URLPath("wrongVariable", testAppId)
_, err := router.Get(routes.ScaleRouteName).URLPath("wrongVariable", testAppId)
Expect(err).To(HaveOccurred())

})
})

Context("when provide not enough route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.ScaleRouteName).URLPath()
_, err := router.Get(routes.ScaleRouteName).URLPath()
Expect(err).To(HaveOccurred())

})
Expand All @@ -273,23 +277,23 @@ var _ = Describe("Routes", func() {
Context("GetScalingHistoriesRoute", func() {
Context("when provide correct route variable", func() {
It("should return the correct path", func() {
path, err := routes.ScalingEngineRoutes().Get(routes.GetScalingHistoriesRouteName).URLPath("guid", testAppId)
path, err := router.Get(routes.GetScalingHistoriesRouteName).URLPath("guid", testAppId)
Expect(err).NotTo(HaveOccurred())
Expect(path.Path).To(Equal("/v1/apps/" + testAppId + "/scaling_histories"))
})
})

Context("when provide wrong route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.GetScalingHistoriesRouteName).URLPath("wrongVariable", testAppId)
_, err := router.Get(routes.GetScalingHistoriesRouteName).URLPath("wrongVariable", testAppId)
Expect(err).To(HaveOccurred())

})
})

Context("when provide not enough route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.GetScalingHistoriesRouteName).URLPath()
_, err := router.Get(routes.GetScalingHistoriesRouteName).URLPath()
Expect(err).To(HaveOccurred())

})
Expand All @@ -299,23 +303,23 @@ var _ = Describe("Routes", func() {
Context("SetActiveScheduleRoute", func() {
Context("when provide correct route variable", func() {
It("should return the correct path", func() {
path, err := routes.ScalingEngineRoutes().Get(routes.SetActiveScheduleRouteName).URLPath("appid", testAppId, "scheduleid", testScheduleId)
path, err := router.Get(routes.SetActiveScheduleRouteName).URLPath("appid", testAppId, "scheduleid", testScheduleId)
Expect(err).NotTo(HaveOccurred())
Expect(path.Path).To(Equal("/v1/apps/" + testAppId + "/active_schedules/" + testScheduleId))
})
})

Context("when provide wrong route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.SetActiveScheduleRouteName).URLPath("wrongVariable", testAppId)
_, err := router.Get(routes.SetActiveScheduleRouteName).URLPath("wrongVariable", testAppId)
Expect(err).To(HaveOccurred())

})
})

Context("when provide not enough route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.SetActiveScheduleRouteName).URLPath("appid", testAppId)
_, err := router.Get(routes.SetActiveScheduleRouteName).URLPath("appid", testAppId)
Expect(err).To(HaveOccurred())

})
Expand All @@ -325,23 +329,23 @@ var _ = Describe("Routes", func() {
Context("DeleteActiveScheduleRoute", func() {
Context("when provide correct route variable", func() {
It("should return the correct path", func() {
path, err := routes.ScalingEngineRoutes().Get(routes.DeleteActiveScheduleRouteName).URLPath("appid", testAppId, "scheduleid", testScheduleId)
path, err := router.Get(routes.DeleteActiveScheduleRouteName).URLPath("appid", testAppId, "scheduleid", testScheduleId)
Expect(err).NotTo(HaveOccurred())
Expect(path.Path).To(Equal("/v1/apps/" + testAppId + "/active_schedules/" + testScheduleId))
})
})

Context("when provide wrong route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.DeleteActiveScheduleRouteName).URLPath("wrongVariable", testAppId)
_, err := router.Get(routes.DeleteActiveScheduleRouteName).URLPath("wrongVariable", testAppId)
Expect(err).To(HaveOccurred())

})
})

Context("when provide not enough route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.DeleteActiveScheduleRouteName).URLPath("appid", testAppId)
_, err := router.Get(routes.DeleteActiveScheduleRouteName).URLPath("appid", testAppId)
Expect(err).To(HaveOccurred())

})
Expand All @@ -351,25 +355,24 @@ var _ = Describe("Routes", func() {
Context("GetActiveSchedulesRoute", func() {
Context("when provide correct route variable", func() {
It("should return the correct path", func() {
path, err := routes.ScalingEngineRoutes().Get(routes.GetActiveSchedulesRouteName).URLPath("appid", testAppId)
path, err := router.Get(routes.GetActiveSchedulesRouteName).URLPath("appid", testAppId)
Expect(err).NotTo(HaveOccurred())
Expect(path.Path).To(Equal("/v1/apps/" + testAppId + "/active_schedules"))
})
})

Context("when provide wrong route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.GetActiveSchedulesRouteName).URLPath("wrongVariable", testAppId)
_, err := router.Get(routes.GetActiveSchedulesRouteName).URLPath("wrongVariable", testAppId)
Expect(err).To(HaveOccurred())

})
})

Context("when provide not enough route variable", func() {
It("should return error", func() {
_, err := routes.ScalingEngineRoutes().Get(routes.GetActiveSchedulesRouteName).URLPath()
_, err := router.Get(routes.GetActiveSchedulesRouteName).URLPath()
Expect(err).To(HaveOccurred())

})
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler/scalingengine/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = Describe("Server", func() {
err error
method string
bodyReader io.Reader
route = routes.ScalingEngineRoutes()
route = routes.NewRouter().CreateScalingEngineRoutes()

scalingEngineDB *fakes.FakeScalingEngineDB
sychronizer *fakes.FakeActiveScheduleSychronizer
Expand Down

0 comments on commit 9532689

Please sign in to comment.