From 2c5400a0ccda23acdf48445666b914f4a706402b Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 13 Nov 2019 21:29:17 +0100 Subject: [PATCH] fix tests --- .gitlab-ci.yml | 11 ++++++--- config/config.go | 25 ++++++++++++--------- database/database.go | 1 + routes/healthz/healthz_test.go | 41 +++++++++++++++------------------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index dad8f07..b6c2331 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: diff --git a/config/config.go b/config/config.go index c9dbf31..233ac96 100644 --- a/config/config.go +++ b/config/config.go @@ -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) diff --git a/database/database.go b/database/database.go index d7f5eca..21d5d63 100644 --- a/database/database.go +++ b/database/database.go @@ -30,6 +30,7 @@ func InitDB(cfg *config.Config) *gorm.DB { if err != nil { log.Fatal(err) } + DBpool = db MigrateModels(db) diff --git a/routes/healthz/healthz_test.go b/routes/healthz/healthz_test.go index 9d633a3..e33152a 100644 --- a/routes/healthz/healthz_test.go +++ b/routes/healthz/healthz_test.go @@ -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) }