summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32015-02-21 18:37:28 +0100
committerHorus32015-02-21 18:37:28 +0100
commitbdb2d33a30765a25be3dd8c82b92517efa00dda8 (patch)
tree7332415c103980a6d4e87ca6b8f563a9f6a87496
parentb58e21a525e7e4aeda3ec51bfcb07390af8be465 (diff)
downloadstatuspage-bdb2d33a30765a25be3dd8c82b92517efa00dda8.tar.gz
Add basic unit tests.
-rw-r--r--app/Makefile11
-rw-r--r--app/config.go2
-rw-r--r--app/dependecies_test.go24
-rw-r--r--app/fetch.go2
-rw-r--r--app/main.go4
-rw-r--r--app/utilities_test.go33
-rwxr-xr-xhooks/pre-commit1
7 files changed, 72 insertions, 5 deletions
diff --git a/app/Makefile b/app/Makefile
index 15da585..e10dbc4 100644
--- a/app/Makefile
+++ b/app/Makefile
@@ -33,7 +33,7 @@ clean: kill
@echo "Removing sqlite3 database..."
@rm $(STATUS_DB_CREDENTIALS) || true
@echo "Removing tar archiv..."
- @rm ../$(APP).tar.gz || true
+ @rm ../$(APP).tar.gz 1>&2 2>/dev/null || true
@echo "Removing binary..."
@rm $(APP)
@echo "Done."
@@ -78,3 +78,12 @@ pack:
tar czf $(APP).tar.gz app/$(APP) app/env.sh app/Makefile views static db 2>/dev/null && \
echo "../$(APP).tar.gz is ready." || \
(echo "Run \"make build\" first." && exit 1)
+
+test:
+ go test -race -run '^(Database|Redis)'
+
+test_all:
+ go test -race
+
+test_dependencies:
+ go test -race -run '(Database|Redis)'
diff --git a/app/config.go b/app/config.go
index 2c1ea5e..dafc846 100644
--- a/app/config.go
+++ b/app/config.go
@@ -5,7 +5,7 @@ import (
"os"
)
-func check() {
+func checkConfig() {
check := os.Getenv("STATUS_DB_DRIVER")
if check == "" {
log.Fatal("Env STATUS_DB_DRIVER not found. No database connection can be established.")
diff --git a/app/dependecies_test.go b/app/dependecies_test.go
new file mode 100644
index 0000000..d220f96
--- /dev/null
+++ b/app/dependecies_test.go
@@ -0,0 +1,24 @@
+package main
+
+import (
+ "testing"
+)
+
+func TestDatabase(t *testing.T) {
+ // h := Host{Url: "http://example.com"}
+ err := Db.DB().Ping()
+ if err != nil {
+ t.Fatal("No database connection established.")
+ }
+
+}
+
+func TestRedis(t *testing.T) {
+ c := pool.Get()
+ defer c.Close()
+
+ if _, err := c.Do("PING"); err != nil {
+ //t.Fatal("Redis server down.")
+ t.Error("Down")
+ }
+}
diff --git a/app/fetch.go b/app/fetch.go
index c8b0376..a3d3ae7 100644
--- a/app/fetch.go
+++ b/app/fetch.go
@@ -8,7 +8,7 @@ import (
"log"
)
-func run() {
+func jobRun() {
c := c
c.AddFunc("@every 10s", healthCheck)
c.Start()
diff --git a/app/main.go b/app/main.go
index d51cf20..17d3157 100644
--- a/app/main.go
+++ b/app/main.go
@@ -16,8 +16,8 @@ var mainTempl = template.Must(template.New("global").ParseGlob("../views/*.html"
var c = cron.New()
func main() {
- check()
- run()
+ checkConfig()
+ jobRun()
InitDB()
insertHost()
diff --git a/app/utilities_test.go b/app/utilities_test.go
new file mode 100644
index 0000000..7037b2f
--- /dev/null
+++ b/app/utilities_test.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "testing"
+)
+
+func TestMd5Hash(t *testing.T) {
+ hash := "f9d08276bc85d30d578e8883f3c7e843"
+ testHash := Md5Hash("md5hash")
+
+ if hash != testHash {
+ t.Fatal("Expected %s as hash. Got %s.", hash, testHash)
+ }
+}
+
+func TestRandomKey(t *testing.T) {
+ key := RandomKey()
+ if len(key) != 40 {
+ t.Fatal("Expected a key with length of 40. Got %s.", key)
+ }
+}
+
+func TestPassword(t *testing.T) {
+
+ testHash, err := HashPassword("password")
+ if err != nil {
+ t.Fatal("Hashing password failed.")
+ }
+ verify := VerifyPassword("password", testHash)
+ if !verify {
+ t.Fatal("Verifying password failed.")
+ }
+}
diff --git a/hooks/pre-commit b/hooks/pre-commit
index 402ec09..5a3ff7d 100755
--- a/hooks/pre-commit
+++ b/hooks/pre-commit
@@ -2,6 +2,7 @@
if [ -f $GIT_DIR/../app/Makefile ]; then
cd "$GIT_DIR/../app"
+ make test
make clean || exit 0
else
echo "No Makefile found." 1>&2