Move sinks to extra package

This commit is contained in:
Peter 2021-01-13 17:59:08 +01:00
parent 6c448fd318
commit bb9f45ce91
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
8 changed files with 84 additions and 33 deletions

View file

@ -12,6 +12,7 @@ import (
"gitlab.com/inetmock/inetmock/internal/endpoints" "gitlab.com/inetmock/inetmock/internal/endpoints"
"gitlab.com/inetmock/inetmock/pkg/api" "gitlab.com/inetmock/inetmock/pkg/api"
"gitlab.com/inetmock/inetmock/pkg/audit" "gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/audit/sink"
"gitlab.com/inetmock/inetmock/pkg/cert" "gitlab.com/inetmock/inetmock/pkg/cert"
"gitlab.com/inetmock/inetmock/pkg/config" "gitlab.com/inetmock/inetmock/pkg/config"
"gitlab.com/inetmock/inetmock/pkg/health" "gitlab.com/inetmock/inetmock/pkg/health"
@ -168,7 +169,7 @@ func NewApp(registrations ...api.Registration) (inetmockApp App, err error) {
return return
} }
err = a.eventStream.RegisterSink(audit.NewLogSink(a.Logger().Named("LogSink"))) err = a.eventStream.RegisterSink(sink.NewLogSink(a.Logger().Named("LogSink")))
return return
} }

View file

@ -11,6 +11,7 @@ import (
"gitlab.com/inetmock/inetmock/pkg/audit" "gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/audit/details" "gitlab.com/inetmock/inetmock/pkg/audit/details"
"gitlab.com/inetmock/inetmock/pkg/logging" "gitlab.com/inetmock/inetmock/pkg/logging"
"gitlab.com/inetmock/inetmock/pkg/wait"
) )
var ( var (
@ -215,13 +216,13 @@ func Test_eventStream_Emit(t *testing.T) {
}(tt.args.evs, emittedWaitGroup) }(tt.args.evs, emittedWaitGroup)
select { select {
case <-waitGroupDone(emittedWaitGroup): case <-wait.ForWaitGroupDone(emittedWaitGroup):
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):
t.Errorf("not all events emitted in time") t.Errorf("not all events emitted in time")
} }
select { select {
case <-waitGroupDone(receivedWaitGroup): case <-wait.ForWaitGroupDone(receivedWaitGroup):
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Errorf("did not get all expected events in time") t.Errorf("did not get all expected events in time")
} }
@ -232,14 +233,3 @@ func Test_eventStream_Emit(t *testing.T) {
t.Run(tt.name, scenario(tt)) t.Run(tt.name, scenario(tt))
} }
} }
func waitGroupDone(wg *sync.WaitGroup) <-chan struct{} {
done := make(chan struct{})
go func(wg *sync.WaitGroup) {
wg.Wait()
close(done)
}(wg)
return done
}

View file

