From 31e14843fe1106c71310114d9c9656e6df188dcd Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 19:35:44 +0300 Subject: [PATCH 1/8] Enabling API tests in CI --- .gitlab-ci.yml | 12 ++++++------ Makefile | 3 +++ api_test.sh | 3 +++ test/fn-api-tests/Dockerfile | 6 ++++++ 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 test/fn-api-tests/Dockerfile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 06435dfab..ec75e28e2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -62,13 +62,13 @@ test_job: script: - DOCKER_LOCATION=container_ip ./test.sh -#integration_tests: -# stage: test -# script: -# - go build -o functions-alpine -# - docker build -t funcy/functions:latest . +integration_tests: + stage: test + script: + - go build -o functions-alpine + - docker build -t funcy/functions:latest . # - make docker-test-run-with-bolt -# - make docker-test-run-with-mysql + - make docker-test-run-with-sqlite3 # - make docker-test-run-with-postgres # - make docker-test-run-with-redis diff --git a/Makefile b/Makefile index 15bda2df6..410057a4b 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,9 @@ docker-test-run-with-mysql: docker-test-run-with-postgres: ./api_test.sh postgres +ci-docker-test-run-with-sqlite3: + docker run --rm -i -v ${CURDIR}:/go/src/gitlab-odx.oracle.com/odx/functions -w /go/src/gitlab-odx.oracle.com/odx/functions -e "datastore=sqlite3" funcy/fn-api-tester + docker-test: docker run -ti --privileged --rm -e LOG_LEVEL=debug \ -v /var/run/docker.sock:/var/run/docker.sock \ diff --git a/api_test.sh b/api_test.sh index e34afffc4..454515c24 100755 --- a/api_test.sh +++ b/api_test.sh @@ -6,6 +6,7 @@ case "$1" in docker run --name func-server --privileged -v /var/run/docker.sock:/var/run/docker.sock -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -p 8080:8080 funcy/functions sleep 1 + docker logs func-server ;; "mysql" ) @@ -17,6 +18,7 @@ case "$1" in export MYSQL_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-mysql-test)" export MYSQL_PORT=3306 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=mysql://root:root@tcp(${MYSQL_HOST}:${MYSQL_PORT})/funcs" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions + docker logs func-server ;; @@ -29,6 +31,7 @@ case "$1" in export POSTGRES_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-postgres-test)" export POSTGRES_PORT=5432 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=postgres://postgres:root@${POSTGRES_HOST}:${POSTGRES_PORT}/funcs?sslmode=disable" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions + docker logs func-server ;; esac diff --git a/test/fn-api-tests/Dockerfile b/test/fn-api-tests/Dockerfile new file mode 100644 index 000000000..97f807269 --- /dev/null +++ b/test/fn-api-tests/Dockerfile @@ -0,0 +1,6 @@ +FROM funcy/go-dind + +ADD . /go/src/gitlab-odx.oracle.com/odx/functions +WORKDIR /go/src/gitlab-odx.oracle.com/odx/functions + +CMD ["/bin/sh", "-c", "set -o pipefail && ./api_test.sh sqlite3"] From bcd79ce4a6382d250b6e955a024cc98e3a3ebfbe Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 19:53:05 +0300 Subject: [PATCH 2/8] Enable integration tests in CI --- .gitlab-ci.yml | 2 +- Makefile | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ec75e28e2..aa26c6537 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -68,7 +68,7 @@ integration_tests: - go build -o functions-alpine - docker build -t funcy/functions:latest . # - make docker-test-run-with-bolt - - make docker-test-run-with-sqlite3 + - DOCKER_LOCATION=container_ip make docker-test-run-with-sqlite3 # - make docker-test-run-with-postgres # - make docker-test-run-with-redis diff --git a/Makefile b/Makefile index 410057a4b..dc632f348 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ docker-test-run-with-postgres: ./api_test.sh postgres ci-docker-test-run-with-sqlite3: + cd ${CURDIR}/test/fn-api-tests/ && docker build -t funcy/fn-api-tester .; cd ${CURDIR} docker run --rm -i -v ${CURDIR}:/go/src/gitlab-odx.oracle.com/odx/functions -w /go/src/gitlab-odx.oracle.com/odx/functions -e "datastore=sqlite3" funcy/fn-api-tester docker-test: From edde79e06156c13be9a3bb53b760ea08085213f9 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 19:56:26 +0300 Subject: [PATCH 3/8] Trying to figure out networking problems --- .gitlab-ci.yml | 7 +++---- api_test.sh | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index aa26c6537..9ce8396f8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -67,10 +67,9 @@ integration_tests: script: - go build -o functions-alpine - docker build -t funcy/functions:latest . -# - make docker-test-run-with-bolt - - DOCKER_LOCATION=container_ip make docker-test-run-with-sqlite3 -# - make docker-test-run-with-postgres -# - make docker-test-run-with-redis +# - DOCKER_LOCATION=container_ip make docker-test-run-with-sqlite3 +# - DOCKER_LOCATION=container_ip make docker-test-run-with-postgres + - DOCKER_LOCATION=container_ip make docker-test-run-with-mysql deploy_job: only: diff --git a/api_test.sh b/api_test.sh index 454515c24..fb02913ad 100755 --- a/api_test.sh +++ b/api_test.sh @@ -7,6 +7,7 @@ case "$1" in docker run --name func-server --privileged -v /var/run/docker.sock:/var/run/docker.sock -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -p 8080:8080 funcy/functions sleep 1 docker logs func-server + docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server ;; "mysql" ) @@ -19,6 +20,8 @@ case "$1" in export MYSQL_PORT=3306 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=mysql://root:root@tcp(${MYSQL_HOST}:${MYSQL_PORT})/funcs" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions docker logs func-server + docker inspect -f '{{.NetworkSettings.IPAddress}}' func-mysql-test + docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server ;; @@ -32,6 +35,8 @@ case "$1" in export POSTGRES_PORT=5432 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=postgres://postgres:root@${POSTGRES_HOST}:${POSTGRES_PORT}/funcs?sslmode=disable" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions docker logs func-server + docker inspect -f '{{.NetworkSettings.IPAddress}}' func-postgres-test + docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server ;; esac From a83cc8996803cbf9b084f5b0162799f3bd77d440 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 20:10:06 +0300 Subject: [PATCH 4/8] Increasing MySQL startup timeout --- Makefile | 2 +- api_test.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index dc632f348..3eda1cc5c 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ docker-test-run-with-postgres: ci-docker-test-run-with-sqlite3: cd ${CURDIR}/test/fn-api-tests/ && docker build -t funcy/fn-api-tester .; cd ${CURDIR} - docker run --rm -i -v ${CURDIR}:/go/src/gitlab-odx.oracle.com/odx/functions -w /go/src/gitlab-odx.oracle.com/odx/functions -e "datastore=sqlite3" funcy/fn-api-tester + docker run --rm -i -v ${CURDIR}:/go/src/gitlab-odx.oracle.com/odx/functions -w /go/src/gitlab-odx.oracle.com/odx/functions -e "datastore=sqlite3" -e "DOCKER_HOST=${DOCKERHOST}" funcy/fn-api-tester docker-test: docker run -ti --privileged --rm -e LOG_LEVEL=debug \ diff --git a/api_test.sh b/api_test.sh index fb02913ad..a7f6ca31d 100755 --- a/api_test.sh +++ b/api_test.sh @@ -15,7 +15,7 @@ case "$1" in docker rm -fv func-server || echo No prev func-server container docker run --name func-mysql-test -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql - sleep 8 + sleep 20 export MYSQL_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-mysql-test)" export MYSQL_PORT=3306 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=mysql://root:root@tcp(${MYSQL_HOST}:${MYSQL_PORT})/funcs" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions From 823ee23d3747c9cc39691f6b196b66a6b47b7f0e Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 22:18:16 +0300 Subject: [PATCH 5/8] More verbosity --- api_test.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api_test.sh b/api_test.sh index a7f6ca31d..11ae6c768 100755 --- a/api_test.sh +++ b/api_test.sh @@ -5,7 +5,7 @@ case "$1" in docker rm -fv func-server || echo No prev func-server container docker run --name func-server --privileged -v /var/run/docker.sock:/var/run/docker.sock -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -p 8080:8080 funcy/functions - sleep 1 + sleep 20 docker logs func-server docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server ;; @@ -16,6 +16,7 @@ case "$1" in docker run --name func-mysql-test -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql sleep 20 + docker logs func-mysql-test export MYSQL_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-mysql-test)" export MYSQL_PORT=3306 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=mysql://root:root@tcp(${MYSQL_HOST}:${MYSQL_PORT})/funcs" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions @@ -30,7 +31,8 @@ case "$1" in docker rm -fv func-server || echo No prev func-server container docker run --name func-postgres-test -e "POSTGRES_DB=funcs" -e "POSTGRES_PASSWORD=root" -p 5432:5432 -d postgres - sleep 8 + sleep 20 + docker logs func-postgres-test export POSTGRES_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-postgres-test)" export POSTGRES_PORT=5432 docker run --name func-server --privileged -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -e "DB_URL=postgres://postgres:root@${POSTGRES_HOST}:${POSTGRES_PORT}/funcs?sslmode=disable" -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock funcy/functions From 84c9bb9b93392dad856562b05b62e83d6f006787 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 22:31:12 +0300 Subject: [PATCH 6/8] Increase startup timeouts --- api_test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api_test.sh b/api_test.sh index 11ae6c768..3961599ab 100755 --- a/api_test.sh +++ b/api_test.sh @@ -5,7 +5,7 @@ case "$1" in docker rm -fv func-server || echo No prev func-server container docker run --name func-server --privileged -v /var/run/docker.sock:/var/run/docker.sock -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -p 8080:8080 funcy/functions - sleep 20 + sleep 10 docker logs func-server docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server ;; @@ -15,7 +15,7 @@ case "$1" in docker rm -fv func-server || echo No prev func-server container docker run --name func-mysql-test -p 3306:3306 -e MYSQL_DATABASE=funcs -e MYSQL_ROOT_PASSWORD=root -d mysql - sleep 20 + sleep 30 docker logs func-mysql-test export MYSQL_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-mysql-test)" export MYSQL_PORT=3306 @@ -31,7 +31,7 @@ case "$1" in docker rm -fv func-server || echo No prev func-server container docker run --name func-postgres-test -e "POSTGRES_DB=funcs" -e "POSTGRES_PASSWORD=root" -p 5432:5432 -d postgres - sleep 20 + sleep 30 docker logs func-postgres-test export POSTGRES_HOST="$(docker inspect -f '{{.NetworkSettings.IPAddress}}' func-postgres-test)" export POSTGRES_PORT=5432 From 0ac91e6e7cdadc12e767415aff26b39c7e0ae533 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Fri, 21 Jul 2017 22:36:54 +0300 Subject: [PATCH 7/8] Run tests against sqlite3 --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9ce8396f8..ceec6005d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -67,9 +67,9 @@ integration_tests: script: - go build -o functions-alpine - docker build -t funcy/functions:latest . -# - DOCKER_LOCATION=container_ip make docker-test-run-with-sqlite3 + - DOCKER_LOCATION=container_ip make docker-test-run-with-sqlite3 # - DOCKER_LOCATION=container_ip make docker-test-run-with-postgres - - DOCKER_LOCATION=container_ip make docker-test-run-with-mysql +# - DOCKER_LOCATION=container_ip make docker-test-run-with-mysql deploy_job: only: From 9654fd4179ebdb905b9dd479c957b8d8e9d0fbaf Mon Sep 17 00:00:00 2001 From: James Jeffrey Date: Fri, 21 Jul 2017 16:10:56 -0700 Subject: [PATCH 8/8] Make tests use local functions server not one in container --- .gitlab-ci.yml | 30 +----------- Makefile | 4 -- api_test.sh | 12 ++--- test/fn-api-tests/Dockerfile | 6 --- test/fn-api-tests/exec_test.go | 3 +- test/fn-api-tests/init_test.go | 13 +++++ test/fn-api-tests/utils.go | 86 +++++++++++++++++++++++++++++----- 7 files changed, 96 insertions(+), 58 deletions(-) delete mode 100644 test/fn-api-tests/Dockerfile create mode 100644 test/fn-api-tests/init_test.go diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ceec6005d..e6958c2bd 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,32 +15,6 @@ before_script: - ln -s `pwd` "${GOPATH}/src/gitlab-odx.oracle.com/odx/" - cd "${GOPATH}/src/gitlab-odx.oracle.com/odx/functions" -# get_deps: -# stage: deps -# script: -# - cd "${GOPATH}/src/gitlab-odx.oracle.com/odx/functions" -# - pwd -# - go get github.com/Masterminds/glide -# - glide install --strip-vendor -# - cd "${GOPATH}/src/gitlab-odx.oracle.com/odx/functions/fn" -# - pwd -# - glide install --strip-vendor -# only: -# - master -# -# load_deps: -# stage: deps -# script: -# - cd "${GOPATH}/src/gitlab-odx.oracle.com/odx/functions" -# - pwd -# - ls vendor/github.com/ > /dev/null || which glide || go get github.com/Masterminds/glide -# - ls vendor/github.com/ > /dev/null || glide install --strip-vendor -# - cd "${GOPATH}/src/gitlab-odx.oracle.com/odx/functions/fn" -# - pwd -# - ls vendor/github.com/ > /dev/null || glide install --strip-vendor -# except: -# - master - build_job: stage: build script: @@ -67,9 +41,7 @@ integration_tests: script: - go build -o functions-alpine - docker build -t funcy/functions:latest . - - DOCKER_LOCATION=container_ip make docker-test-run-with-sqlite3 -# - DOCKER_LOCATION=container_ip make docker-test-run-with-postgres -# - DOCKER_LOCATION=container_ip make docker-test-run-with-mysql + - make docker-test-run-with-sqlite3 deploy_job: only: diff --git a/Makefile b/Makefile index 3eda1cc5c..15bda2df6 100644 --- a/Makefile +++ b/Makefile @@ -49,10 +49,6 @@ docker-test-run-with-mysql: docker-test-run-with-postgres: ./api_test.sh postgres -ci-docker-test-run-with-sqlite3: - cd ${CURDIR}/test/fn-api-tests/ && docker build -t funcy/fn-api-tester .; cd ${CURDIR} - docker run --rm -i -v ${CURDIR}:/go/src/gitlab-odx.oracle.com/odx/functions -w /go/src/gitlab-odx.oracle.com/odx/functions -e "datastore=sqlite3" -e "DOCKER_HOST=${DOCKERHOST}" funcy/fn-api-tester - docker-test: docker run -ti --privileged --rm -e LOG_LEVEL=debug \ -v /var/run/docker.sock:/var/run/docker.sock \ diff --git a/api_test.sh b/api_test.sh index 3961599ab..c306fa3ed 100755 --- a/api_test.sh +++ b/api_test.sh @@ -2,12 +2,12 @@ set -ex case "$1" in "sqlite3" ) - docker rm -fv func-server || echo No prev func-server container - - docker run --name func-server --privileged -v /var/run/docker.sock:/var/run/docker.sock -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -p 8080:8080 funcy/functions - sleep 10 - docker logs func-server - docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server + # docker rm -fv func-server || echo No prev func-server container + # + # docker run --name func-server --privileged -v /var/run/docker.sock:/var/run/docker.sock -d -e NO_PROXY -e HTTP_PROXY -e DOCKER_HOST=${DOCKER_HOST} -e LOG_LEVEL=debug -p 8080:8080 funcy/functions + # sleep 10 + # docker logs func-server + # docker inspect -f '{{.NetworkSettings.IPAddress}}' func-server ;; "mysql" ) diff --git a/test/fn-api-tests/Dockerfile b/test/fn-api-tests/Dockerfile deleted file mode 100644 index 97f807269..000000000 --- a/test/fn-api-tests/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM funcy/go-dind - -ADD . /go/src/gitlab-odx.oracle.com/odx/functions -WORKDIR /go/src/gitlab-odx.oracle.com/odx/functions - -CMD ["/bin/sh", "-c", "set -o pipefail && ./api_test.sh sqlite3"] diff --git a/test/fn-api-tests/exec_test.go b/test/fn-api-tests/exec_test.go index 687da3c64..94f46d103 100644 --- a/test/fn-api-tests/exec_test.go +++ b/test/fn-api-tests/exec_test.go @@ -51,6 +51,7 @@ func CallAsync(t *testing.T, u url.URL, content io.Reader) string { func TestRouteExecutions(t *testing.T) { s := SetupDefaultSuite() + newRouteType := "async" CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{}) @@ -203,7 +204,7 @@ func TestRouteExecutions(t *testing.T) { u.Path = path.Join(u.Path, "r", s.AppName, routePath) callID := CallAsync(t, u, &bytes.Buffer{}) - time.Sleep(7 * time.Second) + time.Sleep(15 * time.Second) cfg := &operations.GetCallsCallLogParams{ Call: callID, diff --git a/test/fn-api-tests/init_test.go b/test/fn-api-tests/init_test.go new file mode 100644 index 000000000..02db3798a --- /dev/null +++ b/test/fn-api-tests/init_test.go @@ -0,0 +1,13 @@ +package tests + +import ( + "os" + "testing" +) + +func TestMain(m *testing.M) { + // call flag.Parse() here if TestMain uses flags + s := SetupDefaultSuite() + defer s.Cancel() + os.Exit(m.Run()) +} diff --git a/test/fn-api-tests/utils.go b/test/fn-api-tests/utils.go index 988dad553..a0f04bb6d 100644 --- a/test/fn-api-tests/utils.go +++ b/test/fn-api-tests/utils.go @@ -3,21 +3,26 @@ package tests import ( "context" "strings" + "sync" "testing" "time" + "gitlab-odx.oracle.com/odx/functions/api/server" + "fmt" + "io" + "log" + "net/http" + "net/url" + "os" + "github.com/funcy/functions_go/client" "github.com/funcy/functions_go/client/apps" "github.com/funcy/functions_go/client/routes" "github.com/funcy/functions_go/models" httptransport "github.com/go-openapi/runtime/client" "github.com/go-openapi/strfmt" - "io" - "log" - "net/http" - "net/url" - "os" + "github.com/spf13/viper" ) func Host() string { @@ -41,16 +46,53 @@ func APIClient() *client.Functions { // create the API client, with the transport client := client.New(transport, strfmt.Default) - _, err := client.Version.GetVersion(nil) - if err != nil { - if Host() != "localhost:8080" { - panic("Cannot reach remote api for functions") - } - } return client } +var ( + getServer sync.Once +) + +func getServerWithCancel() (*server.Server, context.CancelFunc) { + var cancel2 context.CancelFunc + var s *server.Server + getServer.Do(func() { + ctx, cancel := context.WithCancel(context.Background()) + + viper.Set(server.EnvPort, "8080") + viper.Set(server.EnvAPIURL, "http://localhost:8080") + viper.Set(server.EnvLogLevel, "fatal") + timeString := time.Now().Format("2006_01_02_15_04_05") + tmpDir := os.TempDir() + tmpMq := fmt.Sprintf("%s/fn_integration_test_%s_worker_mq.db", tmpDir, timeString) + tmpDB := fmt.Sprintf("%s/fn_integration_test_%s_fn.db", tmpDir, timeString) + viper.Set(server.EnvMQURL, fmt.Sprintf("bolt://%s", tmpMq)) + viper.Set(server.EnvDBURL, fmt.Sprintf("sqlite3://%s", tmpDB)) + + s = server.NewFromEnv(ctx) + + go s.Start(ctx) + started := false + time.AfterFunc(time.Second*10, func() { + if !started { + panic("Failed to start server.") + } + }) + _, err := http.Get(viper.GetString(server.EnvAPIURL) + "/version") + for err != nil { + _, err = http.Get(viper.GetString(server.EnvAPIURL) + "/version") + } + started = true + cancel2 = context.CancelFunc(func() { + cancel() + os.Remove(tmpMq) + os.Remove(tmpDB) + }) + }) + return s, cancel2 +} + type SuiteSetup struct { Context context.Context Client *client.Functions @@ -62,10 +104,11 @@ type SuiteSetup struct { Memory int64 RouteConfig map[string]string RouteHeaders map[string][]string + Cancel context.CancelFunc } func SetupDefaultSuite() *SuiteSetup { - return &SuiteSetup{ + ss := &SuiteSetup{ Context: context.Background(), Client: APIClient(), AppName: "test-app", @@ -75,7 +118,26 @@ func SetupDefaultSuite() *SuiteSetup { RouteType: "async", RouteConfig: map[string]string{}, RouteHeaders: map[string][]string{}, + Cancel: func() {}, } + + _, ok := ss.Client.Version.GetVersion(nil) + if ok != nil { + if Host() != "localhost:8080" { + _, ok := http.Get(fmt.Sprintf("http://%s/version", Host())) + if ok != nil { + panic("Cannot reach remote api for functions") + } + } else { + _, ok := http.Get(fmt.Sprintf("http://%s/version", Host())) + if ok != nil { + log.Println("Making functions server") + _, cancel := getServerWithCancel() + ss.Cancel = cancel + } + } + } + return ss } func CheckAppResponseError(t *testing.T, err error) {