From 38dc00903af96debdcd0b2187921d5532f10fd12 Mon Sep 17 00:00:00 2001 From: Matthew Sykes Date: Wed, 4 Feb 2015 23:28:01 -0500 Subject: [PATCH] Add CF routing helpers to receptor [#86337946] --- resources_test.go | 17 +++-- routing_info_helpers.go | 40 +++++++++++ routing_info_helpers_test.go | 124 +++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 routing_info_helpers.go create mode 100644 routing_info_helpers_test.go diff --git a/resources_test.go b/resources_test.go index 2df4f69..9079263 100644 --- a/resources_test.go +++ b/resources_test.go @@ -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" @@ -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)) }) diff --git a/routing_info_helpers.go b/routing_info_helpers.go new file mode 100644 index 0000000..d4d7718 --- /dev/null +++ b/routing_info_helpers.go @@ -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 +} diff --git a/routing_info_helpers_test.go b/routing_info_helpers_test.go new file mode 100644 index 0000000..e5e3e96 --- /dev/null +++ b/routing_info_helpers_test.go @@ -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()) + }) + }) + }) +})