Skip to content

Commit

Permalink
Add IT for mysql5/postgresql16 initialize/upgrade (apache#15063)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanwenjun authored and xdu-chenrj committed Oct 30, 2023
1 parent f8305f8 commit f212b8b
Show file tree
Hide file tree
Showing 17 changed files with 588 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
@@ -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<String, DatabaseContainerProvider> 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();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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

}

}
Loading

0 comments on commit f212b8b

Please sign in to comment.