• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15################################################################################
16
17# By default when run locally this script runs the command below directly on the
18# host. The CONTAINER_IMAGE variable can be set to run on a custom container
19# image for local testing. E.g.:
20#
21# CONTAINER_IMAGE="us-docker.pkg.dev/tink-test-infrastructure/tink-ci-images/linux-tink-java-base:latest" \
22#  sh ./kokoro/gcp_ubuntu/bazel/run_tests.sh
23#
24# The user may specify TINK_BASE_DIR as the folder where to look for
25# tink-java. That is ${TINK_BASE_DIR}/tink_java.
26set -eEuo pipefail
27
28RUN_COMMAND_ARGS=()
29if [[ -n "${KOKORO_ARTIFACTS_DIR:-}" ]] ; then
30  TINK_BASE_DIR="$(echo "${KOKORO_ARTIFACTS_DIR}"/git*)"
31  source \
32    "${TINK_BASE_DIR}/tink_java/kokoro/testutils/java_test_container_images.sh"
33  CONTAINER_IMAGE="${TINK_JAVA_BASE_IMAGE}"
34  RUN_COMMAND_ARGS+=( -k "${TINK_GCR_SERVICE_KEY}" )
35fi
36: "${TINK_BASE_DIR:=$(cd .. && pwd)}"
37readonly TINK_BASE_DIR
38readonly CONTAINER_IMAGE
39
40if [[ -n "${CONTAINER_IMAGE}" ]]; then
41  RUN_COMMAND_ARGS+=( -c "${CONTAINER_IMAGE}" )
42fi
43
44cd "${TINK_BASE_DIR}/tink_java"
45
46if [[ -n "${TINK_REMOTE_BAZEL_CACHE_GCS_BUCKET:-}" ]]; then
47  cp "${TINK_REMOTE_BAZEL_CACHE_SERVICE_KEY}" ./cache_key
48  cat <<EOF > /tmp/env_variables.txt
49BAZEL_REMOTE_CACHE_NAME=${TINK_REMOTE_BAZEL_CACHE_GCS_BUCKET}/bazel/${TINK_JAVA_BASE_IMAGE_HASH}
50EOF
51  RUN_COMMAND_ARGS+=( -e /tmp/env_variables.txt )
52fi
53
54readonly RUN_COMMAND_ARGS
55
56cat <<'EOF' > _do_run_test.sh
57set -euo pipefail
58
59./tools/create_maven_build_file.sh -o BUILD.bazel.temp
60if ! cmp -s BUILD.bazel BUILD.bazel.temp; then
61  echo "ERROR: Update your BUILD.bazel file using ./tools/create_maven_build_file.sh" >&2
62  diff -u BUILD.bazel BUILD.bazel.temp
63  exit 1
64fi
65
66CACHE_FLAGS=()
67if [[ -n "${BAZEL_REMOTE_CACHE_NAME:-}" ]]; then
68  CACHE_FLAGS+=( -c "${BAZEL_REMOTE_CACHE_NAME}" )
69fi
70readonly CACHE_FLAGS
71
72./kokoro/testutils/run_bazel_tests.sh "${CACHE_FLAGS[@]}" .
73./kokoro/testutils/run_bazel_tests.sh "${CACHE_FLAGS[@]}" examples
74EOF
75chmod +x _do_run_test.sh
76
77# Run cleanup on EXIT.
78trap cleanup EXIT
79
80cleanup() {
81  rm -rf _do_run_test.sh
82  rm -rf BUILD.bazel.temp
83  rm -rf /tmp/env_variables.txt
84}
85
86./kokoro/testutils/run_command.sh "${RUN_COMMAND_ARGS[@]}" ./_do_run_test.sh
87