diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/BaseDolphinSchedulerManagerIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DatabaseContainerProvider.java similarity index 65% rename from dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/BaseDolphinSchedulerManagerIT.java rename to dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DatabaseContainerProvider.java index 11ec784863..0e93140acb 100644 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/BaseDolphinSchedulerManagerIT.java +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DatabaseContainerProvider.java @@ -15,16 +15,14 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.tools.datasource; +package org.apache.dolphinscheduler.tools.datasource.jupiter; -import org.apache.dolphinscheduler.dao.DaoConfiguration; +import org.testcontainers.containers.GenericContainer; -import org.springframework.boot.test.context.SpringBootTest; -import org.testcontainers.containers.Network; +public interface DatabaseContainerProvider { -@SpringBootTest(classes = {UpgradeDolphinScheduler.class, DaoConfiguration.class}) -public abstract class BaseDolphinSchedulerManagerIT { + GenericContainer getContainer(DolphinSchedulerDatabaseContainer dataSourceContainer); - protected static final Network NETWORK = Network.newNetwork(); + String getType(); } diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DolphinSchedulerDatabaseContainer.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DolphinSchedulerDatabaseContainer.java new file mode 100644 index 0000000000..d315042186 --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DolphinSchedulerDatabaseContainer.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.jupiter; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; + +@Inherited +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@TestMethodOrder(OrderAnnotation.class) +@ExtendWith(DolphinSchedulerDatabaseContainerExtension.class) +public @interface DolphinSchedulerDatabaseContainer { + + String imageName(); + +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DolphinSchedulerDatabaseContainerExtension.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DolphinSchedulerDatabaseContainerExtension.java new file mode 100644 index 0000000000..4b463e1ef9 --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/jupiter/DolphinSchedulerDatabaseContainerExtension.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.jupiter; + +import java.util.HashMap; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.stream.Stream; + +import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.lifecycle.Startables; +import org.testcontainers.utility.DockerImageName; + +@Slf4j +public class DolphinSchedulerDatabaseContainerExtension implements BeforeAllCallback, AfterAllCallback { + + private static GenericContainer databaseContainer; + + @Override + public void beforeAll(ExtensionContext context) { + databaseContainer = getDataSourceContainer(context); + log.info("Create {} successfully.", databaseContainer.getDockerImageName()); + databaseContainer.start(); + + log.info("Starting {}...", databaseContainer.getDockerImageName()); + Startables.deepStart(Stream.of(databaseContainer)).join(); + log.info("{} started", databaseContainer.getDockerImageName()); + + } + + private GenericContainer getDataSourceContainer(ExtensionContext context) { + Class requiredTestClass = context.getRequiredTestClass(); + DolphinSchedulerDatabaseContainer annotation = + requiredTestClass.getAnnotation(DolphinSchedulerDatabaseContainer.class); + if (annotation == null) { + throw new IllegalArgumentException("@DolphinSchedulerDataSourceContainer annotation not found"); + } + Map dataSourceContainerProviderMap = new HashMap<>(); + ServiceLoader.load(DatabaseContainerProvider.class) + .forEach(databaseContainerProvider -> dataSourceContainerProviderMap + .put(databaseContainerProvider.getType(), databaseContainerProvider)); + + DockerImageName dockerImageName = DockerImageName.parse(annotation.imageName()); + + if (!dataSourceContainerProviderMap.containsKey(dockerImageName.getRepository())) { + throw new IllegalArgumentException( + "DataSourceContainerProvider not found for type: " + annotation.imageName()); + } + DatabaseContainerProvider databaseContainerProvider = + dataSourceContainerProviderMap.get(dockerImageName.getRepository()); + return databaseContainerProvider.getContainer(annotation); + } + + @Override + public void afterAll(ExtensionContext context) { + if (databaseContainer != null) { + databaseContainer.stop(); + } + } +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/BaseDolphinSchedulerDatabaseWithMysqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/BaseDolphinSchedulerDatabaseWithMysqlIT.java deleted file mode 100644 index 13a288562e..0000000000 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/BaseDolphinSchedulerDatabaseWithMysqlIT.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.mysql; - -import org.apache.dolphinscheduler.tools.datasource.BaseDolphinSchedulerManagerIT; -import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; - -import java.util.stream.Stream; - -import javax.sql.DataSource; - -import lombok.extern.slf4j.Slf4j; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ActiveProfiles; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.MySQLContainer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.lifecycle.Startables; -import org.testcontainers.utility.DockerImageName; - -import com.google.common.collect.Lists; - -// todo: use TestTemplate to test multiple mysql version -@Slf4j -@ActiveProfiles("mysql") -public class BaseDolphinSchedulerDatabaseWithMysqlIT extends BaseDolphinSchedulerManagerIT { - - @Autowired - protected DolphinSchedulerManager dolphinSchedulerManager; - - @Autowired - protected DataSource dataSource; - - protected static GenericContainer databaseContainer; - - @BeforeAll - public static void initializeContainer() { - // todo: test with multiple mysql version - databaseContainer = new MySQLContainer(DockerImageName.parse("mysql:8.0")) - .withUsername("root") - .withPassword("root") - .withDatabaseName("dolphinscheduler") - .withNetwork(NETWORK) - .withExposedPorts(3306) - .waitingFor(Wait.forHealthcheck()); - databaseContainer.setPortBindings(Lists.newArrayList("3306:3306")); - - log.info("Create MySQLContainer successfully."); - databaseContainer.start(); - - log.info("Starting MySQLContainer..."); - Startables.deepStart(Stream.of(databaseContainer)).join(); - log.info("MySQLContainer started"); - } - - @AfterAll - public static void closeContainer() { - if (databaseContainer != null) { - databaseContainer.stop(); - } - } - -} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerMysqlProfile.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerMysqlProfile.java new file mode 100644 index 0000000000..f32f4d80d4 --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerMysqlProfile.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.mysql; + +import org.apache.dolphinscheduler.dao.DaoConfiguration; +import org.apache.dolphinscheduler.tools.datasource.UpgradeDolphinScheduler; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +@Inherited +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@ActiveProfiles("mysql") +@SpringBootTest(classes = {UpgradeDolphinScheduler.class, DaoConfiguration.class}) +public @interface DolphinSchedulerMysqlProfile { + +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/MysqlDatabaseContainerProvider.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/MysqlDatabaseContainerProvider.java new file mode 100644 index 0000000000..80d0b8978d --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/MysqlDatabaseContainerProvider.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.mysql; + +import org.apache.dolphinscheduler.tools.datasource.jupiter.DatabaseContainerProvider; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.MySQLContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; + +import com.google.auto.service.AutoService; +import com.google.common.collect.Lists; + +@AutoService(DatabaseContainerProvider.class) +public class MysqlDatabaseContainerProvider implements DatabaseContainerProvider { + + private static final Network NETWORK = Network.newNetwork(); + + @Override + public GenericContainer getContainer(DolphinSchedulerDatabaseContainer dataSourceContainer) { + GenericContainer mysqlContainer = new MySQLContainer(DockerImageName.parse(dataSourceContainer.imageName())) + .withUsername("root") + .withPassword("root") + .withDatabaseName("dolphinscheduler") + .withNetwork(NETWORK) + .withExposedPorts(3306) + .waitingFor(Wait.forHealthcheck()); + mysqlContainer.setPortBindings(Lists.newArrayList("3306:3306")); + return mysqlContainer; + } + + @Override + public String getType() { + return "mysql"; + } + +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v5/InitializeWithMysqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v5/InitializeWithMysqlIT.java new file mode 100644 index 0000000000..e760c55ec8 --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v5/InitializeWithMysqlIT.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.mysql.v5; + +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.mysql.DolphinSchedulerMysqlProfile; + +import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.springframework.beans.factory.annotation.Autowired; + +@Slf4j +@EnabledOnOs(OS.LINUX) +@DolphinSchedulerMysqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "mysql:5.7") +class InitializeWithMysqlIT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; + + @Test + @DisplayName("Test Initialize DolphinScheduler database in MySQL") + void testInitializeWithMysqlProfile() { + Assertions.assertDoesNotThrow(() -> dolphinSchedulerManager.initDolphinScheduler()); + // todo: Assert table count + + } + +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v5/UpgradeWithMysqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v5/UpgradeWithMysqlIT.java new file mode 100644 index 0000000000..d3c67bfaf1 --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v5/UpgradeWithMysqlIT.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.mysql.v5; + +import org.apache.dolphinscheduler.common.sql.SqlScriptRunner; +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.mysql.DolphinSchedulerMysqlProfile; + +import java.io.IOException; +import java.sql.SQLException; + +import javax.sql.DataSource; + +import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.springframework.beans.factory.annotation.Autowired; + +@Slf4j +@EnabledOnOs(OS.LINUX) +@DolphinSchedulerMysqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "mysql:5.7") +class UpgradeWithMysqlIT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; + + @Autowired + private DataSource dataSource; + + @Test + @DisplayName("Test Upgrade DolphinScheduler database in MySQL") + void testUpgradeWithMysqlProfile() throws SQLException, IOException { + + // initialize the 3.0.0 schema + SqlScriptRunner sqlScriptRunner = new SqlScriptRunner(dataSource, "3.0.0_schema/mysql_3.0.0.sql"); + sqlScriptRunner.execute(); + log.info("Initialize the 3.0.0 schema successfully."); + + Assertions.assertDoesNotThrow(() -> dolphinSchedulerManager.upgradeDolphinScheduler()); + // todo: Assert table count + + } + +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerDatabaseInitializeWithMysqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v8/InitializeWithMysqlIT.java similarity index 67% rename from dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerDatabaseInitializeWithMysqlIT.java rename to dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v8/InitializeWithMysqlIT.java index 499a687243..5553384a92 100644 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerDatabaseInitializeWithMysqlIT.java +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v8/InitializeWithMysqlIT.java @@ -15,18 +15,26 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.tools.datasource.mysql; +package org.apache.dolphinscheduler.tools.datasource.mysql.v8; + +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.mysql.DolphinSchedulerMysqlProfile; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.test.context.ActiveProfiles; +import org.springframework.beans.factory.annotation.Autowired; @Slf4j -@ActiveProfiles("mysql") -class DolphinSchedulerDatabaseInitializeWithMysqlIT extends BaseDolphinSchedulerDatabaseWithMysqlIT { +@DolphinSchedulerMysqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "mysql:8.0") +class InitializeWithMysqlIT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; @Test @DisplayName("Test Initialize DolphinScheduler database in MySQL") diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerDatabaseUpgradeWithMysqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v8/UpgradeWithMysqlIT.java similarity index 71% rename from dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerDatabaseUpgradeWithMysqlIT.java rename to dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v8/UpgradeWithMysqlIT.java index d80d9a94be..a81eae5ea6 100644 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/DolphinSchedulerDatabaseUpgradeWithMysqlIT.java +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/mysql/v8/UpgradeWithMysqlIT.java @@ -15,21 +15,35 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.tools.datasource.mysql; +package org.apache.dolphinscheduler.tools.datasource.mysql.v8; import org.apache.dolphinscheduler.common.sql.SqlScriptRunner; +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.mysql.DolphinSchedulerMysqlProfile; import java.io.IOException; import java.sql.SQLException; +import javax.sql.DataSource; + import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; @Slf4j -class DolphinSchedulerDatabaseUpgradeWithMysqlIT extends BaseDolphinSchedulerDatabaseWithMysqlIT { +@DolphinSchedulerMysqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "mysql:8.0") +class UpgradeWithMysqlIT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; + + @Autowired + private DataSource dataSource; @Test @DisplayName("Test Upgrade DolphinScheduler database in MySQL") diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/BaseDolphinSchedulerManagerWithPostgresqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/BaseDolphinSchedulerManagerWithPostgresqlIT.java deleted file mode 100644 index 68fbcd1e5e..0000000000 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/BaseDolphinSchedulerManagerWithPostgresqlIT.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.postgresql; - -import org.apache.dolphinscheduler.tools.datasource.BaseDolphinSchedulerManagerIT; -import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; - -import java.util.stream.Stream; - -import javax.sql.DataSource; - -import lombok.extern.slf4j.Slf4j; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ActiveProfiles; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.lifecycle.Startables; -import org.testcontainers.utility.DockerImageName; - -import com.google.common.collect.Lists; - -// todo: use TestTemplate to test multiple PG version -@Slf4j -@ActiveProfiles("postgresql") -public class BaseDolphinSchedulerManagerWithPostgresqlIT extends BaseDolphinSchedulerManagerIT { - - @Autowired - protected DolphinSchedulerManager dolphinSchedulerManager; - - @Autowired - protected DataSource dataSource; - - protected static GenericContainer databaseContainer; - - @BeforeAll - public static void initializeContainer() { - // todo: test with multiple pg version - databaseContainer = new PostgreSQLContainer(DockerImageName.parse("postgres:11.1")) - .withUsername("root") - .withPassword("root") - .withDatabaseName("dolphinscheduler") - .withNetwork(NETWORK) - .withExposedPorts(5432); - databaseContainer.setPortBindings(Lists.newArrayList("5432:5432")); - - log.info("Create PostgreSQLContainer successfully."); - databaseContainer.start(); - - log.info("Starting PostgreSQLContainer..."); - Startables.deepStart(Stream.of(databaseContainer)).join(); - log.info("PostgreSQLContainer started"); - } - - @AfterAll - public static void closeContainer() { - if (databaseContainer != null) { - databaseContainer.stop(); - } - } -} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerPostgresqlProfile.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerPostgresqlProfile.java new file mode 100644 index 0000000000..1a5a07ad93 --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerPostgresqlProfile.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.postgresql; + +import org.apache.dolphinscheduler.dao.DaoConfiguration; +import org.apache.dolphinscheduler.tools.datasource.UpgradeDolphinScheduler; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +@Inherited +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@ActiveProfiles("postgresql") +@SpringBootTest(classes = {UpgradeDolphinScheduler.class, DaoConfiguration.class}) +public @interface DolphinSchedulerPostgresqlProfile { + +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/PostgresqlDatabaseContainerProvider.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/PostgresqlDatabaseContainerProvider.java new file mode 100644 index 0000000000..91d4499cdb --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/PostgresqlDatabaseContainerProvider.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.postgresql; + +import org.apache.dolphinscheduler.tools.datasource.jupiter.DatabaseContainerProvider; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; + +import lombok.extern.slf4j.Slf4j; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +import com.google.auto.service.AutoService; +import com.google.common.collect.Lists; + +@Slf4j +@AutoService(DatabaseContainerProvider.class) +public class PostgresqlDatabaseContainerProvider implements DatabaseContainerProvider { + + private static final Network NETWORK = Network.newNetwork(); + + @SuppressWarnings("unchecked") + @Override + public GenericContainer getContainer(DolphinSchedulerDatabaseContainer dataSourceContainer) { + // todo: test with multiple pg version + GenericContainer postgresqlContainer = new PostgreSQLContainer(DockerImageName.parse("postgres:11.1")) + .withUsername("root") + .withPassword("root") + .withDatabaseName("dolphinscheduler") + .withNetwork(NETWORK) + .withExposedPorts(5432); + postgresqlContainer.setPortBindings(Lists.newArrayList("5432:5432")); + + return postgresqlContainer; + } + + @Override + public String getType() { + return "postgres"; + } +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerInitializeWithPostgresqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v11/InitializeWithPostgresqlIT.java similarity index 67% rename from dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerInitializeWithPostgresqlIT.java rename to dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v11/InitializeWithPostgresqlIT.java index 0ced99c433..a707b23174 100644 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerInitializeWithPostgresqlIT.java +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v11/InitializeWithPostgresqlIT.java @@ -15,16 +15,26 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.tools.datasource.postgresql; +package org.apache.dolphinscheduler.tools.datasource.postgresql.v11; + +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.postgresql.DolphinSchedulerPostgresqlProfile; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; @Slf4j -class DolphinSchedulerInitializeWithPostgresqlIT extends BaseDolphinSchedulerManagerWithPostgresqlIT { +@DolphinSchedulerPostgresqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "postgres:11.1") +class InitializeWithPostgresqlIT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; @Test @DisplayName("Test initDolphinScheduler database in PostgreSQL") diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerDatabaseUpgradeWithPostgresqlIT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v11/UpgradeWithPostgresqlIT.java similarity index 69% rename from dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerDatabaseUpgradeWithPostgresqlIT.java rename to dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v11/UpgradeWithPostgresqlIT.java index 5389367b5b..6c848e0e90 100644 --- a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/DolphinSchedulerDatabaseUpgradeWithPostgresqlIT.java +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v11/UpgradeWithPostgresqlIT.java @@ -15,9 +15,14 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.tools.datasource.postgresql; +package org.apache.dolphinscheduler.tools.datasource.postgresql.v11; import org.apache.dolphinscheduler.common.sql.SqlScriptRunner; +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.postgresql.DolphinSchedulerPostgresqlProfile; + +import javax.sql.DataSource; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -25,9 +30,17 @@ import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; @Slf4j -class DolphinSchedulerDatabaseUpgradeWithPostgresqlIT extends BaseDolphinSchedulerManagerWithPostgresqlIT { +@DolphinSchedulerPostgresqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "postgres:11.1") +class UpgradeWithPostgresqlIT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; + @Autowired + private DataSource dataSource; @Test @SneakyThrows diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v16/InitializeWithPostgresql16IT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v16/InitializeWithPostgresql16IT.java new file mode 100644 index 0000000000..7989b97f2a --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v16/InitializeWithPostgresql16IT.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.postgresql.v16; + +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.postgresql.DolphinSchedulerPostgresqlProfile; + +import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +@Slf4j +@DolphinSchedulerPostgresqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "postgres:16.0") +class InitializeWithPostgresql16IT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; + + @Test + @DisplayName("Test initDolphinScheduler database in PostgreSQL") + void testInitializeWithPostgreSQLProfile() { + Assertions.assertDoesNotThrow(() -> { + dolphinSchedulerManager.initDolphinScheduler(); + }); + // todo: Assert table count + } +} diff --git a/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v16/UpgradeWithPostgresql16IT.java b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v16/UpgradeWithPostgresql16IT.java new file mode 100644 index 0000000000..176603d85f --- /dev/null +++ b/dolphinscheduler-tools/src/test/java/org/apache/dolphinscheduler/tools/datasource/postgresql/v16/UpgradeWithPostgresql16IT.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dolphinscheduler.tools.datasource.postgresql.v16; + +import org.apache.dolphinscheduler.common.sql.SqlScriptRunner; +import org.apache.dolphinscheduler.tools.datasource.DolphinSchedulerManager; +import org.apache.dolphinscheduler.tools.datasource.jupiter.DolphinSchedulerDatabaseContainer; +import org.apache.dolphinscheduler.tools.datasource.postgresql.DolphinSchedulerPostgresqlProfile; + +import javax.sql.DataSource; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +@Slf4j +@DolphinSchedulerPostgresqlProfile +@DolphinSchedulerDatabaseContainer(imageName = "postgres:16.0") +class UpgradeWithPostgresql16IT { + + @Autowired + private DolphinSchedulerManager dolphinSchedulerManager; + @Autowired + private DataSource dataSource; + + @Test + @SneakyThrows + @DisplayName("Test Upgrade DolphinScheduler database in PostgreSQL") + void testUpgradeWithPostgreSQLProfile() { + // initialize the 3.0.0 schema + SqlScriptRunner sqlScriptRunner = new SqlScriptRunner(dataSource, "3.0.0_schema/postgresql_3.0.0.sql"); + sqlScriptRunner.execute(); + log.info("Initialize the 3.0.0 schema successfully."); + + Assertions.assertDoesNotThrow(() -> dolphinSchedulerManager.upgradeDolphinScheduler()); + // todo: Assert table count + } +}