ci: parametrize DB port for unit tests
Otherwise we can't run tests in parallel on the same host. Also the container name has to be different depending on executor. Resolves errors like: ``` docker: Error response from daemon: Conflict. The container name "/status-go-test-db" is already in use by container "123...". You have to remove (or rename) that container to be able to reuse that name. ``` Resolves: https://github.com/status-im/status-go/issues/4040 Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
e98952588d
commit
494c6707ba
|
@ -25,7 +25,8 @@ pipeline {
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
TARGET = 'tests'
|
TARGET = 'tests'
|
||||||
DB_CONT = 'status-go-test-db'
|
DB_CONT = "status-go-test-db-${env.EXECUTOR_NUMBER.toInteger() + 1}"
|
||||||
|
DB_PORT = "${5432 + env.EXECUTOR_NUMBER.toInteger()}"
|
||||||
TMPDIR = "${WORKSPACE_TMP}"
|
TMPDIR = "${WORKSPACE_TMP}"
|
||||||
GOPATH = "${WORKSPACE_TMP}/go"
|
GOPATH = "${WORKSPACE_TMP}/go"
|
||||||
GOCACHE = "${WORKSPACE_TMP}/gocache"
|
GOCACHE = "${WORKSPACE_TMP}/gocache"
|
||||||
|
@ -69,12 +70,15 @@ pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Unit Tests') {
|
stage('Unit Tests') {
|
||||||
|
environment {
|
||||||
|
TEST_POSTGRES_PORT = "${env.DB_PORT}"
|
||||||
|
}
|
||||||
steps { script {
|
steps { script {
|
||||||
db = docker.image('postgres:9.6-alpine').withRun([
|
db = docker.image('postgres:9.6-alpine').withRun([
|
||||||
"--name=${DB_CONT}",
|
"--name=${DB_CONT}",
|
||||||
'--env=POSTGRES_HOST_AUTH_METHOD=trust',
|
"--env=POSTGRES_HOST_AUTH_METHOD=trust",
|
||||||
'--publish=5432:5432',
|
"--publish=${DB_PORT}:${DB_PORT}",
|
||||||
].join(' ')) { c ->
|
].join(' '), "-p ${DB_PORT}") { c ->
|
||||||
nix.shell('make generate-handlers', pure: true)
|
nix.shell('make generate-handlers', pure: true)
|
||||||
nix.shell('make test-unit V=1', pure: false)
|
nix.shell('make test-unit V=1', pure: false)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,28 @@ package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
// Import postgres driver
|
// Import postgres driver
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
DefaultTestURI = "postgres://postgres@127.0.0.1:5432/postgres?sslmode=disable"
|
DefaultTestDBHost = GetEnvDefault("TEST_POSTGRES_HOST", "localhost")
|
||||||
DropTableURI = "postgres://postgres@127.0.0.1:5432/template1?sslmode=disable"
|
DefaultTestDBPort = GetEnvDefault("TEST_POSTGRES_PORT", "5432")
|
||||||
|
DefaultTestURI = fmt.Sprintf("postgres://postgres@%s:%s/postgres?sslmode=disable", DefaultTestDBHost, DefaultTestDBPort)
|
||||||
|
DropTableURI = fmt.Sprintf("postgres://postgres@%s:%s/template1?sslmode=disable", DefaultTestDBHost, DefaultTestDBPort)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetEnvDefault(key, fallback string) string {
|
||||||
|
value := os.Getenv(key)
|
||||||
|
if len(value) == 0 {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
func ResetDefaultTestPostgresDB() error {
|
func ResetDefaultTestPostgresDB() error {
|
||||||
db, err := sql.Open("postgres", DropTableURI)
|
db, err := sql.Open("postgres", DropTableURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue