Skip to content

Commit

Permalink
Add CF routing helpers to receptor
Browse files Browse the repository at this point in the history
[#86337946]
  • Loading branch information
sykesm committed Feb 5, 2015
1 parent 4364c95 commit 38dc009
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 5 deletions.
17 changes: 12 additions & 5 deletions resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"

"github.com/cloudfoundry-incubator/receptor"
"github.com/cloudfoundry-incubator/runtime-schema/cc_messages"
"github.com/cloudfoundry-incubator/runtime-schema/models"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -341,12 +340,20 @@ var _ = Describe("Resources", func() {

Context("MarshalJson", func() {
It("marshals routes when present", func() {
routingInfo := *cc_messages.NewRoutingInfo([]string{"a", "b"}, 1)
routingInfo := receptor.RoutingInfo{}

otherRawMessage := json.RawMessage([]byte(`"bar"`))
routingInfo["foo"] = &otherRawMessage
bytes, err := json.Marshal(receptor.CFRoutes{
{Hostnames: []string{"a", "b"}, Port: 1},
})
Ω(err).ShouldNot(HaveOccurred())

cfRawMessage := json.RawMessage(bytes)
routingInfo[receptor.CF_ROUTER] = &cfRawMessage

fooRawMessage := json.RawMessage([]byte(`"bar"`))
routingInfo["foo"] = &fooRawMessage

bytes, err := json.Marshal(routingInfo)
bytes, err = json.Marshal(routingInfo)
Ω(err).ShouldNot(HaveOccurred())
Ω(bytes).Should(MatchJSON(jsonRoutes))
})
Expand Down
40 changes: 40 additions & 0 deletions routing_info_helpers.go
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
}
124 changes: 124 additions & 0 deletions routing_info_helpers_test.go
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())
})
})
})
})

0 comments on commit 38dc009

Please sign in to comment.