Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically run dropall before and after all integration tests #5493

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package liquibase.extension.testing.testsystem.spock;

import liquibase.Scope;
import liquibase.command.CommandScope;
import liquibase.command.core.DropAllCommandStep;
import liquibase.command.core.helpers.DbUrlConnectionArgumentsCommandStep;
import liquibase.configuration.ConfigurationValueConverter;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.extension.testing.testsystem.DatabaseTestSystem;
import liquibase.extension.testing.testsystem.TestSystem;
import liquibase.lockservice.LockService;
import liquibase.lockservice.LockServiceFactory;
import org.junit.Assume;
import org.spockframework.runtime.extension.AbstractMethodInterceptor;
import org.spockframework.runtime.extension.IMethodInvocation;
import org.spockframework.runtime.model.FieldInfo;
import org.spockframework.runtime.model.SpecInfo;

import java.io.ByteArrayOutputStream;
import java.util.*;

public class LiquibaseIntegrationMethodInterceptor extends AbstractMethodInterceptor {
Expand Down Expand Up @@ -57,7 +63,13 @@ private void verifyNoMultipleDatabases(SpecInfo spec) {
public void interceptSetupSpecMethod(IMethodInvocation invocation) throws Throwable {
final List<FieldInfo> containers = findAllContainers();
startContainers(containers, invocation);
dropAllDatabases();
invocation.proceed();
}

@Override
public void interceptSetupMethod(IMethodInvocation invocation) throws Throwable {
dropAllDatabases();
invocation.proceed();
}

Expand Down Expand Up @@ -112,6 +124,32 @@ private static TestSystem readContainerFromField(FieldInfo f, IMethodInvocation
*/
@Override
public void interceptCleanupSpecMethod(IMethodInvocation invocation) throws Throwable {
dropAllDatabases();
invocation.proceed();
}

private static void dropAllDatabases() throws Exception {
for (TestSystem startedTestSystem : startedTestSystems) {
if (startedTestSystem instanceof DatabaseTestSystem) {
runDropAll(((DatabaseTestSystem) startedTestSystem));
}
}
}

@Override
public void interceptCleanupMethod(IMethodInvocation invocation) throws Throwable {
dropAllDatabases();
invocation.proceed();
}

private static void runDropAll(DatabaseTestSystem db) throws Exception {
LockService lockService = LockServiceFactory.getInstance().getLockService(db.getDatabaseFromFactory());
lockService.releaseLock();
CommandScope commandScope = new CommandScope(DropAllCommandStep.COMMAND_NAME);
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, db.getConnectionUrl());
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, db.getUsername());
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, db.getPassword());
commandScope.setOutput(new ByteArrayOutputStream());
commandScope.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ class ClearCheckSumsIntegrationTest extends Specification {
@Shared
private DatabaseTestSystem h2 = (DatabaseTestSystem) Scope.currentScope.getSingleton(TestSystemFactory.class).getTestSystem("h2")

def setupSpec(){
def "validate checksums are cleared"() {
given:
def updateCommand = new CommandScope(UpdateCommandStep.COMMAND_NAME)
updateCommand.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, h2.getDatabaseFromFactory())
updateCommand.addArgumentValue(UpdateSqlCommandStep.CHANGELOG_FILE_ARG, "liquibase/update-tests.yml")
updateCommand.execute()
}

def "validate checksums are cleared"() {
when:
def h2Database = h2.getDatabaseFromFactory()
def commandResults = new CommandScope("clearChecksums")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GenerateChangeLogMySQLIntegrationTest extends Specification {
@Shared
private DatabaseTestSystem mysql = (DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("mysql")

def setupSpec() {
def setup() {
CommandUtil.runDropAll(mysql)
def sql = """
create table str4 (
Expand Down