Skip to content

Commit eb45806

Browse files
author
ace-n
committed
Add system test
1 parent b002935 commit eb45806

File tree

5 files changed

+188
-9
lines changed

5 files changed

+188
-9
lines changed

run/jobs/e2e_test_cleanup.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
steps:
2+
3+
- id: 'Delete image and service'
4+
name: 'gcr.io/cloud-builders/gcloud'
5+
entrypoint: '/bin/bash'
6+
args:
7+
- '-c'
8+
- |
9+
# ./test/retry.sh "gcloud artifacts docker images delete ${_REGION}-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/${_JOB} --quiet"
10+
11+
./test/retry.sh ""
12+
13+
substitutions:
14+
_JOB: jobs
15+
_REGION: us-central1

run/jobs/e2e_test_setup.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
steps:
2+
3+
- id: 'Build container'
4+
name: 'gcr.io/cloud-builders/gcloud:latest'
5+
entrypoint: /bin/bash
6+
args:
7+
- '-c'
8+
- |
9+
gcloud beta builds submit --pack image=gcr.io/$PROJECT_ID/$_JOB:$_VERSION
10+
- id: 'Deploy to Cloud Run'
11+
name: 'gcr.io/cloud-builders/gcloud:latest'
12+
entrypoint: /bin/bash
13+
args:
14+
- '-c'
15+
- |
16+
./test/retry.sh ""
17+
substitutions:
18+
_JOB: logger
19+
_VERSION: manual

run/jobs/pom.xml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,24 @@ limitations under the License.
3232
<maven.compiler.target>11</maven.compiler.target>
3333
<maven.compiler.source>11</maven.compiler.source>
3434
</properties>
35+
<dependencyManagement>
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>libraries-bom</artifactId>
40+
<version>21.0.0</version>
41+
<type>pom</type>
42+
<scope>import</scope>
43+
</dependency>
44+
</dependencies>
45+
</dependencyManagement>
3546
<dependencies>
3647
<!-- The following dependencies are only required for testing -->
48+
<dependency>
49+
<groupId>com.google.cloud</groupId>
50+
<artifactId>google-cloud-logging</artifactId>
51+
<scope>test</scope>
52+
</dependency>
3753
<dependency>
3854
<groupId>junit</groupId>
3955
<artifactId>junit</artifactId>
@@ -52,13 +68,5 @@ limitations under the License.
5268
<version>1.1.3</version>
5369
<scope>test</scope>
5470
</dependency>
55-
56-
<!-- Required for mocking env vars -->
57-
<dependency>
58-
<groupId>com.github.stefanbirkner</groupId>
59-
<artifactId>system-rules</artifactId>
60-
<version>1.19.0</version>
61-
<scope>test</scope>
62-
</dependency>
6371
</dependencies>
6472
</project>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
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+
package com.example;
18+
19+
import com.google.api.gax.paging.Page;
20+
import com.google.cloud.logging.LogEntry;
21+
import com.google.cloud.logging.Logging;
22+
import com.google.cloud.logging.Logging.EntryListOption;
23+
import com.google.cloud.logging.LoggingOptions;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.nio.charset.StandardCharsets;
29+
import java.time.Duration;
30+
import java.time.Instant;
31+
import java.time.format.DateTimeFormatter;
32+
import java.util.Iterator;
33+
import org.junit.AfterClass;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.JUnit4;
38+
39+
@RunWith(JUnit4.class)
40+
public class JobsExampleSystemTest {
41+
// Environment variables
42+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
43+
private static final String SERVICE_NAME = getenvOrDefault("SERVICE_NAME", "cr-jobs-test");
44+
private static final String SAMPLE_VERSION = getenvOrDefault("SAMPLE_VERSION", "manual");
45+
46+
// Settings
47+
private static final String JOB_NAME = SERVICE_NAME + "-" + SAMPLE_VERSION;
48+
private static final String REGION = "us-central1";
49+
private static final String JOB_NUM = "10";
50+
51+
public static String getenvOrDefault(String name, String defaultValue) {
52+
if (System.getenv().containsKey(name)) {
53+
return System.getenv(name);
54+
}
55+
56+
return defaultValue;
57+
}
58+
59+
public static String runGCloudCommand(String cmd) throws IOException, InterruptedException {
60+
String baseDir = System.getProperty("user.dir");
61+
62+
ProcessBuilder builder = new ProcessBuilder()
63+
.command(cmd.split(" "))
64+
.directory(new File(baseDir));
65+
66+
Process process = builder.start();
67+
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
68+
69+
InputStream stdoutStream = process.getInputStream();
70+
InputStream stderrStream = process.getErrorStream();
71+
72+
process.waitFor();
73+
74+
// Fetch command output, in case it's needed for debugging
75+
outBytes.write(stdoutStream.readNBytes(stdoutStream.available()));
76+
outBytes.write(stderrStream.readNBytes(stderrStream.available()));
77+
78+
String output = outBytes.toString(StandardCharsets.UTF_8);
79+
80+
// Done!
81+
return output;
82+
}
83+
84+
@BeforeClass
85+
public static void setUp() throws Exception {
86+
String substitutions = String.format(
87+
"--substitutions _JOB=%s,_VERSION=%s",
88+
JOB_NAME, SAMPLE_VERSION
89+
);
90+
String command = "gcloud builds submit " +
91+
"--project " + PROJECT_ID + " " +
92+
"--config e2e_test_setup.yaml " +
93+
substitutions;
94+
95+
String output = runGCloudCommand(command);
96+
}
97+
98+
@AfterClass
99+
public static void tearDown() throws Exception {
100+
String substitutions = String.format(
101+
"--substitutions _JOB=%s,_REGION=%s",
102+
JOB_NAME, REGION
103+
);
104+
String command = "gcloud builds submit " +
105+
"--project " + PROJECT_ID + " " +
106+
"--config e2e_test_cleanup.yaml " +
107+
substitutions;
108+
109+
runGCloudCommand(command);
110+
}
111+
112+
@Test
113+
public void ranAllTasksSuccessfully() {
114+
// Ignore logs older than 4 minutes
115+
Instant startInstant = Instant.now().minus(Duration.ofMinutes(4));
116+
String startTimestamp = DateTimeFormatter.ISO_INSTANT.format(startInstant);
117+
118+
// Construct log filter
119+
String logFilter =
120+
"resource.type=\"cloud_run_revision\" " +
121+
"resource.labels.service_name=\"" + JOB_NAME + "\" " +
122+
"timestamp >= \"" + startTimestamp + "\" " +
123+
"\"Starting\"";
124+
125+
// Get matching log entries
126+
LoggingOptions options = LoggingOptions.getDefaultInstance();
127+
Logging logging = options.getService();
128+
129+
Page<LogEntry> entries = logging.listLogEntries(
130+
EntryListOption.filter(logFilter));
131+
132+
Iterator<LogEntry> entryIterator = entries.iterateAll().iterator();
133+
134+
// Check that a matching log entry was found
135+
assert entryIterator.hasNext();
136+
}
137+
}

run/jobs/src/test/java/com/example/JobsExampleTests.java renamed to run/jobs/src/test/java/com/example/JobsExampleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static com.google.common.truth.Truth.assertThat;
3030

3131
@RunWith(JUnit4.class)
32-
public class JobsExampleTests {
32+
public class JobsExampleTest {
3333

3434
private String runSample(int failRate) throws IOException, InterruptedException {
3535
// Initialize the JAR-running process

0 commit comments

Comments
 (0)