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

SDK-350 - Speed up execution of integration tests by limiting the num… #305

Merged
merged 1 commit into from
Nov 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
Expand All @@ -54,17 +53,12 @@ public abstract class AbstractSdkIntegrationTest {
/**
* contains name of directory in project's target dir, where integration tests are conducted
*/
static final String TEST_DIRECTORY = "/integration-test";
static final String TEST_DIRECTORY = "integration-test";
static final String MOJO_OPTION_TMPL = "-D%s=\"%s\"";
protected static final String BATCH_ANSWERS = "batchAnswers";

protected final ArrayDeque<String> batchAnswers = new ArrayDeque<>();

/**
* contains files in test directory which are not created during tests and will not be cleaned up
*/
List<File> testFilesToPersist;

/**
* maven utility for integration tests
*/
Expand All @@ -77,38 +71,61 @@ public abstract class AbstractSdkIntegrationTest {

Path testDirectoryPath;

public static String resolveSdkArtifact() throws MojoExecutionException {
InputStream sdkPom = AbstractSdkIntegrationTest.class.getClassLoader().getResourceAsStream("sdk.properties");
File distroFile;

public String resolveSdkArtifact() throws MojoExecutionException {
Properties sdk = new Properties();
try {
try (InputStream sdkPom = getClass().getClassLoader().getResourceAsStream("sdk.properties")) {
sdk.load(sdkPom);
} catch (IOException e) {
}
catch (IOException e) {
throw new MojoExecutionException(e.getMessage());
} finally {
IOUtils.closeQuietly(sdkPom);
}
return sdk.get("groupId")+":"+sdk.get("artifactId")+":"+sdk.get("version");
}

void includeTestResource(String fileName) throws Exception {
File source = getTestFile(TEST_DIRECTORY, fileName);
File target = new File(testDirectory, fileName);
if (source.isDirectory()) {
FileUtils.copyDirectory(source, testDirectory);
}
else {
FileUtils.copyFile(source, target);
}
}

void includeDistroPropertiesFile(String fileName) throws Exception {
File source = getTestFile(TEST_DIRECTORY, fileName);
File target = new File(testDirectory, DistroProperties.DISTRO_FILE_NAME);
FileUtils.copyFile(source, target);
}

void addTestResources() throws Exception {
includeTestResource("pom.xml");
includeDistroPropertiesFile(DistroProperties.DISTRO_FILE_NAME);
}

@Before
public void setup() throws Exception{
testDirectory = ResourceExtractor.simpleExtractResources(getClass(), TEST_DIRECTORY);
testDirectoryPath = testDirectory.toPath();
public void setup() throws Exception {
String tempDirPath = System.getProperty("maven.test.tmpdir", System.getProperty("java.io.tmpdir"));
String executionDirName = "openmrs-sdk-" + getClass().getSimpleName() + "-" + UUID.randomUUID();
testDirectoryPath = Paths.get(tempDirPath, executionDirName);
testDirectory = testDirectoryPath.toFile();
if (!testDirectory.mkdirs()) {
throw new RuntimeException("Unable to create test directory: " + testDirectory);
}
ResourceExtractor.extractResourcePath(getClass(), "/" + TEST_DIRECTORY, testDirectory, true);
addTestResources();
verifier = new Verifier(testDirectory.getAbsolutePath());
verifier.setAutoclean(false);

testFilesToPersist = new ArrayList<>(Arrays.asList(testDirectory.listFiles()));

addTaskParam("openMRSPath",testDirectory.getAbsolutePath());
addTaskParam("openMRSPath", testDirectory.getAbsolutePath());
distroFile = new File(testDirectory, DistroProperties.DISTRO_FILE_NAME);
}

@After
public void teardown() throws Exception {
for(File file : testDirectory.listFiles()){
if(!testFilesToPersist.contains(file)){
FileUtils.deleteQuietly(file);
}
}
public void teardown() {
FileUtils.deleteQuietly(testDirectory);
cleanAnswers();
}

Expand Down Expand Up @@ -138,11 +155,6 @@ private void cleanAnswers() {
batchAnswers.clear();
}

public static void deleteTestServer(String serverId) throws Exception{
File testDir = ResourceExtractor.simpleExtractResources(AbstractSdkIntegrationTest.class, TEST_DIRECTORY);
FileUtils.deleteDirectory(new File(testDir, serverId));
}

/**
* Adds CLI parameter to executed Mojo in format -D'param'='value'
* @param param name of parameter, eg. "serverId"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.openmrs.maven.plugins;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.utility.DistroHelper;
Expand All @@ -15,20 +12,9 @@

public class AddDependencyTest extends AbstractSdkIntegrationTest {

private String distroFile;

private DistroProperties originalProperties;


@Before
public void setUp() {
distroFile = testDirectory + File.separator + "openmrs-distro.properties";
originalProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
}

@Test
public void shouldAddOmodDependency() throws Exception {
addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("type", "OMOD");
addTaskParam("groupId", "org.openmrs.module");
addTaskParam("artifactId", "webservices.rest");
Expand All @@ -37,23 +23,23 @@ public void shouldAddOmodDependency() throws Exception {
executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);
assertTrue(distroProperties.getAllKeys().contains("omod.webservices.rest"));
assertEquals(distroProperties.getParam("omod.webservices.rest"), "2.30.0");
}

@Test
public void shouldAddSpaDependency() throws Exception {
addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("type", "SPA");
addTaskParam("moduleName", "@openmrs/esm-system-admin-app");
addTaskParam("version", "4.0.3");

executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("spa.frontendModules.@openmrs/esm-system-admin-app"));
Expand All @@ -62,15 +48,15 @@ public void shouldAddSpaDependency() throws Exception {

@Test
public void shouldAddOwaDependency() throws Exception {
addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("type", "OWA");
addTaskParam("artifactId", "sysadmin");
addTaskParam("version", "1.2.0");

executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("owa.sysadmin"));
Expand All @@ -79,14 +65,14 @@ public void shouldAddOwaDependency() throws Exception {

@Test
public void shouldAddWarDependency() throws Exception {
addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("type", "WAR");
addTaskParam("version", "2.6.1");

executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("war.openmrs"));
Expand All @@ -95,26 +81,26 @@ public void shouldAddWarDependency() throws Exception {

@Test
public void shouldAddCustomDependencyIfTypeIsNotSpecified() throws Exception {
addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("property", "custom.property");
addTaskParam("version", "1.2.3");

executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertTrue(distroProperties.getAllKeys().contains("custom.property"));
assertEquals(distroProperties.getParam("custom.property"), "1.2.3");

}

@Test
public void shouldOverrideIfPropertyAlreadyExists() throws Exception {
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);
assertTrue(distroProperties.getAllKeys().contains("omod.uiframework"));

addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("type", "OMOD");
addTaskParam("groupId", "org.openmrs.module");
addTaskParam("artifactId", "uiframework");
Expand All @@ -123,15 +109,10 @@ public void shouldOverrideIfPropertyAlreadyExists() throws Exception {
executeTask("add");
assertSuccess();

distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("omod.uiframework"));
assertEquals(distroProperties.getParam("omod.uiframework"), "2.30.0");
}

@After
public void reset() throws MojoExecutionException {
originalProperties.saveTo(new File(distroFile).getParentFile());
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
package org.openmrs.maven.plugins;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.utility.DistroHelper;

import java.io.File;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class AddExclusionIntegrationTest extends AbstractSdkIntegrationTest {

private String distroFile;

private DistroProperties originalProperties;

@Before
public void setUp() {
distroFile = testDirectory + File.separator + "openmrs-distro.properties";
originalProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
}

@Test
public void shouldAddExclusion() throws Exception {
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertNotNull(distroProperties);
assertFalse(distroProperties.getExclusions().contains("omod.uicommons"));

addTaskParam("distro", distroFile);
addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("property", "omod.uicommons");
executeTask("exclusion");

assertSuccess();
distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertTrue(distroProperties.getExclusions().contains("omod.uicommons"));
}


@After
public void reset() throws MojoExecutionException {
originalProperties.saveTo(new File(distroFile).getParentFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public void testBuildDistroFromDistroFile() throws Exception {
assertFilePresent("target/web/startup.sh");
assertFilePresent("target/web/wait-for-it.sh");
assertFilePresent("target/web/modules");
assertFilePresent("target/web/frontend/index.html");
assertFilePresent("target/web/owa");
assertFilePresent("target/web/openmrs.war");
assertFilePresent("target/web/openmrs-distro.properties");
Expand Down Expand Up @@ -74,4 +73,43 @@ public void testBundledBuildDistro() throws Exception {

assertSuccess();
}

@Test
public void testBuildDistroWithSpaBuildConfig() throws Exception {
includeDistroPropertiesFile("openmrs-distro-spa-build.properties");
addTaskParam("dir", "target");
addTaskParam("ignorePeerDependencies", "false");
executeTask("build-distro");

assertFilePresent("target/docker-compose.yml");
assertFilePresent("target/docker-compose.override.yml");
assertFilePresent("target/docker-compose.prod.yml");
assertFilePresent("target/.env");
assertFilePresent("target/web/Dockerfile");
assertFilePresent("target/web/setenv.sh");
assertFilePresent("target/web/startup.sh");
assertFilePresent("target/web/wait-for-it.sh");
assertFilePresent("target/web/modules");
assertFilePresent("target/web/frontend/index.html");
assertFilePresent("target/web/owa");
assertFilePresent("target/web/openmrs.war");
assertFilePresent("target/web/openmrs-distro.properties");
assertSuccess();
}

@Test
public void testBuildDistroWithSpaArtifacts() throws Exception {
includeDistroPropertiesFile("openmrs-distro-spa-artifacts.properties");
addTaskParam("dir", "target");
addTaskParam("ignorePeerDependencies", "false");
executeTask("build-distro");

assertFilePresent("target/docker-compose.yml");
assertFilePresent("target/web/openmrs_modules");
assertFilePresent("target/web/openmrs_spa/index.html");
assertFilePresent("target/web/openmrs_owas");
assertFilePresent("target/web/openmrs_core/openmrs.war");
assertFilePresent("target/web/openmrs-distro.properties");
assertSuccess();
}
}
Loading
Loading