forked from cloudfoundry-attic/receptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#86337946]
- Loading branch information
Showing
3 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package receptor | ||
|
||
import "encoding/json" | ||
|
||
const CF_ROUTER = "cf-router" | ||
|
||
type CFRoutes []CFRoute | ||
|
||
type CFRoute struct { | ||
Hostnames []string `json:"hostnames"` | ||
Port uint16 `json:"port"` | ||
} | ||
|
||
func (c CFRoutes) RoutingInfo() *RoutingInfo { | ||
data, _ := json.Marshal(c) | ||
routingInfo := json.RawMessage(data) | ||
return &RoutingInfo{ | ||
CF_ROUTER: &routingInfo, | ||
} | ||
} | ||
|
||
func CFRoutesFromRoutingInfo(routingInfo *RoutingInfo) (CFRoutes, error) { | ||
if routingInfo == nil { | ||
return nil, nil | ||
} | ||
|
||
data, found := (*routingInfo)[CF_ROUTER] | ||
if !found { | ||
return nil, nil | ||
} | ||
|
||
if data == nil { | ||
return nil, nil | ||
} | ||
|
||
routes := CFRoutes{} | ||
err := json.Unmarshal(*data, &routes) | ||
|
||
return routes, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package receptor_test | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/cloudfoundry-incubator/receptor" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("RoutingInfoHelpers", func() { | ||
var ( | ||
route1 receptor.CFRoute | ||
route2 receptor.CFRoute | ||
route3 receptor.CFRoute | ||
|
||
routes receptor.CFRoutes | ||
) | ||
|
||
BeforeEach(func() { | ||
route1 = receptor.CFRoute{ | ||
Hostnames: []string{"foo1.example.com", "bar1.examaple.com"}, | ||
Port: 11111, | ||
} | ||
route2 = receptor.CFRoute{ | ||
Hostnames: []string{"foo2.example.com", "bar2.examaple.com"}, | ||
Port: 22222, | ||
} | ||
route3 = receptor.CFRoute{ | ||
Hostnames: []string{"foo3.example.com", "bar3.examaple.com"}, | ||
Port: 33333, | ||
} | ||
|
||
routes = receptor.CFRoutes{route1, route2, route3} | ||
}) | ||
|
||
Describe("RoutingInfo", func() { | ||
var routingInfo *receptor.RoutingInfo | ||
|
||
JustBeforeEach(func() { | ||
routingInfo = routes.RoutingInfo() | ||
}) | ||
|
||
It("wraps the serialized routes with the correct key", func() { | ||
expectedBytes, err := json.Marshal(routes) | ||
Ω(err).ShouldNot(HaveOccurred()) | ||
|
||
payload, err := (*routingInfo)[receptor.CF_ROUTER].MarshalJSON() | ||
Ω(err).ShouldNot(HaveOccurred()) | ||
|
||
Ω(payload).Should(MatchJSON(expectedBytes)) | ||
}) | ||
|
||
Context("when CFRoutes is empty", func() { | ||
BeforeEach(func() { | ||
routes = receptor.CFRoutes{} | ||
}) | ||
|
||
It("marshals an empty list", func() { | ||
payload, err := (*routingInfo)[receptor.CF_ROUTER].MarshalJSON() | ||
Ω(err).ShouldNot(HaveOccurred()) | ||
|
||
Ω(payload).Should(MatchJSON(`[]`)) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("CFRoutesFromRoutingInfo", func() { | ||
var ( | ||
routesResult receptor.CFRoutes | ||
conversionError error | ||
|
||
routingInfo *receptor.RoutingInfo | ||
) | ||
|
||
JustBeforeEach(func() { | ||
routesResult, conversionError = receptor.CFRoutesFromRoutingInfo(routingInfo) | ||
}) | ||
|
||
Context("when CF routes are present in the routing info", func() { | ||
BeforeEach(func() { | ||
routingInfo = routes.RoutingInfo() | ||
}) | ||
|
||
It("returns the routes", func() { | ||
Ω(routes).Should(Equal(routesResult)) | ||
}) | ||
|
||
Context("when the CF routes are nil", func() { | ||
BeforeEach(func() { | ||
routingInfo = &receptor.RoutingInfo{receptor.CF_ROUTER: nil} | ||
}) | ||
|
||
It("returns nil routes", func() { | ||
Ω(conversionError).ShouldNot(HaveOccurred()) | ||
Ω(routesResult).Should(BeNil()) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("when CF routes are not present in the routing info", func() { | ||
BeforeEach(func() { | ||
routingInfo = &receptor.RoutingInfo{} | ||
}) | ||
|
||
It("returns nil routes", func() { | ||
Ω(conversionError).ShouldNot(HaveOccurred()) | ||
Ω(routesResult).Should(BeNil()) | ||
}) | ||
}) | ||
|
||
Context("when the routing info is nil", func() { | ||
BeforeEach(func() { | ||
routingInfo = nil | ||
}) | ||
|
||
It("returns nil routes", func() { | ||
Ω(conversionError).ShouldNot(HaveOccurred()) | ||
Ω(routesResult).Should(BeNil()) | ||
}) | ||
}) | ||
}) | ||
}) |