Skip to content

Commit 8f22246

Browse files
committed
add tests
Signed-off-by: alexferl <[email protected]>
1 parent a9c11e4 commit 8f22246

28 files changed

+401
-183
lines changed

Diff for: cmd/tinysyslogd/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package main
33
import (
44
"github.com/rs/zerolog/log"
55

6-
"tinysyslog"
76
"tinysyslog/config"
7+
"tinysyslog/server"
88
)
99

1010
func main() {
11-
c := config.NewConfig()
11+
c := config.New()
1212
c.BindFlags()
1313

14-
server := tinysyslog.NewServer()
15-
err := server.Run()
14+
s, err := server.New()
1615
if err != nil {
1716
log.Fatal().Err(err).Msg("failed staring server")
1817
}
18+
19+
s.Run()
1920
}

Diff for: config/config.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package config
33
import (
44
"fmt"
55

6+
"tinysyslog/filters"
7+
"tinysyslog/mutators"
8+
"tinysyslog/sinks"
9+
610
libConfig "github.com/alexferl/golib/config"
711
libLog "github.com/alexferl/golib/log"
812
"github.com/spf13/pflag"
@@ -58,8 +62,8 @@ type RegexFilter struct {
5862
Regex string
5963
}
6064

61-
// NewConfig creates a Config instance
62-
func NewConfig() *Config {
65+
// New creates a Config instance
66+
func New() *Config {
6367
c := libConfig.New("TINYSYSLOG")
6468
c.AppName = "tinysyslog"
6569
c.EnvName = "PROD"
@@ -83,11 +87,11 @@ func NewConfig() *Config {
8387
LogFile: "stdout",
8488
LogFormat: "text",
8589
LogLevel: "info",
86-
MutatorType: constants.MutatorText,
90+
MutatorType: mutators.TextKind.String(),
8791
RegexFilter: RegexFilter{
8892
Regex: "",
8993
},
90-
SinkTypes: []string{constants.SinkConsole},
94+
SinkTypes: []string{sinks.ConsoleKind.String()},
9195
SocketType: "",
9296
}
9397
}
@@ -124,14 +128,14 @@ const (
124128
func (c *Config) addFlags(fs *pflag.FlagSet) {
125129
fs.StringVar(&c.BindAddr, BindAddr, c.BindAddr, "IP and port to listen on.")
126130
fs.StringVar(&c.FilterType, Filter, c.FilterType,
127-
fmt.Sprintf("Filter to filter logs with. Valid filters: %s", constants.Filters),
131+
fmt.Sprintf("Filter to filter logs with. Valid filters: %s", filters.Kinds),
128132
)
129133
fs.StringVar(&c.RegexFilter.Regex, FilterRegex, c.RegexFilter.Regex, "Regex to filter with.")
130134
fs.StringVar(&c.MutatorType, Mutator, c.MutatorType,
131-
fmt.Sprintf("Mutator type to use. Valid mutators: %s", constants.Mutators),
135+
fmt.Sprintf("Mutator type to use. Valid mutators: %s", mutators.Kinds),
132136
)
133137
fs.StringSliceVar(&c.SinkTypes, Sinks, c.SinkTypes,
134-
fmt.Sprintf("Sinks to save syslogs to. Valid sinks: %s", constants.Sinks),
138+
fmt.Sprintf("Sinks to save syslogs to. Valid sinks: %s", sinks.Kinds),
135139
)
136140
fs.StringVar(&c.ConsoleSink.Output, SinkConsoleOutput, c.ConsoleSink.Output,
137141
fmt.Sprintf("Console to output to. Valid outputs: %s", constants.ConsoleOutputs))

Diff for: constants/constants.go

-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
package constants
22

3-
const (
4-
MutatorText = "text"
5-
MutatorJSON = "json"
6-
)
7-
8-
var Mutators = []string{MutatorText, MutatorJSON}
9-
10-
const (
11-
FilterRegex = "regex"
12-
)
13-
14-
var Filters = []string{FilterRegex}
15-
16-
const (
17-
SinkConsole = "console"
18-
SinkElasticsearch = "elasticsearch"
19-
SinkFilesystem = "filesystem"
20-
)
21-
22-
var Sinks = []string{SinkConsole, SinkElasticsearch, SinkFilesystem}
23-
243
const (
254
ConsoleStdOut = "stdout"
265
ConsoleStdErr = "stderr"

Diff for: factories.go renamed to factories/factories.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tinysyslog
1+
package factories
22

33
import (
44
"os"
@@ -14,53 +14,53 @@ import (
1414
"tinysyslog/sinks"
1515
)
1616

17-
// MutatorFactory creates a new object with mutators.Mutator interface
18-
func MutatorFactory() mutators.Mutator {
19-
mutatorType := viper.GetString(config.Mutator)
17+
// Mutator creates a new object with mutators.Mutator interface
18+
func Mutator() mutators.Mutator {
19+
mutator := viper.GetString(config.Mutator)
2020

21-
if mutatorType == constants.MutatorText {
22-
log.Debug().Msgf("using mutator '%s'", mutatorType)
21+
if mutator == mutators.TextKind.String() {
22+
log.Debug().Msgf("using mutator '%s'", mutator)
2323
return mutators.NewText()
2424
}
2525

26-
if mutatorType == constants.MutatorJSON {
27-
log.Debug().Msgf("using mutator '%s'", mutatorType)
26+
if mutator == mutators.JSONKind.String() {
27+
log.Debug().Msgf("using mutator '%s'", mutator)
2828
return mutators.NewJSON()
2929
}
3030

31-
log.Warn().Msgf("unknown mutator '%s'. Falling back to '%s'", mutatorType, constants.MutatorText)
31+
log.Warn().Msgf("unknown mutator '%s'. Falling back to '%s'", mutator, mutators.TextKind)
3232
return mutators.NewText()
3333
}
3434

35-
// FilterFactory creates a new object with filters.Filter interface
36-
func FilterFactory() filters.Filter {
37-
filterType := viper.GetString(config.Filter)
35+
// Filter creates a new object with filters.Filter interface
36+
func Filter() filters.Filter {
37+
filter := viper.GetString(config.Filter)
3838

39-
if filterType == "" {
39+
if filter == "" {
4040
log.Debug().Msgf("using no filtering")
4141
return filters.NewNoOp()
4242
}
4343

44-
if filterType == constants.FilterRegex {
45-
filter := viper.GetString(config.FilterRegex)
46-
log.Debug().Msgf("using filter '%s' with regular expression '%s'", filterType, filter)
47-
return filters.NewRegex(filter)
44+
if filter == filters.RegexKind.String() {
45+
regex := viper.GetString(config.FilterRegex)
46+
log.Debug().Msgf("using regex '%s' with regular expression '%s'", filter, regex)
47+
return filters.NewRegex(regex)
4848
}
4949

50-
log.Warn().Msgf("unknown filter '%s', falling back to no filtering", filterType)
50+
log.Warn().Msgf("unknown filter '%s', falling back to no filtering", filter)
5151
return filters.NewNoOp()
5252
}
5353

54-
// SinksFactory creates a new slice of objects with sinks.Sink interface
55-
func SinksFactory() []sinks.Sink {
56-
sinkTypes := viper.GetStringSlice(config.Sinks)
54+
// Sinks creates a new slice of objects with sinks.Sink interface
55+
func Sinks() []sinks.Sink {
56+
sinksSlice := viper.GetStringSlice(config.Sinks)
5757
mutatorType := viper.GetString(config.Mutator)
5858

5959
var sinksList []sinks.Sink
6060

61-
for _, sink := range sinkTypes {
62-
switch sink {
63-
case constants.SinkConsole:
61+
for _, s := range sinksSlice {
62+
switch s {
63+
case sinks.ConsoleKind.String():
6464
cOutput := viper.GetString(config.SinkConsoleOutput)
6565
var stdOutput *os.File
6666

@@ -71,12 +71,12 @@ func SinksFactory() []sinks.Sink {
7171
} else {
7272
log.Warn().Msgf("unknown console output '%s', falling back to '%s'", cOutput, constants.ConsoleStdOut)
7373
}
74-
log.Debug().Msgf("adding sink '%s'", sink)
74+
log.Debug().Msgf("adding sink '%s'", s)
7575
c := sinks.NewConsole(stdOutput)
7676
sinksList = append(sinksList, c)
77-
case constants.SinkElasticsearch:
78-
if mutatorType != constants.MutatorJSON {
79-
log.Panic().Msgf("mutator must be '%s' when using '%s' sink", constants.MutatorJSON, constants.SinkElasticsearch)
77+
case sinks.ElasticsearchKind.String():
78+
if mutatorType != mutators.JSONKind.String() {
79+
log.Panic().Msgf("mutator must be '%s' when using '%s' sink", mutators.JSONKind, sinks.ElasticsearchKind)
8080
}
8181

8282
cfg := sinks.ElasticsearchConfig{
@@ -90,20 +90,20 @@ func SinksFactory() []sinks.Sink {
9090
ServiceToken: viper.GetString(config.SinkElasticsearchServiceToken),
9191
}
9292

93-
log.Debug().Msgf("adding sink '%s'", sink)
93+
log.Debug().Msgf("adding sink '%s'", s)
9494
es := sinks.NewElasticsearch(cfg)
9595
sinksList = append(sinksList, es)
96-
case constants.SinkFilesystem:
96+
case sinks.FilesystemKind.String():
9797
fsFilename := viper.GetString(config.SinkFilesystemFilename)
9898
fsMaxAge := viper.GetInt(config.SinkFilesystemMaxAge)
9999
fsMaxBackups := viper.GetInt(config.SinkFilesystemMaxBackups)
100100
fsMaxSize := viper.GetInt(config.SinkFilesystemMaxSize)
101101

102-
log.Debug().Msgf("adding sink '%s'", sink)
102+
log.Debug().Msgf("adding sink '%s'", s)
103103
fs := sinks.NewFilesystem(fsFilename, fsMaxAge, fsMaxBackups, fsMaxSize)
104104
sinksList = append(sinksList, fs)
105105
default:
106-
log.Warn().Msgf("unknown sink '%s'.", sink)
106+
log.Warn().Msgf("unknown sink '%s'.", s)
107107
}
108108
}
109109
return sinksList

Diff for: factories/factories_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package factories
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"tinysyslog/filters"
9+
"tinysyslog/mutators"
10+
"tinysyslog/sinks"
11+
)
12+
13+
func TestMutator(t *testing.T) {
14+
m := Mutator()
15+
assert.Equal(t, mutators.TextKind, m.GetKind())
16+
}
17+
18+
func TestFilter(t *testing.T) {
19+
f := Filter()
20+
assert.Equal(t, filters.NoOpKind, f.GetKind())
21+
}
22+
23+
func TestSinks(t *testing.T) {
24+
s := Sinks()
25+
assert.Equal(t, []sinks.Sink(nil), s)
26+
}

Diff for: filters/interface.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
package filters
22

3+
type Kind int8
4+
5+
const (
6+
NoOpKind Kind = iota + 1
7+
RegexKind
8+
)
9+
10+
func (k Kind) String() string {
11+
return [...]string{"noop", "regex"}[k-1]
12+
}
13+
14+
var Kinds = []string{NoOpKind.String(), RegexKind.String()}
15+
316
// Filter is a common interface for all filters
417
type Filter interface {
518
Filter(string) (string, error)
19+
GetKind() Kind
620
}

Diff for: filters/noop.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package filters
22

33
// NoOp represents a no operation filter
4-
type NoOp struct{}
4+
type NoOp struct {
5+
kind Kind
6+
}
57

68
// NewNoOp creates a NoOp instance
79
func NewNoOp() Filter {
8-
return Filter(&NoOp{})
10+
return Filter(&NoOp{kind: NoOpKind})
911
}
1012

1113
// Filter filters a log entry
1214
func (n *NoOp) Filter(data string) (string, error) {
1315
return data, nil
1416
}
17+
18+
func (n *NoOp) GetKind() Kind {
19+
return n.kind
20+
}

Diff for: filters/noop_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ func TestNoOp(t *testing.T) {
1313
s, err := f.Filter(msg)
1414
assert.NoError(t, err)
1515
assert.Equal(t, msg, s)
16+
assert.Equal(t, NoOpKind, f.GetKind())
1617
}

Diff for: filters/regex.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
// Regex represents a regex filter
88
type Regex struct {
99
regex string
10+
kind Kind
1011
}
1112

1213
// NewRegex creates a Regex instance
1314
func NewRegex(s string) Filter {
14-
return Filter(&Regex{regex: s})
15+
return Filter(&Regex{regex: s, kind: RegexKind})
1516
}
1617

1718
// Filter filters a log entry
@@ -28,3 +29,7 @@ func (r *Regex) Filter(data string) (string, error) {
2829
}
2930
return data, nil
3031
}
32+
33+
func (r *Regex) GetKind() Kind {
34+
return r.kind
35+
}

Diff for: filters/regex_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestRegex(t *testing.T) {
2525
t.Run(tc.name, func(t *testing.T) {
2626
f := NewRegex(tc.re)
2727
s, err := f.Filter(msg)
28+
assert.Equal(t, RegexKind, f.GetKind())
2829
if tc.err {
2930
assert.Error(t, err)
3031
} else {

0 commit comments

Comments
 (0)