Skip to content

Commit

Permalink
Add GetTarget test
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Oct 28, 2023
1 parent ffb6dfd commit 159fb6c
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 3 deletions.
225 changes: 223 additions & 2 deletions syslogconf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,223 @@
package syslogsidecar

import "strconv"
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/g41797/sputnik/sidecar"
)

type slfEntry struct {
Selector string
Target string
}

type targetFinder struct {
facilities []string
severities []string
target string
gettarget func(facility, severiry string) (string, bool)
}

func (tf *targetFinder) data(facility, severity string) (string, bool) {
if (facility == tf.facilities[0]) && (len(severity) == 0) {
return tf.target, true
}

if !isFacility(facility) {
return "", false
}

if !isSeverity(severity) {
return "", false
}

return "", true
}

func (tf *targetFinder) listoffacilities(facility, severity string) (string, bool) {
if !isFacility(facility) {
return "", false
}

if !isSeverity(severity) {
return "", false
}

for _, fac := range tf.facilities {
if facility == fac {
return tf.target, true
}
}

return "", true
}

func (tf *targetFinder) severities_(facility, severity string) (string, bool) {
if !isFacility(facility) {
return "", false
}

if !isSeverity(severity) {
return "", false
}

for _, sev := range tf.severities {
if severity == sev {
return tf.target, true
}
}

return "", true
}

func (tf *targetFinder) severitiesOfFacitity(facility, severity string) (string, bool) {
if !isFacility(facility) {
return "", false
}

if !isSeverity(severity) {
return "", false
}

if facility != tf.facilities[0] {
return "", true
}
return tf.severities_(facility, severity)
}

func (se *slfEntry) toFinder() (*targetFinder, error) {

fmap := make(map[string]bool)
smap := make(map[string]bool)

tf := new(targetFinder)
tf.target = se.Target

// data
if se.Selector == Formermessage {
tf.facilities = append(tf.facilities, se.Selector)
tf.gettarget = tf.data
return tf, nil
}

// f.s1,s2,...sN
before, after, found := strings.Cut(se.Selector, ".")

if found {
if !isFacility(before) {
return nil, fmt.Errorf("wrong facility %s", before)
}
_, exists := fmap[before]
if !exists {
tf.facilities = append(tf.facilities, before)
}

if len(after) == 0 {
tf.gettarget = tf.listoffacilities
return tf, nil
}

// s1,s2,....sN
severities := strings.Split(after, ",")

for _, sev := range severities {
if !isSeverity(sev) {
return nil, fmt.Errorf("wrong severity %s", sev)
}
_, exists := smap[sev]
if !exists {
tf.severities = append(tf.severities, sev)
}
}

tf.gettarget = tf.severitiesOfFacitity
return tf, nil
}
// s1,s2,....sN or f1,f2,...fN ?

list := strings.Split(before, ",")

if isSeverity(list[0]) {

for _, sev := range list {
if !isSeverity(sev) {
return nil, fmt.Errorf("wrong severity %s", sev)
}
_, exists := smap[sev]
if !exists {
tf.severities = append(tf.severities, sev)
}
}

tf.gettarget = tf.severities_
return tf, nil
}

for _, fac := range list {
if !isFacility(fac) {
return nil, fmt.Errorf("wrong facility %s", fac)
}
_, exists := fmap[fac]
if !exists {
tf.facilities = append(tf.facilities, fac)
}
}

tf.gettarget = tf.listoffacilities
return tf, nil
}

func readsyslogconf() ([]slfEntry, error) {

confFolder, err := sidecar.ConfFolder()

if err != nil {
return nil, err
}

fPath := filepath.Join(confFolder, "syslogconf.json")

return slogconfbypath(fPath)
}

