Skip to content

Commit

Permalink
Rough Fix for Syslog output & Add testing for it (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrod1598 authored Jun 22, 2021
1 parent bcffda1 commit 25d54af
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
16 changes: 12 additions & 4 deletions operator/builtin/input/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,28 @@ func (c SyslogInputConfig) Build(context operator.BuildContext) ([]operator.Oper
if c.Tcp == nil && c.Udp == nil {
return nil, fmt.Errorf("need tcp config or udp config")
}
parentID := c.InputConfig.ID()
if parentID == "" {
parentID = c.InputConfig.Type()
}
subContext := context.WithSubNamespace(parentID)
if c.Tcp == nil && c.Udp == nil {
return nil, fmt.Errorf("need tcp config or udp config")
}

c.SyslogParserConfig.OutputIDs = c.OutputIDs
ops, err := c.SyslogParserConfig.Build(context)
ops, err := c.SyslogParserConfig.Build(subContext)
if err != nil {
return nil, fmt.Errorf("failed to resolve syslog config: %s", err)
}

if c.Tcp != nil {
c.Tcp.OutputIDs = []string{ops[0].ID()}
inputOps, err := c.Tcp.Build(context)
inputOps, err := c.Tcp.Build(subContext)
if err != nil {
return nil, fmt.Errorf("failed to resolve tcp config: %s", err)
}
ops = append(ops, inputOps...)
ops = append(inputOps, ops...)
}

if c.Udp != nil {
Expand All @@ -72,7 +80,7 @@ func (c SyslogInputConfig) Build(context operator.BuildContext) ([]operator.Oper
if err != nil {
return nil, fmt.Errorf("failed to resolve upd config: %s", err)
}
ops = append(ops, inputOps...)
ops = append(inputOps, ops...)
}

return ops, nil
Expand Down
55 changes: 53 additions & 2 deletions operator/builtin/input/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,63 @@ func SyslogInputTest(t *testing.T, cfg *SyslogInputConfig, tc syslog.Case) {
}
}

func TestSyslogIDs(t *testing.T) {
basicConfig := func() *syslog.SyslogParserConfig {
cfg := syslog.NewSyslogParserConfig("test_syslog_parser")
return cfg
}

cases := []struct {
Name string
Cfg *syslog.SyslogParserConfig
ExpectedOpIDs []string
UDPorTCP string
}{
{
Name: "default",
Cfg: func() *syslog.SyslogParserConfig {
sysCfg := basicConfig()
sysCfg.Protocol = "RFC3164"
return sysCfg
}(),
UDPorTCP: "UDP",
ExpectedOpIDs: []string{
"$.test_syslog.test_syslog_parser",
"$.fake",
},
},
}

for _, tc := range cases {
t.Run(fmt.Sprintf("TCP-%s", tc.Name), func(t *testing.T) {
cfg := NewSyslogInputConfigWithTcp(tc.Cfg)
bc := testutil.NewBuildContext(t)
ops, err := cfg.Build(bc)
require.NoError(t, err)
for i, op := range ops {
out := op.GetOutputIDs()
require.Equal(t, tc.ExpectedOpIDs[i], out[0])
}
})
t.Run(fmt.Sprintf("UDP-%s", tc.Name), func(t *testing.T) {
cfg := NewSyslogInputConfigWithUdp(tc.Cfg)
bc := testutil.NewBuildContext(t)
ops, err := cfg.Build(bc)
require.NoError(t, err)
for i, op := range ops {
out := op.GetOutputIDs()
require.Equal(t, tc.ExpectedOpIDs[i], out[0])
}
})
}
}

func NewSyslogInputConfigWithTcp(syslogCfg *syslog.SyslogParserConfig) *SyslogInputConfig {
cfg := NewSyslogInputConfig("test_syslog")
cfg.SyslogParserConfig = *syslogCfg
cfg.Tcp = tcp.NewTCPInputConfig("test_syslog_tcp")
cfg.Tcp.ListenAddress = ":14201"
cfg.OutputIDs = []string{"fake"}
cfg.OutputIDs = []string{"$.fake"}
return cfg
}

Expand All @@ -107,7 +158,7 @@ func NewSyslogInputConfigWithUdp(syslogCfg *syslog.SyslogParserConfig) *SyslogIn
cfg.SyslogParserConfig = *syslogCfg
cfg.Udp = udp.NewUDPInputConfig("test_syslog_udp")
cfg.Udp.ListenAddress = ":12032"
cfg.OutputIDs = []string{"fake"}
cfg.OutputIDs = []string{"$.fake"}
return cfg
}

Expand Down

0 comments on commit 25d54af

Please sign in to comment.