func TestAddingForAnotherApp(t *testing.T) {
	store := NewMessageStore(2)

	appId := "myApp"
	message, err := logmessage.ParseMessage(testhelpers.MarshalledLogMessage(t, "Message", appId))
	assert.NoError(t, err)
	store.Add(message, appId)

	anotherAppId := "anotherApp"
	anotherAppMessage, err := logmessage.ParseMessage(testhelpers.MarshalledLogMessage(t, "AnotherAppMessage", anotherAppId))
	assert.NoError(t, err)
	store.Add(anotherAppMessage, anotherAppId)

	messages, err := testhelpers.ParseDumpedMessages(store.DumpFor(appId))
	assert.NoError(t, err)

	assert.Equal(t, len(messages), 1)
	assert.Equal(t, message.GetRawMessage(), messages[0])

	messages, err = testhelpers.ParseDumpedMessages(store.DumpFor(anotherAppId))
	assert.NoError(t, err)

	assert.Equal(t, len(messages), 1)
	assert.Equal(t, anotherAppMessage.GetRawMessage(), messages[0])
}
Example #2
0
func logMessageWithTime(t *testing.T, messageString string, timestamp int64) *logmessage.Message {
	data, err := proto.Marshal(generateMessage(messageString, timestamp))
	assert.NoError(t, err)
	message, err := logmessage.ParseMessage(data)
	assert.NoError(t, err)

	return message
}
Example #3
0
func logMessageWithTime(messageString string, timestamp int) *logmessage.Message {
	data, err := proto.Marshal(generateMessage(messageString, int64(timestamp)))
	Expect(err).NotTo(HaveOccurred())
	message, err := logmessage.ParseMessage(data)
	Expect(err).NotTo(HaveOccurred())

	return message
}
func TestThatItWorksLikeAChannel(t *testing.T) {
	inMessageChan := make(chan *logmessage.Message)
	outMessageChan := make(chan *logmessage.Message, 2)
	go OverwritingMessageChannel(inMessageChan, outMessageChan, nil)

	logMessage1, err := logmessage.ParseMessage(testhelpers.MarshalledLogMessage(t, "message 1", "appId"))
	assert.NoError(t, err)
	inMessageChan <- logMessage1
	readMessage := <-outMessageChan
	assert.Contains(t, string(readMessage.GetRawMessage()), "message 1")

	logMessage2, err := logmessage.ParseMessage(testhelpers.MarshalledLogMessage(t, "message 2", "appId"))
	assert.NoError(t, err)
	inMessageChan <- logMessage2
	readMessage2 := <-outMessageChan
	assert.Contains(t, string(readMessage2.GetRawMessage()), "message 2")

}
Example #5
0
func TestRecentLogsFor(t *testing.T) {
	// out of order messages we will send
	messagesSent := [][]byte{
		marshalledLogMessageWithTime(t, "My message", int64(3000)),
	}

	websocketEndpoint := func(conn *websocket.Conn) {
		request := conn.Request()
		assert.Equal(t, request.URL.Path, "/dump/")
		assert.Equal(t, request.URL.RawQuery, "app=my-app-guid")
		assert.Equal(t, request.Method, "GET")
		assert.Contains(t, request.Header.Get("Authorization"), "BEARER my_access_token")

		for _, msg := range messagesSent {
			conn.Write(msg)
		}
		time.Sleep(time.Duration(2) * time.Second)
		conn.Close()
	}
	websocketServer := httptest.NewTLSServer(websocket.Handler(websocketEndpoint))
	defer websocketServer.Close()

	expectedMessage, err := logmessage.ParseMessage(messagesSent[0])
	assert.NoError(t, err)

	app := cf.Application{Name: "my-app", Guid: "my-app-guid"}
	config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: "https://localhost"}

	endpointRepo := &testapi.FakeEndpointRepo{GetEndpointEndpoints: map[cf.EndpointType]string{
		cf.LoggregatorEndpointKey: strings.Replace(websocketServer.URL, "https", "wss", 1),
	}}

	logsRepo := NewLoggregatorLogsRepository(config, endpointRepo)

	connected := false
	onConnect := func() {
		connected = true
	}

	logChan := make(chan *logmessage.Message, 1000)
	err = logsRepo.RecentLogsFor(app, onConnect, logChan)

	// ordered messages we expect to receive
	dumpedMessages := []*logmessage.Message{}
	for msg := range logChan {
		dumpedMessages = append(dumpedMessages, msg)
	}

	assert.NoError(t, err)

	assert.Equal(t, len(dumpedMessages), 1)
	assert.Equal(t, dumpedMessages[0].GetShortSourceTypeName(), expectedMessage.GetShortSourceTypeName())
	assert.Equal(t, dumpedMessages[0].GetLogMessage().GetMessage(), expectedMessage.GetLogMessage().GetMessage())
	assert.Equal(t, dumpedMessages[0].GetLogMessage().GetMessageType(), expectedMessage.GetLogMessage().GetMessageType())
}
Example #6
0
func (sinkServer *sinkServer) parseMessages(incomingProtobufChan <-chan []byte) {
	for {
		data := <-incomingProtobufChan
		message, err := logmessage.ParseMessage(data)
		if err != nil {
			sinkServer.logger.Error(fmt.Sprintf("Log message could not be unmarshaled. Dropping it... Error: %v. Data: %v", err, data))
			continue
		}
		sinkServer.parsedMessageChan <- message
	}
}
Example #7
0
func createMessage(t *testing.T, protoMsg *logmessage.LogMessage, sourceName *string, msgType *logmessage.LogMessage_MessageType) (msg *logmessage.Message) {
	protoMsg.SourceName = sourceName
	protoMsg.MessageType = msgType

	data, err := proto.Marshal(protoMsg)
	assert.NoError(t, err)

	msg, err = logmessage.ParseMessage(data)
	assert.NoError(t, err)

	return
}
Example #8
0
func createMessage(protoMsg *logmessage.LogMessage, sourceName *string, msgType *logmessage.LogMessage_MessageType) (msg *logmessage.Message) {
	protoMsg.SourceName = sourceName
	protoMsg.MessageType = msgType

	data, err := proto.Marshal(protoMsg)
	Expect(err).NotTo(HaveOccurred())

	msg, err = logmessage.ParseMessage(data)
	Expect(err).NotTo(HaveOccurred())

	return
}
func TestThatItSendsStdErrAsErr(t *testing.T) {
	sink, err := NewSyslogSink("appId", "syslog://localhost:24631", testhelpers.Logger())
	assert.NoError(t, err)
	closeChan := make(chan Sink)
	go sink.Run(closeChan)
	logMessage, err := logmessage.ParseMessage(testhelpers.MarshalledErrorLogMessage(t, "err", "appId"))
	assert.NoError(t, err)
	sink.Channel() <- logMessage
	data := <-dataReadChannel
	assert.Contains(t, string(data), "<3>")
	assert.Contains(t, string(data), "appId")
	assert.Contains(t, string(data), "err")
}
// This test exists because the ring buffer will dump messages
// that actually exist.
func TestOnlyDumpsMessagesThatHaveALength(t *testing.T) {
	store := NewMessageStore(2)

	target := "appId"
	message, err := logmessage.ParseMessage(testhelpers.MarshalledLogMessage(t, "Hello world", target))
	assert.NoError(t, err)
	store.Add(message, target)

	messages, err := testhelpers.ParseDumpedMessages(store.DumpFor(target))
	assert.NoError(t, err)

	assert.Equal(t, len(messages), 1)
	assert.Equal(t, messages[0], message.GetRawMessage())
}
Example #11
0
File: helpers.go Project: nsnt/cli
func NewLogMessage(msgText, appGuid, sourceName string, timestamp time.Time) (msg *logmessage.Message) {
	messageType := logmessage.LogMessage_ERR

	logMsg := logmessage.LogMessage{
		Message:     []byte(msgText),
		AppId:       proto.String(appGuid),
		MessageType: &messageType,
		SourceName:  proto.String(sourceName),
		Timestamp:   proto.Int64(timestamp.UnixNano()),
	}
	data, _ := proto.Marshal(&logMsg)
	msg, _ = logmessage.ParseMessage(data)

	return
}
Example #12
0
func (repo LoggregatorLogsRepository) listenForMessages(ws *websocket.Conn, msgChan chan<- *logmessage.Message) {
	for {
		var data []byte
		err := websocket.Message.Receive(ws, &data)
		if err != nil {
			break
		}

		msg, msgErr := logmessage.ParseMessage(data)
		if msgErr != nil {
			continue
		}
		msgChan <- msg
	}
}
Example #13
0
func TestThatItSendsStdErrAsErr(t *testing.T) {
	sysLogger := NewSyslogWriter("syslog", "localhost:24632", "appId", false)
	sink := NewSyslogSink("appId", "syslog://localhost:24632", loggertesthelper.Logger(), sysLogger, make(chan *logmessage.Message))
	go sink.Run()
	defer close(sink.Channel())

	logMessage, err := logmessage.ParseMessage(messagetesthelpers.MarshalledErrorLogMessage(t, "err", "appId"))
	assert.NoError(t, err)

	sink.Channel() <- logMessage
	data := <-fakeSyslogServer2.dataReadChannel

	assert.Contains(t, string(data), "<11>1")
	assert.Contains(t, string(data), "appId")
	assert.Contains(t, string(data), "err")
}
Example #14
0
func (l *FakeLogsRepository) logsFor(app cf.Application, logMessages []logmessage.LogMessage, onConnect func(), logChan chan *logmessage.Message, stopLoggingChan chan bool) {
	l.AppLogged = app
	onConnect()
	for _, logMsg := range logMessages {
		data, _ := proto.Marshal(&logMsg)
		msg, _ := logmessage.ParseMessage(data)
		logChan <- msg
	}

	close(logChan)

	go func() {
		l.TailLogStopCalled = <-stopLoggingChan
	}()

	return
}
func TestRegisterAndFor(t *testing.T) {
	store := NewMessageStore(2)

	appId := "myApp"
	appMessageString := "AppMessage"
	appMessage := testhelpers.MarshalledLogMessage(t, appMessageString, "myApp")
	message, err := logmessage.ParseMessage(appMessage)

	store.Add(message, appId)

	messages, err := testhelpers.ParseDumpedMessages(store.DumpFor(appId))
	assert.NoError(t, err)

	assert.Equal(t, len(messages), 1)

	testhelpers.AssertProtoBufferMessageEquals(t, appMessageString, messages[0])
}
Example #16
0
func logMessageWithTime(t *testing.T, messageString string, timestamp int64) *logmessage.Message {
	messageType := logmessage.LogMessage_OUT
	sourceType := logmessage.LogMessage_DEA
	logMessage := &logmessage.LogMessage{
		Message:     []byte(messageString),
		AppId:       proto.String("my-app-guid"),
		MessageType: &messageType,
		SourceType:  &sourceType,
		Timestamp:   proto.Int64(timestamp),
	}

	data, err := proto.Marshal(logMessage)
	assert.NoError(t, err)
	message, err := logmessage.ParseMessage(data)
	assert.NoError(t, err)

	return message
}
Example #17
0
func (cnsmr *consumer) listenForMessages(msgChan chan<- *logmessage.LogMessage) error {
	defer cnsmr.ws.Close()

	for {
		var data []byte

		_, data, err := cnsmr.ws.ReadMessage()
		if err != nil {
			return err
		}

		msg, msgErr := logmessage.ParseMessage(data)
		if msgErr != nil {
			continue
		}

		msgChan <- msg.GetLogMessage()
	}
}
Example #18
0
func TestRecentLogsFor(t *testing.T) {
	expectedMessage := messagetesthelpers.MarshalledLogMessage(t, "My message", "my-app-id")
	message, err := logmessage.ParseMessage(expectedMessage)
	assert.NoError(t, err)

	ts := httptest.NewTLSServer(http.HandlerFunc(recentLogsEndpoint(message)))
	defer ts.Close()

	app := cf.Application{Name: "my-app", Guid: "my-app-guid"}
	config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: ts.URL}
	gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{})
	loggregatorHostResolver := func(hostname string) string { return hostname }
	logsRepo := NewLoggregatorLogsRepository(config, gateway, loggregatorHostResolver)

	logs, err := logsRepo.RecentLogsFor(app)
	assert.NoError(t, err)

	assert.Equal(t, len(logs), 1)
	actualMessage, err := proto.Marshal(logs[0])
	assert.NoError(t, err)
	assert.Equal(t, actualMessage, expectedMessage)
}
		})

		Context("with a read timeout set", func() {
			It("does not wait forever", func(done Done) {
				err := l.Start(fmt.Sprintf("ws://%s", ts.Listener.Addr()), "myApp", outputChan, stopChan)
				Expect(err).To(HaveOccurred())

				close(done)
			})

			It("sends an error message to the channel", func(done Done) {
				l.Start(fmt.Sprintf("ws://%s", ts.Listener.Addr()), "myApp", outputChan, stopChan)
				var msgData []byte
				Eventually(outputChan).Should(Receive(&msgData))

				msg, _ := logmessage.ParseMessage(msgData)
				Expect(msg.GetLogMessage().GetSourceName()).To(Equal("LGR"))
				Expect(string(msg.GetLogMessage().GetMessage())).To(Equal("WebsocketListener.Start: Timed out listening to a doppler server after 500ms"))
				close(done)
			})
		})

		Context("without a read timeout", func() {
			It("waits for messages to come in", func() {
				converter := func(d []byte) ([]byte, error) { return d, nil }
				l = listener.NewWebsocket(marshaller.LoggregatorLogMessage, converter, 0, handshakeTimeout, mockBatcher, loggertesthelper.Logger())

				go l.Start(fmt.Sprintf("ws://%s", ts.Listener.Addr()), "myApp", outputChan, stopChan)

				go func() {
					time.Sleep(750 * time.Millisecond)
Example #20
0
func logMessageForBenchmark(b *testing.B, messageString string, timestamp int64) *logmessage.Message {
	data, _ := proto.Marshal(generateMessage(messageString, timestamp))
	message, _ := logmessage.ParseMessage(data)
	return message
}
Example #21
0
func parseMessage(msgBytes []byte) (msg *logmessage.Message) {
	msg, err := logmessage.ParseMessage(msgBytes)
	Expect(err).ToNot(HaveOccurred())
	return
}