Skip to content

Commit 2acc6b4

Browse files
author
Ace Nassri
authored
chore(functions): fix failing E2E tests (GoogleCloudPlatform#6684)
* chore(functions): fix failing E2E tests * Fix typo * Fix more typos * Move GCF deploy *before* test run * dbg * Add missing file var * Add missing SCRIPT_DIR * Add region * Use per-file gating * Fix variable typo * Add missing file * Add sleep * Fix trap not working in btlr * Use correct env var * Add explicit status code check (for debugging) * Enclose fn name in quotes * DBG: print out fn url * Moar dbg * change token type: access -> identity * moar dbg 3 * Use source to preserve env vars * Move ID token to build functions script * Revert unnecessary changes * rm take 2
1 parent af4ad3d commit 2acc6b4

File tree

6 files changed

+149
-8
lines changed

6 files changed

+149
-8
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eo pipefail
18+
19+
file="$(pwd)"
20+
FUNCTIONS_JAVA_RUNTIME="java11"
21+
FUNCTIONS_REGION="us-central1"
22+
23+
requireEnv() {
24+
test "${!1}" || (echo "Environment Variable '$1' not found" && exit 1)
25+
}
26+
requireEnv "FUNCTIONS_TOPIC"
27+
requireEnv "FUNCTIONS_BUCKET"
28+
29+
# We must explicitly specify function names for event-based functions
30+
31+
# Version is in the format <PR#>-<GIT COMMIT SHA>.
32+
# Ensures PR-based triggers of the same branch don't collide if Kokoro attempts
33+
# to run them concurrently.
34+
export SAMPLE_VERSION="${KOKORO_GIT_COMMIT:-latest}"
35+
# Builds not triggered by a PR will fall back to the commit hash then "latest".
36+
SUFFIX=${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-${SAMPLE_VERSION:0:12}}
37+
38+
export FUNCTIONS_HTTP_FN_NAME="http-${SUFFIX}"
39+
export FUNCTIONS_PUBSUB_FN_NAME="pubsub-${SUFFIX}"
40+
export FUNCTIONS_GCS_FN_NAME="gcs-${SUFFIX}"
41+
42+
# Set identity token (required for functions without --allow-unauthenticated)
43+
export FUNCTIONS_IDENTITY_TOKEN=$(gcloud auth print-identity-token)
44+
45+
# Deploy functions
46+
set -x
47+
48+
if [[ "$file" == *"hello-http"* ]]; then
49+
echo "Deploying function HelloHttp to: ${FUNCTIONS_HTTP_FN_NAME}"
50+
gcloud functions deploy $FUNCTIONS_HTTP_FN_NAME \
51+
--region $FUNCTIONS_REGION \
52+
--runtime $FUNCTIONS_JAVA_RUNTIME \
53+
--entry-point "functions.HelloHttp" \
54+
--trigger-http
55+
elif [[ "$file" == *"hello-pubsub"* ]]; then
56+
echo "Deploying function HelloPubSub to: ${FUNCTIONS_PUBSUB_FN_NAME}"
57+
gcloud functions deploy $FUNCTIONS_PUBSUB_FN_NAME \
58+
--region $FUNCTIONS_REGION \
59+
--runtime $FUNCTIONS_JAVA_RUNTIME \
60+
--entry-point "functions.HelloPubSub" \
61+
--trigger-topic $FUNCTIONS_SYSTEM_TEST_TOPIC
62+
elif [[ "$file" == *"hello-gcs"* ]]; then
63+
echo "Deploying function HelloGcs to: ${FUNCTIONS_GCS_FN_NAME}"
64+
gcloud functions deploy $FUNCTIONS_GCS_FN_NAME \
65+
--region $FUNCTIONS_REGION \
66+
--runtime $FUNCTIONS_JAVA_RUNTIME \
67+
--entry-point "functions.HelloGcs" \
68+
--trigger-bucket $FUNCTIONS_BUCKET
69+
fi
70+
71+
set +x
72+
73+
echo
74+
echo '---'
75+
echo
76+
77+
# Do not use exec to preserve trap behavior.
78+
"$@"

.kokoro/tests/run_test_java.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
file="$(pwd)"
17+
SCRIPT_DIR="$(dirname $0)/"
18+
1619
# Fail the tests if no Java version was found.
1720
POM_JAVA=$(grep -oP '(?<=<maven.compiler.target>).*?(?=</maven.compiler.target>)' pom.xml)
1821
ALLOWED_VERSIONS=("1.8" "11")
@@ -35,6 +38,23 @@ if ! [[ ",$JAVA_VERSION," =~ ",$POM_JAVA," ]]; then
3538
exit 0
3639
fi
3740

41+
# Build and deploy Cloud Functions hello-world samples
42+
# (Some of these samples have E2E tests that use deployed functions.)
43+
if [[ "$file" == *"functions/helloworld/"* ]]; then
44+
source "$SCRIPT_DIR"/build_cloud_functions.sh
45+
EXIT=$?
46+
47+
if [[ $EXIT -ne 0 ]]; then
48+
RTN=1
49+
echo -e "\n Cloud Functions build/deploy failed: gcloud returned a non-zero exit code. \n"
50+
else
51+
echo -e "\n Cloud Functions build/deploy completed.\n"
52+
53+
# Wait for functions to warm up (and start detecting events)
54+
sleep 1m
55+
fi
56+
fi
57+
3858
# Use maven to execute the tests for the project.
3959
mvn --quiet --batch-mode --fail-at-end clean verify \
4060
-Dfile.encoding="UTF-8" \
@@ -44,6 +64,11 @@ mvn --quiet --batch-mode --fail-at-end clean verify \
4464
-Dbigtable.instanceID=instance
4565
EXIT=$?
4666

67+
# Tear down (deployed) Cloud Functions after deployment tests are run
68+
if [[ "$file" == *"functions/helloworld/"* ]]; then
69+
source "$SCRIPT_DIR"/teardown_cloud_functions.sh
70+
fi
71+
4772
if [[ $EXIT -ne 0 ]]; then
4873
RTN=1
4974
echo -e "\n Testing failed: Maven returned a non-zero exit code. \n"
@@ -73,4 +98,4 @@ if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then
7398
$KOKORO_GFILE_DIR/linux_amd64/flakybot
7499
fi
75100

76-
exit $RTN
101+
exit $RTN
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
if [[ "$file" == *"hello-http"* ]]; then
18+
gcloud functions delete $FUNCTIONS_HTTP_FN_NAME \
19+
--region="$FUNCTIONS_REGION" -q || true
20+
elif [[ "$file" == *"hello-pubsub"* ]]; then
21+
gcloud functions delete $FUNCTIONS_PUBSUB_FN_NAME \
22+
--region="$FUNCTIONS_REGION" -q || true
23+
elif [[ "$file" == *"hello-gcs"* ]]; then
24+
gcloud functions delete $FUNCTIONS_GCS_FN_NAME \
25+
--region="$FUNCTIONS_REGION" -q || true
26+
fi
27+
mvn -q -B clean

functions/helloworld/hello-gcs/src/test/java/functions/ExampleSystemIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public class ExampleSystemIT {
4646
// TODO<developer>: set these values (as environment variables)
4747
private static final String PROJECT_ID = System.getenv("GCP_PROJECT");
4848
private static final String FUNCTIONS_BUCKET = System.getenv("FUNCTIONS_BUCKET");
49-
private static final String FUNCTION_DEPLOYED_NAME = "HelloGcs";
49+
50+
// TODO<developer>: set this value (as an environment variable) to HelloGcs
51+
private static final String FUNCTION_DEPLOYED_NAME = System.getenv("FUNCTIONS_GCS_FN_NAME");
5052
private static final Storage STORAGE = StorageOptions.getDefaultInstance().getService();
5153

5254
private static Logging loggingClient;

functions/helloworld/hello-http/src/test/java/functions/ExampleSystemIT.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.common.truth.Truth.assertThat;
2222

2323
import java.io.IOException;
24+
import java.net.HttpURLConnection;
2425
import java.net.URI;
2526
import java.net.http.HttpClient;
2627
import java.net.http.HttpRequest;
@@ -35,17 +36,21 @@ public class ExampleSystemIT {
3536
// TODO<developer>: set this value, as an environment variable or within your test code
3637
private static final String BASE_URL = System.getenv("FUNCTIONS_BASE_URL");
3738

38-
// Access token used to send requests to authenticated-only functions
39+
// Identity token used to send requests to authenticated-only functions
3940
// TODO<developer>: Set this value if your function requires authentication.
4041
// See the documentation for more info:
4142
// https://cloud.google.com/functions/docs/securing/authenticating
42-
private static final String ACCESS_TOKEN = System.getenv("FUNCTIONS_ACCESS_TOKEN");
43+
private static final String IDENTITY_TOKEN = System.getenv("FUNCTIONS_IDENTITY_TOKEN");
44+
45+
// Name of the deployed function
46+
// TODO<developer>: Set this to HelloHttp, as an environment variable or within your test code
47+
private static final String FUNCTION_DEPLOYED_NAME = System.getenv("FUNCTIONS_HTTP_FN_NAME");
4348

4449
private static HttpClient client = HttpClient.newHttpClient();
4550

4651
@Test
4752
public void helloHttp_shouldRunWithFunctionsFramework() throws IOException, InterruptedException {
48-
String functionUrl = BASE_URL + "/HelloHttp";
53+
String functionUrl = BASE_URL + "/" + FUNCTION_DEPLOYED_NAME;
4954

5055
// [END functions_http_system_test]
5156
// Skip this test if FUNCTIONS_BASE_URL is not set
@@ -60,13 +65,15 @@ public void helloHttp_shouldRunWithFunctionsFramework() throws IOException, Inte
6065
.GET();
6166

6267
// Used to test functions that require authenticated invokers
63-
if (ACCESS_TOKEN != null) {
64-
getRequestBuilder.header("Authorization", "Bearer " + ACCESS_TOKEN);
68+
if (IDENTITY_TOKEN != null) {
69+
getRequestBuilder.header("Authorization", "Bearer " + IDENTITY_TOKEN);
6570
}
6671

6772
java.net.http.HttpRequest getRequest = getRequestBuilder.build();
6873

6974
HttpResponse response = client.send(getRequest, HttpResponse.BodyHandlers.ofString());
75+
76+
assertThat(response.statusCode()).isEqualTo(HttpURLConnection.HTTP_OK);
7077
assertThat(response.body().toString()).isEqualTo("Hello world!");
7178
}
7279
}

functions/helloworld/hello-pubsub/src/test/java/functions/ExampleSystemIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public class ExampleSystemIT {
4747
// TODO<developer>: set these values (as environment variables)
4848
private static final String PROJECT_ID = System.getenv("GCP_PROJECT");
4949
private static final String TOPIC_NAME = System.getenv("FUNCTIONS_SYSTEM_TEST_TOPIC");
50-
private static final String FUNCTION_DEPLOYED_NAME = "HelloPubSub";
50+
51+
// TODO<developer>: set this value (as an environment variable) to HelloPubSub
52+
private static final String FUNCTION_DEPLOYED_NAME = System.getenv("FUNCTIONS_PUBSUB_FN_NAME");
5153

5254
private static Logging loggingClient;
5355

0 commit comments

Comments
 (0)