Skip to content

Commit 83ce51d

Browse files
authored
[v8] Add examples for hash-based routing (#3746)
* Add examples for hash-based routing * Implement tests * Enable hash_based_routing for test
1 parent 6a82890 commit 83ce51d

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

command/v7/map_route_command.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package v7
22

33
import (
44
"code.cloudfoundry.org/cli/v8/actor/actionerror"
5-
"code.cloudfoundry.org/cli/v8/api/cloudcontroller/ccversion"
6-
"code.cloudfoundry.org/cli/v8/command"
5+
"code.cloudfoundry.org/cli/v8/api/cloudcontroller/ccversion"
6+
"code.cloudfoundry.org/cli/v8/command"
77
"code.cloudfoundry.org/cli/v8/command/flag"
88
"code.cloudfoundry.org/cli/v8/resources"
99
)
@@ -34,10 +34,11 @@ func (cmd MapRouteCommand) Examples() string {
3434
return `
3535
CF_NAME map-route my-app example.com # example.com
3636
CF_NAME map-route my-app example.com --hostname myhost # myhost.example.com
37-
CF_NAME map-route my-app example.com --hostname myhost -o loadbalancing=least-connection # myhost.example.com with a per-route option
3837
CF_NAME map-route my-app example.com --hostname myhost --path foo # myhost.example.com/foo
3938
CF_NAME map-route my-app example.com --hostname myhost --app-protocol http2 # myhost.example.com
4039
CF_NAME map-route my-app example.com --hostname myhost --app-port 8090 # myhost.example.com
40+
CF_NAME map-route my-app example.com --hostname myhost -o loadbalancing=least-connection # myhost.example.com with a per-route option
41+
CF_NAME map-route my-app example.com -o loadbalancing=hash -o hash_header=My-Hash-Header # use hash-based routing for example.com
4142
CF_NAME map-route my-app example.com --port 5000 # example.com:5000`
4243
}
4344

command/v7/update_route_command.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ Update an existing HTTP route:
2929

3030
func (cmd UpdateRouteCommand) Examples() string {
3131
return `
32-
CF_NAME update-route example.com -o loadbalancing=round-robin,
33-
CF_NAME update-route example.com -o loadbalancing=least-connection,
34-
CF_NAME update-route example.com -r loadbalancing,
35-
CF_NAME update-route example.com --hostname myhost --path foo -o loadbalancing=round-robin`
32+
CF_NAME update-route example.com -o loadbalancing=round-robin # use round-robin load balancing for route
33+
CF_NAME update-route example.com -o loadbalancing=least-connection # use least-connection load balancing for route
34+
CF_NAME update-route example.com -o loadbalancing=hash -o hash_header=My-Hash-Header # use hash-based load balancing for route
35+
CF_NAME update-route example.com -o loadbalancing=hash -o hash_header=My-Hash-Header -o hash_balance=1.3 # use hash-based load balancing with balance factor
36+
CF_NAME update-route example.com -r loadbalancing # remove load balancing option
37+
CF_NAME update-route example.com --hostname myhost --path foo -o loadbalancing=round-robin # update route myhost.example.com/foo
38+
`
3639
}
3740
func (cmd UpdateRouteCommand) Execute(args []string) error {
3841
err := cmd.SharedActor.CheckTarget(true, true)

integration/v7/isolated/map_route_command_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ var _ = Describe("map-route command", func() {
3737
Eventually(session).Should(Say(`EXAMPLES:`))
3838
Eventually(session).Should(Say(`cf map-route my-app example.com # example.com`))
3939
Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost # myhost.example.com`))
40-
Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost -o loadbalancing=least-connection # myhost.example.com with a per-route option`))
4140
Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost --path foo # myhost.example.com/foo`))
4241
Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost --app-protocol http2 # myhost.example.com`))
4342
Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost --app-port 8090 # myhost.example.com`))
43+
Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost -o loadbalancing=least-connection # myhost.example.com with a per-route option`))
44+
Eventually(session).Should(Say(`cf map-route my-app example.com -o loadbalancing=hash -o hash_header=My-Hash-Header # use hash-based routing for example.com`))
4445
Eventually(session).Should(Say(`cf map-route my-app example.com --port 5000 # example.com:5000`))
4546
Eventually(session).Should(Say(`\n`))
4647

@@ -241,6 +242,27 @@ var _ = Describe("map-route command", func() {
241242
Eventually(session).Should(Exit(0))
242243
})
243244
})
245+
Context("when per-route options are provided", func() {
246+
BeforeEach(func() {
247+
helpers.EnableFeatureFlag("hash_based_routing")
248+
})
249+
AfterEach(func() {
250+
helpers.DisableFeatureFlag("hash_based_routing")
251+
})
252+
It("creates the route and maps it to an app", func() {
253+
optionLBAlgo := "loadbalancing=hash"
254+
optionHashHeader := "hash_header=X-Header"
255+
optionHashBalance := "hash_balance=1.3"
256+
257+
session := helpers.CF("map-route", appName, domainName, "--hostname", hostName, "--path", path, "--option", optionLBAlgo, "--option", optionHashHeader, "--option", optionHashBalance)
258+
Eventually(session).Should(Say(`Creating route %s.%s%s for org %s / space %s as %s\.\.\.`, hostName, domainName, path, orgName, spaceName, userName))
259+
Eventually(session).Should(Say(`OK`))
260+
Eventually(session).Should(Say(`Mapping route %s.%s%s to app %s in org %s / space %s as %s\.\.\.`, hostName, domainName, path, appName, orgName, spaceName, userName))
261+
Eventually(session).Should(Say(`OK`))
262+
Eventually(session).Should(Exit(0))
263+
})
264+
})
265+
244266
})
245267

