Skip to content

Commit

Permalink
CI: Run PostgreSQL once for all Scala tests. (#5919)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirTalwar authored May 14, 2020
1 parent 9f43ab9 commit 57a8d0b
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 124 deletions.
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ dev_env_tool(
"bin/pg_ctl",
"bin/postgres",
],
required_tools = {
"initdb": ["postgres"],
"pg_ctl": ["postgres"],
},
tools = [
"createdb",
"dropdb",
Expand Down
44 changes: 31 additions & 13 deletions bazel_tools/dev_env_tool/dev_env_tool.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,42 @@
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
load("@rules_sh//sh:posix.bzl", "posix")

def _create_build_content(rule_name, tools, win_paths, nix_paths):
def _create_build_content(rule_name, is_windows, tools, required_tools, win_paths, nix_paths):
content = """
# DO NOT EDIT: automatically generated BUILD file for dev_env_tool.bzl: {rule_name}
package(default_visibility = ["//visibility:public"])
filegroup(
name = "all",
srcs = glob(["**"]),
)
""".format(rule_name = rule_name)
""".format(rule_name = rule_name)

for i in range(0, len(tools)):
content += """
if is_windows:
content += """
# Running tools with `bazel run` is not supported on Windows.
filegroup(
name = "{tool}",
srcs = select({{
":windows": ["{win_path}"],
"//conditions:default": ["{nix_path}"],
}}),
srcs = ["{path}"],
)
""".format(
tool = tools[i],
win_path = win_paths[i],
nix_path = nix_paths[i],
)
""".format(
tool = tools[i],
path = win_paths[i],
)
else:
content += """
sh_binary(
name = "{tool}",
srcs = ["{path}"],
data = {dependencies},
)
""".format(
tool = tools[i],
dependencies = [":{}".format(dep) for dep in required_tools.get(tools[i], [])],
path = nix_paths[i],
)

content += """
config_setting(
Expand Down Expand Up @@ -95,7 +106,8 @@ dadew = repository_rule(
)

def _dev_env_tool_impl(ctx):
if get_cpu_value(ctx) == "x64_windows":
is_windows = get_cpu_value(ctx) == "x64_windows"
if is_windows:
ps = ctx.which("powershell")
dadew = _dadew_where(ctx, ps)
find = _dadew_tool_home(dadew, "msys2") + "\\usr\\bin\\find.exe"
Expand All @@ -119,7 +131,9 @@ def _dev_env_tool_impl(ctx):
build_path = ctx.path("BUILD")
build_content = _create_build_content(
rule_name = ctx.name,
is_windows = is_windows,
tools = ctx.attr.tools,
required_tools = ctx.attr.required_tools,
win_paths = [
"%s/%s" % (ctx.attr.prefix, path)
for path in ctx.attr.win_paths
Expand All @@ -137,6 +151,10 @@ dev_env_tool = repository_rule(
"tools": attr.string_list(
mandatory = True,
),
"required_tools": attr.string_list_dict(
mandatory = False,
default = {},
),
"win_tool": attr.string(
mandatory = True,
),
Expand Down
59 changes: 47 additions & 12 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0


set -euo pipefail

eval "$($(dirname "$0")/dev-env/bin/dade-assist)"
eval "$("$(dirname "$0")/dev-env/bin/dade-assist")"

execution_log_postfix=${1:-}

Expand All @@ -18,23 +17,59 @@ if [[ "$execution_log_postfix" == "_Darwin" ]]; then
tag_filter="-dont-run-on-darwin,-scaladoc,-pdfdocs"
fi

# Bazel test only builds targets that are dependencies of a test suite
# so do a full build first.
# Bazel test only builds targets that are dependencies of a test suite so do a full build first.
bazel build //... --build_tag_filters "$tag_filter"
bazel test //... --build_tag_filters "$tag_filter" --test_tag_filters "$tag_filter" --experimental_execution_log_file "$ARTIFACT_DIRS/test_execution${execution_log_postfix}.log"

# Set up a shared PostgreSQL instance.
export POSTGRESQL_ROOT_DIR="${TMPDIR:-/tmp}/daml/postgresql"
export POSTGRESQL_DATA_DIR="${POSTGRESQL_ROOT_DIR}/data"
export POSTGRESQL_LOG_FILE="${POSTGRESQL_ROOT_DIR}/postgresql.log"
export POSTGRESQL_HOST='localhost'
export POSTGRESQL_PORT=54321
export POSTGRESQL_USERNAME='test'
export POSTGRESQL_PASSWORD=''
function start_postgresql() {
mkdir -p "$POSTGRESQL_DATA_DIR"
bazel run -- @postgresql_dev_env//:initdb --auth=trust --encoding=UNICODE --locale=en_US.UTF-8 --username="$POSTGRESQL_USERNAME" "$POSTGRESQL_DATA_DIR"
envsubst -no-unset -i ci/postgresql.conf -o "$POSTGRESQL_DATA_DIR/postgresql.conf"
bazel run -- @postgresql_dev_env//:pg_ctl -w --pgdata="$POSTGRESQL_DATA_DIR" --log="$POSTGRESQL_LOG_FILE" start || {
if [[ -f "$POSTGRESQL_LOG_FILE" ]]; then
echo >&2 'PostgreSQL logs:'
cat >&2 "$POSTGRESQL_LOG_FILE"
fi
return 1
}
}
function stop_postgresql() {
if [[ -e "$POSTGRESQL_DATA_DIR" ]]; then
bazel run -- @postgresql_dev_env//:pg_ctl -w --pgdata="$POSTGRESQL_DATA_DIR" --mode=immediate stop || :
rm -rf "$POSTGRESQL_ROOT_DIR"
fi
}
trap stop_postgresql EXIT
stop_postgresql # in case it's running from a previous build
start_postgresql

# Run the tests.
bazel test //... \
--build_tag_filters "$tag_filter" \
--test_tag_filters "$tag_filter" \
--test_env "POSTGRESQL_HOST=${POSTGRESQL_HOST}" \
--test_env "POSTGRESQL_PORT=${POSTGRESQL_PORT}" \
--test_env "POSTGRESQL_USERNAME=${POSTGRESQL_USERNAME}" \
--test_env "POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD}" \
--experimental_execution_log_file "$ARTIFACT_DIRS/test_execution${execution_log_postfix}.log"

# Make sure that Bazel query works.
bazel query 'deps(//...)' > /dev/null
bazel query 'deps(//...)' >/dev/null

# Check that we can load damlc in ghci
GHCI_SCRIPT=$(mktemp)
function cleanup {
rm -rf "$GHCI_SCRIPT"
}
trap cleanup EXIT
# Disabled on darwin since it sometimes seem to hang and this only
# tests our dev setup rather than our code so issues are not critical.
if [[ "$(uname)" != "Darwin" ]]; then
da-ghci --data yes //compiler/damlc:damlc -e ':main --help'
da-ghci --data yes //compiler/damlc:damlc -e ':main --help'
fi

# Check that our IDE works on our codebase
ghcide compiler/damlc/exe/Main.hs 2>&1 | tee ide-log
grep -q "1 file worked, 0 files failed" ide-log
8 changes: 8 additions & 0 deletions ci/postgresql.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
listen_addresses = '${POSTGRESQL_HOST}'
port = ${POSTGRESQL_PORT}
unix_socket_directories = '${POSTGRESQL_ROOT_DIR}'
fsync = off
synchronous_commit = off
full_page_writes = off
log_min_duration_statement = 0
log_connections = on
6 changes: 4 additions & 2 deletions daml-assistant/integration-tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ genrule(
set -euo pipefail
TMP_DIR=$$(mktemp -d)
MVN_DB="$$TMP_DIR/m2"
MVN=($(locations @mvn_dev_env//:mvn))
MVN="$${{MVN[0]}}"
install_mvn() {{
$(location @mvn_dev_env//:mvn) -q install:install-file \
"$$MVN" -q install:install-file \
-Dmaven.repo.local=$$MVN_DB \
"-DgroupId=$$1" \
"-DartifactId=$$2" \
Expand Down Expand Up @@ -62,7 +64,7 @@ genrule(
"com.daml" "ledger-api-auth-client" \
$(location //ledger/ledger-api-auth-client:libledger-api-auth-client.jar) \
$(location //ledger/ledger-api-auth-client:ledger-api-auth-client_pom.xml)
$(location @mvn_dev_env//:mvn) -q -Dmaven.repo.local=$$MVN_DB -f "$$TMP_DIR/quickstart-java/pom.xml" dependency:resolve dependency:resolve-plugins
"$$MVN" -q -Dmaven.repo.local=$$MVN_DB -f "$$TMP_DIR/quickstart-java/pom.xml" dependency:resolve dependency:resolve-plugins
tar cf $(location integration-tests-mvn.tar) -C $$(dirname $$MVN_DB) $$(basename $$MVN_DB) \
--owner=0 --group=0 --numeric-owner --mtime=2000-01-01\ 00:00Z --sort=name
""".format(mvn = mvn_version),
Expand Down
1 change: 1 addition & 0 deletions dev-env/bin/envsubst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ trait ExtractorFixture extends SandboxFixture with PostgresAroundSuite with Type

protected def target: PostgreSQLTarget = PostgreSQLTarget(
connectUrl = postgresDatabase.url,
user = PostgresAround.userName,
password = PostgresAround.password,
user = postgresDatabase.userName,
password = postgresDatabase.password,
outputFormat = outputFormat,
schemaPerPackage = false,
mergeIdentical = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ object MainWithEphemeralPostgresql extends PostgresAround {
.parse[Unit]("SQL Ledger", _ => (), (), args)
.getOrElse(sys.exit(1))

startEphemeralPostgres()
connectToPostgresqlServer()
val database = createNewRandomDatabase()
sys.addShutdownHook(stopAndCleanUpPostgres())
sys.addShutdownHook(disconnectFromPostgresqlServer())
val config = originalConfig.copy(
participants = originalConfig.participants.map(_.copy(serverJdbcUrl = database.url)),
extra = ExtraConfig(jdbcUrl = Some(database.url)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.daml.testing.postgresql.PostgresAround

object MainWithEphemeralPostgresql extends PostgresAround {
def main(args: Array[String]): Unit = {
startEphemeralPostgres()
connectToPostgresqlServer()
val database = createNewRandomDatabase()
sys.addShutdownHook(stopAndCleanUpPostgres())
sys.addShutdownHook(disconnectFromPostgresqlServer())
SandboxMain.main(args ++ Array("--sql-backend-jdbcurl", database.url))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.daml.testing.postgresql.PostgresAround

object MainWithEphemeralPostgresql extends PostgresAround {
def main(args: Array[String]): Unit = {
startEphemeralPostgres()
connectToPostgresqlServer()
val database = createNewRandomDatabase()
sys.addShutdownHook(stopAndCleanUpPostgres())
sys.addShutdownHook(disconnectFromPostgresqlServer())
Main.main(args ++ Array("--sql-backend-jdbcurl", database.url))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.daml.api.util.TimeProvider
import com.daml.bazeltools.BazelRunfiles.rlocation
import com.daml.daml_lf_dev.DamlLf
import com.daml.ledger.api.domain.LedgerId
import com.daml.ledger.api.health.{Healthy, Unhealthy}
import com.daml.ledger.api.health.Healthy
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.daml.ledger.participant.state.v1.ParticipantId
import com.daml.lf.archive.DarReader
Expand Down Expand Up @@ -146,32 +146,6 @@ class SqlLedgerSpec
ledger.currentHealth() should be(Healthy)
}
}

"be unhealthy if the underlying database is inaccessible 3 or more times in a row" in {
for {
ledger <- createSqlLedger()
} yield {
withClue("before shutting down postgres,") {
ledger.currentHealth() should be(Healthy)
}

stopPostgres()

eventually {
withClue("after shutting down postgres,") {
ledger.currentHealth() should be(Unhealthy)
}
}

startPostgres()

eventually {
withClue("after starting up postgres,") {
ledger.currentHealth() should be(Healthy)
}
}
}
}
}

private def createSqlLedger(): Future[Ledger] =
Expand Down
Loading

0 comments on commit 57a8d0b

Please sign in to comment.