• 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="gcr.io/tink-test-infrastructure/linux-tink-go-base:latest" \
22#  sh ./kokoro/gcp_ubuntu/gomod/run_tests.sh
23#
24# The user may specify TINK_BASE_DIR as the folder where to look for
25# tink-java. That is:
26#   ${TINK_BASE_DIR}/tink_java
27set -eEuo pipefail
28
29IS_KOKORO="false"
30if [[ -n "${KOKORO_ARTIFACTS_DIR:-}" ]] ; then
31  IS_KOKORO="true"
32fi
33readonly IS_KOKORO
34
35RUN_COMMAND_ARGS=()
36if [[ "${IS_KOKORO}" == "true" ]] ; then
37  TINK_BASE_DIR="$(echo "${KOKORO_ARTIFACTS_DIR}"/git*)"
38  source \
39    "${TINK_BASE_DIR}/tink_java/kokoro/testutils/java_test_container_images.sh"
40  CONTAINER_IMAGE="${TINK_JAVA_BASE_IMAGE}"
41  RUN_COMMAND_ARGS+=( -k "${TINK_GCR_SERVICE_KEY}" )
42fi
43
44: "${TINK_BASE_DIR:=$(cd .. && pwd)}"
45readonly TINK_BASE_DIR
46readonly CONTAINER_IMAGE
47
48# If running from the tink_java folder this has no effect.
49cd "${TINK_BASE_DIR}/tink_java"
50
51if [[ -n "${CONTAINER_IMAGE:-}" ]]; then
52  RUN_COMMAND_ARGS+=( -c "${CONTAINER_IMAGE}" )
53fi
54
55# File that stores environment variables to pass to the container.
56readonly ENV_VARIABLES_FILE="/tmp/env_variables.txt"
57
58if [[ -n "${TINK_REMOTE_BAZEL_CACHE_GCS_BUCKET:-}" ]]; then
59  cp "${TINK_REMOTE_BAZEL_CACHE_SERVICE_KEY}" ./cache_key
60  cat <<EOF > "${ENV_VARIABLES_FILE}"
61BAZEL_REMOTE_CACHE_NAME=${TINK_REMOTE_BAZEL_CACHE_GCS_BUCKET}/bazel/${TINK_JAVA_BASE_IMAGE_HASH}
62EOF
63  RUN_COMMAND_ARGS+=( -e "${ENV_VARIABLES_FILE}" )
64fi
65
66cat <<'EOF' > _do_run_test.sh
67set -euo pipefail
68
69# Compare the dependencies of the ":tink" target with the declared dependencies.
70# These should match the dependencies declared in tink-java.pom.xml, since
71# since these are the dependencies which are declared on maven.
72./kokoro/testutils/check_maven_bazel_deps_consistency.sh "//:tink" \
73  "maven/tink-java.pom.xml"
74
75# For Android, we compare the unshaded version -- trial and error revealed that
76# this is the target that has the correct dependencies.
77./kokoro/testutils/check_maven_bazel_deps_consistency.sh \
78  -e com.google.protobuf:protobuf-javalite \
79  "//:tink-android-unshaded" "maven/tink-java-android.pom.xml"
80
81MAVEN_DEPLOY_LIBRARY_OPTS=()
82if [[ -n "${BAZEL_REMOTE_CACHE_NAME:-}" ]]; then
83  MAVEN_DEPLOY_LIBRARY_OPTS+=( -c "${BAZEL_REMOTE_CACHE_NAME}" )
84fi
85readonly MAVEN_DEPLOY_LIBRARY_OPTS
86
87# Install the latest snapshot for tink-java and tink-android locally.
88./maven/maven_deploy_library.sh "${MAVEN_DEPLOY_LIBRARY_OPTS[@]}" install \
89  tink maven/tink-java.pom.xml HEAD
90./maven/maven_deploy_library.sh "${MAVEN_DEPLOY_LIBRARY_OPTS[@]}" install \
91  tink-android maven/tink-java-android.pom.xml HEAD
92
93# TODO(tholenst): find a good way to test these jar files.
94./examples/android/helloworld/gradlew -PmavenLocation=local \
95  -p ./examples/android/helloworld build
96EOF
97chmod +x _do_run_test.sh
98
99# Run cleanup on EXIT.
100trap cleanup EXIT
101
102cleanup() {
103  rm -rf _do_run_test.sh "${ENV_VARIABLES_FILE}"
104}
105
106./kokoro/testutils/run_command.sh "${RUN_COMMAND_ARGS[@]}" ./_do_run_test.sh
107
108readonly GITHUB_JOB_NAME="tink/github/java/gcp_ubuntu/maven/continuous"
109
110if [[ "${IS_KOKORO}" == "true" \
111      && "${KOKORO_JOB_NAME}" == "${GITHUB_JOB_NAME}" ]]; then
112  # GITHUB_ACCESS_TOKEN is populated by Kokoro.
113  readonly GIT_CREDENTIALS="ise-crypto:${GITHUB_ACCESS_TOKEN}"
114  readonly GITHUB_URL="https://${GIT_CREDENTIALS}@github.com/tink-crypto/tink-java.git"
115
116  # Share the required env variables with the container to allow publishing the
117  # snapshot on Sonatype.
118  cat <<EOF >> "${ENV_VARIABLES_FILE}"
119SONATYPE_USERNAME
120SONATYPE_PASSWORD
121EOF
122
123  MAVEN_DEPLOY_LIBRARY_OPTS=( -u "${GITHUB_URL}" )
124  if [[ -n "${BAZEL_REMOTE_CACHE_NAME:-}" ]]; then
125    MAVEN_DEPLOY_LIBRARY_OPTS+=( -c "${BAZEL_REMOTE_CACHE_NAME}" )
126  fi
127  readonly MAVEN_DEPLOY_LIBRARY_OPTS
128
129  ./kokoro/testutils/run_command.sh "${RUN_COMMAND_ARGS[@]}" \
130    ./maven/maven_deploy_library.sh "${MAVEN_DEPLOY_LIBRARY_OPTS[@]}" snapshot \
131    tink maven/tink-java.pom.xml HEAD
132  ./kokoro/testutils/run_command.sh "${RUN_COMMAND_ARGS[@]}" \
133    ./maven/maven_deploy_library.sh "${MAVEN_DEPLOY_LIBRARY_OPTS[@]}" snapshot \
134    tink-android maven/tink-java-android.pom.xml HEAD
135fi
136