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 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)
}
Exemplo n.º 3
0
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 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)
}
Exemplo n.º 5
0
func (sinkServer *sinkServer) registerDrainUrls(appId string, drainUrls []string) {
	if len(drainUrls) == 0 {
		for _, sink := range sinkServer.drainUrlsForApps[appId] {
			sinkServer.activeSinksChans.Delete(sink.Channel())
			close(sink.Channel())
		}
		delete(sinkServer.drainUrlsForApps, appId)
		return
	}
	if sinkServer.drainUrlsForApps[appId] == nil {
		sinkServer.drainUrlsForApps[appId] = make(map[string]sinks.Sink, len(drainUrls))
	}
	for _, sink := range sinkServer.drainUrlsForApps[appId] {
		if contains(sink.Identifier(), drainUrls) {
			continue
		}
		sinkServer.activeSinksChans.Delete(sink.Channel())
		close(sink.Channel())
		delete(sinkServer.drainUrlsForApps[appId], sink.Identifier())
	}
	for _, drainUrl := range drainUrls {
		if sinkServer.drainUrlsForApps[appId][drainUrl] == nil {
			s, err := sinks.NewSyslogSink(appId, drainUrl, sinkServer.logger)
			if err != nil {
				sinkServer.logger.Error(err.Error())
				continue
			}
			go s.Run(sinkServer.sinkCloseChan)
			sinkServer.activeSinksChans.Register(s.Channel(), appId)
			sinkServer.drainUrlsForApps[appId][drainUrl] = s
		}
	}
}
func TestDrainsFor(t *testing.T) {
	groupedSinks := NewGroupedSinks()
	target := "789"
	otherTarget := "790"

	sink1 := sinks.NewDumpSink(target, 10, loggertesthelper.Logger(), make(chan sinks.Sink, 1), time.Second)
	sink2 := sinks.NewSyslogSink(target, "url", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message))
	sink3 := sinks.NewSyslogSink(otherTarget, "url", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message))

	groupedSinks.Register(sink1)
	groupedSinks.Register(sink2)
	groupedSinks.Register(sink3)

	appSinks := groupedSinks.DrainsFor(target)
	assert.Equal(t, len(appSinks), 1)
	assert.Equal(t, appSinks[0], sink2)
}
Exemplo n.º 7
0
func TestDrainsFor(t *testing.T) {
	groupedSinks := NewGroupedSinks()
	target := "789"
	otherTarget := "790"

	sink1 := sinks.NewDumpSink(target, 10, loggertesthelper.Logger())
	sink2 := sinks.NewSyslogSink(target, "url", loggertesthelper.Logger(), syslogWriter)
	sink3 := sinks.NewSyslogSink(otherTarget, "url", loggertesthelper.Logger(), syslogWriter)

	groupedSinks.Register(sink1)
	groupedSinks.Register(sink2)
	groupedSinks.Register(sink3)

	appSinks := groupedSinks.DrainsFor(target)
	assert.Equal(t, len(appSinks), 1)
	assert.Equal(t, appSinks[0], sink2)
}
func TestRegisterReturnsFalseForEmptyIdentifier(t *testing.T) {
	groupedSinks := NewGroupedSinks()

	appId := "appId"
	appSink := sinks.NewSyslogSink(appId, "", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message))
	result := groupedSinks.Register(appSink)

	assert.False(t, result)
}
Exemplo n.º 9
0
func TestRegisterReturnsFalseForEmptyIdentifier(t *testing.T) {
	groupedSinks := NewGroupedSinks()

	appId := "appId"
	appSink := sinks.NewSyslogSink(appId, "", loggertesthelper.Logger(), syslogWriter)
	result := groupedSinks.Register(appSink)

	assert.False(t, result)
}
Exemplo n.º 10
0
func TestRegisterReturnsFalseWhenAttemptingToAddADuplicateSink(t *testing.T) {
	groupedSinks := NewGroupedSinks()

	appId := "789"
	appSink := sinks.NewSyslogSink(appId, "url", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message))
	groupedSinks.Register(appSink)
	result := groupedSinks.Register(appSink)

	assert.False(t, result)
}
Exemplo n.º 11
0
func TestDumpForReturnsNilIfThereIsNone(t *testing.T) {
	groupedSinks := NewGroupedSinks()
	target := "789"

	sink1 := sinks.NewSyslogSink(target, "url1", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message))

	groupedSinks.Register(sink1)

	appSink := groupedSinks.DumpFor(target)
	assert.Nil(t, appSink)
}
Exemplo n.º 12
0
func TestRegisterAndFor(t *testing.T) {
	groupedSinks := NewGroupedSinks()

	appId := "789"
	appSink := sinks.NewSyslogSink(appId, "url", loggertesthelper.Logger(), DummySyslogWriter{}, make(chan<- *logmessage.Message))
	result := groupedSinks.Register(appSink)

	appSinks := groupedSinks.For(appId)
	assert.True(t, result)
	assert.Equal(t, len(appSinks), 1)
	assert.Equal(t, appSinks[0], appSink)
}
Exemplo n.º 13
0
func (messageRouter *messageRouter) manageDrains(activeSinks *groupedsinks.GroupedSinks, appId string, drainUrls []string, sourceName string) {
	if sourceName != "App" {
		return
	}
	//delete all drains for app
	if len(drainUrls) == 0 {
		for _, sink := range activeSinks.DrainsFor(appId) {
			messageRouter.unregisterSink(sink, activeSinks)
		}
		return
	}

	//delete all drains that were not sent
	for _, sink := range activeSinks.DrainsFor(appId) {
		if contains(sink.Identifier(), drainUrls) {
			continue
		}
		messageRouter.unregisterSink(sink, activeSinks)
	}

	//add all drains that didn't exist
	for _, drainUrl := range drainUrls {
		if activeSinks.DrainFor(appId, drainUrl) == nil && !messageRouter.urlIsBlackListed(drainUrl) {
			dl, err := url.Parse(drainUrl)
			if err != nil {
				messageRouter.blacklistedURLS = append(messageRouter.blacklistedURLS, drainUrl)
				errorMessage := fmt.Sprintf("MessageRouter: Error when trying to parse syslog url %v. Requesting close. Err: %v", drainUrl, err)
				messageRouter.sendLoggregatorErrorMessage(errorMessage, appId)
				continue
			}
			ipNotBlacklisted, err := iprange.IpOutsideOfRanges(*dl, messageRouter.blackListIPs)
			if err != nil {
				messageRouter.blacklistedURLS = append(messageRouter.blacklistedURLS, drainUrl)
				errorMessage := fmt.Sprintf("MessageRouter: Error when trying to check syslog url %v against blacklist ip ranges. Requesting close. Err: %v", drainUrl, err)
				messageRouter.sendLoggregatorErrorMessage(errorMessage, appId)
				continue
			}
			if ipNotBlacklisted {
				sysLogger := sinks.NewSyslogWriter(dl.Scheme, dl.Host, appId, messageRouter.skipCertVerify)
				s := sinks.NewSyslogSink(appId, drainUrl, messageRouter.logger, sysLogger, messageRouter.errorChannel)
				ok := messageRouter.registerSink(s, activeSinks)
				if ok {
					go s.Run()
				}
			} else {
				messageRouter.blacklistedURLS = append(messageRouter.blacklistedURLS, drainUrl)
				errorMsg := fmt.Sprintf("MessageRouter: Syslog drain url is blacklisted: %s", drainUrl)
				messageRouter.sendLoggregatorErrorMessage(errorMsg, appId)
			}
		}
	}
}
Exemplo n.º 14
0
func (sinkManager *SinkManager) registerNewSyslogSinks(appId string, syslogSinkUrls []string) {
	for _, syslogSinkUrl := range syslogSinkUrls {
		if sinkManager.sinks.DrainFor(appId, syslogSinkUrl) == nil && !sinkManager.urlBlacklistManager.IsBlacklisted(syslogSinkUrl) {
			parsedSyslogDrainUrl, err := sinkManager.urlBlacklistManager.CheckUrl(syslogSinkUrl)
			if err != nil {
				sinkManager.urlBlacklistManager.BlacklistUrl(syslogSinkUrl)
				errorMsg := fmt.Sprintf("SinkManager: Invalid syslog drain URL: %s. Err: %v", syslogSinkUrl, err)
				sinkManager.sendSyslogErrorToLoggregator(errorMsg, appId)
			} else {
				syslogWriter := syslogwriter.NewSyslogWriter(parsedSyslogDrainUrl.Scheme, parsedSyslogDrainUrl.Host, appId, sinkManager.skipCertVerify)
				syslogSink := sinks.NewSyslogSink(appId, syslogSinkUrl, sinkManager.logger, syslogWriter, sinkManager.errorChannel)
				if sinkManager.RegisterSink(syslogSink) {
					go syslogSink.Run()
				}
			}
		}
	}
}
Exemplo n.º 15
0
func (messageRouter *messageRouter) manageDrains(activeSinks *groupedsinks.GroupedSinks, appId string, drainUrls []string, sourceType logmessage.LogMessage_SourceType) {
	if sourceType != logmessage.LogMessage_WARDEN_CONTAINER {
		return
	}
	//delete all drains for app
	if len(drainUrls) == 0 {
		for _, sink := range activeSinks.DrainsFor(appId) {
			messageRouter.unregisterSink(sink, activeSinks)
		}
		return
	}

	//delete all drains that were not sent
	for _, sink := range activeSinks.DrainsFor(appId) {
		if contains(sink.Identifier(), drainUrls) {
			continue
		}
		messageRouter.unregisterSink(sink, activeSinks)
	}

	//add all drains that didn't exist
	for _, drainUrl := range drainUrls {
		if activeSinks.DrainFor(appId, drainUrl) == nil {
			dl, err := url.Parse(drainUrl)
			if err != nil {
				messageRouter.logger.Warnf("MessageRouter: Error when trying to parse syslog url %v. Requesting close. Err: %v", drainUrl, err)
				continue
			}
			sysLogger := sinks.NewSyslogWriter("tcp", dl.Host, appId)
			s := sinks.NewSyslogSink(appId, drainUrl, messageRouter.logger, sysLogger)
			ok := messageRouter.registerSink(s, activeSinks)
			if ok {
				go s.Run()
			}
		}
	}
}
Exemplo n.º 16
0
		mutex.Lock()
		defer mutex.Unlock()
		close(sysLoggerDoneChan)
	}

	newSysLoggerDoneChan := func() {
		mutex.Lock()
		defer mutex.Unlock()
		sysLoggerDoneChan = make(chan bool)
	}

	BeforeEach(func() {
		newSysLoggerDoneChan()
		sysLogger = NewSyslogWriterRecorder()
		errorChannel = make(chan *logmessage.Message, 10)
		syslogSink = sinks.NewSyslogSink("appId", "syslog://localhost:24632", loggertesthelper.Logger(), sysLogger, errorChannel)
	})

	AfterEach(func() {
		select {
		case <-sysLoggerDoneChan:
		default:
			closeSysLoggerDoneChan()
		}
	})

	Context("when remote syslog server is down", func() {
		BeforeEach(func() {
			sysLogger.SetDown(true)
			go func() {
				syslogSink.Run()