func defaultCrashedActual(crashCount int, lastCrashed int64) models.ActualLRP { return models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("p-guid", 0, "domain"), State: models.ActualLRPStateCrashed, CrashCount: crashCount, Since: lastCrashed, } }
func (h *ActualLRPHandler) KillByProcessGuidAndIndex(w http.ResponseWriter, req *http.Request) { processGuid := req.FormValue(":process_guid") indexString := req.FormValue(":index") logger := h.logger.Session("kill-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 } index, err := strconv.Atoi(indexString) if err != 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) { responseErr := fmt.Errorf("process-guid '%s' does not exist or has no instance at index %d", processGuid, index) logger.Error("no-instances-to-delete", responseErr) writeJSONResponse(w, http.StatusNotFound, receptor.Error{ Type: receptor.ActualLRPIndexNotFound, Message: responseErr.Error(), }) } else { logger.Error("failed-to-fetch-actual-lrps-by-process-guid", err) writeUnknownErrorResponse(w, err) } return } actualLRP, _ := actualLRPGroup.Resolve() actualLRPKey := oldmodels.NewActualLRPKey(actualLRP.ProcessGuid, int(actualLRP.Index), actualLRP.Domain) h.legacyBBS.RetireActualLRPs(logger, []oldmodels.ActualLRPKey{actualLRPKey}) w.WriteHeader(http.StatusNoContent) }
func itValidatesPresenceOfTheLRPKey(lrp *models.ActualLRP) { Context("when the lrp key is set", func() { BeforeEach(func() { lrp.ActualLRPKey = models.NewActualLRPKey("some-guid", 1, "domain") }) It("validate does not return an error", func() { Expect(lrp.Validate()).NotTo(HaveOccurred()) }) }) Context("when the lrp key is not set", func() { BeforeEach(func() { lrp.ActualLRPKey = models.ActualLRPKey{} }) It("validate returns an error", func() { err := lrp.Validate() Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("process_guid")) }) }) }
legacyNetInfo oldmodels.ActualLRPNetInfo ) BeforeEach(func() { oldDesiredLRP = oldmodels.DesiredLRP{ ProcessGuid: processGuid, Domain: domain, RootFS: "some:rootfs", Instances: 1, Action: &oldmodels.RunAction{ Path: "true", User: "******", }, } legacyKey = oldmodels.NewActualLRPKey(processGuid, 0, domain) legacyOldInstanceKey = oldmodels.NewActualLRPInstanceKey("instance-guid", "cell-id") legacyNewInstanceKey = oldmodels.NewActualLRPInstanceKey("other-instance-guid", "other-cell-id") legacyNetInfo = oldmodels.NewActualLRPNetInfo("1.1.1.1", []oldmodels.PortMapping{}) key = models.NewActualLRPKey(processGuid, 0, domain) oldInstanceKey = models.NewActualLRPInstanceKey("instance-guid", "cell-id") newInstanceKey = models.NewActualLRPInstanceKey("other-instance-guid", "other-cell-id") netInfo = models.NewActualLRPNetInfo("1.1.1.1") }) It("receives events", func() { By("creating a ActualLRP") err := legacyBBS.DesireLRP(logger, oldDesiredLRP) Expect(err).NotTo(HaveOccurred())
), State: proto.String(models.ActualLRPStateClaimed), Since: proto.Int64(4444), } evacuatingLRP2 = actualLRP2 evacuatingLRP2.State = proto.String(models.ActualLRPStateRunning) evacuatingLRP2.Since = proto.Int64(3417) }) // old before each BeforeEach(func() { oldActualLRP1 = oldmodels.ActualLRP{ ActualLRPKey: oldmodels.NewActualLRPKey( "process-guid-0", 1, "domain-0", ), ActualLRPInstanceKey: oldmodels.NewActualLRPInstanceKey( "instance-guid-0", "cell-id-0", ), State: oldmodels.ActualLRPStateRunning, Since: 1138, } oldActualLRP2 = oldmodels.ActualLRP{ ActualLRPKey: oldmodels.NewActualLRPKey( "process-guid-1", 2, "domain-1",
var _ = Describe("Actual LRP API", func() { const lrpCount = 6 var ( evacuatingLRPKey models.ActualLRPKey oldEvacuatingLRPKey oldmodels.ActualLRPKey ) BeforeEach(func() { receptorProcess = ginkgomon.Invoke(receptorRunner) for i := 0; i < lrpCount; i++ { index := strconv.Itoa(i) lrpKey := oldmodels.NewActualLRPKey( "process-guid-"+index, i, fmt.Sprintf("domain-%d", i/2), ) instanceKey := oldmodels.NewActualLRPInstanceKey( "instance-guid-"+index, "cell-id", ) netInfo := oldmodels.NewActualLRPNetInfo("the-host", []oldmodels.PortMapping{{ContainerPort: 80, HostPort: uint16(1000 + i)}}) err := legacyBBS.StartActualLRP(logger, lrpKey, instanceKey, netInfo) Expect(err).NotTo(HaveOccurred()) } desiredLRP := oldmodels.DesiredLRP{ ProcessGuid: "process-guid-0", Domain: "domain-0", Instances: 1,
Expect(err).NotTo(HaveOccurred()) 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())
Expect(hub.EmitCallCount()).To(Equal(1)) event := hub.EmitArgsForCall(0) desiredLRPRemovedEvent, ok := event.(receptor.DesiredLRPRemovedEvent) Expect(ok).To(BeTrue()) Expect(desiredLRPRemovedEvent.DesiredLRPResponse).To(Equal(serialization.DesiredLRPToResponse(desiredLRP))) }) }) }) Describe("Actual LRP changes", func() { var actualLRP models.ActualLRP BeforeEach(func() { actualLRP = models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey(expectedProcessGuid, 1, "domain"), 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)
if state == models.ActualLRPStateCrashed { Expect(actual.ShouldRestartCrash(now, calc)).To(BeTrue(), "should restart CRASHED lrp") } else { Expect(actual.ShouldRestartCrash(now, calc)).To(BeFalse(), fmt.Sprintf("should not restart %s lrp", state)) } } }) }) }) Describe("ActualLRPKey", func() { Describe("Validate", func() { var actualLRPKey models.ActualLRPKey BeforeEach(func() { actualLRPKey = models.NewActualLRPKey("process-guid", 1, "domain") }) Context("when valid", func() { It("returns nil", func() { Expect(actualLRPKey.Validate()).To(BeNil()) }) }) Context("when the ProcessGuid is blank", func() { BeforeEach(func() { actualLRPKey.ProcessGuid = "" }) It("returns a validation error", func() { Expect(actualLRPKey.Validate()).To(ConsistOf(models.ErrInvalidField{"process_guid"}))
newInstanceKey models.ActualLRPInstanceKey netInfo models.ActualLRPNetInfo ) BeforeEach(func() { desiredLRP = models.DesiredLRP{ ProcessGuid: processGuid, Domain: domain, RootFS: "some:rootfs", Instances: 1, Action: &models.RunAction{ Path: "true", }, } key = models.NewActualLRPKey(processGuid, 0, domain) instanceKey = models.NewActualLRPInstanceKey("instance-guid", "cell-id") newInstanceKey = models.NewActualLRPInstanceKey("other-instance-guid", "other-cell-id") netInfo = models.NewActualLRPNetInfo("1.1.1.1", []models.PortMapping{}) }) It("receives events", func() { By("creating a ActualLRP") err := bbs.DesireLRP(logger, desiredLRP) Expect(err).NotTo(HaveOccurred()) actualLRPGroup, err := bbs.ActualLRPGroupByProcessGuidAndIndex(logger, desiredLRP.ProcessGuid, 0) Expect(err).NotTo(HaveOccurred()) actualLRP := *actualLRPGroup.Instance var event receptor.Event
"github.com/cloudfoundry-incubator/receptor/serialization" "github.com/cloudfoundry-incubator/runtime-schema/diego_errors" "github.com/cloudfoundry-incubator/runtime-schema/models" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("ActualLRP Serialization", func() { Describe("ActualLRPToResponse", func() { var actualLRP models.ActualLRP BeforeEach(func() { actualLRP = models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey( "process-guid-0", 3, "some-domain", ), ActualLRPInstanceKey: models.NewActualLRPInstanceKey( "instance-guid-0", "cell-id-0", ), ActualLRPNetInfo: models.NewActualLRPNetInfo( "address-0", []models.PortMapping{ { ContainerPort: 2345, HostPort: 9876, }, }, ),
} desiredLRP := oldmodels.DesiredLRP{ ProcessGuid: "process-guid-0", Domain: "domain-0", Instances: 1, RootFS: "some:rootfs", Ports: []uint16{80}, Action: &oldmodels.RunAction{User: "******", Path: "/bin/true"}, } err := legacyBBS.DesireLRP(logger, desiredLRP) Expect(err).NotTo(HaveOccurred()) evacuatingLRPKey = models.NewActualLRPKey("process-guid-0", 0, "domain-0") oldEvacuatingLRPKey = oldmodels.NewActualLRPKey("process-guid-0", 0, "domain-0") instanceKey := oldmodels.NewActualLRPInstanceKey("instance-guid-0", "cell-id") netInfo := oldmodels.NewActualLRPNetInfo("the-host", []oldmodels.PortMapping{{ContainerPort: 80, HostPort: 1000}}) _, err = legacyBBS.EvacuateRunningActualLRP(logger, oldEvacuatingLRPKey, instanceKey, netInfo, 0) Expect(err).To(Equal(bbserrors.ErrServiceUnavailable)) }) AfterEach(func() { ginkgomon.Kill(receptorProcess) }) Describe("ActualLRPs", func() { var actualLRPResponses []receptor.ActualLRPResponse var getErr error BeforeEach(func() {