chore(): improve testing

This commit is contained in:
Nicolas Carlier 2018-08-28 15:45:10 +00:00 committed by Nicolas Carlier
parent e1dd6aa0d1
commit ef962ee653
14 changed files with 224 additions and 62 deletions

View File

@ -1,5 +1,6 @@
# webhookd
[![Build Status](https://travis-ci.org/ncarlier/webhookd.svg?branch=master)](https://travis-ci.org/ncarlier/webhookd)
[![Image size](https://images.microbadger.com/badges/image/ncarlier/webhookd.svg)](https://microbadger.com/images/ncarlier/webhookd)
[![Docker pulls](https://img.shields.io/docker/pulls/ncarlier/webhookd.svg)](https://hub.docker.com/r/ncarlier/webhookd/)
@ -108,10 +109,6 @@ echo "bar bar bar"
```bash
$ curl -XPOST http://localhost/foo/bar
data: Hook work request "foo/bar" queued...
data: Running foo/bar script...
data: foo foo foo
data: bar bar bar
@ -149,10 +146,6 @@ The result:
```bash
$ curl --data @test.json http://localhost/echo?foo=bar
data: Hook work request "echo" queued...
data: Running echo script...
data: Query parameter: foo=bar
data: Header parameter: user-agent=curl/7.52.1

58
pkg/assert/assert.go Normal file
View File

@ -0,0 +1,58 @@
package assert
import (
"testing"
)
// Nil assert that an object is nil
func Nil(t *testing.T, actual interface{}, message string) {
if message == "" {
message = "Nil assertion failed"
}
if actual != nil {
t.Fatalf("%s - actual: %s", message, actual)
}
}
// NotNil assert that an object is not nil
func NotNil(t *testing.T, actual interface{}, message string) {
if message == "" {
message = "Not nil assertion failed"
}
if actual == nil {
t.Fatalf("%s - actual: nil", message)
}
}
// Equal assert that an object is equal to an expected value
func Equal(t *testing.T, expected interface{}, actual interface{}, message string) {
if message == "" {
message = "Equal assertion failed"
}
if actual != expected {
t.Fatalf("%s - expected: %s, actual: %s", message, expected, actual)
}
}
// NotEqual assert that an object is not equal to an expected value
func NotEqual(t *testing.T, expected interface{}, actual interface{}, message string) {
if message == "" {
message = "Not equal assertion failed"
}
if actual == expected {
t.Fatalf("%s - unexpected: %s, actual: %s", message, expected, actual)
}
}
// ContainsStr assert that an array contains an expected value
func ContainsStr(t *testing.T, expected string, array []string, message string) {
if message == "" {
message = "Array don't contains expected value"
}
for _, str := range array {
if str == expected {
return
}
}
t.Fatalf("%s - array: %v, expected value: %s", message, array, expected)
}

View File

@ -0,0 +1,45 @@
package tools_test
import (
"net/http"
"net/url"
"testing"
"github.com/ncarlier/webhookd/pkg/assert"
"github.com/ncarlier/webhookd/pkg/tools"
)
func TestToSnakeCase(t *testing.T) {
testCases := []struct {
value string
expected string
}{
{"hello-world", "helloworld"},
{"helloWorld", "hello_world"},
{"HelloWorld", "hello_world"},
}
for _, tc := range testCases {
value := tools.ToSnakeCase(tc.value)
assert.Equal(t, tc.expected, value, "")
}
}
func TestQueryParamsToShellVars(t *testing.T) {
tc := url.Values{
"string": []string{"foo"},
"list": []string{"foo", "bar"},
}
values := tools.QueryParamsToShellVars(tc)
assert.ContainsStr(t, "string=foo", values, "")
assert.ContainsStr(t, "list=foo,bar", values, "")
}
func TestHTTPHeadersToShellVars(t *testing.T) {
tc := http.Header{
"Content-Type": []string{"text/plain"},
"X-Foo-Bar": []string{"foo", "bar"},
}
values := tools.HTTPHeadersToShellVars(tc)
assert.ContainsStr(t, "content_type=text/plain", values, "")
assert.ContainsStr(t, "x_foo_bar=foo,bar", values, "")
}

View File

@ -0,0 +1,20 @@
package tools_test
import (
"testing"
"github.com/ncarlier/webhookd/pkg/assert"
"github.com/ncarlier/webhookd/pkg/tools"
)
func TestResolveScript(t *testing.T) {
script, err := tools.ResolveScript("../../scripts", "echo")
assert.Nil(t, err, "")
assert.Equal(t, "../../scripts/echo.sh", script, "")
}
func TestNotResolveScript(t *testing.T) {
_, err := tools.ResolveScript("../../scripts", "foo")
assert.NotNil(t, err, "")
assert.Equal(t, "Script not found: ../../scripts/foo.sh", err.Error(), "")
}

View File

@ -28,7 +28,7 @@ var (
workingdir = os.Getenv("APP_WORKING_DIR")
)
func runScript(work *WorkRequest) (string, error) {
func run(work *WorkRequest) (string, error) {
if workingdir == "" {
workingdir = os.TempDir()
}
@ -70,7 +70,7 @@ func runScript(work *WorkRequest) (string, error) {
return logFilename, err
}
// Write script output to log file and the work message cahnnel.
// Write script output to log file and the work message channel.
go func(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {

View File

@ -0,0 +1,57 @@
package worker
import (
"testing"
"github.com/ncarlier/webhookd/pkg/assert"
"github.com/ncarlier/webhookd/pkg/logger"
)
func printWorkMessages(work *WorkRequest) {
go func() {
for {
msg, open := <-work.MessageChan
if !open {
break
}
logger.Info.Println(string(msg))
}
}()
}
func TestWorkRunner(t *testing.T) {
logger.Init("debug")
script := "../../tests/test_simple.sh"
args := []string{
"name=foo",
"user_agent=test",
}
payload := "{\"foo\": \"bar\"}"
work := NewWorkRequest("test", script, payload, args, 5)
assert.NotNil(t, work, "")
printWorkMessages(work)
_, err := run(work)
assert.Nil(t, err, "")
}
func TestWorkRunnerWithError(t *testing.T) {
logger.Init("debug")
script := "../../tests/test_error.sh"
work := NewWorkRequest("test", script, "", []string{}, 5)
assert.NotNil(t, work, "")
printWorkMessages(work)
_, err := run(work)
assert.NotNil(t, err, "")
assert.Equal(t, "exit status 1", err.Error(), "")
}
func TestWorkRunnerWithTimeout(t *testing.T) {
logger.Init("debug")
script := "../../tests/test_timeout.sh"
work := NewWorkRequest("test", script, "", []string{}, 1)
assert.NotNil(t, work, "")
printWorkMessages(work)
_, err := run(work)
assert.NotNil(t, err, "")
assert.Equal(t, "signal: killed", err.Error(), "")
}

View File

@ -42,7 +42,7 @@ func (w Worker) Start() {
case work := <-w.Work:
// Receive a work request.
logger.Debug.Printf("Worker #%d received work request: %s#%d\n", w.ID, work.Name, work.ID)
filename, err := runScript(&work)
filename, err := run(&work)
if err != nil {
subject := fmt.Sprintf("Webhook %s#%d FAILED.", work.Name, work.ID)
work.MessageChan <- []byte(fmt.Sprintf("error: %s", err.Error()))

View File

@ -1,8 +1,13 @@
#!/bin/bash
echo "Echo from script..."
# Usage: http POST :8080/echo msg==hello foo=bar
echo "hostname: `hostname`"
echo "Echo script:"
echo "user-agent: $user_agent"
echo "Command result: hostname=`hostname`"
echo "Header variable: User-Agent=$user_agent"
echo "Query parameter: msg=$msg"
echo "Body payload: $1"

View File

@ -1,20 +0,0 @@
#!/bin/bash
echo "Running test script..."
echo "Environment parameters:"
echo "firstname: $firstname"
echo "lastname: $lastname"
echo "user-agent: $user_agent"
echo "x-api-key: $x_api_key"
echo "Script parameters: $1"
for i in {1..5}; do
sleep .5
echo "running..."
done
echo "Expected error."
exit 1

View File

@ -1,3 +0,0 @@
{
"foo": "bar"
}

View File

@ -1,25 +0,0 @@
#!/bin/sh
URL=http://localhost:8080
echo "Test URL: $URL"
echo "Testing bad request..."
curl -H "Content-Type: application/json" \
--data @test.json \
$URL/bad/action
echo "Testing nominal case..."
curl -H "Content-Type: application/json" \
-H "X-API-Key: test" \
--data @test.json \
$URL/test?firstname=obi-wan\&lastname=kenobi
echo "Testing parallel request..."
curl -XPOST $URL/test &
curl -XPOST $URL/test &
curl -XPOST $URL/test &
wait
echo "Done"

6
tests/test_error.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
echo "Running error test script..."
echo "Expected error"
exit 1

14
tests/test_simple.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
echo "Running simple test script..."
echo "Testing parameters..."
[ -z "$name" ] && echo "Name variable undefined" && exit 1
[ -z "$user_agent" ] && echo "User-Agent variable undefined" && exit 1
[ "$user_agent" != "test" ] && echo "Invalid User-Agent variable: $user_agent" && exit 1
echo "Testing payload..."
[ -z "$1" ] && echo "Payload undefined" && exit 1
[ "$1" != "{\"foo\": \"bar\"}" ] && echo "Invalid payload: $1" && exit 1
exit 0

12
tests/test_timeout.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
echo "Running timeout test script..."
for i in {1..5}; do
sleep .5
echo "running..."
done
echo "This line should not be executed!"
exit 0