예제 #1
0
파일: routing.go 프로젝트: cfibmers/nsync
func CCRouteInfoToRoutes(ccRoutes cc_messages.CCRouteInfo, ports []uint32) (models.Routes, error) {
	var defaultPort uint32
	if len(ports) > 0 {
		defaultPort = ports[0]
	} else {
		defaultPort = 8080
	}

	routes := models.Routes{}

	if ccRoutes[cc_messages.CC_HTTP_ROUTES] != nil {
		httpRoutingInfo, err := constructHttpRoutes(ccRoutes, defaultPort)
		if err != nil {
			return nil, err
		}
		routes[cfroutes.CF_ROUTER] = httpRoutingInfo[cfroutes.CF_ROUTER]
	} else {
		cfRoutes := cfroutes.CFRoutes{}
		httpRoutingInfo := cfRoutes.RoutingInfo()
		routes[cfroutes.CF_ROUTER] = httpRoutingInfo[cfroutes.CF_ROUTER]
	}

	if ccRoutes[cc_messages.CC_TCP_ROUTES] != nil {
		tcpRoutingInfo, err := constructTcpRoutes(ccRoutes)
		if err != nil {
			return nil, err
		}
		routes[tcp_routes.TCP_ROUTER] = tcpRoutingInfo[tcp_routes.TCP_ROUTER]
	} else {
		tcpRoutes := tcp_routes.TCPRoutes{}
		tcpRoutingInfo := tcpRoutes.RoutingInfo()
		routes[tcp_routes.TCP_ROUTER] = (*tcpRoutingInfo)[tcp_routes.TCP_ROUTER]
	}

	return routes, nil
}
			Port:      22222,
		}
		route3 = cfroutes.CFRoute{
			Hostnames:       []string{"foo3.example.com", "bar3.examaple.com"},
			Port:            33333,
			RouteServiceUrl: "rs.example.com",
		}

		routes = cfroutes.CFRoutes{route1, route2, route3}
	})

	Describe("RoutingInfo", func() {
		var routingInfo models.Routes

		JustBeforeEach(func() {
			routingInfo = routes.RoutingInfo()
		})

		It("wraps the serialized routes with the correct key", func() {
			expectedBytes, err := json.Marshal(routes)
			Expect(err).NotTo(HaveOccurred())

			payload, err := routingInfo[cfroutes.CF_ROUTER].MarshalJSON()
			Expect(err).NotTo(HaveOccurred())

			Expect(payload).To(MatchJSON(expectedBytes))
		})

		Context("when CFRoutes is empty", func() {
			BeforeEach(func() {
				routes = cfroutes.CFRoutes{}
	. "github.com/onsi/gomega"
)

var _ = Describe("ByRoutingKey", func() {
	Describe("RoutesByRoutingKeyFromDesireds", func() {
		It("should build a map of routes", func() {
			abcRoutes := cfroutes.CFRoutes{
				{Hostnames: []string{"foo.com", "bar.com"}, Port: 8080, RouteServiceUrl: "https://something.creative"},
				{Hostnames: []string{"foo.example.com"}, Port: 9090},
			}
			defRoutes := cfroutes.CFRoutes{
				{Hostnames: []string{"baz.com"}, Port: 8080},
			}

			routes := routing_table.RoutesByRoutingKeyFromSchedulingInfos([]*models.DesiredLRPSchedulingInfo{
				{DesiredLRPKey: models.NewDesiredLRPKey("abc", "tests", "abc-guid"), Routes: abcRoutes.RoutingInfo()},
				{DesiredLRPKey: models.NewDesiredLRPKey("def", "tests", "def-guid"), Routes: defRoutes.RoutingInfo()},
			})

			Expect(routes).To(HaveLen(3))
			Expect(routes[routing_table.RoutingKey{ProcessGuid: "abc", ContainerPort: 8080}].Hostnames).To(Equal([]string{"foo.com", "bar.com"}))
			Expect(routes[routing_table.RoutingKey{ProcessGuid: "abc", ContainerPort: 8080}].LogGuid).To(Equal("abc-guid"))
			Expect(routes[routing_table.RoutingKey{ProcessGuid: "abc", ContainerPort: 8080}].RouteServiceUrl).To(Equal("https://something.creative"))

			Expect(routes[routing_table.RoutingKey{ProcessGuid: "abc", ContainerPort: 9090}].Hostnames).To(Equal([]string{"foo.example.com"}))
			Expect(routes[routing_table.RoutingKey{ProcessGuid: "abc", ContainerPort: 9090}].LogGuid).To(Equal("abc-guid"))

			Expect(routes[routing_table.RoutingKey{ProcessGuid: "def", ContainerPort: 8080}].Hostnames).To(Equal([]string{"baz.com"}))
			Expect(routes[routing_table.RoutingKey{ProcessGuid: "def", ContainerPort: 8080}].LogGuid).To(Equal("def-guid"))
		})