246268
When("it is an TCP domain", func() {

integration/v7/isolated/update_route_command_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var _ = Describe("update-route command", func() {
3131
Eventually(session).Should(Say(`EXAMPLES:`))
3232
Eventually(session).Should(Say(`cf update-route example.com -o loadbalancing=round-robin`))
3333
Eventually(session).Should(Say(`cf update-route example.com -o loadbalancing=least-connection`))
34+
Eventually(session).Should(Say(`cf update-route example.com -o loadbalancing=hash -o hash_header=My-Hash-Header`))
35+
Eventually(session).Should(Say(`cf update-route example.com -o loadbalancing=hash -o hash_header=My-Hash-Header -o hash_balance=1.3`))
3436
Eventually(session).Should(Say(`cf update-route example.com -r loadbalancing`))
3537
Eventually(session).Should(Say(`cf update-route example.com --hostname myhost --path foo -o loadbalancing=round-robin`))
3638
Eventually(session).Should(Say(`\n`))
@@ -102,7 +104,7 @@ var _ = Describe("update-route command", func() {
102104
AfterEach(func() {
103105
domain.Delete()
104106
})
105-
When("a route option is specified", func() {
107+
When("a loadbalancing route option is specified", func() {
106108
It("updates the route and runs to completion without failing", func() {
107109
option = "loadbalancing=round-robin"
108110
session := helpers.CF("update-route", domainName, "--hostname", hostname, "--path", path, "--option", option)
@@ -113,6 +115,45 @@ var _ = Describe("update-route command", func() {
113115
})
114116
})
115117

118+
When("a hash-based routing is enabled and options are specified", func() {
119+
BeforeEach(func() {
120+
helpers.EnableFeatureFlag("hash_based_routing")
121+
})
122+
AfterEach(func() {
123+
helpers.DisableFeatureFlag("hash_based_routing")
124+
})
125+
126+
It("updates the route and runs to completion without failing", func() {
127+
optionLBAlgo := "loadbalancing=hash"
128+
optionHashHeader := "hash_header=X-Header"
129+
optionHashBalance := "hash_balance=1.3"
130+
session := helpers.CF("update-route", domainName, "--hostname", hostname, "--path", path, "--option", optionLBAlgo, "--option", optionHashHeader, "--option", optionHashBalance)
131+
Eventually(session).Should(Say(`Updating route %s\.%s%s for org %s / space %s as %s\.\.\.`, hostname, domainName, path, orgName, spaceName, userName))
132+
Eventually(session).Should(Say(`Route %s\.%s%s has been updated`, hostname, domainName, path))
133+
Eventually(session).Should(Say(`OK`))
134+
Eventually(session).Should(Exit(0))
135+
})
136+
Context("missing required options for hash-based routing", func() {
137+
It("update fails", func() {
138+
optionLBAlgo := "loadbalancing=hash"
139+
session := helpers.CF("update-route", domainName, "--hostname", hostname, "--path", path, "--option", optionLBAlgo)
140+
Eventually(session.Err).Should(Say(`Hash header must be present when loadbalancing is set to hash`))
141+
Eventually(session).Should(Exit(1))
142+
})
143+
})
144+
Context("with wrong loadbalancing option", func() {
145+
It("update fails", func() {
146+
optionLBAlgo := "loadbalancing=round-robin"
147+
optionHashHeader := "hash_header=X-Header"
148+
optionHashBalance := "hash_balance=1.3"
149+
session := helpers.CF("update-route", domainName, "--hostname", hostname, "--path", path, "--option", optionLBAlgo, "--option", optionHashHeader, "--option", optionHashBalance)
150+
Eventually(session.Err).Should(Say(`Options Hash header can only be set when loadbalancing is hash`))
151+
Eventually(session.Err).Should(Say(`Options Hash balance can only be set when loadbalancing is hash`))
152+
Eventually(session).Should(Exit(1))
153+
})
154+
})
155+
})
156+
116157
When("route options are not specified", func() {
117158
It("gives an error message and fails", func() {
118159
session := helpers.CF("update-route", domainName, "--hostname", hostname, "--path", path)

0 commit comments

Comments
 (0)