From a3e91b0e2e7b685793e39e2cf57f2625b572dfc8 Mon Sep 17 00:00:00 2001 From: Burke Davison <40617934+burkedavison@users.noreply.github.com> Date: Mon, 10 Jul 2023 13:07:14 +0000 Subject: [PATCH] ci: showcase native check (#1833) * ci: showcase native check * fix: add explicit java version * fix: adjust syntax * fix: add resource-config entry for ITGdch * fix: copy file to temp folder so it can be accessed by path * chore: formatting * fix: prevent shutdown warnings with client.awaitTermination * ci: fix build file location for downstream test * chore: use static imports for Truth assertions --- .github/workflows/ci.yaml | 27 +++++++++++++ .kokoro/presubmit/common.cfg | 4 +- .../google/showcase/v1beta1/it/ITGdch.java | 39 ++++++++++++------- .../native-image/resource-config.json | 5 ++- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c7268149fc..5677dd5bb6 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -208,6 +208,33 @@ jobs: --batch-mode \ --no-transfer-progress + showcase-native: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: graalvm/setup-graalvm@v1 + with: + version: '22.3.2' + java-version: '17' + components: 'native-image' + github-token: ${{ secrets.GITHUB_TOKEN }} + - run: mvn -version + - run: native-image --version + - name: Install sdk-platform-java + run: mvn install -B -ntp -DskipTests -Dclirr.skip -Dcheckstyle.skip + - name: Install showcase server + run: | + sudo mkdir -p /usr/src/showcase + sudo chown -R ${USER} /usr/src/ + curl --location https://github.com/googleapis/gapic-showcase/releases/download/v${SHOWCASE_VERSION}/gapic-showcase-${SHOWCASE_VERSION}-linux-amd64.tar.gz --output /usr/src/showcase/showcase-${SHOWCASE_VERSION}-linux-amd64.tar.gz + cd /usr/src/showcase/ + tar -xf showcase-* + ./gapic-showcase run & + cd - + - name: Build native image + working-directory: showcase + run: mvn test -Pnative,-showcase -ntp -B + showcase-clirr: if: ${{ github.base_ref != '' }} # Only execute on pull_request trigger event runs-on: ubuntu-22.04 diff --git a/.kokoro/presubmit/common.cfg b/.kokoro/presubmit/common.cfg index 1f21b18ef8..c5b2eb7bfb 100644 --- a/.kokoro/presubmit/common.cfg +++ b/.kokoro/presubmit/common.cfg @@ -4,11 +4,11 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # All builds use the trampoline script to run in docker. -build_file: "gapic-generator-java/.kokoro/trampoline.sh" +build_file: "sdk-platform-java/.kokoro/trampoline.sh" env_vars: { key: "TRAMPOLINE_BUILD_FILE" - value: "github/gapic-generator-java/.kokoro/presubmit/downstream-build.sh" + value: "github/sdk-platform-java/.kokoro/presubmit/downstream-build.sh" } # TODO: remove this after we've migrated all tests and scripts diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java index 83dfb3d704..f124f50295 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java @@ -16,6 +16,8 @@ package com.google.showcase.v1beta1.it; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertThrows; @@ -32,15 +34,17 @@ import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoSettings; import com.google.showcase.v1beta1.it.util.InterceptingMockTokenServerTransportFactory; -import com.google.showcase.v1beta1.stub.EchoStub; +import com.google.showcase.v1beta1.it.util.TestClientInitializer; import com.google.showcase.v1beta1.stub.EchoStubSettings; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -53,10 +57,11 @@ */ public class ITGdch { - private static final String TEST_GDCH_CREDENTIAL_FILE = "/test_gdch_credential.json"; - private static final String CA_CERT_RESOURCE_PATH = "/fake_cert.pem"; + private static final String CA_CERT_FILENAME = "fake_cert.pem"; + private static final String CA_CERT_RESOURCE_PATH = "/" + CA_CERT_FILENAME; private static final String CA_CERT_JSON_KEY = "ca_cert_path"; - private static final String TEMP_CREDENTIAL_JSON_FILENAME = "temp_gdch_credential.json"; + private static final String GDCH_CREDENTIAL_FILENAME = "test_gdch_credential.json"; + private static final String GDCH_CREDENTIAL_RESOURCE_PATH = "/" + GDCH_CREDENTIAL_FILENAME; private static final String GDCH_TOKEN_STRING = "1/MkSJoj1xsli0AccessToken_NKPY2"; private static final String SID_NAME = "service-identity-name"; @@ -67,7 +72,6 @@ public class ITGdch { private EchoStubSettings stubSettings; private Credentials initialCredentials; private ClientContext context; - private EchoStub stub; private InterceptingMockTokenServerTransportFactory transportFactory; private String projectId; private URI tokenUri; @@ -76,7 +80,6 @@ public class ITGdch { public void setup() throws IOException, URISyntaxException { transportFactory = new InterceptingMockTokenServerTransportFactory(); prepareCredentials(); - tempFolder.create(); settings = EchoSettings.newBuilder() .setCredentialsProvider(FixedCredentialsProvider.create(initialCredentials)) @@ -84,29 +87,35 @@ public void setup() throws IOException, URISyntaxException { } @After - public void tearDown() { + public void tearDown() throws InterruptedException { if (client != null) { client.close(); + client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); } } - private void prepareCredentials() throws IOException, URISyntaxException { - // compute absolute path of the CA certificate - Path caCertPath = Paths.get(getClass().getResource(CA_CERT_RESOURCE_PATH).toURI()); + private void prepareCredentials() throws IOException { + // Copy file so it can be referenced by Path even in native-image builds + File caCertFile = tempFolder.newFile(CA_CERT_FILENAME); + try (InputStream inputStream = getClass().getResourceAsStream(CA_CERT_RESOURCE_PATH)) { + assertThat(inputStream).isNotNull(); + Files.copy(inputStream, caCertFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + assertWithMessage(caCertFile.toPath() + " should exist").that(caCertFile.exists()).isTrue(); // open gdch credential json (still needs its "ca_cert_path" to point to the CA certificate // obtained from above) JsonFactory factory = new GsonFactory(); GenericJson converted = factory.fromInputStream( - getClass().getResourceAsStream(TEST_GDCH_CREDENTIAL_FILE), GenericJson.class); + getClass().getResourceAsStream(GDCH_CREDENTIAL_RESOURCE_PATH), GenericJson.class); // modify and save to a temporary folder - converted.set(CA_CERT_JSON_KEY, caCertPath.toAbsolutePath().toString()); + converted.set(CA_CERT_JSON_KEY, caCertFile.toPath().toAbsolutePath().toString()); projectId = converted.get("project").toString(); tokenUri = URI.create(converted.get("token_uri").toString()); - File tempGdchCredentialFile = tempFolder.newFile(TEMP_CREDENTIAL_JSON_FILENAME); + File tempGdchCredentialFile = tempFolder.newFile(GDCH_CREDENTIAL_FILENAME); try (FileWriter fileWriter = new FileWriter(tempGdchCredentialFile)) { String preparedJson = converted.toPrettyString(); fileWriter.write(preparedJson); diff --git a/showcase/gapic-showcase/src/test/resources/META-INF/native-image/resource-config.json b/showcase/gapic-showcase/src/test/resources/META-INF/native-image/resource-config.json index 80d7bc0ea2..bfa2659f0a 100644 --- a/showcase/gapic-showcase/src/test/resources/META-INF/native-image/resource-config.json +++ b/showcase/gapic-showcase/src/test/resources/META-INF/native-image/resource-config.json @@ -1,3 +1,6 @@ { - "resources":[{"pattern": ".*.json"}] + "resources": [ + { "pattern": ".*.json" }, + { "pattern": ".*.pem" } + ] } \ No newline at end of file