MCPcopy
hub / github.com/iron-io/functions / preparePostgresTest

Function preparePostgresTest

api/datastore/postgres/postgres_test.go:17–67  ·  view source on GitHub ↗
(logf, fatalf func(string, ...interface{}))

Source from the content-addressed store, hash-verified

15const tmpPostgres = "postgres://postgres@%v:15432/funcs?sslmode=disable"
16
17func preparePostgresTest(logf, fatalf func(string, ...interface{})) (func(), func()) {
18 fmt.Println("initializing postgres for test")
19 tryRun(logf, "remove old postgres container", exec.Command("docker", "rm", "-f", "iron-postgres-test"))
20 mustRun(fatalf, "start postgres container", exec.Command("docker", "run", "--name", "iron-postgres-test", "-p", "15432:5432", "-d", "postgres"))
21
22 wait := 1 * time.Second
23 for {
24 db, err := sql.Open("postgres", fmt.Sprintf("postgres://postgres@%v:15432?sslmode=disable",
25 datastoretest.GetContainerHostIP()))
26 if err != nil {
27 fmt.Println("failed to connect to postgres:", err)
28 fmt.Println("retrying in:", wait)
29 time.Sleep(wait)
30 wait = 2 * wait
31 continue
32 }
33
34 _, err = db.Exec(`CREATE DATABASE funcs;`)
35 if err != nil {
36 fmt.Println("failed to create database:", err)
37 fmt.Println("retrying in:", wait)
38 time.Sleep(wait)
39 wait = 2 * wait
40 continue
41 }
42 _, err = db.Exec(`GRANT ALL PRIVILEGES ON DATABASE funcs TO postgres;`)
43 if err == nil {
44 break
45 }
46 fmt.Println("failed to grant privileges:", err)
47 fmt.Println("retrying in:", wait)
48 time.Sleep(wait)
49 wait = 2 * wait
50 }
51 fmt.Println("postgres for test ready")
52 return func() {
53 db, err := sql.Open("postgres", fmt.Sprintf(tmpPostgres, datastoretest.GetContainerHostIP()))
54 if err != nil {
55 fatalf("failed to connect for truncation: %s\n", err)
56 }
57 for _, table := range []string{"routes", "apps", "extras"} {
58 _, err = db.Exec(`TRUNCATE TABLE ` + table)
59 if err != nil {
60 fatalf("failed to truncate table %q: %s\n", table, err)
61 }
62 }
63 },
64 func() {
65 tryRun(logf, "stop postgres container", exec.Command("docker", "rm", "-f", "iron-postgres-test"))
66 }
67}
68
69func TestDatastore(t *testing.T) {
70 _, close := preparePostgresTest(t.Logf, t.Fatalf)

Callers 1

TestDatastoreFunction · 0.85

Calls 3

CommandMethod · 0.80
tryRunFunction · 0.70
mustRunFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…