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) {