8000 chore: add E2E tests for Cloud Run (#6709) · tmcalves/java-docs-samples@04b1774 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04b1774

Browse files
authored
chore: add E2E tests for Cloud Run (GoogleCloudPlatform#6709)
* chore: add E2E tests for Cloud Run * lint * Update JobsIntegrationTests.java * add headers * lint * update command * Update JobsIntegrationTests.java * debug * Update JobsIntegrationTests.java * Update e2e_test_setup.yaml
1 parent 1eb408f commit 04b1774

File tree

7 files changed

+289
-4
lines changed

7 files changed

+289
-4
lines changed

run/jobs/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cloud Run Sample
1+
# Cloud Run Jobs Sample
22

33
## Build
44

@@ -29,6 +29,18 @@ docker run --rm -e FAIL_RATE=0.9 -e SLEEP_MS=1000 gcr.io/${GOOGLE_CLOUD_PROJECT}
2929
mvn clean verify
3030
```
3131

32-
## Deploy
32+
## Create a Job
3333

34-
~coming soon~
34+
```
35+
gcloud alpha run jobs create job-quickstart \
36+
--image=gcr.io/$PROJECT_ID/logger-job \
37+
--tasks 50 \
38+
--set-env-vars=SLEEP_MS=10000 \
39+
--set-env-vars=FAIL_RATE=0.5 \
40+
--max-retries 10
41+
```
42+
43+
## Run the Job
44+
```
45+
gcloud alpha run jobs run job-quickstart
46+
```

run/jobs/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ limitations under the License.
4646
<version>1.1.3</version>
4747
<scope>test</scope>
4848
</dependency>
49+
<dependency>
50+
<groupId>com.google.cloud</groupId>
51+
<artifactId>google-cloud-logging</artifactId>
52+
<version>3.6.2</version>
53+
<scope>test</scope>
54+
</dependency>
4955
</dependencies>
5056

5157
<build>

run/jobs/src/main/java/com/example/JobsExample.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.example;
1818

1919
abstract class JobsExample {
20-
20+
// [START cloudrun_jobs_env_vars]
2121
// These values are provided automatically by the Cloud Run Jobs runtime.
2222
private static String TASK_NUM = System.getenv().getOrDefault("TASK_NUM", "0");
2323
private static String ATTEMPT_NUM = System.getenv().getOrDefault("ATTEMPT_NUM", "0");
@@ -26,6 +26,7 @@ abstract class JobsExample {
2626
private static int SLEEP_MS = Integer.parseInt(System.getenv().getOrDefault("SLEEP_MS", "0"));
2727
private static float FAIL_RATE =
2828
Float.parseFloat(System.getenv().getOrDefault("FAIL_RATE", "0.0"));
29+
// [END cloudrun_jobs_env_vars]
2930

3031
// Start script
3132
public static void main(String[] args) {
@@ -34,8 +35,10 @@ public static void main(String[] args) {
3435
runTask(SLEEP_MS, FAIL_RATE);
3536
} catch (RuntimeException | InterruptedException e) {
3637
System.err.println(String.format("Task #%s, Attempt #%s failed.", TASK_NUM, ATTEMPT_NUM));
38+
// [START cloudrun_jobs_exit_process]
3739
// Catch error and denote process-level failure to retry Task
3840
System.exit(1);
41+
// [END cloudrun_jobs_exit_process]
3942
}
4043
}
4144

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
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+
package com.example;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import com.google.api.gax.paging.Page;
22+
import com.google.cloud.logging.LogEntry;
23+
import com.google.cloud.logging.Logging;
24+
import com.google.cloud.logging.Logging.EntryListOption;
25+
import com.google.cloud.logging.LoggingOptions;
26+
import com.google.cloud.logging.Payload;
27+
import java.io.BufferedReader;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
30+
import java.nio.channels.InterruptedByTimeoutException;
31+
import java.text.DateFormat;
32+
import java.text.SimpleDateFormat;
33+
import java.util.Calendar;
34+
import java.util.TimeZone;
35+
import java.util.UUID;
36+
import java.util.concurrent.TimeUnit;
37+
import org.junit.AfterClass;
38+
import org.junit.BeforeClass;
39+
import org.junit.Test;
40+
41+
public class JobsIntegrationTests {
42+
43+
private static final String project = System.getenv("GOOGLE_CLOUD_PROJECT");
44+
private static final String suffix = UUID.randomUUID().toString();
45+
private static String service;
46+
47+
@BeforeClass
48+
public static void setup() throws Exception {
49+
service = "job-quickstart-" + suffix;
50+
51+
ProcessBuilder setup = new ProcessBuilder();
52+
setup.command(
53+
"gcloud",
54+
"builds",
55+
"submit",
56+
"--project=" + project,
57+
"--config=./src/test/java/com/example/resources/e2e_test_setup.yaml",
58+
String.format("--substitutions=_SERVICE=%s,_VERSION=%s", service, suffix));
59+
60+
setup.redirectErrorStream(true);
61+
System.out.println("Start Cloud Build...");
62+
Process p = setup.start();
63+
64+
// Read process output
65+
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
66+
String line;
67+
while ((line = in.readLine()) != null) {
68+
System.out.println(line);
69+
}
70+
in.close();
71+
System.out.println("Cloud Build completed.");
72+
}
73+
74+
@AfterClass
75+
public static void cleanup() throws IOException, InterruptedException {
76+
ProcessBuilder cleanup = new ProcessBuilder();
77+
cleanup.command(
78+
"gcloud",
79+
"builds",
80+
"submit",
81+
"--config",
82+
"./src/test/java/com/example/resources/e2e_test_cleanup.yaml",
83+
"--region=us-central1",
84+
"--project=" + project,
85+
String.format("--substitutions _SERVICE=%s,_VERSION=%s", service, suffix));
86+
87+
cleanup.start();
88+
}
89+
90+
@Test
91+
public void generatesLogs() throws Exception {
92+
try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {
93+
94+
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
95+
calendar.add(Calendar.MINUTE, -5);
96+
DateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
97+
String logFilter =
98+
"resource.type = \"cloud_run_revision\""
99+
+ " resource.labels.service_name = \""
100+
+ service
101+
+ "\" resource.labels.location = \"us-central1\""
102+
+ " timestamp>=\""
103+
+ rfc3339.format(calendar.getTime())
104+
+ "\" -protoPayload.serviceName=\"run.googleapis.com\"";
105+
106+
System.out.println(logFilter);
107+
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(logFilter));
108+
Boolean found = false;
109+
for (LogEntry logEntry : entries.iterateAll()) {
110+
if (!logEntry.getLogName().contains("cloudaudit")) {
111+
Payload<String> payload = logEntry.getPayload();
112+
if (payload.getData().contains("Task")) {
113+
found = true;
114+
}
115+
}
116+
}
117+
assertTrue("Log was not found.", found);
118+
}
119+
}
120+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
steps:
16+
17+
- id: 'Delete image and service'
18+
name: 'gcr.io/cloud-builders/gcloud'
19+
entrypoint: '/bin/bash'
20+
args:
21+
- '-c'
22+
- |
23+
./src/test/java/com/example/resources/retry.sh "gcloud container images describe gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}" \
24+
"gcloud container images delete gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} --quiet"
25+
26+
./src/test/java/com/example/resources/retry.sh "gcloud alpha run jobs describe ${_SERVICE} --region ${_REGION}" \
27+
"gcloud alpha run jobs delete ${_SERVICE} --region ${_REGION} --quiet"
28+
29+
substitutions:
30+
_SERVICE: logger-job
31+
_VERSION: manual
32+
_REGION: us-central1
33+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
steps:
16+
17+
- id: 'Build Container Image'
18+
name: 'gcr.io/cloud-builders/gcloud:latest'
19+
entrypoint: /bin/bash
20+
args:
21+
- '-c'
22+
- |
23+
./src/test/java/com/example/resources/retry.sh "gcloud builds submit --pack image=gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}"
24+
25+
- id: 'Deploy to Cloud Run'
26+
name: 'gcr.io/cloud-builders/gcloud:latest'
27+
entrypoint: /bin/bash
28+
args:
29+
- '-c'
30+
- |
31+
gcloud components update --quiet
32+
33+
./src/test/java/com/example/resources/retry.sh "gcloud alpha run jobs create ${_SERVICE} \
34+
--image gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} \
35+
--region ${_REGION} \
36+
--tasks 5 \
37+
--set-env-vars=SLEEP_MS=10 \
38+
--max-retries 0 \
39+
--wait"
40+
41+
substitutions:
42+
_SERVICE: logger-job
43+
_VERSION: manual
44+
_REGION: us-central1
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env 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+
##
17+
# retry.sh
18+
# Provides utility function commonly needed across Cloud Build pipelines to
19+
# retry commands on failure.
20+
#
21+
# Usage:
22+
# 1. Retry single command:
23+
#
24+
# ./retry.sh "CMD"
25+
#
26+
# 2. Retry with check:
27+
#
28+
# ./retry.sh "gcloud RESOURCE EXISTS?" "gcloud ACTION"
29+
#
30+
##
31+
32+
# Usage: try "cmd1" "cmd2"
33+
# If first cmd executes successfully then execute second cmd
34+
runIfSuccessful() {
35+
echo "running: $1"
36+
$($1 > /dev/null)
37+
if [ $? -eq 0 ]; then
38+
echo "running: $2"
39+
$($2 > /dev/null)
40+
fi
41+
}
42+
43+
# Define max retries
44+
max_attempts=3;
45+
attempt_num=1;
46+
47+
arg1="$1"
48+
arg2="$2"
49+
50+
if [ $# -eq 1 ]
51+
then
52+
cmd="$arg1"
53+
else
54+
cmd="runIfSuccessful \"$arg1\" \"$arg2\""
55+
fi
56+
57+
until eval $cmd
58+
do
59+
if ((attempt_num==max_attempts))
60+
then
61+
echo "Attempt $attempt_num / $max_attempts failed! No more retries left!"
62+
exit 1
63+
else
64+
echo "Attempt $attempt_num / $max_attempts failed!"
65+
sleep $((attempt_num++))
66+
fi
67+
done

0 commit comments

Comments
 (0)
0