Add schema deployment tests (#265)

* Add schema deployment tests

Updated flyway schema script files so that they reflect what is
currently deployed in alpha: ClaimsList and PremiumList related
elements.

Put post-schema-push pg_dump output in nomulus.golden.sql as the
authoritative schema. Also added test to verify that the schema
pushed by flyway will result in exactly the golden schema.

Upgraded testcontainers to 1.12.1.

Added a custom Truth subject for better diffing of multi-line
text blocks.

Removed claims_list.sql and premium_list.sql, as we do not have use for
them.

* Add schema deployment tests

Updated flyway schema script files so that they reflect what is
currently deployed in alpha: ClaimsList and PremiumList related
elements.

Put post-schema-push pg_dump output in nomulus.golden.sql as the
authoritative schema. Also added test to verify that the schema
pushed by flyway will result in exactly the golden schema.

Upgraded testcontainers to 1.12.1.

Added a custom Truth subject for better diffing of multi-line
text blocks.

Removed claims_list.sql and premium_list.sql, as we do not have use for
them.

* Add schema deployment tests

Updated flyway schema script files so that they reflect what is
currently deployed in alpha: ClaimsList and PremiumList related
elements.

Put post-schema-push pg_dump output in nomulus.golden.sql as the
authoritative schema. Also added test to verify that the schema
pushed by flyway will result in exactly the golden schema.

Upgraded testcontainers to 1.12.1.

Added a custom Truth subject for better diffing of multi-line
text blocks.

Removed claims_list.sql and premium_list.sql, as we do not have use for
them.
This commit is contained in:
Weimin Yu 2019-09-12 15:16:12 -04:00 committed by GitHub
parent 40a6b788a0
commit 3fb799f112
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 651 additions and 61 deletions

View file

@ -0,0 +1,108 @@
// Copyright 2019 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.sql.flyway;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.TextDiffSubject.assertThat;
import com.google.common.base.Joiner;
import com.google.common.io.Resources;
import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import org.flywaydb.core.Flyway;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.PostgreSQLContainer;
/** Unit tests about Cloud SQL schema. */
@RunWith(JUnit4.class)
public class SchemaTest {
// Resource path that is mapped to the testcontainer instance.
private static final String MOUNTED_RESOURCE_PATH = "testcontainer/mount";
// The mount point in the container.
private static final String CONTAINER_MOUNT_POINT = "/tmp/pg_dump_out";
// pg_dump output file name.
private static final String DUMP_OUTPUT_FILE = "dump.txt";
/**
* The target database for schema deployment.
*
* <p>A resource path is mapped to this container in READ_WRITE mode to retrieve the deployed
* schema generated by the 'pg_dump' command. We do not communicate over stdout because
* testcontainer adds spurious newlines. See <a
* href=https://github.com/testcontainers/testcontainers-java/issues/1854>this link</a> for more
* information.
*/
@Rule
public PostgreSQLContainer sqlContainer =
new PostgreSQLContainer<>("postgres:9.6.12")
.withClasspathResourceMapping(
MOUNTED_RESOURCE_PATH, CONTAINER_MOUNT_POINT, BindMode.READ_WRITE);
@Test
public void deploySchema_success() throws Exception {
Flyway flyway =
Flyway.configure()
.locations("sql/flyway")
.dataSource(
sqlContainer.getJdbcUrl(), sqlContainer.getUsername(), sqlContainer.getPassword())
.load();
// flyway.migrate() returns the number of newly pushed scripts. This is a variable
// number as our schema evolves.
assertThat(flyway.migrate()).isGreaterThan(0);
flyway.validate();
Container.ExecResult execResult =
sqlContainer.execInContainer(
StandardCharsets.UTF_8,
getSchemaDumpCommand(sqlContainer.getUsername(), sqlContainer.getDatabaseName()));
if (execResult.getExitCode() != 0) {
throw new RuntimeException(execResult.toString());
}
URL dumpedSchema =
Resources.getResource(
Joiner.on(File.separatorChar).join(MOUNTED_RESOURCE_PATH, DUMP_OUTPUT_FILE));
assertThat(dumpedSchema)
.hasSameContentAs(Resources.getResource("sql/schema/nomulus.golden.sql"));
}
private static String[] getSchemaDumpCommand(String username, String dbName) {
return new String[] {
"pg_dump",
"-h",
"localhost",
"-U",
username,
"-f",
Paths.get(CONTAINER_MOUNT_POINT, DUMP_OUTPUT_FILE).toString(),
"--schema-only",
"--no-owner",
"--no-privileges",
"--exclude-table",
"flyway_schema_history",
dbName
};
}
}