func TestLoggregator(t *testing.T) { RegisterFailHandler(Fail) etcdPort := 5500 + (config.GinkgoConfig.ParallelNode-1)*10 etcdRunner := etcdstorerunner.NewETCDClusterRunner(etcdPort, 1) etcdRunner.Start() etcdUrl := fmt.Sprintf("http://localhost:%d", etcdPort) loggregatorConfig := &loggregator.Config{ EtcdUrls: []string{etcdUrl}, EtcdMaxConcurrentRequests: 10, Index: 0, IncomingPort: 3456, OutgoingPort: 8083, LogFilePath: "", MaxRetainedLogMessages: 100, WSMessageBufferSize: 100, SharedSecret: "secret", SkipCertVerify: true, BlackListIps: []iprange.IPRange{}, } cfcomponent.Logger = loggertesthelper.Logger() l := loggregator.New("127.0.0.1", loggregatorConfig, loggertesthelper.Logger()) go l.Start() RunSpecs(t, "Loggregator Suite") l.Stop() etcdRunner.Stop() }
func TestDrainForReturnsOnly(t *testing.T) { groupedSinks := NewGroupedSinks() target := "789" sink1 := sinks.NewSyslogSink(target, "other sink", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message)) sink2 := sinks.NewSyslogSink(target, "sink we are searching for", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message)) groupedSinks.Register(sink1) groupedSinks.Register(sink2) sinkDrain := groupedSinks.DrainFor(target, "sink we are searching for") assert.Equal(t, sink2, sinkDrain) }
func TestDumpForReturnsOnlyDumpsForTheGivenAppId(t *testing.T) { groupedSinks := NewGroupedSinks() target := "789" otherTarget := "790" sink1 := sinks.NewDumpSink(target, 5, loggertesthelper.Logger(), make(chan sinks.Sink, 1), time.Second) sink2 := sinks.NewDumpSink(otherTarget, 5, loggertesthelper.Logger(), make(chan sinks.Sink, 1), time.Second) groupedSinks.Register(sink1) groupedSinks.Register(sink2) appSink := groupedSinks.DumpFor(target) assert.Equal(t, appSink, sink1) }
func TestOutgoingProxyConfigWithEmptyAZ(t *testing.T) { config := &Config{ Loggregators: map[string][]string{ "z1": []string{"10.244.0.14"}, "z2": []string{}, }, } assert.NotPanics(t, func() { makeOutgoingProxy("0.0.0.0", config, loggertesthelper.Logger()) }) hashers := makeHashers(config.Loggregators, 3456, loggertesthelper.Logger()) assert.Equal(t, len(hashers), 1) }
func TestDumpForReturnsOnyDumps(t *testing.T) { groupedSinks := NewGroupedSinks() target := "789" sink1 := sinks.NewSyslogSink(target, "url1", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message)) sink2 := sinks.NewSyslogSink(target, "url2", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message)) sink3 := sinks.NewDumpSink(target, 5, loggertesthelper.Logger(), make(chan sinks.Sink, 1), time.Second) groupedSinks.Register(sink1) groupedSinks.Register(sink2) groupedSinks.Register(sink3) appSink := groupedSinks.DumpFor(target) assert.Equal(t, appSink, sink3) }
func TestDumpForReturnsOnyDumps(t *testing.T) { groupedSinks := NewGroupedSinks() target := "789" sink1 := sinks.NewSyslogSink(target, "url1", loggertesthelper.Logger(), syslogWriter) sink2 := sinks.NewSyslogSink(target, "url2", loggertesthelper.Logger(), syslogWriter) sink3 := sinks.NewDumpSink(target, 5, loggertesthelper.Logger()) groupedSinks.Register(sink1) groupedSinks.Register(sink2) groupedSinks.Register(sink3) appSink := groupedSinks.DumpFor(target) assert.Equal(t, appSink, sink3) }
func TestThatItDoesNotCreateAnotherSyslogDrainIfItIsAlreadyThere(t *testing.T) { logger := loggertesthelper.Logger() sinkManager := NewSinkManager(1024, false, nil, logger) oldActiveSyslogSinksCounter := sinkManager.Metrics.SyslogSinks go sinkManager.Start() incomingLogChan := make(chan []byte, 10) testMessageRouter := NewMessageRouter(incomingLogChan, testhelpers.UnmarshallerMaker("secret"), sinkManager, 2048, logger) go testMessageRouter.Start() ourSink := testSink{make(chan *logmessage.Message, 100), false} sinkManager.sinkOpenChan <- ourSink <-time.After(1 * time.Millisecond) message := messagetesthelpers.NewMessage(t, "error msg", "appId") message.GetLogMessage().DrainUrls = []string{"http://10.10.123.1"} testMessageRouter.outgoingLogChan <- message waitForMessageGettingProcessed(t, ourSink, 10*time.Millisecond) assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter+1) testMessageRouter.outgoingLogChan <- message waitForMessageGettingProcessed(t, ourSink, 10*time.Millisecond) assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter+1) }
func TestDumpWithLotsOfMessagesAndLargeBuffer(t *testing.T) { dump := NewDumpSink("myApp", 200, loggertesthelper.Logger(), make(chan Sink, 1), time.Second) go dump.Run() for i := 0; i < 1000; i++ { logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId") dump.Channel() <- logMessage } runtime.Gosched() logMessages := dump.Dump() assert.Equal(t, len(logMessages), 200) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "800") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "801") for i := 1000; i < 2000; i++ { logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId") dump.Channel() <- logMessage } runtime.Gosched() logMessages = dump.Dump() assert.Equal(t, len(logMessages), 200) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801") logMessages = dump.Dump() assert.Equal(t, len(logMessages), 200) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801") }
func TestThatARemovedTaskWillBeRemoved(t *testing.T) { writeToFile(t, `{"instances": [{"state": "RUNNING", "instance_index": 123}]}`, true) agent := &agent{filePath(), loggertesthelper.Logger()} tasksChan := agent.watchInstancesJsonFileForChanges() inst, ok := <-tasksChan assert.True(t, ok, "Channel is closed") assert.NotNil(t, inst) os.Remove(filePath()) select { case inst = <-tasksChan: t.Errorf("We just got an old task: %v", inst) case <-time.After(2 * time.Millisecond): // OK } writeToFile(t, `{"instances": [{"state": "RUNNING", "instance_index": 1234}]}`, true) inst, ok = <-tasksChan assert.True(t, ok, "Channel is closed") expectedInst := task{index: 1234, sourceName: "App"} assert.Equal(t, expectedInst, inst) }
func TestProxyingWithHashingBetweenServers(t *testing.T) { go Server("localhost:62024", "Hello World from the server 1", 1) go Server("localhost:62025", "Hello World from the server 2", 1) proxy := NewProxy( "localhost:62026", []*hasher.Hasher{hasher.NewHasher([]string{"localhost:62024", "localhost:62025"})}, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62024", "/") receivedChan := Client(t, "62026", "/?app=0") select { case data := <-receivedChan: assert.Equal(t, string(data), "Hello World from the server 1") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } receivedChan = Client(t, "62026", "/?app=1") select { case data := <-receivedChan: assert.Equal(t, string(data), "Hello World from the server 2") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } }
func TestProxyWithoutAuthorization(t *testing.T) { proxy := NewProxy( "localhost:62061", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() time.Sleep(time.Millisecond * 50) config, err := websocket.NewConfig("ws://localhost:62061/?app=myApp", "http://localhost") assert.NoError(t, err) receivedChan := ClientWithAuth(t, "62061", "/?app=myApp", config) select { case data := <-receivedChan: messagetesthelpers.AssertProtoBufferMessageEquals(t, "Error: Authorization not provided", data) case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } _, stillOpen := <-receivedChan assert.False(t, stillOpen) }
func TestThatWeListenToStdErrUnixSocket(t *testing.T) { tmpdir, err := ioutil.TempDir("", "testing") assert.NoError(t, err) defer os.RemoveAll(tmpdir) instance := &instance{ applicationId: "1234", wardenJobId: 56, wardenContainerPath: tmpdir, index: 4, drainUrls: []string{"syslog://10.20.30.40:8050"}} os.MkdirAll(instance.identifier(), 0777) stdoutSocketPath := filepath.Join(instance.identifier(), "stdout.sock") stderrSocketPath := filepath.Join(instance.identifier(), "stderr.sock") os.Remove(stdoutSocketPath) os.Remove(stderrSocketPath) stdoutListener, err := net.Listen("unix", stdoutSocketPath) defer stdoutListener.Close() assert.NoError(t, err) stderrListener, err := net.Listen("unix", stderrSocketPath) defer stderrListener.Close() assert.NoError(t, err) expectedMessage := "Some Output\n" secondLogMessage := "toally different\n" mockLoggregatorEmitter := new(MockLoggregatorEmitter) mockLoggregatorEmitter.received = make(chan *logmessage.LogMessage) instance.startListening(mockLoggregatorEmitter, loggertesthelper.Logger()) connection, err := stderrListener.Accept() defer connection.Close() assert.NoError(t, err) _, err = connection.Write([]byte(expectedMessage)) assert.NoError(t, err) receivedMessage := <-mockLoggregatorEmitter.received assert.Equal(t, "1234", receivedMessage.GetAppId()) assert.Equal(t, logmessage.LogMessage_WARDEN_CONTAINER, receivedMessage.GetSourceType()) assert.Equal(t, logmessage.LogMessage_ERR, receivedMessage.GetMessageType()) assert.Equal(t, expectedMessage, string(receivedMessage.GetMessage())) assert.Equal(t, []string{"syslog://10.20.30.40:8050"}, receivedMessage.GetDrainUrls()) assert.Equal(t, "4", receivedMessage.GetSourceId()) _, err = connection.Write([]byte(secondLogMessage)) assert.NoError(t, err) receivedMessage = <-mockLoggregatorEmitter.received assert.Equal(t, "1234", receivedMessage.GetAppId()) assert.Equal(t, logmessage.LogMessage_WARDEN_CONTAINER, receivedMessage.GetSourceType()) assert.Equal(t, logmessage.LogMessage_ERR, receivedMessage.GetMessageType()) assert.Equal(t, secondLogMessage, string(receivedMessage.GetMessage())) assert.Equal(t, []string{"syslog://10.20.30.40:8050"}, receivedMessage.GetDrainUrls()) assert.Equal(t, "4", receivedMessage.GetSourceId()) }
func TestSysLoggerComesUpLate(t *testing.T) { sysLogger := NewSyslogWriterRecorder() sysLogger.SetUp(false) sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger) done := make(chan bool) go func() { sink.Run() done <- true }() for i := 0; i < 15; i++ { msg := fmt.Sprintf("message no %v", i) logMessage := messagetesthelpers.NewMessage(t, msg, "appId") sink.Channel() <- logMessage } close(sink.Channel()) sysLogger.SetUp(true) <-done data := sysLogger.ReceivedMessages() assert.Equal(t, len(data), 10) for i := 0; i < 10; i++ { msg := fmt.Sprintf("out: message no %v", i+5) assert.Equal(t, data[i], msg) } }
func TestDelete(t *testing.T) { groupedSinks := NewGroupedSinks() target := "789" sink1 := sinks.NewSyslogSink(target, "url1", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message)) sink2 := sinks.NewSyslogSink(target, "url2", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message)) groupedSinks.Register(sink1) groupedSinks.Register(sink2) groupedSinks.Delete(sink1) appSinks := groupedSinks.For(target) assert.Equal(t, len(appSinks), 1) assert.Equal(t, appSinks[0], sink2) }
func TestDumpReturnsAllRecentMessagesToMultipleDumpRequests(t *testing.T) { testDump := dump.NewDumpSink("myApp", 2, loggertesthelper.Logger(), time.Second) dumpRunnerDone := make(chan struct{}) inputChan := make(chan *logmessage.Message) go func() { testDump.Run(inputChan) close(dumpRunnerDone) }() logMessage := messagetesthelpers.NewMessage(t, "1", "appId") inputChan <- logMessage logMessage = messagetesthelpers.NewMessage(t, "2", "appId") inputChan <- logMessage logMessage = messagetesthelpers.NewMessage(t, "3", "appId") inputChan <- logMessage close(inputChan) <-dumpRunnerDone logMessages := testDump.Dump() assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3") logMessages = testDump.Dump() assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3") }
func TestProxyingWithMultipleAZs(t *testing.T) { go Server("localhost:62027", "Hello World from the server 1 - AZ1", 1) go Server("localhost:62028", "Hello World from the server 2 - AZ1", 1) go Server("localhost:62029", "Hello World from the server 1 - AZ2", 1) go Server("localhost:62030", "Hello World from the server 2 - AZ2", 1) proxy := NewProxy( "localhost:62031", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62027", "localhost:62028"}), hasher.NewHasher([]string{"localhost:62029", "localhost:62030"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62027", "/") receivedChan := Client(t, "62031", "/?app=0") messages := "" for message := range receivedChan { messages = messages + string(message) } assert.Contains(t, messages, "Hello World from the server 1 - AZ1") assert.Contains(t, messages, "Hello World from the server 1 - AZ2") receivedChan = Client(t, "62031", "/?app=1") messages = "" for message := range receivedChan { messages = messages + string(message) } assert.Contains(t, messages, "Hello World from the server 2 - AZ1") assert.Contains(t, messages, "Hello World from the server 2 - AZ2") }
func TestKeepAliveWithMultipleAZs(t *testing.T) { keepAliveChan1 := make(chan []byte) keepAliveChan2 := make(chan []byte) go KeepAliveServer("localhost:62032", keepAliveChan1) go KeepAliveServer("localhost:62033", keepAliveChan2) proxy := NewProxy( "localhost:62034", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), hasher.NewHasher([]string{"localhost:62033"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62032", "/") KeepAliveClient(t, "62034", "/?app=myServerApp1") select { case data := <-keepAliveChan1: assert.Equal(t, string(data), "keep alive") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } select { case data := <-keepAliveChan2: assert.Equal(t, string(data), "keep alive") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } }
func TestDumpWithLotsOfMessages(t *testing.T) { dump := NewDumpSink("myApp", 2, loggertesthelper.Logger()) for i := 0; i < 100; i++ { logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId") dump.Channel() <- logMessage } runtime.Gosched() logMessages := dumpAllMessages(dump) assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "98") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "99") for i := 100; i < 200; i++ { logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId") dump.Channel() <- logMessage } runtime.Gosched() logMessages = dumpAllMessages(dump) assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199") logMessages = dumpAllMessages(dump) assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199") }
func TestProxyWhenAuthorizationFailsThroughQueryParams(t *testing.T) { proxy := NewProxy( "localhost:62062", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() time.Sleep(time.Millisecond * 50) config, err := websocket.NewConfig("ws://localhost:62061/?app=myApp&authorization="+url.QueryEscape(testhelpers.INVALID_AUTHENTICATION_TOKEN), "http://localhost") assert.NoError(t, err) receivedChan := ClientWithAuth(t, "62062", "/?app=myApp", config) select { case data := <-receivedChan: messagetesthelpers.AssertProtoBufferMessageEquals(t, "Error: Invalid authorization", data) case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } _, stillOpen := <-receivedChan assert.False(t, stillOpen) }
func TestDumpWithLotsOfDumps(t *testing.T) { runtime.GOMAXPROCS(runtime.NumCPU()) dump := NewDumpSink("myApp", 5, loggertesthelper.Logger(), time.Second, fakeTimeProvider) dumpRunnerDone := make(chan struct{}) inputChan := make(chan *logmessage.Message) go func() { dump.Run(inputChan) close(dumpRunnerDone) }() for i := 0; i < 10; i++ { logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId") inputChan <- logMessage } close(inputChan) <-dumpRunnerDone for i := 0; i < 200; i++ { go func() { logMessages := dump.Dump() assert.Equal(t, len(logMessages), 5) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "5") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "6") }() } }
func TestThatWeListenToStdErrUnixSocket(t *testing.T) { task, tmpdir := setupTask(t, 4) defer os.RemoveAll(tmpdir) stdoutListener, stderrListener := setupSockets(t, task) defer stdoutListener.Close() defer stderrListener.Close() expectedMessage := "Some Output\n" secondLogMessage := "toally different\n" receiveChannel := setupEmitter(t, task, loggertesthelper.Logger()) connection, err := stderrListener.Accept() defer connection.Close() assert.NoError(t, err) _, err = connection.Write([]byte(SOCKET_PREFIX + expectedMessage)) assert.NoError(t, err) receivedMessage := <-receiveChannel assert.Equal(t, expectedMessage, string(receivedMessage.GetMessage())) _, err = connection.Write([]byte(secondLogMessage)) assert.NoError(t, err) receivedMessage = <-receiveChannel assert.Equal(t, secondLogMessage, string(receivedMessage.GetMessage())) }
func xTestDumpSinkClosingTimeIsResetWhenAMessageArrives(t *testing.T) { dump := NewDumpSink("myApp", 5, loggertesthelper.Logger(), 10*time.Millisecond, fakeTimeProvider) dumpRunnerDone := make(chan struct{}) inputChan := make(chan *logmessage.Message) go func() { dump.Run(inputChan) close(dumpRunnerDone) }() fakeTimeProvider.IncrementBySeconds(uint64(5)) logMessage := messagetesthelpers.NewMessage(t, "0", "appId") inputChan <- logMessage fakeTimeProvider.IncrementBySeconds(uint64(8)) fakeTimeProvider.IncrementBySeconds(uint64(3)) select { case sink := <-dumpRunnerDone: assert.Equal(t, sink, dump) case <-time.After(5 * time.Millisecond): assert.Fail(t, "Should have closed") } }
func TestSendingToErrorChannelDoesNotBlock(t *testing.T) { testMessageRouter := NewMessageRouter(1024, false, nil, loggertesthelper.Logger()) testMessageRouter.errorChannel = make(chan *logmessage.Message, 1) go testMessageRouter.Start() sinkChannel := make(chan *logmessage.Message, 10) ourSink := testSink{sinkChannel, false} testMessageRouter.sinkOpenChan <- ourSink <-time.After(1 * time.Millisecond) for i := 0; i < 10; i++ { badMessage := messagetesthelpers.NewMessage(t, "error msg", "appIdWeDontCareAbout") badMessage.GetLogMessage().DrainUrls = []string{fmt.Sprintf("<nil%d>", i)} testMessageRouter.parsedMessageChan <- badMessage } goodMessage := messagetesthelpers.NewMessage(t, "error msg", "appId") testMessageRouter.parsedMessageChan <- goodMessage select { case _ = <-ourSink.Channel(): case <-time.After(1000 * time.Millisecond): t.Error("Should have received a message") } }
func TestDumpAlwaysReturnsTheNewestMessages(t *testing.T) { dump := NewDumpSink("myApp", 2, loggertesthelper.Logger(), time.Second, fakeTimeProvider) dumpRunnerDone := make(chan struct{}) inputChan := make(chan *logmessage.Message) go func() { dump.Run(inputChan) close(dumpRunnerDone) }() logMessage := messagetesthelpers.NewMessage(t, "1", "appId") inputChan <- logMessage logMessage = messagetesthelpers.NewMessage(t, "2", "appId") inputChan <- logMessage logMessage = messagetesthelpers.NewMessage(t, "3", "appId") inputChan <- logMessage close(inputChan) <-dumpRunnerDone logMessages := dump.Dump() assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3") }
func TestThatThereIsNoLeakingGoRoutine(t *testing.T) { logger := loggertesthelper.Logger() server := startHTTPServer() defer server.Close() authorizer := NewLogAccessAuthorizer(server.URL, true) authorizer("bearer something", "myAppId", logger) time.Sleep(10 * time.Millisecond) var buf bytes.Buffer goRoutineProfiles := pprof.Lookup("goroutine") goRoutineProfiles.WriteTo(&buf, 2) match, err := regexp.Match("readLoop", buf.Bytes()) if err != nil { t.Error("Unable to match /readLoop/ regexp against goRoutineProfile") goRoutineProfiles.WriteTo(os.Stdout, 2) } if match { t.Error("We are leaking readLoop goroutines.") } match, err = regexp.Match("writeLoop", buf.Bytes()) if err != nil { t.Error("Unable to match /writeLoop/ regexp against goRoutineProfile") } if match { t.Error("We are leaking writeLoop goroutines.") goRoutineProfiles.WriteTo(os.Stdout, 2) } }
func TestSysLoggerComesUpLate(t *testing.T) { sysLogger := NewSyslogWriterRecorder() sysLogger.SetUp(false) sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger, make(chan *logmessage.Message, 10)) done := make(chan bool) go func() { sink.Run() done <- true }() for i := 0; i < 105; i++ { msg := fmt.Sprintf("message no %v", i) logMessage := messagetesthelpers.NewMessage(t, msg, "appId") sink.Channel() <- logMessage } close(sink.Channel()) sysLogger.SetUp(true) <-done data := sysLogger.ReceivedMessages() assert.Equal(t, len(data), 6) msg := "err: Log message output too high. We've dropped 100 messages" assert.Equal(t, data[0], msg) for i := 0; i < 5; i++ { msg := fmt.Sprintf("out: message no %v", i+100) assert.Equal(t, data[i+1], msg) } }
func TestDumpReturnsAllRecentMessagesToMultipleDumpRequestsWithMessagesCloningInInTheMeantime(t *testing.T) { dump := NewDumpSink("myApp", 2, loggertesthelper.Logger(), make(chan Sink, 1), time.Second) go dump.Run() logMessage := messagetesthelpers.NewMessage(t, "1", "appId") dump.Channel() <- logMessage logMessage = messagetesthelpers.NewMessage(t, "2", "appId") dump.Channel() <- logMessage logMessage = messagetesthelpers.NewMessage(t, "3", "appId") dump.Channel() <- logMessage runtime.Gosched() logMessages := dump.Dump() assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3") logMessage = messagetesthelpers.NewMessage(t, "4", "appId") dump.Channel() <- logMessage runtime.Gosched() logMessages = dump.Dump() assert.Equal(t, len(logMessages), 2) assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "3") assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "4") }
func TestSinkReportsErrorsOnTheErrorChannel(t *testing.T) { sysLogger := NewSyslogWriterRecorder() sysLogger.SetUp(false) errorChannel := make(chan *logmessage.Message, 10) sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger, errorChannel) done := make(chan bool) go func() { sink.Run() done <- true }() msg := fmt.Sprintf("first message") logMessage := messagetesthelpers.NewMessage(t, msg, "appId") sink.Channel() <- logMessage select { case errorLog := <-errorChannel: errorMsg := string(errorLog.GetLogMessage().GetMessage()) assert.Contains(t, errorMsg, "Syslog Sink url_not_used: Error when dialing out. Backing off for") assert.Contains(t, errorMsg, "Err: Error connecting.") assert.Equal(t, errorLog.GetLogMessage().GetSourceName(), "LGR") case <-time.After(10 * time.Millisecond): t.Error("Should have received an error message by now") } }
func init() { dataReadChannel = make(chan []byte) logger := loggertesthelper.Logger() sinkManager = NewSinkManager(1024, false, nil, logger) go sinkManager.Start() TestMessageRouter = NewMessageRouter(dataReadChannel, testhelpers.UnmarshallerMaker(SECRET), sinkManager, 2048, logger) go TestMessageRouter.Start() apiEndpoint := "localhost:" + SERVER_PORT TestWebsocketServer = NewWebsocketServer(apiEndpoint, sinkManager, 10*time.Millisecond, 100, loggertesthelper.Logger()) go TestWebsocketServer.Start() blackListDataReadChannel = make(chan []byte) blacklistSinkManager := NewSinkManager(1024, false, []iprange.IPRange{iprange.IPRange{Start: "127.0.0.0", End: "127.0.0.2"}}, logger) go blacklistSinkManager.Start() blacklistTestMessageRouter := NewMessageRouter(blackListDataReadChannel, testhelpers.UnmarshallerMaker(SECRET), blacklistSinkManager, 2048, logger) go blacklistTestMessageRouter.Start() blacklistApiEndpoint := "localhost:" + BLACKLIST_SERVER_PORT blackListTestWebsocketServer = NewWebsocketServer(blacklistApiEndpoint, blacklistSinkManager, 10*time.Millisecond, 100, loggertesthelper.Logger()) go blackListTestWebsocketServer.Start() time.Sleep(2 * time.Millisecond) }
func TestTLSConnectionRejectsSelfSignedCertsByDefault(t *testing.T) { startTLSSyslogServer(loggertesthelper.Logger()) w := NewSyslogWriter("syslog-tls", "localhost:9999", "appId", false) err := w.Connect() assert.Error(t, err) }