1#!/usr/bin/env bash
2# Copyright 2022 The gRPC Authors
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
16set -ex
17
18# avoid slow finalization after the script has exited.
19source $(dirname $0)/../../../tools/internal_ci/helper_scripts/move_src_tree_and_respawn_itself_rc
20
21# change to grpc repo root
22cd $(dirname $0)/../../..
23
24source tools/internal_ci/helper_scripts/prepare_build_macos_rc
25
26# Make sure actions run by bazel can find python3.
27# Without this the build will fail with "env: python3: No such file or directory".
28# When on kokoro MacOS Mojave image.
29sudo ln -s $(which python3) /usr/bin/python3 || true
30
31# make sure bazel is available
32tools/bazel version
33
34# for kokoro mac workers, exact image version is store in a well-known location on disk
35KOKORO_IMAGE_VERSION="$(cat /VERSION)"
36
37BAZEL_REMOTE_CACHE_ARGS=(
38  # Enable uploading to remote cache. Requires the "roles/remotebuildexecution.actionCacheWriter" permission.
39  --remote_upload_local_results=true
40  # allow invalidating the old cache by setting to a new random key
41  --remote_default_exec_properties="grpc_cache_silo_key1=83d8e488-1ca9-40fd-929e-d37d13529c99"
42  # make sure we only get cache hits from binaries built on exact same macos image
43  --remote_default_exec_properties="grpc_cache_silo_key2=${KOKORO_IMAGE_VERSION}"
44)
45
46EXAMPLE_TARGETS=(
47  # TODO(jtattermusch): ideally we'd say "//src/objective-c/examples/..." but not all the targets currently build
48  //src/objective-c/examples:Sample
49  //src/objective-c/examples:tvOS-sample
50)
51
52TEST_TARGETS=(
53  # TODO(jtattermusch): ideally we'd say "//src/objective-c/tests/..." but not all the targets currently build
54  //src/objective-c/tests:InteropTestsLocalCleartext
55  //src/objective-c/tests:InteropTestsLocalSSL
56  //src/objective-c/tests:InteropTestsRemote
57  //src/objective-c/tests:MacTests
58  //src/objective-c/tests:UnitTests
59  # TODO: Enable this again once @CronetFramework is working
60  #//src/objective-c/tests:CppCronetTests
61  #//src/objective-c/tests:CronetTests
62  #//src/objective-c/tests:PerfTests
63  //src/objective-c/tests:CFStreamTests
64  //src/objective-c/tests:tvtests_build_test
65  # codegen plugin tests
66  //src/objective-c/tests:objc_codegen_plugin_test
67  //src/objective-c/tests:objc_codegen_plugin_option_test
68)
69
70# === BEGIN SECTION: run interop_server on the background ====
71# Before testing objC at all, build the interop server since many of the ObjC test rely on it.
72# Use remote cache to build the interop_server binary as quickly as possible (interop_server
73# is not what we want to test actually, we just use it as a backend for ObjC test).
74# TODO(jtattermusch): can we make ObjC test not depend on running a local interop_server?
75python3 tools/run_tests/python_utils/bazel_report_helper.py --report_path build_interop_server
76build_interop_server/bazel_wrapper \
77  --bazelrc=tools/remote_build/mac.bazelrc \
78  build \
79  --google_credentials="${KOKORO_GFILE_DIR}/GrpcTesting-d0eeee2db331.json" \
80  "${BAZEL_REMOTE_CACHE_ARGS[@]}" \
81  -- \
82  //test/cpp/interop:interop_server
83
84# Start port server and allocate ports to run interop_server
85python3 tools/run_tests/start_port_server.py
86
87PLAIN_PORT=$(curl localhost:32766/get)
88TLS_PORT=$(curl localhost:32766/get)
89
90INTEROP_SERVER_BINARY=bazel-bin/test/cpp/interop/interop_server
91# run the interop server on the background. The port numbers must match TestConfigs in BUILD.
92# TODO(jtattermusch): can we make the ports configurable (but avoid breaking bazel build cache at the same time?)
93"${INTEROP_SERVER_BINARY}" --port=$PLAIN_PORT --max_send_message_size=8388608 &
94"${INTEROP_SERVER_BINARY}" --port=$TLS_PORT --max_send_message_size=8388608 --use_tls &
95# make sure the interop_server processes we started on the background are killed upon exit.
96trap 'echo "KILLING interop_server binaries running on the background"; kill -9 $(jobs -p)' EXIT
97# === END SECTION: run interop_server on the background ====
98
99# Environment variables that will be visible to objc tests.
100OBJC_TEST_ENV_ARGS=(
101  --test_env=HOST_PORT_LOCAL=localhost:$PLAIN_PORT
102  --test_env=HOST_PORT_LOCALSSL=localhost:$TLS_PORT
103)
104
105python3 tools/run_tests/python_utils/bazel_report_helper.py --report_path objc_bazel_tests
106
107# NOTE: When using bazel to run the tests, test env variables like GRPC_VERBOSITY or GRPC_TRACE
108# seem to be correctly applied to the test environment even when running tests on a simulator.
109# The below configuration runs all the tests with --test_env=GRPC_VERBOSITY=debug, which makes
110# the test logs much more useful.
111objc_bazel_tests/bazel_wrapper \
112  --bazelrc=tools/remote_build/include/test_locally_with_resultstore_results.bazelrc \
113  test \
114  --google_credentials="${KOKORO_GFILE_DIR}/GrpcTesting-d0eeee2db331.json" \
115  "${BAZEL_REMOTE_CACHE_ARGS[@]}" \
116  $BAZEL_FLAGS \
117  "${OBJC_TEST_ENV_ARGS[@]}" \
118  -- \
119  "${EXAMPLE_TARGETS[@]}" \
120  "${TEST_TARGETS[@]}"
121