func (h *DesiredLRPHandler) DesireDesiredLRP_r0(w http.ResponseWriter, req *http.Request) { logger := h.logger.Session("desire-lrp") request := &models.DesireLRPRequest{} response := &models.DesiredLRPLifecycleResponse{} defer func() { exitIfUnrecoverable(logger, h.exitChan, response.Error) }() defer writeResponse(w, response) err := parseRequestForDesireDesiredLRP_r0(logger, req, request) if err != nil { response.Error = models.ConvertError(err) return } err = h.desiredLRPDB.DesireLRP(logger, request.DesiredLrp) if err != nil { response.Error = models.ConvertError(err) return } desiredLRP, err := h.desiredLRPDB.DesiredLRPByProcessGuid(logger, request.DesiredLrp.ProcessGuid) if err != nil { response.Error = models.ConvertError(err) return } go h.desiredHub.Emit(models.NewDesiredLRPCreatedEvent(desiredLRP)) schedulingInfo := request.DesiredLrp.DesiredLRPSchedulingInfo() h.startInstanceRange(logger, 0, schedulingInfo.Instances, &schedulingInfo) }
desiredLRP = &models.DesiredLRP{ ProcessGuid: "some-guid", Domain: "some-domain", RootFs: "some-rootfs", Action: models.WrapAction(&models.RunAction{ Path: "true", User: "******", }), } }) Context("when receiving a DesiredLRPCreatedEvent", func() { var expectedEvent *models.DesiredLRPCreatedEvent BeforeEach(func() { expectedEvent = models.NewDesiredLRPCreatedEvent(desiredLRP) payload, err := proto.Marshal(expectedEvent) Expect(err).NotTo(HaveOccurred()) payload = []byte(base64.StdEncoding.EncodeToString(payload)) fakeRawEventSource.NextReturns( sse.Event{ ID: "hi", Name: string(expectedEvent.EventType()), Data: payload, }, nil, ) }) It("returns the event", func() {
Action: models.WrapAction(&models.RunAction{ User: "******", Path: "ls", }), Domain: "tests", ProcessGuid: expectedProcessGuid, Ports: []uint32{expectedContainerPort}, Routes: &routes, LogGuid: logGuid, } }) JustBeforeEach(func() { table.SetRoutesReturns(dummyMessagesToEmit) nextEvent.Store(EventHolder{models.NewDesiredLRPCreatedEvent(desiredLRP)}) }) It("should set the routes on the table", func() { Eventually(table.SetRoutesCallCount).Should(Equal(1)) key, routes := table.SetRoutesArgsForCall(0) Expect(key).To(Equal(expectedRoutingKey)) Expect(routes).To(Equal(routing_table.Routes{Hostnames: expectedRoutes, LogGuid: logGuid, RouteServiceUrl: expectedRouteServiceUrl})) }) It("sends a 'routes registered' metric", func() { Eventually(func() uint64 { return fakeMetricSender.GetCounter("RoutesRegistered") }).Should(BeEquivalentTo(2)) })
It("emits events from the stream to the connection", func(done Done) { response := &http.Response{} Eventually(responseChan).Should(Receive(&response)) reader := sse.NewReadCloser(response.Body) desiredLRP := &models.DesiredLRP{ ProcessGuid: "some-guid", Domain: "some-domain", RootFs: "some-rootfs", Action: models.WrapAction(&models.RunAction{ Path: "true", User: "******", }), } eventChannel <- models.NewDesiredLRPCreatedEvent(desiredLRP) data, err := json.Marshal(receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(desiredLRP))) Expect(err).NotTo(HaveOccurred()) event, err := reader.Next() Expect(err).NotTo(HaveOccurred()) Expect(event.ID).To(Equal("0")) Expect(event.Name).To(Equal(string(receptor.EventTypeDesiredLRPCreated))) Expect(event.Data).To(MatchJSON(data)) actualLRP := models.NewUnclaimedActualLRP(models.NewActualLRPKey("some-guid", 3, "some-domain"), 0) actualLRPGroup := &models.ActualLRPGroup{Instance: actualLRP} eventChannel <- models.NewActualLRPCreatedEvent(actualLRPGroup) data, err = json.Marshal(receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, false)))
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler.Subscribe_r0(w, r) close(eventStreamDone) })) }) Describe("Subscribe to Desired Events", func() { ItStreamsEventsFromHub(&desiredHub) It("migrates desired lrps down to v0", func() { response, err := http.Get(server.URL) Expect(err).NotTo(HaveOccurred()) reader := sse.NewReadCloser(response.Body) desiredLRP := model_helpers.NewValidDesiredLRP("guid") event := models.NewDesiredLRPCreatedEvent(desiredLRP) migratedLRP := desiredLRP.VersionDownTo(format.V0) Expect(migratedLRP).NotTo(Equal(desiredLRP)) migratedEvent := models.NewDesiredLRPCreatedEvent(migratedLRP) expectedEvent, err := events.NewEventFromModelEvent(0, migratedEvent) Expect(err).NotTo(HaveOccurred()) desiredHub.Emit(event) Expect(reader.Next()).To(Equal(expectedEvent)) }) }) Describe("Subscribe to Actual Events", func() {