func TestItDumpsAllMessagesForAnAppUser(t *testing.T) { expectedMessageString := "Some data" logMessage := messagetesthelpers.NewLogMessage(expectedMessageString, "myOtherApp") envelope := messagetesthelpers.MarshalledLogEnvelope(t, logMessage, SECRET) dataReadChannel <- envelope dataReadChannel <- envelope time.Sleep(100 * time.Millisecond) receivedChan := make(chan []byte, 2) _, stopKeepAlive, droppedChannel := testhelpers.AddWSSink(t, receivedChan, SERVER_PORT, RECENT_LOGS_PATH+"?app=myOtherApp") logMessages := dumpAllMessages(receivedChan) assert.Equal(t, len(logMessages), 2) messagetesthelpers.AssertProtoBufferMessageEquals(t, expectedMessageString, logMessages[len(logMessages)-1]) select { case <-droppedChannel: // we should have been dropped case <-time.After(10 * time.Millisecond): t.Error("we should have been dropped") } stopKeepAlive <- true }
func TestThatItSendsLogsToProperAppSink(t *testing.T) { receivedChan := make(chan []byte) otherLogMessage := messagetesthelpers.NewLogMessage("Some other message", "otherApp") otherLogEnvelope := messagetesthelpers.MarshalledLogEnvelope(t, otherLogMessage, SECRET) expectedMessageString := "My important message" logEnvelope := messagetesthelpers.MarshalledLogEnvelopeForMessage(t, expectedMessageString, "myApp02", SECRET) _, stopKeepAlive, _ := testhelpers.AddWSSink(t, receivedChan, SERVER_PORT, TAIL_PATH+"?app=myApp02") WaitForWebsocketRegistration() dataReadChannel <- otherLogEnvelope dataReadChannel <- logEnvelope select { case <-time.After(1 * time.Second): t.Errorf("Did not get message from app sink.") case message := <-receivedChan: messagetesthelpers.AssertProtoBufferMessageEquals(t, expectedMessageString, message) } stopKeepAlive <- true WaitForWebsocketRegistration() }
func TestFromLogEnvelopeProtobuffer(t *testing.T) { logMessage := testhelpers.NewLogMessage("message", "my_app_id") message := testhelpers.MarshalledLogEnvelope(t, logMessage, "secret") appid, err := FromProtobufferMessage(message) assert.NoError(t, err) assert.Equal(t, "my_app_id", appid) }
func TestThatItSendsAllDataToOnlyAuthoritiveMessagesWithDrainUrls(t *testing.T) { client1ReceivedChan := make(chan []byte) client2ReceivedChan := make(chan []byte) fakeSyslogDrain1, err := NewFakeService(client1ReceivedChan, "127.0.0.1:34569") defer fakeSyslogDrain1.Stop() assert.NoError(t, err) go fakeSyslogDrain1.Serve() <-fakeSyslogDrain1.ReadyChan fakeSyslogDrain2, err := NewFakeService(client2ReceivedChan, "127.0.0.1:34540") defer fakeSyslogDrain2.Stop() assert.NoError(t, err) go fakeSyslogDrain2.Serve() <-fakeSyslogDrain2.ReadyChan expectedMessageString := "Some Data" logEnvelope := messagetesthelpers.MarshalledLogEnvelopeForMessage(t, expectedMessageString, "myApp", SECRET, "syslog://localhost:34569") dataReadChannel <- logEnvelope select { case <-time.After(200 * time.Millisecond): t.Errorf("Did not get message 1") case message := <-client1ReceivedChan: assert.Contains(t, string(message), expectedMessageString) } expectedSecondMessageString := "loggregator myApp: loggregator myApp: Some More Data" logMessage2 := messagetesthelpers.NewLogMessage(expectedSecondMessageString, "myApp") sourceName := "DEA" logMessage2.SourceName = &sourceName logMessage2.DrainUrls = []string{"syslog://localhost:34540"} logEnvelope2 := messagetesthelpers.MarshalledLogEnvelope(t, logMessage2, SECRET) dataReadChannel <- logEnvelope2 select { case <-time.After(200 * time.Millisecond): t.Errorf("Did not get message 2") case message := <-client1ReceivedChan: matched, _ := regexp.MatchString(`<14>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}([-+]\d{2}:\d{2}) loggregator myApp DEA - - loggregator myApp: loggregator myApp: Some More Data`, string(message)) assert.True(t, matched, string(message)) case <-client2ReceivedChan: t.Error("Should not have gotten the new message in this drain") } }
func TestEndtoEndEnvelopeToMessage(t *testing.T) { receivedChan := make(chan []byte) ws, _, _ := testhelpers.AddWSSink(t, receivedChan, "8083", "/tail/?app=myApp") defer ws.Close() time.Sleep(50 * time.Millisecond) connection, err := net.Dial("udp", "localhost:3456") expectedMessageString := "Some Data" unmarshalledLogMessage := messagetesthelpers.NewLogMessage(expectedMessageString, "myApp") expectedMessage := messagetesthelpers.MarshalledLogEnvelope(t, unmarshalledLogMessage, "secret") _, err = connection.Write(expectedMessage) assert.NoError(t, err) messagetesthelpers.AssertProtoBufferMessageEquals(t, expectedMessageString, <-receivedChan) }
func TestInvalidEnvelopeEndtoEnd(t *testing.T) { receivedChan := make(chan []byte) ws, _, _ := testhelpers.AddWSSink(t, receivedChan, "8083", "/tail/?app=myApp2") defer ws.Close() time.Sleep(50 * time.Millisecond) connection, err := net.Dial("udp", "localhost:3456") expectedMessageString := "Some Data" unmarshalledLogMessage := messagetesthelpers.NewLogMessage(expectedMessageString, "myApp2") expectedMessage := messagetesthelpers.MarshalledLogEnvelope(t, unmarshalledLogMessage, "invalid") _, err = connection.Write(expectedMessage) assert.NoError(t, err) select { case _ = <-receivedChan: t.Error("Message should have been dropped") case <-time.After(2 * time.Second): //success } }
Ω(receivedMessage.GetSourceId()).Should(Equal("42")) Ω(receivedMessage.GetMessageType()).Should(Equal(logmessage.LogMessage_OUT)) }) It("should emit stderr", func() { emitter.EmitError("appid", "foo") receivedMessage := extractLogMessage(<-received) Ω(receivedMessage.GetMessage()).Should(Equal([]byte("foo"))) Ω(receivedMessage.GetAppId()).Should(Equal("appid")) Ω(receivedMessage.GetSourceId()).Should(Equal("42")) Ω(receivedMessage.GetMessageType()).Should(Equal(logmessage.LogMessage_ERR)) }) It("should emit fully formed log messages", func() { logMessage := testhelpers.NewLogMessage("test_msg", "test_app_id") logMessage.SourceId = proto.String("src_id") emitter.EmitLogMessage(logMessage) receivedMessage := extractLogMessage(<-received) Ω(receivedMessage.GetMessage()).Should(Equal([]byte("test_msg"))) Ω(receivedMessage.GetAppId()).Should(Equal("test_app_id")) Ω(receivedMessage.GetSourceId()).Should(Equal("src_id")) }) It("should truncate long messages", func() { longMessage := strings.Repeat("7", MAX_MESSAGE_BYTE_SIZE*2) logMessage := testhelpers.NewLogMessage(longMessage, "test_app_id") emitter.EmitLogMessage(logMessage)
AfterEach(func(done Done) { if dontKeepAliveChan != nil { close(dontKeepAliveChan) ws.Close() Eventually(receivedChan).Should(BeClosed()) close(done) } }) It("should work from udp socket to websocket client", func() { connection, err := net.Dial("udp", "127.0.0.1:3456") Expect(err).To(BeNil()) expectedMessageString := "Some Data" unmarshalledLogMessage := messagetesthelpers.NewLogMessage(expectedMessageString, "myApp") expectedMessage := MarshalledLogEnvelope(unmarshalledLogMessage, "secret") _, err = connection.Write(expectedMessage) Expect(err).To(BeNil()) receivedMessageString := parseProtoBufMessageString(<-receivedChan) Expect(expectedMessageString).To(Equal(receivedMessageString)) }) It("should drop invalid log envelopes", func() { time.Sleep(50 * time.Millisecond) connection, err := net.Dial("udp", "127.0.0.1:3456") Expect(err).To(BeNil())