fix tests

This commit is contained in:
Steffen Vogel 2019-11-13 21:29:17 +01:00
parent a1f7ea9ed9
commit 2c5400a0cc
4 changed files with 42 additions and 36 deletions

View file

@ -76,13 +76,14 @@ test:apidoc:
- doc/api/index.html
- doc/api/swagger.json
test:all:
test:gotest:
stage: test
variables:
DB_NAME: ${POSTGRES_DB}
DB_HOST: ${POSTGRES_HOST}
DB_USER: ${POSTGRES_USER}
DB_PASS: ${POSTGRES_PASSWORD}
MODE: test
tags:
- docker
image: golang:1.12.9-buster
@ -179,9 +180,13 @@ deploy:docker:
# deploy:upload:
# stage: deploy
# image:
# name: rclone/rclone:1.50
# entrypoint: [""]
# before_script:
# - rclone config create fein webdav url ${DEPLOY_PATH} vendor other user ${DEPLOY_USER} pass ${DEPLOY_PASS}
# script:
# - cd doc/api
# - rsync --copy-links --chown ${DEPLOY_USER}:${DEPLOY_USER} index.html swagger.json ${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_PATH}
# - rclone copy --include=index.html --include=swagger.json doc/api fein:villas/api/web
# dependencies:
# - test:apidoc
# only:

View file

@ -9,9 +9,13 @@ import (
)
// Global configuration
var Config *config.Config
var Config *config.Config = nil
func InitConfig() *config.Config {
if Config != nil {
return Config
}
var (
dbHost = flag.String("dbhost", "/var/run/postgresql", "Host of the PostgreSQL database (default is /var/run/postgresql for localhost DB on Ubuntu systems)")
dbName = flag.String("dbname", "villas", "Name of the database to use (default is villasdb)")
@ -48,15 +52,16 @@ func InitConfig() *config.Config {
}
mappings := map[string]string{
"DB_HOST": "db.host",
"DB_NAME": "db.name",
"DB_USER": "db.user",
"DB_PASS": "db.pass",
"DB_SSLMOE": "db.ssl",
"DB_INIT": "db.init",
"AMQP_URL": "amqp.url",
"BASE_HOST": "base.host",
"BASE_PATH": "base.path",
"DB_HOST": "db.host",
"DB_NAME": "db.name",
"DB_USER": "db.user",
"DB_PASS": "db.pass",
"DB_SSLMODE": "db.ssl",
"DB_INIT": "db.init",
"AMQP_URL": "amqp.url",
"BASE_HOST": "base.host",
"BASE_PATH": "base.path",
"MODE": "mode",
}
defaults := config.NewStatic(static)

View file

@ -30,6 +30,7 @@ func InitDB(cfg *config.Config) *gorm.DB {
if err != nil {
log.Fatal(err)
}
DBpool = db
MigrateModels(db)

View file

@ -1,15 +1,16 @@
package healthz
import (
"net/http"
"os"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/config"
c "git.rwth-aachen.de/acs/public/villas/web-backend-go/config"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
@ -17,51 +18,45 @@ import (
var router *gin.Engine
var db *gorm.DB
func TestMain(m *testing.M) {
c.InitConfig()
os.Exit(m.Run())
}
func TestHealthz(t *testing.T) {
// connect DB
c := config.InitConfig()
db = database.InitDB(c)
defer db.Close()
assert.NoError(t, database.DBAddAdminAndUserAndGuest(db))
db = database.InitDB(c.Config)
router = gin.Default()
api := router.Group("/api")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication(true))
RegisterHealthzEndpoint(api.Group("/healthz"))
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
assert.NoError(t, err)
RegisterHealthzEndpoint(router.Group("/healthz"))
// close db connection
err = db.Close()
err := db.Close()
assert.NoError(t, err)
// test healthz endpoint for unconnected DB and AMQP client
code, resp, err := helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
code, resp, err := helper.TestEndpoint(router, "", "healthz", http.MethodGet, nil)
assert.NoError(t, err)
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
// reconnect DB
db = database.InitDB(c)
db = database.InitDB(c.Config)
defer db.Close()
// test healthz endpoint for connected DB and unconnected AMQP client
code, resp, err = helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
code, resp, err = helper.TestEndpoint(router, "", "healthz", http.MethodGet, nil)
assert.NoError(t, err)
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
// connect AMQP client (make sure that AMQP_URL is set via command line parameter -amqp)
url, _ := c.String("amqp.url")
url, _ := c.Config.String("amqp.url")
err = amqp.ConnectAMQP(url)
assert.NoError(t, err)
// test healthz endpoint for connected DB and AMQP client
code, resp, err = helper.TestEndpoint(router, token, "api/healthz", http.MethodGet, nil)
code, resp, err = helper.TestEndpoint(router, "", "healthz", http.MethodGet, nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
}