Beispiel #1
0
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.legacyBBS.ActualLRPGroupsByProcessGuid(logger, 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, err := actualLRPGroup.Resolve()
		if err != nil {
			continue
		}
		responses = append(responses, serialization.ActualLRPToResponse(*lrp, evacuating))
	}

	writeJSONResponse(w, http.StatusOK, responses)
}
Beispiel #2
0
func (w *watcher) watchActual(logger lager.Logger) (chan<- bool, <-chan error) {
	return w.bbs.WatchForActualLRPChanges(logger,
		func(created models.ActualLRP, evacuating bool) {
			logger.Debug("handling-actual-create")
			w.hub.Emit(receptor.NewActualLRPCreatedEvent(serialization.ActualLRPToResponse(created, evacuating)))
		},
		func(changed models.ActualLRPChange, evacuating bool) {
			logger.Debug("handling-actual-change")
			w.hub.Emit(receptor.NewActualLRPChangedEvent(
				serialization.ActualLRPToResponse(changed.Before, evacuating),
				serialization.ActualLRPToResponse(changed.After, evacuating),
			))
		},
		func(deleted models.ActualLRP, evacuating bool) {
			logger.Debug("handling-actual-delete")
			w.hub.Emit(receptor.NewActualLRPRemovedEvent(serialization.ActualLRPToResponse(deleted, evacuating)))
		})
}
Beispiel #3
0
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.legacyBBS.ActualLRPGroupByProcessGuidAndIndex(logger, processGuid, index)
	if err == bbserrors.ErrStoreResourceNotFound {
		writeJSONResponse(w, http.StatusNotFound, nil)
		return
	}

	if err != nil {
		logger.Error("failed-to-fetch-actual-lrps-by-process-guid", err)
		writeUnknownErrorResponse(w, err)
		return
	}

	actualLRP, evacuating, err := actualLRPGroup.Resolve()

	writeJSONResponse(w, http.StatusOK, serialization.ActualLRPToResponse(*actualLRP, evacuating))
}
					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),
						serialization.ActualLRPToResponse(oldEvacuatingLRP2, true),
					}

					Expect(response).To(ConsistOf(expectedResponses))
				})
			})
		})

		Context("when the BBS returns no lrps", func() {
			BeforeEach(func() {
				fakeBBS.ActualLRPGroupsReturns([]*models.ActualLRPGroup{}, nil)
			})

			It("call the BBS to retrieve the actual LRPs", func() {
				handler.GetAll(responseRecorder, newTestRequest(""))
Beispiel #5
0
		})

		It("responds without an error", func() {
			Expect(getErr).NotTo(HaveOccurred())
		})

		It("fetches all of the actual lrps", func() {
			Expect(actualLRPResponses).To(HaveLen(2))
		})

		It("has the correct data from the bbs", func() {
			expectedResponses := []receptor.ActualLRPResponse{}

			instanceLRPGroup, err := legacyBBS.ActualLRPGroupByProcessGuidAndIndex(logger, "process-guid-1", 1)
			Expect(err).NotTo(HaveOccurred())
			expectedResponses = append(expectedResponses, serialization.ActualLRPToResponse(*instanceLRPGroup.Instance, false))

			evacuatingLRP, err := legacyBBS.EvacuatingActualLRPByProcessGuidAndIndex(logger, oldEvacuatingLRPKey.ProcessGuid, oldEvacuatingLRPKey.Index)
			Expect(err).NotTo(HaveOccurred())
			expectedResponses = append(expectedResponses, serialization.ActualLRPToResponse(evacuatingLRP, true))

			Expect(actualLRPResponses).To(ConsistOf(expectedResponses))
		})
	})

	Describe("ActualLRPsByProcessGuid", func() {
		var actualLRPResponses []receptor.ActualLRPResponse
		var getErr error

		JustBeforeEach(func() {
			actualLRPResponses, getErr = client.ActualLRPsByProcessGuid("process-guid-0")
					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.ActualLRPToResponse(
					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{
Beispiel #7
0
					ActualLRPInstanceKey: models.NewActualLRPInstanceKey(expectedInstanceGuid, "cell-id"),
				}
			})

			Context("when a non-evacuating create arrives", func() {
				BeforeEach(func() {
					actualCreateCB(actualLRP, false)
				})

				It("emits an ActualLRPCreatedEvent to the hub", func() {
					Expect(hub.EmitCallCount()).To(Equal(1))
					event := hub.EmitArgsForCall(0)
					Expect(event).To(BeAssignableToTypeOf(receptor.ActualLRPCreatedEvent{}))

					actualLRPCreatedEvent := event.(receptor.ActualLRPCreatedEvent)
					Expect(actualLRPCreatedEvent.ActualLRPResponse).To(Equal(serialization.ActualLRPToResponse(actualLRP, false)))
				})
			})

			Context("when a non-evacuating change arrives", func() {
				BeforeEach(func() {
					actualChangeCB(models.ActualLRPChange{Before: actualLRP, After: actualLRP}, false)
				})

				It("emits an ActualLRPChangedEvent to the hub", func() {
					Expect(hub.EmitCallCount()).To(Equal(1))
					event := hub.EmitArgsForCall(0)
					Expect(event).To(BeAssignableToTypeOf(receptor.ActualLRPChangedEvent{}))

					actualLRPChangedEvent := event.(receptor.ActualLRPChangedEvent)
					Expect(actualLRPChangedEvent.Before).To(Equal(serialization.ActualLRPToResponse(actualLRP, false)))
					{
						ContainerPort: 2345,
						HostPort:      9876,
					},
				},
				State:      receptor.ActualLRPStateRunning,
				CrashCount: 42,
				Since:      99999999999,
				Evacuating: true,
				ModificationTag: receptor.ModificationTag{
					Epoch: "some-guid",
					Index: 50,
				},
			}

			actualResponse := serialization.ActualLRPToResponse(actualLRP, true)
			Expect(actualResponse).To(Equal(expectedResponse))
		})

		It("maps model states to receptor states", func() {
			expectedStateMap := map[models.ActualLRPState]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.ActualLRPToResponse(actualLRP, false).State).To(Equal(jsonState))
			}