Skip to content

Commit

Permalink
Merge pull request rapidpro#96 from nyaruka/docker_test_db
Browse files Browse the repository at this point in the history
Support restoring test database inside docker container
  • Loading branch information
rowanseymour authored Jul 19, 2023
2 parents 8aa8cf9 + 6782e08 commit 38ee06d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
POSTGRES_PASSWORD: temba
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
options: --name textit-postgres-1 --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
elastic:
image: elasticsearch:7.17.9
ports:
Expand Down
31 changes: 20 additions & 11 deletions testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"path"
"strings"

"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
Expand All @@ -22,6 +21,7 @@ var _db *sqlx.DB

const elasticURL = "http://localhost:9200"
const elasticContactsIndex = "test_contacts"
const postgresContainerName = "textit-postgres-1"

const attachmentStorageDir = "_test_attachments_storage"
const sessionStorageDir = "_test_session_storage"
Expand Down Expand Up @@ -162,17 +162,12 @@ func resetDB() {
}

func loadTestDump() {
dir, _ := os.Getwd()
dump, err := os.Open(absPath("./mailroom_test.dump"))
must(err)
defer dump.Close()

// our working directory is set to the directory of the module being tested, we want to get just
// the portion that points to the mailroom directory
for !strings.HasSuffix(dir, "mailroom") && dir != "/" {
dir = path.Dir(dir)
}

cmd := exec.Command("pg_restore", "-h", "localhost", "-d", "mailroom_test", "-U", "mailroom_test", path.Join(dir, "./mailroom_test.dump"))
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PGPASSWORD=temba")
cmd := exec.Command("docker", "exec", "-i", postgresContainerName, "pg_restore", "-d", "mailroom_test", "-U", "mailroom_test")
cmd.Stdin = dump

output, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -186,6 +181,20 @@ func loadTestDump() {
}
}

// Converts a project root relative path to an absolute path usable in any test. This is needed because go tests
// are run with a working directory set to the current module being tested.
func absPath(p string) string {
// start in working directory and go up until we are in a directory containing go.mod
dir, _ := os.Getwd()
for dir != "/" {
dir = path.Dir(dir)
if _, err := os.Stat(path.Join(dir, "go.mod")); err == nil {
break
}
}
return path.Join(dir, p)
}

// resets our redis database
func resetRedis() {
rc, err := redis.Dial("tcp", "localhost:6379")
Expand Down

0 comments on commit 38ee06d

Please sign in to comment.