func slogconfbypath(fPath string) ([]slfEntry, error) {

entriesRaw, err := os.ReadFile(fPath)
if err != nil {
return nil, err
}

var entries []slfEntry

json.Unmarshal([]byte(entriesRaw), &entries)

for _, entry := range entries {

if len(entry.Selector) > 0 {
entry.Selector = strings.ToLower(strings.ReplaceAll(entry.Selector, " ", ""))
}

if len(entry.Selector) == 0 {
return nil, fmt.Errorf("empty selector")
}

if len(entry.Target) > 0 {
entry.Target = strings.TrimSpace(entry.Target)
}

if len(entry.Target) == 0 {
return nil, fmt.Errorf("empty target")
}

}

return entries, nil
}

var fis = map[int]string{
0: "kern",
Expand Down Expand Up @@ -41,6 +258,10 @@ func facsev(priority string) (facility, severiry string) {
return "", ""
}

if priority == Formermessage {
return Formermessage, ""
}

prval, _ := strconv.Atoi(priority)

facility, _ = fis[prval/8]
Expand All @@ -55,7 +276,7 @@ var ssi = swap(sis)
func swap(in map[int]string) map[string]int {
result := make(map[string]int)

for key, val := range fis {
for key, val := range in {
result[val] = key
}

Expand Down
92 changes: 92 additions & 0 deletions syslogconf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package syslogsidecar

import "testing"

func Test_GetTarget(t *testing.T) {

for _, test := range tests {
run(test, t)
}

}

var tests = [][]any{
// selector target t/f facility severity t/f exptarget
{"notice,warning", "folder4", false, "ftp", "warning", false, "folder4"},
{"notice,notice", "mailfolder", false, "ftp", "warning", false, ""},
{"notice,mail", "mailfolder", true},
{"mail.notice,warning", "mailfolder", false, "mail", "warning", false, "mailfolder"},
{"mail.notice", "ftpfolder", false, "ftp", "notice", false, ""},
{"mail,ftp", "ftpfolder", false, "ftp", "notice", false, "ftpfolder"},
{"mail,mail", "anyfolder", false, "ftp", "notice", false, ""},
{"mail,data", "anyfolder", true},
{"data", "anyfolder", false, "data", "", false, "anyfolder"},
{"data", "anyfolder", false, "data", "crit", true},
{"data", "anyfolder", false, "ftp", "crit", false, ""},
{"any", "", true},
{"", "", true},
}

const (
selindex = 0
targindex = 1
tofindshouldfailindex = 2
facilindex = 3
severindex = 4
gettargshouldfailindex = 5
exptargetindex = 6
)

func run(params []any, t *testing.T) {
selector := params[selindex].(string)
target := params[targindex].(string)
slfEntry := slfEntry{selector, target}

tf, err := slfEntry.toFinder()

shouldfail := params[tofindshouldfailindex].(bool)

if shouldfail && (err == nil) {
t.Errorf("toFinder should fail for %s %s", selector, target)
return
}

if !shouldfail && (err != nil) {
t.Errorf("toFinder should not fail for %s %s. error - %v", selector, target, err)
return
}

if err != nil {
return
}

if tf == nil {
t.Errorf("toFinder should be not nil for %s %s", selector, target)
return
}

facility := params[facilindex].(string)
severity := params[severindex].(string)

targ, ok := tf.gettarget(facility, severity)
shouldfail = params[gettargshouldfailindex].(bool)

if shouldfail && ok {
t.Errorf("gettarget should fail for %s %s", facility, severity)
return
}

if !shouldfail && !ok {
t.Errorf("gettarget should not fail for %s %s", facility, severity)
return
}

if shouldfail {
return
}

exptarg := params[exptargetindex].(string)
if targ != exptarg {
t.Errorf("actual target %s != expected target%s", targ, exptarg)
}
}
2 changes: 1 addition & 1 deletion syslogmsgparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (mp *syslogmsgparts) priority() (string, error) {
count, _ := mp.runeAt(0)

if int(count) <= badMessageParts {
return "", fmt.Errorf("non rfc message")
return Formermessage, fmt.Errorf("non rfc message")
}

rfclen, _ := mp.runeAt(1)
Expand Down

0 comments on commit 159fb6c

Please sign in to comment.