Skip to content

Commit

Permalink
[DAT-12910]/[DAT-19440]/[DAT-19439] DiffTests changes (#982)
Browse files Browse the repository at this point in the history
* DAT-12910 refactored data for Diff tests/Added additional assertion (check objects folder)

* DAT-12910 small test fixes, (removed createPackage/createPackageBody PostgreSQL, MariaDB, MySQL, and SQL Server (MSSQL) do not have a direct CREATE PACKAGE)

* DAT-12910 changed config to diff/diffChangelog

* DAT-12910 changed create-infra.sh
  • Loading branch information
Tamelianovych authored Jan 27, 2025
1 parent 6ea55d4 commit 7d81e3b
Show file tree
Hide file tree
Showing 35 changed files with 1,307 additions and 1,149 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/advanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ on:
default: "[\"mysql-5.6\",\"mysql-5.7\",\"mysql-8\",\"mysql-8.4\",\"postgres-12\",\"postgres-13\",\"postgres-14\",\"postgres-15\",\"postgres-16\",\"postgres-17\",\"mariadb-10.2\",\"mariadb-10.3\",\"mariadb-10.4\",\"mariadb-10.5\",\"mariadb-10.6\",
\"mariadb-10.7\",\"mariadb-11.4\",\"mssql-2017\",\"mssql-2019\",\"mssql-2022\",\"crdb-23.1\",\"crdb-23.2\",\"crdb-24.1\",\"percona-xtradb-cluster-5.7\",\"percona-xtradb-cluster-8.0\",
\"edb-edb-12\",\"edb-edb-13\",\"edb-edb-14\",\"edb-edb-15\",\"edb-edb-16\",\"db2-luw\",\"H2Database-2.2\",\"sqlite\",\"derby\",
\"firebird-3\",\"firebird-4\",\"hsqldb-2.4\",\"hsqldb-2.5\",\"diff\",\"tidb\"]"
\"firebird-3\",\"firebird-4\",\"hsqldb-2.4\",\"hsqldb-2.5\",\"tidb\",\"diff\",\"diffChangelog\"]"

jobs:
check_build_safety:
Expand All @@ -53,7 +53,7 @@ jobs:
"postgres-12","postgres-13","postgres-14","postgres-15","postgres-16","postgres-17","mariadb-10.2","mariadb-10.3","mariadb-10.4","mariadb-10.5","mariadb-10.6",
"mariadb-10.7","mariadb-11.4","mssql-2017","mssql-2019","mssql-2022","crdb-23.1","crdb-23.2","crdb-24.1","percona-xtradb-cluster-5.7","percona-xtradb-cluster-8.0",
"edb-edb-12","edb-edb-13","edb-edb-14","edb-edb-15","edb-edb-16","db2-luw","H2Database-2.2","sqlite","derby","firebird-3","firebird-4",
"hsqldb-2.4","hsqldb-2.5","diff","tidb"]' }}
"hsqldb-2.4","hsqldb-2.5","tidb","diff",\"diffChangelog\"]' }}
testClasses: ${{ 'AdvancedHarnessSuiteTest' }}
steps:
- uses: actions/checkout@v4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.harness.diff

import com.datical.liquibase.ext.config.LiquibaseProConfiguration
import liquibase.database.jvm.JdbcConnection
import liquibase.harness.config.DatabaseUnderTest
import liquibase.harness.config.TestConfig
Expand All @@ -9,14 +10,16 @@ import spock.lang.Specification
import spock.lang.Unroll

import static liquibase.harness.diff.DiffCommandTestHelper.*
import static liquibase.harness.util.TestUtils.*
import static liquibase.harness.util.FileUtils.*
import static liquibase.harness.util.FileUtils.deleteFile
import static liquibase.harness.util.FileUtils.getResourceContent
import static liquibase.harness.util.TestUtils.chooseRollbackStrategy
import static liquibase.harness.util.TestUtils.executeCommandScope

