mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
fix tests
This commit is contained in:
parent
a1f7ea9ed9
commit
2c5400a0cc
4 changed files with 42 additions and 36 deletions
|
@ -76,13 +76,14 @@ test:apidoc:
|
||||||
- doc/api/index.html
|
- doc/api/index.html
|
||||||
- doc/api/swagger.json
|
- doc/api/swagger.json
|
||||||
|
|
||||||
test:all:
|
test:gotest:
|
||||||
stage: test
|
stage: test
|
||||||
variables:
|
variables:
|
||||||
DB_NAME: ${POSTGRES_DB}
|
DB_NAME: ${POSTGRES_DB}
|
||||||
DB_HOST: ${POSTGRES_HOST}
|
DB_HOST: ${POSTGRES_HOST}
|
||||||
DB_USER: ${POSTGRES_USER}
|
DB_USER: ${POSTGRES_USER}
|
||||||
DB_PASS: ${POSTGRES_PASSWORD}
|
DB_PASS: ${POSTGRES_PASSWORD}
|
||||||
|
MODE: test
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
image: golang:1.12.9-buster
|
image: golang:1.12.9-buster
|
||||||
|
@ -179,9 +180,13 @@ deploy:docker:
|
||||||
|
|
||||||
# deploy:upload:
|
# deploy:upload:
|
||||||
# stage: deploy
|
# 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:
|
# script:
|
||||||
# - cd doc/api
|
# - rclone copy --include=index.html --include=swagger.json doc/api fein:villas/api/web
|
||||||
# - rsync --copy-links --chown ${DEPLOY_USER}:${DEPLOY_USER} index.html swagger.json ${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_PATH}
|
|
||||||
# dependencies:
|
# dependencies:
|
||||||
# - test:apidoc
|
# - test:apidoc
|
||||||
# only:
|
# only:
|
||||||
|
|
|
@ -9,9 +9,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Global configuration
|
// Global configuration
|
||||||
var Config *config.Config
|
var Config *config.Config = nil
|
||||||
|
|
||||||
func InitConfig() *config.Config {
|
func InitConfig() *config.Config {
|
||||||
|
if Config != nil {
|
||||||
|
return Config
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dbHost = flag.String("dbhost", "/var/run/postgresql", "Host of the PostgreSQL database (default is /var/run/postgresql for localhost DB on Ubuntu systems)")
|
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)")
|
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{
|
mappings := map[string]string{
|
||||||
"DB_HOST": "db.host",
|
"DB_HOST": "db.host",
|
||||||
"DB_NAME": "db.name",
|
"DB_NAME": "db.name",
|
||||||
"DB_USER": "db.user",
|
"DB_USER": "db.user",
|
||||||
"DB_PASS": "db.pass",
|
"DB_PASS": "db.pass",
|
||||||
"DB_SSLMOE": "db.ssl",
|
"DB_SSLMODE": "db.ssl",
|
||||||
"DB_INIT": "db.init",
|
"DB_INIT": "db.init",
|
||||||
"AMQP_URL": "amqp.url",
|
"AMQP_URL": "amqp.url",
|
||||||
"BASE_HOST": "base.host",
|
"BASE_HOST": "base.host",
|
||||||
"BASE_PATH": "base.path",
|
"BASE_PATH": "base.path",
|
||||||
|
"MODE": "mode",
|
||||||
}
|
}
|
||||||
|
|
||||||
defaults := config.NewStatic(static)
|
defaults := config.NewStatic(static)
|
||||||
|
|
|
@ -30,6 +30,7 @@ func InitDB(cfg *config.Config) *gorm.DB {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
DBpool = db
|
DBpool = db
|
||||||
|
|
||||||
MigrateModels(db)
|
MigrateModels(db)
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
package healthz
|
package healthz
|
||||||
|
|
||||||
import (
|
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/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/database"
|
||||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
|
"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/gin-gonic/gin"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -17,51 +18,45 @@ import (
|
||||||
var router *gin.Engine
|
var router *gin.Engine
|
||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
c.InitConfig()
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
func TestHealthz(t *testing.T) {
|
func TestHealthz(t *testing.T) {
|
||||||
// connect DB
|
// connect DB
|
||||||
c := config.InitConfig()
|
db = database.InitDB(c.Config)
|
||||||
db = database.InitDB(c)
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
assert.NoError(t, database.DBAddAdminAndUserAndGuest(db))
|
|
||||||
|
|
||||||
router = gin.Default()
|
router = gin.Default()
|
||||||
api := router.Group("/api")
|
|
||||||
|
|
||||||
user.RegisterAuthenticate(api.Group("/authenticate"))
|
RegisterHealthzEndpoint(router.Group("/healthz"))
|
||||||
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)
|
|
||||||
|
|
||||||
// close db connection
|
// close db connection
|
||||||
err = db.Close()
|
err := db.Close()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// test healthz endpoint for unconnected DB and AMQP client
|
// 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.NoError(t, err)
|
||||||
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
|
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
|
||||||
|
|
||||||
// reconnect DB
|
// reconnect DB
|
||||||
db = database.InitDB(c)
|
db = database.InitDB(c.Config)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// test healthz endpoint for connected DB and unconnected AMQP client
|
// 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.NoError(t, err)
|
||||||
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
|
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)
|
// 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)
|
err = amqp.ConnectAMQP(url)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// test healthz endpoint for connected DB and AMQP client
|
// 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.NoError(t, err)
|
||||||
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
|
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue