mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Deploy sh
This commit is contained in:
committed by
Reed Allman
parent
aa170c918f
commit
79f1dab007
@@ -4,46 +4,67 @@ import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitlab-odx.oracle.com/odx/functions/api/datastore/internal/datastoretest"
|
||||
)
|
||||
|
||||
const tmpMysql = "mysql://root:root@tcp(%v:3307)/funcs"
|
||||
const tmpMysql = "mysql://root:root@tcp(%s:%d)/funcs"
|
||||
|
||||
var (
|
||||
mysqlHost = func() string {
|
||||
host := os.Getenv("MYSQL_HOST")
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return host
|
||||
}()
|
||||
mysqlPort = func() int {
|
||||
port := os.Getenv("MYSQL_PORT")
|
||||
if port == "" {
|
||||
port = "3307"
|
||||
}
|
||||
p, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return p
|
||||
}()
|
||||
)
|
||||
|
||||
func prepareMysqlTest(logf, fatalf func(string, ...interface{})) (func(), func()) {
|
||||
fmt.Println("initializing mysql for test")
|
||||
tryRun(logf, "remove old mysql container", exec.Command("docker", "rm", "-f", "func-mysql-test"))
|
||||
mustRun(fatalf, "start mysql container", exec.Command(
|
||||
"docker", "run", "--name", "func-mysql-test", "-p", "3307:3306", "-e", "MYSQL_DATABASE=funcs",
|
||||
"-e", "MYSQL_ROOT_PASSWORD=root", "-d", "mysql"))
|
||||
maxWait := 16 * time.Second
|
||||
timeout := time.After(60 * time.Second)
|
||||
wait := 2 * time.Second
|
||||
var db *sql.DB
|
||||
var err error
|
||||
var buf bytes.Buffer
|
||||
time.Sleep(time.Second * 25)
|
||||
for {
|
||||
db, err = sql.Open("mysql", fmt.Sprintf("root:root@tcp(%v:3307)/",
|
||||
datastoretest.GetContainerHostIP()))
|
||||
db, err = sql.Open("mysql", fmt.Sprintf("root:root@tcp(%s:%v)/",
|
||||
mysqlHost, mysqlPort))
|
||||
if err != nil {
|
||||
if wait > maxWait {
|
||||
fatalf("failed to connect to mysql after %d seconds", maxWait)
|
||||
fmt.Fprintln(&buf, "failed to connect to mysql:", err)
|
||||
fmt.Fprintln(&buf, "retrying in:", wait)
|
||||
} else {
|
||||
// Ping
|
||||
if _, err = db.Exec("SELECT 1"); err == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println("failed to connect to mysql:", err)
|
||||
fmt.Println("retrying in:", wait)
|
||||
time.Sleep(wait)
|
||||
fmt.Fprintln(&buf, "failed to ping database:", err)
|
||||
}
|
||||
select {
|
||||
case <-timeout:
|
||||
fmt.Println(buf.String())
|
||||
log.Fatal("timed out waiting for mysql")
|
||||
case <-time.After(wait):
|
||||
continue
|
||||
}
|
||||
// Ping
|
||||
if _, err = db.Exec("SELECT 1"); err != nil {
|
||||
fmt.Println("failed to ping database:", err)
|
||||
time.Sleep(wait)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
_, err = db.Exec("DROP DATABASE IF EXISTS funcs;")
|
||||
@@ -62,8 +83,8 @@ func prepareMysqlTest(logf, fatalf func(string, ...interface{})) (func(), func()
|
||||
|
||||
fmt.Println("mysql for test ready")
|
||||
return func() {
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("root:root@tcp(%v:3307)/",
|
||||
datastoretest.GetContainerHostIP()))
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("root:root@tcp(%s:%d)/",
|
||||
mysqlHost, mysqlPort))
|
||||
if err != nil {
|
||||
fatalf("failed to connect for truncation: %s\n", err)
|
||||
}
|
||||
@@ -75,7 +96,7 @@ func prepareMysqlTest(logf, fatalf func(string, ...interface{})) (func(), func()
|
||||
}
|
||||
},
|
||||
func() {
|
||||
tryRun(logf, "stop mysql container", exec.Command("docker", "rm", "-f", "func-mysql-test"))
|
||||
tryRun(logf, "stop mysql container", exec.Command("docker", "rm", "-vf", "func-mysql-test"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +104,7 @@ func TestDatastore(t *testing.T) {
|
||||
_, close := prepareMysqlTest(t.Logf, t.Fatalf)
|
||||
defer close()
|
||||
|
||||
u, err := url.Parse(fmt.Sprintf(tmpMysql, datastoretest.GetContainerHostIP()))
|
||||
u, err := url.Parse(fmt.Sprintf(tmpMysql, mysqlHost, mysqlPort))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse url: %s\n", err)
|
||||
}
|
||||
|
||||
@@ -4,53 +4,76 @@ import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitlab-odx.oracle.com/odx/functions/api/datastore/internal/datastoretest"
|
||||
)
|
||||
|
||||
const tmpPostgres = "postgres://postgres@%v:15432/funcs?sslmode=disable"
|
||||
const tmpPostgres = "postgres://postgres@%s:%d/funcs?sslmode=disable"
|
||||
|
||||
var (
|
||||
postgresHost = func() string {
|
||||
host := os.Getenv("POSTGRES_HOST")
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return host
|
||||
}()
|
||||
postgresPort = func() int {
|
||||
port := os.Getenv("POSTGRES_PORT")
|
||||
if port == "" {
|
||||
port = "15432"
|
||||
}
|
||||
p, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return p
|
||||
}()
|
||||
)
|
||||
|
||||
func preparePostgresTest(logf, fatalf func(string, ...interface{})) (func(), func()) {
|
||||
fmt.Println("initializing postgres for test")
|
||||
tryRun(logf, "remove old postgres container", exec.Command("docker", "rm", "-f", "func-postgres-test"))
|
||||
mustRun(fatalf, "start postgres container", exec.Command("docker", "run", "--name", "func-postgres-test", "-p", "15432:5432", "-d", "postgres"))
|
||||
timeout := time.After(20 * time.Second)
|
||||
wait := 500 * time.Millisecond
|
||||
|
||||
wait := 1 * time.Second
|
||||
for {
|
||||
db, err := sql.Open("postgres", fmt.Sprintf("postgres://postgres@%v:15432?sslmode=disable",
|
||||
datastoretest.GetContainerHostIP()))
|
||||
db, err := sql.Open("postgres", fmt.Sprintf("postgres://postgres@%s:%d?sslmode=disable",
|
||||
postgresHost, postgresPort))
|
||||
if err != nil {
|
||||
fmt.Println("failed to connect to postgres:", err)
|
||||
fmt.Println("retrying in:", wait)
|
||||
time.Sleep(wait)
|
||||
wait = 2 * wait
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
_, err = db.Exec(`CREATE DATABASE funcs;`)
|
||||
if err != nil {
|
||||
fmt.Println("failed to create database:", err)
|
||||
fmt.Println("retrying in:", wait)
|
||||
|
||||
_, err = db.Exec(`CREATE DATABASE funcs;`)
|
||||
if err != nil {
|
||||
fmt.Println("failed to create database:", err)
|
||||
fmt.Println("retrying in:", wait)
|
||||
time.Sleep(wait)
|
||||
wait = 2 * wait
|
||||
} else {
|
||||
_, err = db.Exec(`GRANT ALL PRIVILEGES ON DATABASE funcs TO postgres;`)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println("failed to grant privileges:", err)
|
||||
fmt.Println("retrying in:", wait)
|
||||
}
|
||||
|
||||
}
|
||||
select {
|
||||
case <-timeout:
|
||||
log.Fatal("timed out waiting for postgres")
|
||||
case <-time.After(wait):
|
||||
continue
|
||||
}
|
||||
_, err = db.Exec(`GRANT ALL PRIVILEGES ON DATABASE funcs TO postgres;`)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println("failed to grant privileges:", err)
|
||||
fmt.Println("retrying in:", wait)
|
||||
time.Sleep(wait)
|
||||
wait = 2 * wait
|
||||
}
|
||||
fmt.Println("postgres for test ready")
|
||||
return func() {
|
||||
db, err := sql.Open("postgres", fmt.Sprintf(tmpPostgres, datastoretest.GetContainerHostIP()))
|
||||
db, err := sql.Open("postgres", fmt.Sprintf(tmpPostgres, postgresHost, postgresPort))
|
||||
if err != nil {
|
||||
fatalf("failed to connect for truncation: %s\n", err)
|
||||
}
|
||||
@@ -62,7 +85,7 @@ func preparePostgresTest(logf, fatalf func(string, ...interface{})) (func(), fun
|
||||
}
|
||||
},
|
||||
func() {
|
||||
tryRun(logf, "stop postgres container", exec.Command("docker", "rm", "-f", "func-postgres-test"))
|
||||
tryRun(logf, "stop postgres container", exec.Command("docker", "rm", "-fv", "func-postgres-test"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +93,7 @@ func TestDatastore(t *testing.T) {
|
||||
_, close := preparePostgresTest(t.Logf, t.Fatalf)
|
||||
defer close()
|
||||
|
||||
u, err := url.Parse(fmt.Sprintf(tmpPostgres, datastoretest.GetContainerHostIP()))
|
||||
u, err := url.Parse(fmt.Sprintf(tmpPostgres, postgresHost, postgresPort))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse url: %v", err)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -13,16 +15,34 @@ import (
|
||||
"gitlab-odx.oracle.com/odx/functions/api/datastore/internal/datastoretest"
|
||||
)
|
||||
|
||||
const tmpRedis = "redis://%v:6301/"
|
||||
const tmpRedis = "redis://%s:%d/"
|
||||
|
||||
var (
|
||||
redisHost = func() string {
|
||||
host := os.Getenv("REDIS_HOST")
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return host
|
||||
}()
|
||||
redisPort = func() int {
|
||||
port := os.Getenv("REDIS_PORT")
|
||||
if port == "" {
|
||||
port = "6301"
|
||||
}
|
||||
p, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return p
|
||||
}()
|
||||
)
|
||||
|
||||
func prepareRedisTest(logf, fatalf func(string, ...interface{})) (func(), func()) {
|
||||
fmt.Println("initializing redis for test")
|
||||
tryRun(logf, "remove old redis container", exec.Command("docker", "rm", "-f", "func-redis-test"))
|
||||
mustRun(fatalf, "start redis container", exec.Command("docker", "run", "--name", "func-redis-test", "-p", "6301:6379", "-d", "redis"))
|
||||
timeout := time.After(20 * time.Second)
|
||||
|
||||
for {
|
||||
c, err := redis.DialURL(fmt.Sprintf(tmpRedis, datastoretest.GetContainerHostIP()))
|
||||
c, err := redis.DialURL(fmt.Sprintf(tmpRedis, redisHost, redisPort))
|
||||
if err == nil {
|
||||
_, err = c.Do("PING")
|
||||
c.Close()
|
||||
@@ -41,7 +61,7 @@ func prepareRedisTest(logf, fatalf func(string, ...interface{})) (func(), func()
|
||||
fmt.Println("redis for test ready")
|
||||
return func() {},
|
||||
func() {
|
||||
tryRun(logf, "stop redis container", exec.Command("docker", "rm", "-f", "func-redis-test"))
|
||||
tryRun(logf, "stop redis container", exec.Command("docker", "rm", "-fv", "func-redis-test"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +69,7 @@ func TestDatastore(t *testing.T) {
|
||||
_, close := prepareRedisTest(t.Logf, t.Fatalf)
|
||||
defer close()
|
||||
|
||||
u, err := url.Parse(fmt.Sprintf(tmpRedis, datastoretest.GetContainerHostIP()))
|
||||
u, err := url.Parse(fmt.Sprintf(tmpRedis, redisHost, redisPort))
|
||||
if err != nil {
|
||||
t.Fatal("failed to parse url: ", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user