Skip to content

Commit

Permalink
Adding functional tests (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoj3 authored Jun 23, 2019
1 parent 94edb2a commit cca80a0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ matrix:
script:
- make dep
- make check
- make test
- make tests
- make functional-tests

after_success:
- bash <(curl -s https://codecov.io/bash)
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ dep:
@go get -u github.com/securego/gosec/cmd/gosec
@dep ensure

.PHONY: test
.PHONY: tests
## Execute tests
test:
tests:
@echo "Executing tests"
@CGO_ENABLED=1 go test $(TESTARGS) ./...

.PHONY: functional-tests
## Execute functional test
functional-tests: build build-testserver
./functional-tests.sh

.PHONY: check
## Run checks against the codebase
Expand Down Expand Up @@ -60,7 +64,12 @@ release:
build:
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "$(LD_FLAGS)" -o dist/dirstalk cmd/dirstalk/main.go

.PHONY: build
.PHONY: build-testserver
## Builds binary for testserver used for the functional tests
build-testserver:
go build -o dist/testserver cmd/testserver/main.go

.PHONY: help
HELP_WIDTH=" "
## Display makefile help
help:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Then you can just:
go get github.com/stefanoj3/dirstalk # (or your fork) to obtain the source code
cd $GOPATH/src/github.com/stefanoj3/dirstalk # to go inside the project folder
make dep # to fetch all the required tools and dependencies
make test # to run the test suite
make tests # to run the test suite
make check # to check for any code style issue
make fix # to automatically fix the code style using goimports
make build # to build an executable for your host OS (not tested under windows)
Expand Down
14 changes: 14 additions & 0 deletions cmd/testserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"net/http"
)

func main() {
http.HandleFunc("/home", func(writer http.ResponseWriter, request *http.Request) {})
http.HandleFunc("/index", func(writer http.ResponseWriter, request *http.Request) {})

if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
74 changes: 74 additions & 0 deletions functional-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash

################################################################################################################
## The purpose of this script is to make sure dirstalk basic functionalities are working as expected
################################################################################################################

###################################
## function to assert that the given string contains the given substring
## example usage: assert_contains "error" "my_special_error: blabla" "an error is expected for XY"
###################################
function assert_contains {
local actual=$1
local contains=$2
local msg=$3

if ! echo "$actual" | grep "$contains" > /dev/null; then
echo "ERROR: $msg"
echo "Failed to assert that $actual contains $contains"
exit 1;
fi
echo "Assertion passing"
}

###################################
## function to assert that the given string does not contain the given substring
## example usage: assert_contains "error" "my_special_error: blabla" "an error is expected for XY"
###################################
function assert_not_contains {
local actual=$1
local contains=$2
local msg=$3

if ! echo "$actual" | grep -v "$contains" > /dev/null; then
echo "ERROR: $msg"
echo "Failed to assert that $actual does not contain $contains"
exit 1;
fi
echo "Assertion passing"
}

## Starting test server running on the 8080 port
echo "Starting test server"
./dist/testserver&
SERVER_PID=$!
sleep 1
echo "Done"

function finish {
echo "Killing test server $SERVER_PID"
kill -9 "$SERVER_PID"
echo "Done"
}
trap finish EXIT

## Tests

VERSION_RESULT=$(./dist/dirstalk version 2>&1);
assert_contains "$VERSION_RESULT" "Version" "the version is expected to be printed when calling the version command"
assert_contains "$VERSION_RESULT" "Built" "the build time is expected to be printed when calling the version command"

SCAN_RESULT=$(./dist/dirstalk scan 2>&1 || true);
assert_contains "$SCAN_RESULT" "error" "an error is expected when no argument is passed"

SCAN_RESULT=$(./dist/dirstalk scan -d resources/tests/dictionary.txt http://localhost:8080 2>&1 || true);
assert_contains "$SCAN_RESULT" "/index" "result expected when performing scan"
assert_contains "$SCAN_RESULT" "6 requests made, 2 results found" "a recap was expected when performing a scan"
assert_not_contains "$SCAN_RESULT" "error" "no error is expected for a successful scan"


DICTIONARY_GENERATE_RESULT=$(./dist/dirstalk dictionary.generate resources/tests 2>&1 || true);
assert_contains "$DICTIONARY_GENERATE_RESULT" "dictionary.txt" "dictionary generation should contains a file in the folder"
assert_not_contains "$DICTIONARY_GENERATE_RESULT" "error" "no error is expected when generating a dictionary successfully"


2 changes: 2 additions & 0 deletions resources/tests/dictionary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
home
index

0 comments on commit cca80a0

Please sign in to comment.