func NewEventFromBBS(bbsEvent models.Event) (receptor.Event, error) { switch bbsEvent := bbsEvent.(type) { case *models.ActualLRPCreatedEvent: actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve() return receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil case *models.ActualLRPChangedEvent: before, evacuating := bbsEvent.Before.Resolve() after, evacuating := bbsEvent.After.Resolve() return receptor.NewActualLRPChangedEvent( serialization.ActualLRPProtoToResponse(before, evacuating), serialization.ActualLRPProtoToResponse(after, evacuating), ), nil case *models.ActualLRPRemovedEvent: actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve() return receptor.NewActualLRPRemovedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil case *models.DesiredLRPCreatedEvent: return receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil case *models.DesiredLRPChangedEvent: return receptor.NewDesiredLRPChangedEvent( serialization.DesiredLRPProtoToResponse(bbsEvent.Before), serialization.DesiredLRPProtoToResponse(bbsEvent.After), ), nil case *models.DesiredLRPRemovedEvent: return receptor.NewDesiredLRPRemovedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil } return nil, fmt.Errorf("unknown event type: %#v", bbsEvent) }
func (h *ActualLRPHandler) GetAllByProcessGuid(w http.ResponseWriter, req *http.Request) { processGuid := req.FormValue(":process_guid") logger := h.logger.Session("get-all-by-process-guid", lager.Data{ "ProcessGuid": processGuid, }) if processGuid == "" { err := errors.New("process_guid missing from request") logger.Error("missing-process-guid", err) writeBadRequestResponse(w, receptor.InvalidRequest, err) return } actualLRPGroupsByIndex, err := h.bbs.ActualLRPGroupsByProcessGuid(processGuid) if err != nil { logger.Error("failed-to-fetch-actual-lrp-groups-by-process-guid", err) writeUnknownErrorResponse(w, err) return } responses := make([]receptor.ActualLRPResponse, 0, len(actualLRPGroupsByIndex)) for _, actualLRPGroup := range actualLRPGroupsByIndex { lrp, evacuating := actualLRPGroup.Resolve() responses = append(responses, serialization.ActualLRPProtoToResponse(lrp, evacuating)) } writeJSONResponse(w, http.StatusOK, responses) }
func (h *ActualLRPHandler) GetAll(w http.ResponseWriter, req *http.Request) { domain := req.FormValue("domain") logger := h.logger.Session("get-all", lager.Data{ "domain": domain, }) filter := models.ActualLRPFilter{Domain: domain} actualLRPGroups, err := h.bbs.ActualLRPGroups(filter) if err != nil { logger.Error("failed-to-fetch-actual-lrp-groups", err) writeUnknownErrorResponse(w, err) return } responses := make([]receptor.ActualLRPResponse, 0, len(actualLRPGroups)) for _, actualLRPGroup := range actualLRPGroups { lrp, evacuating, err := actualLRPGroup.Resolve() if err != nil { continue } responses = append(responses, serialization.ActualLRPProtoToResponse(*lrp, evacuating)) } writeJSONResponse(w, http.StatusOK, responses) }
func (h *ActualLRPHandler) GetByProcessGuidAndIndex(w http.ResponseWriter, req *http.Request) { processGuid := req.FormValue(":process_guid") indexString := req.FormValue(":index") logger := h.logger.Session("get-by-process-guid-and-index", lager.Data{ "ProcessGuid": processGuid, "Index": indexString, }) if processGuid == "" { err := errors.New("process_guid missing from request") logger.Error("missing-process-guid", err) writeBadRequestResponse(w, receptor.InvalidRequest, err) return } if indexString == "" { err := errors.New("index missing from request") logger.Error("missing-index", err) writeBadRequestResponse(w, receptor.InvalidRequest, err) return } var err error index, indexErr := strconv.Atoi(indexString) if indexErr != nil { err = errors.New("index not a number") logger.Error("invalid-index", err) writeBadRequestResponse(w, receptor.InvalidRequest, err) return } actualLRPGroup, err := h.bbs.ActualLRPGroupByProcessGuidAndIndex(processGuid, index) if err != nil { if e, ok := err.(*models.Error); ok && e.Equal(models.ErrResourceNotFound) { writeJSONResponse(w, http.StatusNotFound, nil) } else { logger.Error("failed-to-fetch-actual-lrps-by-process-guid", err) writeUnknownErrorResponse(w, err) } return } actualLRP, evacuating := actualLRPGroup.Resolve() writeJSONResponse(w, http.StatusOK, serialization.ActualLRPProtoToResponse(actualLRP, evacuating)) }
By("creating a ActualLRP") err := legacyBBS.DesireLRP(logger, oldDesiredLRP) Expect(err).NotTo(HaveOccurred()) actualLRPGroup, err := bbsClient.ActualLRPGroupByProcessGuidAndIndex(oldDesiredLRP.ProcessGuid, 0) Expect(err).NotTo(HaveOccurred()) actualLRP := actualLRPGroup.Instance var event receptor.Event Eventually(func() receptor.Event { Eventually(events).Should(Receive(&event)) return event }).Should(BeAssignableToTypeOf(receptor.ActualLRPCreatedEvent{})) actualLRPCreatedEvent := event.(receptor.ActualLRPCreatedEvent) Expect(actualLRPCreatedEvent.ActualLRPResponse).To(Equal(serialization.ActualLRPProtoToResponse(actualLRP, false))) By("updating the existing ActualLRP") _, err = bbsClient.ClaimActualLRP(processGuid, 0, &oldInstanceKey) Expect(err).NotTo(HaveOccurred()) before := actualLRP actualLRPGroup, err = bbsClient.ActualLRPGroupByProcessGuidAndIndex(oldDesiredLRP.ProcessGuid, 0) Expect(err).NotTo(HaveOccurred()) actualLRP = actualLRPGroup.Instance Eventually(func() receptor.Event { Eventually(events).Should(Receive(&event)) return event }).Should(BeAssignableToTypeOf(receptor.ActualLRPChangedEvent{}))
desiredLRPRemovedEvent, ok := event.(receptor.DesiredLRPRemovedEvent) Expect(ok).To(BeTrue()) Expect(desiredLRPRemovedEvent).To(Equal(expectedEvent)) }) }) }) Describe("Actual LRP Events", func() { var actualLRPResponse receptor.ActualLRPResponse BeforeEach(func() { actualLRPResponse = serialization.ActualLRPProtoToResponse( &models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("some-guid", 0, "some-domain"), State: models.ActualLRPStateUnclaimed, Since: 1, }, false, ) }) Context("when receiving a ActualLRPCreatedEvent", func() { var expectedEvent receptor.ActualLRPCreatedEvent BeforeEach(func() { expectedEvent = receptor.NewActualLRPCreatedEvent(actualLRPResponse) payload, err := json.Marshal(expectedEvent) Expect(err).NotTo(HaveOccurred()) fakeRawEventSource.NextReturns( sse.Event{
handler.GetAll(responseRecorder, newTestRequest("")) Expect(responseRecorder.Code).To(Equal(http.StatusOK)) }) Context("when a domain query param is provided", func() { It("returns a list of desired lrp responses for the domain", func() { request, err := http.NewRequest("", "http://example.com?domain=domain-1", nil) Expect(err).NotTo(HaveOccurred()) handler.GetAll(responseRecorder, request) response := []receptor.ActualLRPResponse{} err = json.Unmarshal(responseRecorder.Body.Bytes(), &response) Expect(err).NotTo(HaveOccurred()) Expect(response).To(HaveLen(1)) Expect(response[0]).To(Equal(serialization.ActualLRPProtoToResponse(evacuatingLRP2, true))) }) }) Context("when a domain query param is not provided", func() { It("returns a list of desired lrp responses", func() { handler.GetAll(responseRecorder, newTestRequest("")) response := []receptor.ActualLRPResponse{} err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) Expect(err).NotTo(HaveOccurred()) Expect(response).To(HaveLen(2)) Expect(response[0].ProcessGuid).To(Equal("process-guid-0")) Expect(response[1].ProcessGuid).To(Equal("process-guid-1")) expectedResponses := []receptor.ActualLRPResponse{ serialization.ActualLRPToResponse(oldActualLRP1, false),
It("fetches all of the actual lrps", func() { Expect(actualLRPResponses).To(HaveLen(lrpCount)) }) It("has the correct data from the BBS", func() { actualLRPGroups, err := bbsClient.ActualLRPGroups(models.ActualLRPFilter{}) Expect(err).NotTo(HaveOccurred()) expectedResponses := make([]receptor.ActualLRPResponse, 0, lrpCount) for _, actualLRPGroup := range actualLRPGroups { actualLRP, evacuating, _ := actualLRPGroup.Resolve() if actualLRP.ActualLRPKey == evacuatingLRPKey { continue } expectedResponses = append(expectedResponses, serialization.ActualLRPProtoToResponse(*actualLRP, evacuating)) } Expect(actualLRPResponses).To(ConsistOf(expectedResponses)) }) }) Describe("ActualLRPsByDomain", func() { var actualLRPResponses []receptor.ActualLRPResponse var getErr error BeforeEach(func() { actualLRPResponses, getErr = client.ActualLRPsByDomain("domain-0") }) It("responds without an error", func() {
handler.GetAll(responseRecorder, newTestRequest("")) Expect(responseRecorder.Code).To(Equal(http.StatusOK)) }) Context("when a domain query param is provided", func() { It("returns a list of desired lrp responses for the domain", func() { request, err := http.NewRequest("", "http://example.com?domain=domain-1", nil) Expect(err).NotTo(HaveOccurred()) handler.GetAll(responseRecorder, request) response := []receptor.ActualLRPResponse{} err = json.Unmarshal(responseRecorder.Body.Bytes(), &response) Expect(err).NotTo(HaveOccurred()) Expect(response).To(HaveLen(1)) Expect(response[0]).To(Equal(serialization.ActualLRPProtoToResponse(evacuatingLRP2, true))) }) }) Context("when a domain query param is not provided", func() { It("returns a list of desired lrp responses", func() { handler.GetAll(responseRecorder, newTestRequest("")) response := []receptor.ActualLRPResponse{} err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) Expect(err).NotTo(HaveOccurred()) Expect(response).To(HaveLen(2)) Expect(response[0].ProcessGuid).To(Equal("process-guid-0")) Expect(response[1].ProcessGuid).To(Equal("process-guid-1")) expectedResponses := []receptor.ActualLRPResponse{ serialization.ActualLRPProtoToResponse(actualLRP1, false),
{ ContainerPort: 2345, HostPort: 9876, }, }, State: receptor.ActualLRPStateRunning, CrashCount: 42, Since: 99999999999, Evacuating: true, ModificationTag: receptor.ModificationTag{ Epoch: "some-guid", Index: 50, }, } actualResponse := serialization.ActualLRPProtoToResponse(actualLRP, true) Expect(actualResponse).To(Equal(expectedResponse)) }) It("maps model states to receptor states", func() { expectedStateMap := map[string]receptor.ActualLRPState{ models.ActualLRPStateUnclaimed: receptor.ActualLRPStateUnclaimed, models.ActualLRPStateClaimed: receptor.ActualLRPStateClaimed, models.ActualLRPStateRunning: receptor.ActualLRPStateRunning, models.ActualLRPStateCrashed: receptor.ActualLRPStateCrashed, } for modelState, jsonState := range expectedStateMap { actualLRP.State = modelState Expect(serialization.ActualLRPProtoToResponse(actualLRP, false).State).To(Equal(jsonState)) }
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))) Expect(err).NotTo(HaveOccurred()) event, err = reader.Next() Expect(err).NotTo(HaveOccurred()) Expect(event.ID).To(Equal("1")) Expect(event.Name).To(Equal(string(receptor.EventTypeActualLRPCreated))) Expect(event.Data).To(MatchJSON(data)) eventChannel <- eventfakes.FakeEvent{"B"} close(done) }) It("returns Content-Type as text/event-stream", func() { response := &http.Response{} Eventually(responseChan).Should(Receive(&response))