Skip to content

Commit

Permalink
Merge pull request #1 from worldline-go/feat/bodycheck
Browse files Browse the repository at this point in the history
feat/bodycheck
  • Loading branch information
rytsh authored Sep 22, 2022
2 parents 0c52646 + 561f7a3 commit 0c6e975
Show file tree
Hide file tree
Showing 16 changed files with 577 additions and 52 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,45 @@ Give a json or yaml file with the following structure:
# LogLevel is the log level, default info
log_level: "debug"

# Delimeters for gotemplate, set the delimeter to avoid conflict with the other template engines
# delimeters:
# - "{{"
# - "}}"

client:
rest:
- concurrent: 1
setting:
# InsecureSkipVerify is the flag to skip the verification of the server's certificate chain and host name
insecureSkipVerify: false
check:
# URL could be multiple URLs, separated by space
- url: "https://api.punkapi.com/v2/beers/1 https://api.punkapi.com/v2/beers/2"
- url: "https://api.punkapi.com/v2/beers/1 https://api.punkapi.com/v2/beers/2?pong=test"
# Method is the HTTP method to use, default is GET
method: GET
# Timeout is in millisecond, default is 0 (no timeout)
timeout: 1000
# Status to check, default 200
status: 200
# Body to check, default empty
body:
# Variable hold the variables to be used in the template
variable:
# From is the source of the variable
from:
# Query get the value from the query string
query:
- "pong"
# Set is the set of variables
set:
val1: "abc"
# Map is the body to be compared give raw map with template, default not check
# Subset of the body is allowed
map: |
- name: {{ .pong }}
# Raw is the raw body to be compared, default not check
raw: |-
Raw body to check
```
```sh
Expand Down
23 changes: 11 additions & 12 deletions cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/worldline-go/pong/internal/load"
"github.com/worldline-go/pong/internal/model"
"github.com/worldline-go/pong/internal/route"
"github.com/worldline-go/pong/pkg/template"
)