class DiffCommandTest extends Specification {
class DiffChangelogTests extends Specification {
@Shared
RollbackStrategy strategy;
RollbackStrategy strategy
@Shared
List<DatabaseUnderTest> databases;
List<DatabaseUnderTest> databases

def setupSpec() {
databases = TestConfig.instance.getFilteredDatabasesUnderTest()
Expand Down Expand Up @@ -64,56 +67,49 @@ class DiffCommandTest extends Specification {
executeCommandScope("diffChangelog", argsMap)
}

and: "verify that the 'stored objects' directories are created"
argsMapRef.put("referenceUrl", testInput.targetDatabase.url)
argsMapRef.put("referenceUsername", testInput.targetDatabase.username)
argsMapRef.put("referencePassword", testInput.targetDatabase.password)
argsMapRef.put("changelogFile", testInput.pathToGeneratedXmlDiffChangelogFile)
Map<String, Object> scopeValues = new HashMap<>()
scopeValues.put(LiquibaseProConfiguration.INLINE_SQL_KEY.getKey(), false)
executeCommandScope("diffChangelog", argsMapRef, scopeValues)
File expectedDiffFolder = new File(testInput.pathToExpectedDiffFolder)
File[] subDirs = expectedDiffFolder.listFiles(new FileFilter() {
@Override
boolean accept(File file) {
return file.isDirectory()
}
})
Optional<File> objectsFolder = Arrays.stream(subDirs)
.filter(f -> f.getName().startsWith("objects-"))
.findFirst()

if (objectsFolder.isEmpty()) {
throw new RuntimeException("No 'objects' folder created inside " + testInput.pathToExpectedDiffFolder)
}
File actualObjectsFolder = objectsFolder.get()
for (String expectedObjectType : Arrays.asList("function", "trigger", "storedprocedure")) {
File objectDir = new File(actualObjectsFolder, expectedObjectType)
assert objectDir.exists() && objectDir.isDirectory() :
"Directory for stored object '" + expectedObjectType + "' was not created at path: " + objectDir.getPath() + "!"
}

then: "validate generated diff changelog"
assert validateGeneratedDiffChangelog(testInput)

cleanup: "try to rollback changes out from target database, delete generated changelog file"
argsMap.put("changelogFile", testInput.pathToChangelogFile)
argsMapRef.put("changelogFile", testInput.pathToReferenceChangelogFile)
strategy.performRollback(argsMap)
strategy.performRollback(argsMapRef)
clearFolder(actualObjectsFolder.getAbsolutePath())
for (Map.Entry<String, String> entry : map.entrySet()) {
deleteFile(entry.value)
}

where:
testInput << buildTestInput()
}

@Unroll
def "apply diff test against #testInput.referenceDatabase.name #testInput.referenceDatabase.version to targetDatabase #testInput.targetDatabase.name #testInput.targetDatabase.version"() {
given: "read input data for diff test"
Map<String, Object> argsMap = new HashMap()
argsMap.put("url", testInput.targetDatabase.url)
argsMap.put("username", testInput.targetDatabase.username)
argsMap.put("password", testInput.targetDatabase.password)
argsMap.put("referenceUrl", testInput.referenceDatabase.url)
argsMap.put("referenceUsername", testInput.referenceDatabase.username)
argsMap.put("referencePassword", testInput.referenceDatabase.password)
argsMap.put("changelogFile", testInput.pathToChangelogFile)

and: "check databases are online"
assert testInput.targetDatabase.database.getConnection() instanceof JdbcConnection: "Target database " +
"${testInput.targetDatabase.name}${testInput.targetDatabase.version} is offline!"
assert testInput.referenceDatabase.database.getConnection() instanceof JdbcConnection: "Reference database " +
"${testInput.referenceDatabase.name}${testInput.referenceDatabase.version} is offline!"

when: "execute update and diff commands"

executeCommandScope("update", argsMap)
argsMap.put("excludeObjects", "(?i)posts, (?i)authors, (?i)databasechangelog, (?i)databasechangeloglock")
String generatedDiffContent = removeDatabaseInfoFromDiff(executeCommandScope("diff", argsMap).toString())

then: "validate generated diff"
String expectedDiffContent = removeDatabaseInfoFromDiff(getResourceContent(testInput.pathToExpectedDiffFile))
assert expectedDiffContent == generatedDiffContent

cleanup: "rollback changes"
strategy.performRollback(argsMap)

where:
testInput << buildTestInput()
}
def cleanupSpec() {
strategy.cleanupDatabase(databases)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import liquibase.harness.config.DatabaseUnderTest
import liquibase.harness.config.TestConfig
import liquibase.harness.util.DatabaseConnectionUtil
import liquibase.ui.UIService
import org.apache.commons.io.FileUtils
import org.yaml.snakeyaml.Yaml

import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Collectors

import static liquibase.harness.util.FileUtils.*
Expand Down Expand Up @@ -91,6 +95,7 @@ class DiffCommandTestHelper {
"${targetDatabase.name}${targetDatabase.version}.json")
.targetDatabase(targetDatabase)
.referenceDatabase(referenceDatabase)
.pathToExpectedDiffFolder("src/main/resources${baseDiffPath}" + "expectedDiffChangelogs/")
.build())
}
new DatabaseConnectionUtil().initializeDatabasesConnection(databasesToConnect)
Expand All @@ -117,6 +122,9 @@ class DiffCommandTestHelper {
map.put("createView", new ArrayList<>(List.of("dropView", "drop view")))
map.put("createIndex", new ArrayList<>(List.of("dropIndex", "drop index")))
map.put("createSequence", new ArrayList<>(List.of("dropSequence", "drop sequence")))
map.put("createFunction", new ArrayList<>(List.of("dropFunction", "drop function")))
map.put("createTrigger", new ArrayList<>(List.of("dropTrigger", "drop trigger")))
map.put("createProcedure", new ArrayList<>(List.of("dropProcedure", "drop procedure")))

if(dbName.equals("mariadb") || dbName.equals("mysql")) {
map.put("addForeignKey", new ArrayList<>(List.of("dropForeignKey", "drop foreign key")))
Expand Down Expand Up @@ -156,6 +164,13 @@ class DiffCommandTestHelper {
.replaceAll(replacementRegexpWS, "")
}

static void clearFolder(String pathToObjectDir) {
Path path = Paths.get(pathToObjectDir)
if (path.toFile().isDirectory()) {
FileUtils.forceDelete(new File(pathToObjectDir))
}
}

@Builder
static class TestInput {
String pathToExpectedDiffFile
Expand All @@ -165,6 +180,7 @@ class DiffCommandTestHelper {
String pathToGeneratedSqlDiffChangelogFile
String pathToGeneratedYmlDiffChangelogFile
String pathToGeneratedJsonDiffChangelogFile
String pathToExpectedDiffFolder
DatabaseUnderTest referenceDatabase
DatabaseUnderTest targetDatabase
}
Expand Down
65 changes: 65 additions & 0 deletions src/main/groovy/liquibase/harness/diff/DiffTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package liquibase.harness.diff

import com.datical.liquibase.ext.config.LiquibaseProConfiguration
import liquibase.database.jvm.JdbcConnection
import liquibase.harness.config.DatabaseUnderTest
import liquibase.harness.config.TestConfig
import liquibase.harness.util.rollback.RollbackStrategy
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

import static liquibase.harness.diff.DiffCommandTestHelper.*
import static liquibase.harness.util.TestUtils.*
import static liquibase.harness.util.FileUtils.*

class DiffTests extends Specification {
@Shared
RollbackStrategy strategy
@Shared
List<DatabaseUnderTest> databases

def setupSpec() {
databases = TestConfig.instance.getFilteredDatabasesUnderTest()
strategy = chooseRollbackStrategy()
strategy.prepareForRollback(databases)
}

@Unroll
def "apply diff test against #testInput.referenceDatabase.name #testInput.referenceDatabase.version to targetDatabase #testInput.targetDatabase.name #testInput.targetDatabase.version"() {
given: "read input data for diff test"
Map<String, Object> argsMap = new HashMap()
argsMap.put("url", testInput.targetDatabase.url)
argsMap.put("username", testInput.targetDatabase.username)
argsMap.put("password", testInput.targetDatabase.password)
argsMap.put("referenceUrl", testInput.referenceDatabase.url)
argsMap.put("referenceUsername", testInput.referenceDatabase.username)
argsMap.put("referencePassword", testInput.referenceDatabase.password)
argsMap.put("changelogFile", testInput.pathToChangelogFile)

and: "check databases are online"
assert testInput.targetDatabase.database.getConnection() instanceof JdbcConnection: "Target database " +
"${testInput.targetDatabase.name}${testInput.targetDatabase.version} is offline!"
assert testInput.referenceDatabase.database.getConnection() instanceof JdbcConnection: "Reference database " +
"${testInput.referenceDatabase.name}${testInput.referenceDatabase.version} is offline!"

when: "execute update and diff commands"

executeCommandScope("update", argsMap)
argsMap.put("excludeObjects", "(?i)posts, (?i)authors, (?i)databasechangelog, (?i)databasechangeloglock")
String generatedDiffContent = removeDatabaseInfoFromDiff(executeCommandScope("diff", argsMap).toString())

then: "validate generated diff"
String expectedDiffContent = removeDatabaseInfoFromDiff(getResourceContent(testInput.pathToExpectedDiffFile))
assert expectedDiffContent == generatedDiffContent

cleanup: "rollback changes"
strategy.performRollback(argsMap)

where:
testInput << buildTestInput()
}
def cleanupSpec() {
strategy.cleanupDatabase(databases)
}
}

This file was deleted.

Loading

0 comments on commit 7d81e3b

Please sign in to comment.