From bb3fe3c318105fe52b36c5b6f6fb197df73c2207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 28 Aug 2023 19:24:14 +0200 Subject: [PATCH] test: add test for pgx sample (#1003) --- .gitignore | 1 + .../pgadapter/golang/ITPgxSampleTest.java | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/test/java/com/google/cloud/spanner/pgadapter/golang/ITPgxSampleTest.java diff --git a/.gitignore b/.gitignore index dcd91456b..f0d25a99a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ src/test/golang/**/*.h src/test/golang/**/*.so samples/golang/**/*.h samples/golang/**/*.so +samples/golang/pgx/pgx_sample src/test/csharp/**/bin src/test/csharp/**/obj diff --git a/src/test/java/com/google/cloud/spanner/pgadapter/golang/ITPgxSampleTest.java b/src/test/java/com/google/cloud/spanner/pgadapter/golang/ITPgxSampleTest.java new file mode 100644 index 000000000..fcb664c55 --- /dev/null +++ b/src/test/java/com/google/cloud/spanner/pgadapter/golang/ITPgxSampleTest.java @@ -0,0 +1,103 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.cloud.spanner.pgadapter.golang; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.cloud.spanner.Database; +import com.google.cloud.spanner.pgadapter.IntegrationTest; +import com.google.cloud.spanner.pgadapter.PgAdapterTestEnv; +import com.google.common.collect.ImmutableList; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Scanner; +import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@Category(IntegrationTest.class) +@RunWith(JUnit4.class) +public class ITPgxSampleTest implements IntegrationTest { + private static final PgAdapterTestEnv testEnv = new PgAdapterTestEnv(); + private static Database database; + + @BeforeClass + public static void setup() throws Exception { + String currentPath = new java.io.File(".").getCanonicalPath(); + String relativeSampleFilePath = "samples/golang/pgx/pgx_sample.go"; + String sampleFilePath = String.format("%s/%s", currentPath, relativeSampleFilePath); + File testFile = new File(sampleFilePath); + File directory = testFile.getParentFile(); + // Compile the Go code to ensure that we always have the most recent sample code. + ProcessBuilder builder = new ProcessBuilder(); + String[] compileCommand = String.format("go build %s", testFile.getName()).split(" "); + builder.command(compileCommand); + builder.directory(directory); + Process process = builder.start(); + int res = process.waitFor(); + assertEquals(0, res); + + testEnv.setUp(); + database = testEnv.createDatabase(ImmutableList.of()); + } + + @AfterClass + public static void teardown() { + testEnv.stopPGAdapterServer(); + testEnv.cleanUp(); + } + + @Test + public void testPgxSample() throws Exception { + String currentPath = new java.io.File(".").getCanonicalPath(); + String relativeSampleFilePath = "samples/golang/pgx/pgx_sample"; + String sampleFilePath = String.format("%s/%s", currentPath, relativeSampleFilePath); + ProcessBuilder builder = new ProcessBuilder(); + builder.directory(new File(sampleFilePath).getParentFile()); + builder.command( + String.format( + "./pgx_sample -project %s -instance %s -database %s", + database.getId().getInstanceId().getProject(), + database.getId().getInstanceId().getInstance(), + database.getId().getDatabase()) + .split(" ")); + Process process = builder.start(); + InputStream inputStream = process.getInputStream(); + InputStream errorStream = process.getErrorStream(); + boolean finished = process.waitFor(120L, TimeUnit.SECONDS); + + String output = readAll(inputStream); + String errors = readAll(errorStream); + assertTrue(finished); + assertEquals(errors, 0, process.exitValue()); + assertTrue(output, output.contains("Greeting from Cloud Spanner PostgreSQL: Hello world!")); + } + + static String readAll(InputStream inputStream) { + StringBuilder result = new StringBuilder(); + try (Scanner scanner = new Scanner(new InputStreamReader(inputStream))) { + while (scanner.hasNextLine()) { + result.append(scanner.nextLine()).append("\n"); + } + } + return result.toString(); + } +}