• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2020 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
17set -euo pipefail
18
19REPO_DIR="${KOKORO_ARTIFACTS_DIR}/git/tink"
20
21cd "${REPO_DIR}"
22./kokoro/testutils/copy_credentials.sh "go/testdata" "all"
23# Sourcing required to update callers environment.
24source ./kokoro/testutils/install_go.sh
25
26echo "Using go binary from $(which go): $(go version)"
27
28readonly TINK_VERSION="$(cat ${REPO_DIR}/go/tink_version.bzl \
29                        | grep ^TINK \
30                        | cut -f 2 -d \")"
31
32# Create a temporary directory for performing module tests.
33TMP_DIR="$(mktemp -dt go-module-test.XXXXXX)"
34GO_MOD_DIR="${TMP_DIR}/go-mod-test"
35
36REPO_URL_PREFIX="github.com/google/tink"
37
38#######################################
39# Test an individual Go module within the Tink repository.
40# Globals:
41#   REPO_DIR
42#   TINK_VERISON
43#   GO_MOD_DIR
44#   REPO_URL_PREFIX
45# Arguments:
46#   The name of the Go module, relative to the repository root.
47# Outputs:
48#   Prints progress to STDOUT.
49#######################################
50function test_go_mod() {
51  local mod_name="$1"
52  local full_mod_name="${REPO_URL_PREFIX}/${mod_name}"
53
54  echo "### Testing ${full_mod_name}..."
55  (
56    echo "Using go binary from $(which go): $(go version)"
57    set -x
58    cd "${REPO_DIR}/${mod_name}"
59    go build -v ./...
60    go test -v ./...
61  )
62
63  mkdir "${GO_MOD_DIR}"
64  (
65    cd "${GO_MOD_DIR}"
66
67    echo "Using go binary from $(which go): $(go version)"
68
69    # Display commands being run for the remainder of this subshell.
70    set -x
71
72    # Initialize a test Go module.
73    go mod init tink-go-mod-test
74    overlay_module "${mod_name}" "${full_mod_name}"
75    overlay_internal_deps "${mod_name}"
76
77    # Print the prepared go.mod.
78    cat go.mod
79
80    # Get the module at the latest commit and print graph output depicting
81    # direct dependencies.
82    go get -v "${full_mod_name}@master"
83
84    # Pint contextual information concerning dependencies.
85    go mod graph | grep google/tink
86    go list -m all | grep google/tink
87  )
88
89  # Leave a clean environment for subsequent tests.
90  go clean -modcache
91  rm -rf "${GO_MOD_DIR}"
92}
93
94#######################################
95# Add a require statement for a Tink module and a replace statement to point it
96# to the local copy.
97# Globals:
98#   REPO_DIR
99#   TINK_VERISON
100# Arguments:
101#   The name of the Go module, relative to the repository root.
102#   The full name of the Go module, as specified in import statements.
103#######################################
104function overlay_module() {
105  local mod_name="$1"
106  local full_mod_name="$2"
107
108  go mod edit "-require=${full_mod_name}@v${TINK_VERSION}"
109  go mod edit "-replace=${full_mod_name}=${REPO_DIR}/${mod_name}"
110}
111
112#######################################
113# Search the go.mod being tested for internal dependencies and overlay them with
114# the local copies.
115# Globals:
116#   REPO_DIR
117#   REPO_URL_PREFIX
118# Arguments:
119#   The name of the Go module being tested, relative to the repository root.
120#######################################
121function overlay_internal_deps() {
122  local mod_name="$1"
123
124  declare -a internal_deps
125  while read internal_dep; do
126    internal_deps+=("${internal_dep}")
127  done < <(grep "${REPO_URL_PREFIX}" "${REPO_DIR}/${mod_name}/go.mod" \
128      | grep -v ^module \
129      | awk '{print $1}')
130
131  # If internal_deps are found...
132  if [[ ! -z "${internal_deps+x}" ]]; then
133    for full_dep_name in "${internal_deps[@]}"; do
134      local dep_name="$(echo "${full_dep_name}" | sed "s#${REPO_URL_PREFIX}/##")"
135      overlay_module "${dep_name}" "${full_dep_name}"
136    done
137  fi
138}
139
140function main() {
141  # Extract all go.mod instances from the repository.
142  declare -a go_mod_dirs
143  while read go_mod_dir; do
144    go_mod_dirs+=("${go_mod_dir}")
145  done < <(find "${REPO_DIR}" -name "go.mod" \
146    | sed "s#^${REPO_DIR}/##" \
147    | xargs -n 1 dirname)
148
149  echo "### Go modules found:"
150  for go_mod_dir in "${go_mod_dirs[@]}"; do
151    echo "${go_mod_dir}"
152  done
153
154  for go_mod_dir in "${go_mod_dirs[@]}"; do
155    test_go_mod "${go_mod_dir}"
156  done
157}
158
159main "$@"
160