Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozzie Bock committed Mar 22, 2021
1 parent 806eaf2 commit 9ee3aee
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 188 deletions.
41 changes: 0 additions & 41 deletions .github/workflows/go-cross.yml

This file was deleted.

67 changes: 0 additions & 67 deletions .github/workflows/main.yml

This file was deleted.

45 changes: 0 additions & 45 deletions .golangci.toml

This file was deleted.

5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

export GO111MODULE=on

default: lint test

lint:
golangci-lint run
default: test

test:
go test -v -cover ./...
Expand Down
59 changes: 33 additions & 26 deletions gelflog.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package plugin_gelflog
package traefik_gelf_plugin

import (
"context"
"fmt"
"github.com/kjk/betterguid"
"gopkg.in/Graylog2/go-gelf.v2/gelf"
"log"
"net/http"
"os"
"time"
)

var GelfWriter *gelf.UDPWriter
var GelfHostname string
var MWConfig *Config
// Config holds the plugin configuration.
type Config struct {
GelfEndpoint string `json:"gelfEndpoint,omitempty"`
Expand All @@ -39,6 +35,8 @@ type GelfLog struct {
Name string
Next http.Handler
Config *Config
GelfHostname string
GelfWriter *gelf.UDPWriter
}

// New creates and returns a plugin instance.
Expand All @@ -49,58 +47,67 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
Config: config,
}
if config == nil {
//log.Fatal("config for Gelf Logger empty")
return nil, fmt.Errorf("config can not be empty")
}
MWConfig = config
if config.GelfEndpoint == "" {
return nil, fmt.Errorf("you must specify a GELF compatibile endpoint")
}

if config.GelfPort == 0 || config.GelfPort > 65353 {
return nil, fmt.Errorf("you must specify a valid port")
}

if config.HostnameOverride == "" {
GelfHostname, _ = os.Hostname()
tLog.GelfHostname, _ = os.Hostname()
} else {
tLog.GelfHostname = config.HostnameOverride
}
GelfWriter, _ = gelf.NewUDPWriter(fmt.Sprintf("%s:%d", config.GelfEndpoint, config.GelfPort))
tLog.GelfWriter, _ = gelf.NewUDPWriter(fmt.Sprintf("%s:%d", config.GelfEndpoint, config.GelfPort))
return tLog, nil
}


func (h *GelfLog) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
id := betterguid.New()
if MWConfig.EmitRequestStart {
req.Header.Set(MWConfig.RequestStartTimeHeader, fmt.Sprint(makeTimestampMilli()))
if h.Config.EmitRequestStart {
req.Header.Set(h.Config.RequestStartTimeHeader, fmt.Sprint(makeTimestampMilli()))
}
if MWConfig.EmitTraceId {
req.Header.Set(MWConfig.TraceIdHeader, fmt.Sprint(id))
if h.Config.EmitTraceId {
req.Header.Set(h.Config.TraceIdHeader, fmt.Sprint(id))
}
if GelfWriter != nil {
if h.GelfWriter != nil {
var headerMap = map[string]interface{}{}
for str, val := range req.Header {
headerMap[str] = val
for index, iVal := range val {
headerName := str
if index > 0 {
headerName = fmt.Sprintf("%s_%d", str, index)
}
headerMap[headerName] = iVal
}
}
headerMap["Host"] = req.Host
message := wrapMessage(fmt.Sprintf("Request to %s", req.Host), fmt.Sprintf("Request to %s", req.Host), 5, headerMap)
e := GelfWriter.WriteMessage(message)

if e != nil {
log.Println("Received error when sending GELF message:", e.Error())
}
message := wrapMessage(fmt.Sprintf("Request to %s", req.Host), fmt.Sprintf("Request to %s", req.Host), h.GelfHostname, headerMap)
h.GelfWriter.WriteMessage(message)
}
h.Next.ServeHTTP(rw, req)
}


func wrapMessage(s string, f string, l int32, ex map[string]interface{}) *gelf.Message {
func wrapMessage(s string, f string, h string, ex map[string]interface{}) *gelf.Message {
/*
Level is a stanard syslog level
Level is a standard syslog level
Facility is deprecated
Line is deprecated
File is deprecated
*/

m := &gelf.Message{
Version: "1.1",
Host: GelfHostname,
Host: h,
Short: s,
Full: f,
TimeUnix: float64(time.Now().Unix()),
Level: l,
Level: 5,
Extra: ex,
}

Expand Down
102 changes: 102 additions & 0 deletions gelflog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package traefik_gelf_plugin

import (
"net/http"
"net/http/httptest"
"testing"
)

type httpHandlerMock struct{}

func (h *httpHandlerMock) ServeHTTP(http.ResponseWriter, *http.Request) {}

func TestGoodConfig(t *testing.T) {
config := CreateConfig()
config.GelfEndpoint = "192.168.2.4"
config.GelfPort = 12203
g, err := New(nil, &httpHandlerMock{}, config, "GelfLogger")

if err != nil {
t.Fatal(err)
}


req := httptest.NewRequest(http.MethodGet, "http://localhost/some/path", nil)
req.RemoteAddr = "4.0.0.0:34000"
rw := httptest.NewRecorder()

g.ServeHTTP(rw, req)

if config.EmitTraceId {
traceHeader := req.Header.Get(config.TraceIdHeader)
if traceHeader == "" {
t.Fatal("trace id empty")
}
}

if config.EmitRequestStart {
reqStartHeader := req.Header.Get(config.RequestStartTimeHeader)
if reqStartHeader == "" {
t.Fatal("request start empty")
}
}
}
func TestHostnameOverride(t *testing.T) {
config := CreateConfig()
config.GelfEndpoint = "192.168.2.4"
config.GelfPort = 12203
config.HostnameOverride = "avimac01.av.local"
g, err := New(nil, &httpHandlerMock{}, config, "GelfLogger")

if err != nil {
t.Fatal(err)
}


req := httptest.NewRequest(http.MethodGet, "http://localhost/some/path", nil)
req.RemoteAddr = "4.0.0.0:34000"
req.Header.Add("X-Test-Redundant", "abc")
req.Header.Add("X-Test-Redundant", "123")
rw := httptest.NewRecorder()

g.ServeHTTP(rw, req)

if config.EmitTraceId {
traceHeader := req.Header.Get(config.TraceIdHeader)
if traceHeader == "" {
t.Fatal("trace id empty")
}
}

if config.EmitRequestStart {
reqStartHeader := req.Header.Get(config.RequestStartTimeHeader)
if reqStartHeader == "" {
t.Fatal("request start empty")
}
}
}
func TestBadConfig(t *testing.T) {
config := CreateConfig()

g, err := New(nil, &httpHandlerMock{}, config, "GelfLogger")

if g != nil {
t.Fatal(err)
}

}
func TestMissingPort(t *testing.T) {
config := CreateConfig()
config.GelfEndpoint = "192.168.2.4"
g, err := New(nil, &httpHandlerMock{}, config, "GelfLogger")

if g != nil {
t.Fatal(err)
}
}
func TestMissingConfig(t *testing.T) {
g, err := New(nil, &httpHandlerMock{}, nil, "GelfLogger")
if g != nil {
t.Fatal(err)
}
}
Loading

0 comments on commit 9ee3aee

Please sign in to comment.