@ -1,8 +1,9 @@
package audit package sink
import ( import (
"crypto/tls" "crypto/tls"
"gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/logging" "gitlab.com/inetmock/inetmock/pkg/logging"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -11,7 +12,7 @@ const (
logSinkName = "logging" logSinkName = "logging"
) )
func NewLogSink(logger logging.Logger) Sink { func NewLogSink(logger logging.Logger) audit.Sink {
return &logSink{ return &logSink{
logger: logger, logger: logger,
} }
@ -25,8 +26,8 @@ func (logSink) Name() string {
return logSinkName return logSinkName
} }
func (l logSink) OnSubscribe(evs <-chan Event) { func (l logSink) OnSubscribe(evs <-chan audit.Event) {
go func(logger logging.Logger, evs <-chan Event) { go func(logger logging.Logger, evs <-chan audit.Event) {
for ev := range evs { for ev := range evs {
eventLogger := logger eventLogger := logger

View file

@ -1,4 +1,4 @@
package audit_test package sink_test
import ( import (
"sync" "sync"
@ -8,7 +8,9 @@ import (
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
logging_mock "gitlab.com/inetmock/inetmock/internal/mock/logging" logging_mock "gitlab.com/inetmock/inetmock/internal/mock/logging"
"gitlab.com/inetmock/inetmock/pkg/audit" "gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/audit/sink"
"gitlab.com/inetmock/inetmock/pkg/logging" "gitlab.com/inetmock/inetmock/pkg/logging"
"gitlab.com/inetmock/inetmock/pkg/wait"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -81,7 +83,7 @@ func Test_logSink_OnSubscribe(t *testing.T) {
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(len(tt.events)) wg.Add(len(tt.events))
logSink := audit.NewLogSink(tt.fields.loggerSetup(t, wg)) logSink := sink.NewLogSink(tt.fields.loggerSetup(t, wg))
var evs audit.EventStream var evs audit.EventStream
var err error var err error
if evs, err = audit.NewEventStream(logging.CreateTestLogger(t)); err != nil { if evs, err = audit.NewEventStream(logging.CreateTestLogger(t)); err != nil {
@ -99,7 +101,7 @@ func Test_logSink_OnSubscribe(t *testing.T) {
select { select {
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):
t.Errorf("not all events recorded in time") t.Errorf("not all events recorded in time")
case <-waitGroupDone(wg): case <-wait.ForWaitGroupDone(wg):
} }
} }
} }

View file

@ -1,4 +1,6 @@
package audit package sink
import "gitlab.com/inetmock/inetmock/pkg/audit"
type WriterSinkOption func(sink *writerCloserSink) type WriterSinkOption func(sink *writerCloserSink)
@ -8,7 +10,7 @@ var (
} }
) )
func NewWriterSink(name string, target Writer, opts ...WriterSinkOption) Sink { func NewWriterSink(name string, target audit.Writer, opts ...WriterSinkOption) audit.Sink {
sink := &writerCloserSink{ sink := &writerCloserSink{
name: name, name: name,
target: target, target: target,
@ -23,20 +25,16 @@ func NewWriterSink(name string, target Writer, opts ...WriterSinkOption) Sink {
type writerCloserSink struct { type writerCloserSink struct {
name string name string
target Writer target audit.Writer
closeOnExit bool closeOnExit bool
} }
type syncer interface {
Sync() error
}
func (f writerCloserSink) Name() string { func (f writerCloserSink) Name() string {
return f.name return f.name
} }
func (f writerCloserSink) OnSubscribe(evs <-chan Event) { func (f writerCloserSink) OnSubscribe(evs <-chan audit.Event) {
go func(target Writer, closeOnExit bool, evs <-chan Event) { go func(target audit.Writer, closeOnExit bool, evs <-chan audit.Event) {
for ev := range evs { for ev := range evs {
_ = target.Write(&ev) _ = target.Write(&ev)
} }

View file

@ -1,6 +1,9 @@
package audit_test package sink_test
import ( import (
"crypto/tls"
"net"
"net/http"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -8,7 +11,45 @@ import (
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
audit_mock "gitlab.com/inetmock/inetmock/internal/mock/audit" audit_mock "gitlab.com/inetmock/inetmock/internal/mock/audit"
"gitlab.com/inetmock/inetmock/pkg/audit" "gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/audit/details"
"gitlab.com/inetmock/inetmock/pkg/audit/sink"
"gitlab.com/inetmock/inetmock/pkg/logging" "gitlab.com/inetmock/inetmock/pkg/logging"
"gitlab.com/inetmock/inetmock/pkg/wait"
)
var (
testEvents = []*audit.Event{
{
Transport: audit.TransportProtocol_TCP,
Application: audit.AppProtocol_HTTP,
SourceIP: net.ParseIP("127.0.0.1").To4(),
DestinationIP: net.ParseIP("127.0.0.1").To4(),
SourcePort: 32344,
DestinationPort: 80,
TLS: &audit.TLSDetails{
Version: tls.VersionTLS13,
CipherSuite: tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
ServerName: "localhost",
},
ProtocolDetails: details.HTTP{
Method: "GET",
Host: "localhost",
URI: "http://localhost/asdf",
Proto: "HTTP 1.1",
Headers: http.Header{
"Accept": []string{"application/json"},
},
},
},
{
Transport: audit.TransportProtocol_TCP,
Application: audit.AppProtocol_DNS,
SourceIP: net.ParseIP("::1").To16(),
DestinationIP: net.ParseIP("::1").To16(),
SourcePort: 32344,
DestinationPort: 80,
},
}
) )
func Test_writerCloserSink_OnSubscribe(t *testing.T) { func Test_writerCloserSink_OnSubscribe(t *testing.T) {
@ -42,7 +83,7 @@ func Test_writerCloserSink_OnSubscribe(t *testing.T) {
}). }).
Times(len(tt.events)) Times(len(tt.events))
writerCloserSink := audit.NewWriterSink("WriterMock", writerMock, audit.WithCloseOnExit) writerCloserSink := sink.NewWriterSink("WriterMock", writerMock, sink.WithCloseOnExit)
var evs audit.EventStream var evs audit.EventStream
var err error var err error
@ -61,7 +102,7 @@ func Test_writerCloserSink_OnSubscribe(t *testing.T) {
select { select {
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):
t.Errorf("not all events recorded in time") t.Errorf("not all events recorded in time")
case <-waitGroupDone(wg): case <-wait.ForWaitGroupDone(wg):
} }
} }
} }

View file

@ -52,6 +52,10 @@ type eventWriter struct {
byteOrder binary.ByteOrder byteOrder binary.ByteOrder
} }
type syncer interface {
Sync() error
}
func (e eventWriter) Write(ev *Event) (err error) { func (e eventWriter) Write(ev *Event) (err error) {
if ev == nil { if ev == nil {
return ErrValueMostNotBeNil return ErrValueMostNotBeNil

14
pkg/wait/wg.go Normal file
View file

@ -0,0 +1,14 @@
package wait
import "sync"
func ForWaitGroupDone(wg *sync.WaitGroup) <-chan struct{} {
done := make(chan struct{})
go func(wg *sync.WaitGroup) {
wg.Wait()
close(done)
}(wg)
return done
}