|
| 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 | +} |
0 commit comments