Skip to content

Commit

Permalink
Refactor MigrationAcceptanceTest to test for major version bumps (#8154)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgardens authored Nov 30, 2021
1 parent 6787777 commit ada2e17
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2021 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.commons.concurrency;

import static java.lang.Thread.sleep;

import java.time.Duration;
import java.util.function.Supplier;

public class WaitingUtils {

/**
* Wait for a condition or timeout.
*
* @param interval - frequency with which condition and timeout should be checked.
* @param timeout - how long to wait in total
* @param condition - supplier that returns whether the condition has been met.
* @return true if condition was met before the timeout was reached, otherwise false.
*/
@SuppressWarnings("BusyWait")
public static boolean waitForCondition(final Duration interval, final Duration timeout, final Supplier<Boolean> condition) {
Duration timeWaited = Duration.ZERO;
while (true) {
if (condition.get()) {
return true;
}

if (timeout.minus(timeWaited).isNegative()) {
return false;
}

try {
sleep(interval.toMillis());
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}

timeWaited = timeWaited.plus(interval);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.base.Preconditions;
import com.google.common.io.Resources;
import io.airbyte.commons.lang.Exceptions;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -33,6 +34,11 @@ public static String readResource(final Class<?> klass, final String name) throw
return Resources.toString(url, StandardCharsets.UTF_8);
}

@SuppressWarnings("UnstableApiUsage")
public static File readResourceAsFile(final String name) throws URISyntaxException {
return new File(Resources.getResource(name).toURI());
}

@SuppressWarnings("UnstableApiUsage")
public static byte[] readBytes(final String name) throws IOException {
final URL resource = Resources.getResource(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.commons.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class MoreProperties {

/**
* Read an .env file into a Properties object.
*
* @param envFile - .env file to read
* @return properties object parsed from the contents of the .env
* @throws IOException throws an exception if there are errors while reading the file.
*/
public static Properties envFileToProperties(final File envFile) throws IOException {
final Properties prop = new Properties();
prop.load(new FileInputStream(envFile));
return prop;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.commons.concurrency;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.time.Duration;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;

class WaitingUtilsTest {

@SuppressWarnings("unchecked")
@Test
void testWaitForConditionConditionMet() {
final Supplier<Boolean> condition = mock(Supplier.class);
when(condition.get())
.thenReturn(false)
.thenReturn(false)
.thenReturn(true);
assertTrue(WaitingUtils.waitForCondition(Duration.ofMillis(1), Duration.ofMillis(5), condition));
}

@SuppressWarnings("unchecked")
@Test
void testWaitForConditionTimeout() {
final Supplier<Boolean> condition = mock(Supplier.class);
when(condition.get()).thenReturn(false);
assertFalse(WaitingUtils.waitForCondition(Duration.ofMillis(1), Duration.ofMillis(5), condition));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.common.collect.Sets;
import io.airbyte.commons.io.IOs;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.stream.Collectors;
Expand All @@ -35,6 +38,12 @@ void testResourceReadWithClass() throws IOException {
assertThrows(IllegalArgumentException.class, () -> MoreResources.readResource(MoreResourcesTest.class, "invalid"));
}

@Test
void testReadResourceAsFile() throws URISyntaxException {
final File file = MoreResources.readResourceAsFile("resource_test");
assertEquals("content1\n", IOs.readFile(file.toPath()));
}

@Test
void testReadBytes() throws IOException {
assertEquals("content1\n", new String(MoreResources.readBytes("resource_test"), StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2021 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.commons.util;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.airbyte.commons.io.IOs;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.junit.jupiter.api.Test;

class MorePropertiesTest {

@Test
void testEnvFileToProperties() throws IOException {
final String envFileContents = "OPTION1=hello\n"
+ "OPTION2=2\n"
+ "OPTION3=\n";
final File envFile = File.createTempFile("properties-test", ".env");
IOs.writeFile(envFile.toPath(), envFileContents);

final Properties actual = MoreProperties.envFileToProperties(envFile);
final Properties expected = new Properties();
expected.put("OPTION1", "hello");
expected.put("OPTION2", "2");
expected.put("OPTION3", "");

assertEquals(expected, actual);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
import io.airbyte.api.client.model.SyncMode;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.lang.MoreBooleans;
import io.airbyte.commons.resources.MoreResources;
import io.airbyte.commons.util.MoreProperties;
import io.airbyte.db.Database;
import io.airbyte.db.Databases;
import io.airbyte.test.airbyte_test_container.AirbyteTestContainer;
Expand Down Expand Up @@ -184,7 +186,7 @@ public static void init() throws URISyntaxException, IOException, InterruptedExc
if (!USE_EXTERNAL_DEPLOYMENT) {
LOGGER.info("Using deployment of airbyte managed by test containers.");
airbyteTestContainer = new AirbyteTestContainer.Builder(new File(Resources.getResource(DOCKER_COMPOSE_FILE_NAME).toURI()))
.setEnv(ENV_FILE)
.setEnv(MoreProperties.envFileToProperties(ENV_FILE))
// override env VERSION to use dev to test current build of airbyte.
.setEnvVariable("VERSION", "dev")
// override to use test mounts.
Expand All @@ -194,7 +196,7 @@ public static void init() throws URISyntaxException, IOException, InterruptedExc
.setEnvVariable("LOCAL_ROOT", "/tmp/airbyte_local_migration_test")
.setEnvVariable("LOCAL_DOCKER_MOUNT", "/tmp/airbyte_local_migration_test")
.build();
airbyteTestContainer.start();
airbyteTestContainer.startBlocking();
} else {
LOGGER.info("Using external deployment of airbyte.");
}
Expand Down Expand Up @@ -243,7 +245,7 @@ public void setup() throws ApiException, URISyntaxException, SQLException, IOExc
// seed database.
if (IS_GKE) {
final Database database = getSourceDatabase();
final Path path = Path.of(Resources.getResource("postgres_init.sql").toURI());
final Path path = Path.of(MoreResources.readResourceAsFile("postgres_init.sql").toURI());
final StringBuilder query = new StringBuilder();
for (final String line : java.nio.file.Files.readAllLines(path, UTF8)) {
if (line != null && !line.isEmpty()) {
Expand Down
Loading

0 comments on commit ada2e17

Please sign in to comment.