forked from GoogleCloudPlatform/DataflowTemplates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from GoogleCloudPlatform/main
tests: Adding Forward Migration Tests (GoogleCloudPlatform#2001)
- Loading branch information
Showing
16 changed files
with
886 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
...nner/src/test/java/com/google/cloud/teleport/v2/templates/DataStreamToSpannerMixedIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
/* | ||
* Copyright (C) 2024 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.teleport.v2.templates; | ||
|
||
import static org.apache.beam.it.truthmatchers.PipelineAsserts.assertThatResult; | ||
|
||
import com.google.cloud.teleport.metadata.SkipDirectRunnerTest; | ||
import com.google.cloud.teleport.metadata.TemplateIntegrationTest; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.beam.it.common.PipelineLauncher; | ||
import org.apache.beam.it.common.PipelineOperator; | ||
import org.apache.beam.it.common.utils.ResourceManagerUtils; | ||
import org.apache.beam.it.conditions.ChainedConditionCheck; | ||
import org.apache.beam.it.gcp.pubsub.PubsubResourceManager; | ||
import org.apache.beam.it.gcp.spanner.SpannerResourceManager; | ||
import org.apache.beam.it.gcp.spanner.conditions.SpannerRowsCheck; | ||
import org.apache.beam.it.gcp.spanner.matchers.SpannerAsserts; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* An integration test for {@link DataStreamToSpanner} Flex template which multiple use-cases | ||
* tested: 1. Foreign Keys. 2. Table Dropped. 3. Column Rename. 4. DLQ Retry. 5. Missing PK 6. | ||
* Indexes | ||
*/ | ||
@Category({TemplateIntegrationTest.class, SkipDirectRunnerTest.class}) | ||
@TemplateIntegrationTest(DataStreamToSpanner.class) | ||
@RunWith(JUnit4.class) | ||
public class DataStreamToSpannerMixedIT extends DataStreamToSpannerITBase { | ||
|
||
private static final String TABLE1 = "Authors"; | ||
private static final String TABLE2 = "Books"; | ||
private static final String TABLE3 = "Genre"; | ||
private static PipelineLauncher.LaunchInfo jobInfo; | ||
private static HashSet<DataStreamToSpannerMixedIT> testInstances = new HashSet<>(); | ||
public static PubsubResourceManager pubsubResourceManager; | ||
public static SpannerResourceManager spannerResourceManager; | ||
private static final String SPANNER_DDL_RESOURCE = | ||
"DataStreamToSpannerMixedIT/spanner-schema.sql"; | ||
private static final String SESSION_FILE_RESOURCE = | ||
"DataStreamToSpannerMixedIT/mysql-session.json"; | ||
|
||
/** | ||
* Setup resource managers and Launch dataflow job once during the execution of this test class. | ||
*/ | ||
@Before | ||
public void setUp() throws IOException { | ||
// Prevent cleaning up of dataflow job after a test method is executed. | ||
skipBaseCleanup = true; | ||
synchronized (DataStreamToSpannerMixedIT.class) { | ||
testInstances.add(this); | ||
if (jobInfo == null) { | ||
spannerResourceManager = setUpSpannerResourceManager(); | ||
pubsubResourceManager = setUpPubSubResourceManager(); | ||
createSpannerDDL(spannerResourceManager, SPANNER_DDL_RESOURCE); | ||
jobInfo = | ||
launchDataflowJob( | ||
getClass().getSimpleName(), | ||
SESSION_FILE_RESOURCE, | ||
null, | ||
"MixedIT", | ||
spannerResourceManager, | ||
pubsubResourceManager, | ||
new HashMap<>() { | ||
{ | ||
put("inputFileFormat", "avro"); | ||
} | ||
}, | ||
null, | ||
null); | ||
} | ||
} | ||
} | ||
|
||
/** Cleanup dataflow job and all the resources and resource managers. */ | ||
@AfterClass | ||
public static void cleanUp() throws IOException { | ||
for (DataStreamToSpannerMixedIT instance : testInstances) { | ||
instance.tearDownBase(); | ||
} | ||
ResourceManagerUtils.cleanResources(spannerResourceManager, pubsubResourceManager); | ||
} | ||
|
||
@Test | ||
public void mixedMigrationTest() throws InterruptedException { | ||
// Construct a ChainedConditionCheck with 2 stages. | ||
// 1. Send initial wave of events | ||
// 2. Wait on Spanner to have events | ||
ChainedConditionCheck conditionCheck = | ||
ChainedConditionCheck.builder( | ||
List.of( | ||
uploadDataStreamFile( | ||
jobInfo, | ||
TABLE1, | ||
"authors_1.avro", | ||
"DataStreamToSpannerMixedIT/Authors_1.avro"), | ||
uploadDataStreamFile( | ||
jobInfo, TABLE2, "books.avro", "DataStreamToSpannerMixedIT/Books.avro"), | ||
uploadDataStreamFile( | ||
jobInfo, TABLE3, "genre.avro", "DataStreamToSpannerMixedIT/Genre.avro"), | ||
SpannerRowsCheck.builder(spannerResourceManager, TABLE1) | ||
.setMinRows(1) | ||
.setMaxRows(1) | ||
.build())) | ||
.build(); | ||
|
||
// Wait for conditions | ||
PipelineOperator.Result result = | ||
pipelineOperator() | ||
.waitForCondition(createConfig(jobInfo, Duration.ofMinutes(8)), conditionCheck); | ||
|
||
TimeUnit.MINUTES.sleep(1); | ||
// Assert Conditions | ||
assertThatResult(result).meetsConditions(); | ||
|
||
conditionCheck = | ||
ChainedConditionCheck.builder( | ||
List.of( | ||
uploadDataStreamFile( | ||
jobInfo, | ||
TABLE1, | ||
"authors_2.avro", | ||
"DataStreamToSpannerMixedIT/Authors_2.avro"), | ||
SpannerRowsCheck.builder(spannerResourceManager, TABLE1) | ||
.setMinRows(4) | ||
.setMaxRows(4) | ||
.build(), | ||
SpannerRowsCheck.builder(spannerResourceManager, TABLE2) | ||
.setMinRows(3) | ||
.setMaxRows(3) | ||
.build())) | ||
.build(); | ||
|
||
result = | ||
pipelineOperator() | ||
.waitForCondition(createConfig(jobInfo, Duration.ofMinutes(8)), conditionCheck); | ||
|
||
// Assert Conditions | ||
assertThatResult(result).meetsConditions(); | ||
|
||
assertAuthorsTableContents(); | ||
|
||
assertBooksTableContents(); | ||
} | ||
|
||
private void assertAuthorsTableContents() { | ||
List<Map<String, Object>> authorEvents = new ArrayList<>(); | ||
|
||
Map<String, Object> authorRow1 = new HashMap<>(); | ||
authorRow1.put("author_id", 4); | ||
authorRow1.put("full_name", "Stephen King"); | ||
|
||
Map<String, Object> authorRow2 = new HashMap<>(); | ||
authorRow2.put("author_id", 1); | ||
authorRow2.put("full_name", "Jane Austen"); | ||
|
||
Map<String, Object> authorRow3 = new HashMap<>(); | ||
authorRow3.put("author_id", 2); | ||
authorRow3.put("full_name", "Charles Dickens"); | ||
|
||
Map<String, Object> authorRow4 = new HashMap<>(); | ||
authorRow4.put("author_id", 3); | ||
authorRow4.put("full_name", "Leo Tolstoy"); | ||
|
||
authorEvents.add(authorRow1); | ||
authorEvents.add(authorRow2); | ||
authorEvents.add(authorRow3); | ||
authorEvents.add(authorRow4); | ||
|
||
SpannerAsserts.assertThatStructs(spannerResourceManager.runQuery("select * from Authors")) | ||
.hasRecordsUnorderedCaseInsensitiveColumns(authorEvents); | ||
} | ||
|
||
private void assertBooksTableContents() { | ||
List<Map<String, Object>> bookEvents = new ArrayList<>(); | ||
|
||
Map<String, Object> bookRow1 = new HashMap<>(); | ||
bookRow1.put("id", 1); | ||
bookRow1.put("title", "Pride and Prejudice"); | ||
|
||
Map<String, Object> bookRow2 = new HashMap<>(); | ||
bookRow2.put("id", 2); | ||
bookRow2.put("title", "Oliver Twist"); | ||
|
||
Map<String, Object> bookRow3 = new HashMap<>(); | ||
bookRow3.put("id", 3); | ||
bookRow3.put("title", "War and Peace"); | ||
|
||
bookEvents.add(bookRow1); | ||
bookEvents.add(bookRow2); | ||
bookEvents.add(bookRow3); | ||
|
||
SpannerAsserts.assertThatStructs(spannerResourceManager.runQuery("select id, title from Books")) | ||
.hasRecordsUnorderedCaseInsensitiveColumns(bookEvents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.67 KB
v2/datastream-to-spanner/src/test/resources/DataStreamToSpannerDDLIT/Singers.avro
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.48 KB
v2/datastream-to-spanner/src/test/resources/DataStreamToSpannerMixedIT/Authors_1.avro
Binary file not shown.
Binary file added
BIN
+1.96 KB
v2/datastream-to-spanner/src/test/resources/DataStreamToSpannerMixedIT/Authors_2.avro
Binary file not shown.
Binary file added
BIN
+1.96 KB
v2/datastream-to-spanner/src/test/resources/DataStreamToSpannerMixedIT/Books.avro
Binary file not shown.
Binary file added
BIN
+1.83 KB
v2/datastream-to-spanner/src/test/resources/DataStreamToSpannerMixedIT/Genre.avro
Binary file not shown.
19 changes: 19 additions & 0 deletions
19
v2/datastream-to-spanner/src/test/resources/DataStreamToSpannerMixedIT/mysql-schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
CREATE TABLE `Authors` ( | ||
`author_id` int NOT NULL, -- To: category_id INT64 | ||
`name` varchar(25), -- To: full_name STRING(25) Column name renamed | ||
`last_update` timestamp, -- To: Column dropped in spanner | ||
PRIMARY KEY (`author_id`) | ||
); | ||
|
||
CREATE TABLE `Books` ( | ||
`id` int NOT NULL, | ||
`title` varchar(200), | ||
`author_id` int, | ||
FOREIGN KEY (author_id) REFERENCES Author(author_id) | ||
); | ||
|
||
CREATE TABLE `Genre` ( | ||
`genre_id` int not NULL, | ||
`name` varchar(200), | ||
PRIMARY KEY (`genre_id`) | ||
); |
Oops, something went wrong.