var (
Expand Down Expand Up @@ -81,12 +82,7 @@ func main() {

// check length of the arguments
if len(files) == 0 {
if err := load.Response(&model.ModuleResponse{
Msg: "Missing argument file",
Failed: true,
}); err != nil {
log.Error().Err(err).Msg("error while responding")
}
load.ResponseError(fmt.Errorf("missing argument file"))

exitCode = 1

Expand Down Expand Up @@ -123,12 +119,15 @@ func main() {
for _, file := range files {
args, err := load.ReadConfig(file)
if err != nil {
if err := load.Response(&model.ModuleResponse{
Msg: err.Error(),
Failed: true,
}); err != nil {
log.Error().Err(err).Msg("error while responding")
}
load.ResponseError(err)

exitCode = 1

return
}

if err := template.SetDelimeters(args.Delimeters); err != nil {
load.ResponseError(err)

exitCode = 1

Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ module github.com/worldline-go/pong
go 1.18

require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/rs/zerolog v1.28.0
github.com/worldline-go/logz v0.1.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 // indirect
)

require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
39 changes: 39 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/worldline-go/logz v0.1.0 h1:F4e7xjS/T+wJnCfCY66zeg2Uj2/5ATnXnfTLzXXFa7U=
github.com/worldline-go/logz v0.1.0/go.mod h1:CT9jFCJUR8uNYZacUDV+VaNQWdxQLQ9g2UayR4QmMzg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 h1:bXoxMPcSLOq08zI3/c5dEBT6lE4eh+jOh886GHrn6V8=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 9 additions & 0 deletions internal/load/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ func Response(response *model.ModuleResponse) error {
return nil
}

func ResponseError(err error) {
response := model.ModuleResponse{
Msg: err.Error(),
Failed: true,
}

ResponseLog(&response)
}

func ResponseLog(response *model.ModuleResponse) {
if err := Response(response); err != nil {
log.Error().Err(err).Msg("while responding")
Expand Down
19 changes: 2 additions & 17 deletions internal/model/module.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
package model

type RestCheck struct {
// URL could be multiple URLs, separated by space
URL string `json:"url" yaml:"url"`
// Method is the HTTP method to use, default is GET
Method string `json:"method" yaml:"method"`
// Status to check, default 200
Status int `json:"status" yaml:"status"`
// Timeout is in seconds, default 5
Timeout int `json:"timeout" yaml:"timeout"`
}

type RestClient struct {
// Concurrent is the number of concurrent requests, default 1
Concurrent int `json:"concurrent" yaml:"concurrent"`
Check []RestCheck `json:"check" yaml:"check"`
}

type Client struct {
Rest []RestClient `json:"rest" yaml:"rest"`
}
Expand All @@ -25,6 +8,8 @@ type ModuleArgs struct {
Client Client `json:"client" yaml:"client"`
// LogLevel is the log level, default info
LogLevel string `json:"log_level" yaml:"log_level"`
// Delimeters is the delimeters to use for the template
Delimeters []string `json:"delimeters" yaml:"delimeters"`
}

type ModuleResponse struct {
Expand Down
48 changes: 48 additions & 0 deletions internal/model/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package model

type RestCheckBodyVarsFrom struct {
// Query get the value from the query string
Query []string `json:"query" yaml:"query"`
}

type RestCheckBodyVars struct {
// Set is the set of variables
Set map[string]interface{} `json:"set" yaml:"set"`
// From is the source of the variable
From RestCheckBodyVarsFrom `json:"from" yaml:"from"`
}

type RestCheckBody struct {
// Variable hold the variables to be used in the template
Variable RestCheckBodyVars `json:"variable" yaml:"variable"`
// Raw is the raw body to be compared
Raw *string `json:"raw" yaml:"raw"`
// Map is the body to be compared
Map *string `json:"map" yaml:"map"`
}

type RestCheck struct {
// URL could be multiple URLs, separated by space
URL string `json:"url" yaml:"url"`
// Method is the HTTP method to use, default is GET
Method string `json:"method" yaml:"method"`
// Status to check, default 200
Status int `json:"status" yaml:"status"`
// Body to check
Body *RestCheckBody `json:"body" yaml:"body"`
// Timeout is in seconds, default 5
Timeout int `json:"timeout" yaml:"timeout"`
}

type RestSetting struct {
InsecureSkipVerify bool `json:"insecureSkipVerify" yaml:"insecureSkipVerify"`
}

type RestClient struct {
// Concurrent is the number of concurrent requests, default 1
Concurrent int `json:"concurrent" yaml:"concurrent"`
// Setting is the setting for the client
Setting RestSetting `json:"setting" yaml:"setting"`
// Checks is the list of checks
Check []RestCheck `json:"check" yaml:"check"`
}
12 changes: 7 additions & 5 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
)

type ClientReg struct {
msgChan chan interface{}
clients []interface{}
msgChan chan interface{}
clients []interface{}
ClientData interface{}
*Errors
// mutexErr sync.RWMutex
}

func NewClientReg(errs *Errors) *ClientReg {
func NewClientReg(errs *Errors, cData interface{}) *ClientReg {
msgChan := make(chan interface{})
return &ClientReg{
msgChan: msgChan,
Errors: errs,
msgChan: msgChan,
Errors: errs,
ClientData: cData,
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/route/rest/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func Request(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup,
timeout := time.Duration(args.Timeout) * time.Millisecond

gData := GeneralData{}
cData, _ := reg.ClientData.(model.RestSetting)

// create a new client holderFunc
newClientHolderFn := NewClientHolder(gData)
newClientHolderFn := NewClientHolder(gData, cData)

for i, url := range urls {
selectedClient := i % concurrent
Expand All @@ -35,8 +36,7 @@ func Request(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup,

if err := reg.SendMessage(ctx, &Msg{
URL: url,
Method: args.Method,
Status: args.Status,
Args: *args,
Timeout: timeout,
}); err != nil {
break
Expand Down
2 changes: 1 addition & 1 deletion internal/route/rest/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestRequest(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
wg := &sync.WaitGroup{}
errs := &registry.Errors{}
reg := registry.NewClientReg(errs)
reg := registry.NewClientReg(errs, nil)

handlerFunc = tt.handler
urls := strings.Split(tt.args.check.URL, " ")
Expand Down
Loading

0 comments on commit 0c6e975

Please sign in to comment.