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 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 TestSendingToErrorChannelDoesNotBlock(t *testing.T) { logger := loggertesthelper.Logger() sinkManager := NewSinkManager(1024, false, nil, logger) sinkManager.errorChannel = make(chan *logmessage.Message, 1) go sinkManager.Start() incomingLogChan := make(chan []byte, 10) testMessageRouter := NewMessageRouter(incomingLogChan, testhelpers.UnmarshallerMaker("secret"), sinkManager, 2048, logger) go testMessageRouter.Start() sinkChannel := make(chan *logmessage.Message, 10) ourSink := testSink{sinkChannel, false} sinkManager.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.outgoingLogChan <- badMessage } goodMessage := messagetesthelpers.NewMessage(t, "error msg", "appId") testMessageRouter.outgoingLogChan <- goodMessage select { case _ = <-ourSink.Channel(): case <-time.After(1000 * time.Millisecond): t.Error("Should have received a message") } }
func init() { dataReadChannel = make(chan []byte, 20) TestMessageRouter = NewMessageRouter(1024, loggertesthelper.Logger()) go TestMessageRouter.Start() TestHttpServer = NewHttpServer(TestMessageRouter, 10*time.Millisecond, testhelpers.UnmarshallerMaker(SECRET), loggertesthelper.Logger()) go TestHttpServer.Start(dataReadChannel, "localhost:"+SERVER_PORT) time.Sleep(1 * time.Millisecond) }
func TestSimpleBlacklistRule(t *testing.T) { logger := loggertesthelper.Logger() sinkManager := NewSinkManager(1024, false, []iprange.IPRange{iprange.IPRange{Start: "10.10.123.1", End: "10.10.123.1"}}, 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), true} 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 select { case _ = <-ourSink.Channel(): case <-time.After(100 * time.Millisecond): t.Error("Did not receive real message") } select { case _ = <-ourSink.Channel(): case <-time.After(100 * time.Millisecond): t.Error("Did not message about blacklisted syslog drain") } testMessageRouter.outgoingLogChan <- message select { case _ = <-ourSink.Channel(): case <-time.After(100 * time.Millisecond): t.Error("Did not receive real message") } select { case _ = <-ourSink.Channel(): t.Error("Should not receive another message about the blacklisted url since we cache blacklisted urls") case <-time.After(100 * time.Millisecond): } assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter) message = messagetesthelpers.NewMessage(t, "error msg", "appId") message.GetLogMessage().DrainUrls = []string{"http://10.10.123.2"} testMessageRouter.outgoingLogChan <- message waitForMessageGettingProcessed(t, ourSink, 10*time.Millisecond) assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter+1) }
func init() { logger := gosteno.NewLogger("TestLogger") listener := agentlistener.NewAgentListener("localhost:3456", logger) dataChannel := listener.Start() messageRouter := sinkserver.NewMessageRouter(10, false, nil, logger) go messageRouter.Start() httpServer := sinkserver.NewHttpServer(messageRouter, 30*time.Second, testhelpers.UnmarshallerMaker("secret"), 100, logger) go httpServer.Start(dataChannel, "localhost:8081") time.Sleep(50 * time.Millisecond) }
func init() { dataReadChannel = make(chan []byte, 20) TestMessageRouter = NewMessageRouter(1024, false, nil, loggertesthelper.Logger()) go TestMessageRouter.Start() TestHttpServer = NewHttpServer(TestMessageRouter, 10*time.Millisecond, testhelpers.UnmarshallerMaker(SECRET), 100, loggertesthelper.Logger()) go TestHttpServer.Start(dataReadChannel, "localhost:"+SERVER_PORT) blackListDataReadChannel = make(chan []byte, 20) blacklistTestMessageRouter = NewMessageRouter(1024, false, []iprange.IPRange{iprange.IPRange{Start: "127.0.0.0", End: "127.0.0.2"}}, loggertesthelper.Logger()) go blacklistTestMessageRouter.Start() blackListTestHttpServer = NewHttpServer(blacklistTestMessageRouter, 10*time.Millisecond, testhelpers.UnmarshallerMaker(SECRET), 100, loggertesthelper.Logger()) go blackListTestHttpServer.Start(blackListDataReadChannel, "localhost:"+BLACKLIST_SERVER_PORT) time.Sleep(2 * time.Millisecond) }
func init() { logger := gosteno.NewLogger("TestLogger") listener := agentlistener.NewAgentListener("localhost:3456", logger) incomingLogChan := listener.Start() sinkManager := sinkserver.NewSinkManager(1024, false, nil, logger) go sinkManager.Start() messageRouter := sinkserver.NewMessageRouter(incomingLogChan, testhelpers.UnmarshallerMaker("secret"), sinkManager, 2048, logger) go messageRouter.Start() websocketServer := sinkserver.NewWebsocketServer("localhost:8083", sinkManager, 30*time.Second, 100, logger) go websocketServer.Start() time.Sleep(50 * time.Millisecond) }
func TestErrorMessagesAreNotDeliveredToSinksThatDontAcceptErrors(t *testing.T) { logger := loggertesthelper.Logger() sinkManager := NewSinkManager(1024, false, nil, logger) 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) sinkManager.errorChannel <- messagetesthelpers.NewMessage(t, "error msg", "appId") select { case _ = <-ourSink.Channel(): t.Error("Should not have received a message") case <-time.After(10 * time.Millisecond): break } }
func TestErrorMessagesAreDeliveredToSinksThatSupportThem(t *testing.T) { logger := loggertesthelper.Logger() sinkManager := NewSinkManager(1024, false, nil, logger) 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), true} sinkManager.sinkOpenChan <- ourSink <-time.After(1 * time.Millisecond) sinkManager.errorChannel <- messagetesthelpers.NewMessage(t, "error msg", "appId") select { case errorMsg := <-ourSink.Channel(): assert.Equal(t, string(errorMsg.GetLogMessage().GetMessage()), "error msg") case <-time.After(1 * time.Millisecond): t.Error("Should have received an error message") } }
func TestParseEnvelopesDoesntBlockWhenMessageRouterChannelIsFull(t *testing.T) { logger := gosteno.NewLogger("TestLogger") messageChannelLength := 1 sinkManager := NewSinkManager(1, true, []iprange.IPRange{}, logger) go sinkManager.Start() incomingLogChan := make(chan []byte, 1) messageRouter := NewMessageRouter(incomingLogChan, testhelpers.UnmarshallerMaker("secret"), sinkManager, messageChannelLength, logger) go messageRouter.listenForLogs() testMessage := messagetesthelpers.MarshalledLogEnvelopeForMessage(t, "msg", "appName", "secret") for i := 0; i < 10; i++ { select { case incomingLogChan <- testMessage: break case <-time.After(1 * time.Second): t.Fatal("Shouldn't have blocked") } } }
func TestStopsRetryingWhenSinkIsUnregistered(t *testing.T) { logger := loggertesthelper.Logger() sinkManager := NewSinkManager(1024, false, []iprange.IPRange{iprange.IPRange{Start: "10.10.123.1", End: "10.10.123.1"}}, logger) 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), true} sinkManager.sinkOpenChan <- ourSink runtime.Gosched() go func() { message := messagetesthelpers.NewMessage(t, "error msg", "appId") message.GetLogMessage().DrainUrls = []string{"syslog://localhost:41223"} testMessageRouter.outgoingLogChan <- message newMessage := messagetesthelpers.NewMessage(t, "RemoveSyslogSink", "appId") testMessageRouter.outgoingLogChan <- newMessage }() for { readMessage := <-ourSink.Channel() if string(readMessage.GetLogMessage().GetMessage()) == "RemoveSyslogSink" { break } } select { case message := <-ourSink.Channel(): t.Errorf("Should not receive another message after removal; message was %v", string(message.GetLogMessage().GetMessage())) case <-time.After(2 * time.Second): } }