}) It("logs the route deletion", func() { request = handlers.NewTestRequest(route) routesHandler.Delete(responseRecorder, request) data := map[string]interface{}{ "ip": "1.2.3.4", "log_guid": "", "port": float64(7000), "route": "post_here", "ttl": float64(0), } log_data := map[string][]interface{}{"route_deletion": []interface{}{data}} Expect(logger.Logs()[0].Message).To(ContainSubstring("request")) Expect(logger.Logs()[0].Data["route_deletion"]).To(Equal(log_data["route_deletion"])) }) Context("when the database deletion fails", func() { It("returns a 204 if the key was not found", func() { database.DeleteRouteReturns(db.DBError{Type: db.KeyNotFound, Message: "The specified route could not be found."}) request = handlers.NewTestRequest(route) routesHandler.Delete(responseRecorder, request) Expect(responseRecorder.Code).To(Equal(http.StatusNoContent)) }) It("responds with a server error", func() { database.DeleteRouteReturns(errors.New("stuff broke"))
path := "/v2/service_instances/" + instanceID buffer := &bytes.Buffer{} json.NewEncoder(buffer).Encode(details) request, err := http.NewRequest("PUT", path, buffer) Expect(err).NotTo(HaveOccurred()) request.Header.Add("Content-Type", "application/json") request.SetBasicAuth(credentials.Username, credentials.Password) response = r.Do(request) }) return response } lastLogLine := func() lager.LogFormat { if len(brokerLogger.Logs()) == 0 { // better way to raise error? err := errors.New("expected some log lines but there were none!") Expect(err).NotTo(HaveOccurred()) } return brokerLogger.Logs()[0] } BeforeEach(func() { fakeServiceBroker = &fakes.FakeServiceBroker{ InstanceLimit: 3, } brokerLogger = lagertest.NewTestLogger("broker-api") brokerAPI = brokerapi.New(fakeServiceBroker, brokerLogger, credentials) })
. "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("LagerWriter", func() { var logger *lagertest.TestLogger var lagerWriter io.Writer BeforeEach(func() { logger = lagertest.NewTestLogger("test") lagerWriter = helpers.NewLagerWriter(logger) }) It("writes the payload as lager.Data", func() { payload := []byte("Hello, world!\n") n, err := lagerWriter.Write(payload) Expect(err).NotTo(HaveOccurred()) Expect(n).To(Equal(len(payload))) Expect(logger.Logs()).To(HaveLen(1)) log := logger.Logs()[0] Expect(log.Source).To(Equal("test")) Expect(log.Message).To(Equal("test.write")) Expect(log.LogLevel).To(Equal(lager.INFO)) Expect(log.Data).To(Equal(lager.Data{"payload": string(payload)})) }) })
Eventually(database.SaveRouteCallCount).Should(Equal(2)) Eventually(func() models.Route { return database.SaveRouteArgsForCall(1) }).Should(Equal(route)) Eventually(logger.Logs).Should(HaveLen(0)) }) }) Context("when there are errors", func() { BeforeEach(func() { database.SaveRouteStub = func(route models.Route) error { return errors.New("beep boop, self destruct mode engaged") } }) It("only logs the error once for each attempt", func() { Consistently(func() int { return len(logger.Logs()) }).Should(BeNumerically("<=", 1)) Eventually(func() string { if len(logger.Logs()) > 0 { return logger.Logs()[0].Data["error"].(string) } else { return "" } }).Should(ContainSubstring("beep boop, self destruct mode engaged")) }) }) }) Context("unregistration", func() { It("unregisters the routing api when a SIGTERM is received", func() { process.Signal(syscall.SIGTERM) Eventually(database.DeleteRouteCallCount).Should(Equal(1))
func expectInvalidInput(responseRecorder *httptest.ResponseRecorder, database *fake_db.FakeDB, logger *lagertest.TestLogger) { Expect(responseRecorder.Code).To(Equal(http.StatusBadRequest)) Expect(responseRecorder.Body.String()).To(ContainSubstring("Each tcp mapping requires a positive host port")) Expect(database.SaveRouteCallCount()).To(Equal(0)) Expect(logger.Logs()[0].Message).To(ContainSubstring("error")) }
tcpRouteMappingsHandler.Upsert(responseRecorder, request) tcpMapping = db.NewTcpRouteMapping("router-group-guid-001", 52000, "1.2.3.4", 60000) routeData := map[string]interface{}{ "router_group_guid": "router-group-guid-001", "external_port": float64(52000), } data := map[string]interface{}{ "host_ip": "1.2.3.4", "host_port": float64(60000), "route": routeData, } log_data := map[string][]interface{}{"tcp_mapping_creation": []interface{}{data}} Expect(logger.Logs()[0].Message).To(ContainSubstring("request")) Expect(logger.Logs()[0].Data["tcp_mapping_creation"]).To(Equal(log_data["tcp_mapping_creation"])) }) Context("when database fails to save", func() { BeforeEach(func() { database.SaveTcpRouteMappingReturns(errors.New("stuff broke")) }) It("responds with a server error", func() { request = handlers.NewTestRequest(tcpMappings) tcpRouteMappingsHandler.Upsert(responseRecorder, request) Expect(responseRecorder.Code).To(Equal(http.StatusInternalServerError)) Expect(responseRecorder.Body.String()).To(ContainSubstring("stuff broke")) }) })
} makeInstanceProvisioningRequestWithAcceptsIncomplete := func(instanceID string, details brokerapi.ProvisionDetails, acceptsIncomplete bool) *testflight.Response { var acceptsIncompleteFlag string if acceptsIncomplete { acceptsIncompleteFlag = "?accepts_incomplete=true" } else { acceptsIncompleteFlag = "?accepts_incomplete=false" } return makeInstanceProvisioningRequest(instanceID, details, acceptsIncompleteFlag) } lastLogLine := func() lager.LogFormat { noOfLogLines := len(brokerLogger.Logs()) if noOfLogLines == 0 { // better way to raise error? err := errors.New("expected some log lines but there were none") Expect(err).NotTo(HaveOccurred()) } return brokerLogger.Logs()[noOfLogLines-1] } BeforeEach(func() { fakeServiceBroker = &fakes.FakeServiceBroker{ InstanceLimit: 3, } brokerLogger = lagertest.NewTestLogger("broker-api") brokerAPI = brokerapi.New(fakeServiceBroker, brokerLogger, credentials)
)) }) It("changes the container's state to active", func() { Expect(container.State()).To(Equal(linux_backend.StateBorn)) err := container.Start() Expect(err).ToNot(HaveOccurred()) Expect(container.State()).To(Equal(linux_backend.StateActive)) }) It("should log before and after", func() { Expect(container.Start()).To(Succeed()) logs := logger.Logs() expectedData := lager.Data{"handle": "some-handle"} Expect(logs).To(ContainLogWithData("linux-container.start.iptables-setup-starting", expectedData)) Expect(logs).To(ContainLogWithData("linux-container.start.iptables-setup-ended", expectedData)) Expect(logs).To(ContainLogWithData("linux-container.start.wshd-start-starting", expectedData)) Expect(logs).To(ContainLogWithData("linux-container.start.wshd-start-ended", expectedData)) }) Context("when start.sh fails", func() { nastyError := errors.New("oh no!") JustBeforeEach(func() { fakeRunner.WhenRunning( fake_command_runner.CommandSpec{ Path: containerDir + "/start.sh", }, func(*exec.Cmd) error {
originalMigration := func(otx migration.LimitedTx) error { _, err := otx.Exec(`SELECT 1`) if err != nil { return err } return nil } newMigration := migrations.WithLogger(logger, originalMigration) newMigration(tx) Expect(logger).To(gbytes.Say("starting-migration")) Expect(logger).To(gbytes.Say("finishing-migration")) Expect(logger.Logs()[0].LogLevel).To(Equal(lager.INFO)) Expect(logger.Logs()[1].LogLevel).To(Equal(lager.INFO)) }) It("logs the time that the migration took to apply", func() { tx := new(fakes.FakeLimitedTx) originalMigration := func(otx migration.LimitedTx) error { time.Sleep(1 * time.Millisecond) return nil } newMigration := migrations.WithLogger(logger, originalMigration) newMigration(tx) Expect(logger).To(gbytes.Say("finishing-migration"))