Merge pull request #531 from mposolda/master
Fix ExportImportTest on som envs like windows
This commit is contained in:
commit
60a1fc3020
3 changed files with 116 additions and 99 deletions
|
@ -5,30 +5,31 @@ package org.keycloak.exportimport;
|
|||
*/
|
||||
public class ExportImportConfig {
|
||||
|
||||
public static final String ACTION = "keycloak.migration.action";
|
||||
public static final String PREFIX = "keycloak.migration.";
|
||||
public static final String ACTION = PREFIX + "action";
|
||||
public static final String ACTION_EXPORT = "export";
|
||||
public static final String ACTION_IMPORT = "import";
|
||||
|
||||
public static final String PROVIDER = "keycloak.migration.provider";
|
||||
public static final String PROVIDER = PREFIX + "provider";
|
||||
public static final String PROVIDER_DEFAULT = "zip";
|
||||
|
||||
// Name of the realm to export. If null, then full export will be triggered
|
||||
public static final String REALM_NAME = "keycloak.migration.realmName";
|
||||
public static final String REALM_NAME = PREFIX + "realmName";
|
||||
|
||||
// used for "dir" provider
|
||||
public static final String DIR = "keycloak.migration.dir";
|
||||
public static final String DIR = PREFIX + "dir";
|
||||
// used for "zip" provider
|
||||
public static final String ZIP_FILE = "keycloak.migration.zipFile";
|
||||
public static final String ZIP_PASSWORD = "keycloak.migration.zipPassword";
|
||||
public static final String ZIP_FILE = PREFIX + "zipFile";
|
||||
public static final String ZIP_PASSWORD = PREFIX + "zipPassword";
|
||||
// used for "singleFile" provider
|
||||
public static final String FILE = "keycloak.migration.file";
|
||||
public static final String FILE = PREFIX + "file";
|
||||
|
||||
// Number of users per file used in "dir" and "zip" providers. -1 means adding users to same file with realm. 0 means adding to separate file with unlimited page number
|
||||
public static final String USERS_PER_FILE = "keycloak.migration.usersPerFile";
|
||||
public static final String USERS_PER_FILE = PREFIX + "usersPerFile";
|
||||
public static final Integer DEFAULT_USERS_PER_FILE = 5000;
|
||||
|
||||
// Strategy used during import data
|
||||
public static final String STRATEGY = "keycloak.migration.strategy";
|
||||
public static final String STRATEGY = PREFIX + "strategy";
|
||||
public static final Strategy DEFAULT_STRATEGY = Strategy.OVERWRITE_EXISTING;
|
||||
|
||||
public static String getAction() {
|
||||
|
|
|
@ -1,10 +1,38 @@
|
|||
package org.keycloak.testsuite.exportimport;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExternalResource;
|
||||
import org.junit.rules.RuleChain;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.keycloak.Config;
|
||||
import org.keycloak.exportimport.ExportImportConfig;
|
||||
import org.keycloak.exportimport.dir.DirExportProvider;
|
||||
import org.keycloak.exportimport.dir.DirExportProviderFactory;
|
||||
import org.keycloak.exportimport.singlefile.SingleFileExportProviderFactory;
|
||||
import org.keycloak.exportimport.zip.ZipExportProviderFactory;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RealmProvider;
|
||||
import org.keycloak.models.UserCredentialModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
import org.keycloak.models.UserProvider;
|
||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||
import org.keycloak.services.managers.RealmManager;
|
||||
import org.keycloak.testsuite.rule.KeycloakRule;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
public class ExportImportTest {
|
||||
/*
|
||||
|
||||
|
||||
// We want data to be persisted among server restarts
|
||||
private static ExternalResource hibernateSetupRule = new ExternalResource() {
|
||||
|
@ -76,50 +104,21 @@ public class ExportImportTest {
|
|||
}) {
|
||||
@Override
|
||||
protected void after() {
|
||||
{
|
||||
KeycloakSession session = server.getSessionFactory().create();
|
||||
session.getTransaction().begin();
|
||||
|
||||
try {
|
||||
RealmManager manager = new RealmManager(session);
|
||||
|
||||
RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
|
||||
RealmModel appRealm = manager.getRealm("test");
|
||||
boolean removed = session.users().removeUser(appRealm, "user1");
|
||||
removed = session.users().removeUser(appRealm, "user2");
|
||||
removed = session.users().removeUser(appRealm, "user3");
|
||||
removed = session.users().removeUser(adminstrationRealm, "admin2");
|
||||
|
||||
session.getTransaction().commit();
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
{
|
||||
KeycloakSession session = server.getSessionFactory().create();
|
||||
session.getTransaction().begin();
|
||||
|
||||
try {
|
||||
RealmManager manager = new RealmManager(session);
|
||||
|
||||
RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
|
||||
RealmModel appRealm = manager.getRealm("test");
|
||||
UserModel user1 = session.users().getUserByUsername("user1", appRealm);
|
||||
UserModel user2= session.users().getUserByUsername("user2", appRealm);
|
||||
UserModel user3 = session.users().getUserByUsername("user3", appRealm);
|
||||
UserModel admin2 = session.users().getUserByUsername("admin2", adminstrationRealm);
|
||||
Assert.assertNull(user1);
|
||||
Assert.assertNull(user2);
|
||||
Assert.assertNull(user3);
|
||||
Assert.assertNull(admin2);
|
||||
|
||||
session.getTransaction().commit();
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
super.after();
|
||||
|
||||
// Clear export/import properties after test
|
||||
Properties systemProps = System.getProperties();
|
||||
Set<String> propsToRemove = new HashSet<String>();
|
||||
|
||||
for (Object key : systemProps.keySet()) {
|
||||
if (key.toString().startsWith(ExportImportConfig.PREFIX)) {
|
||||
propsToRemove.add(key.toString());
|
||||
}
|
||||
}
|
||||
|
||||
for (String propToRemove : propsToRemove) {
|
||||
systemProps.remove(propToRemove);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -209,18 +208,18 @@ public class ExportImportTest {
|
|||
// Delete some realm (and some data in admin realm)
|
||||
KeycloakSession session = keycloakRule.startSession();
|
||||
try {
|
||||
ModelProvider model = session.model();
|
||||
RealmProvider realmProvider = session.realms();
|
||||
UserProvider userProvider = session.users();
|
||||
new RealmManager(session).removeRealm(model.getRealmByName("test"));
|
||||
Assert.assertEquals(1, model.getRealms().size());
|
||||
new RealmManager(session).removeRealm(realmProvider.getRealmByName("test"));
|
||||
Assert.assertEquals(1, realmProvider.getRealms().size());
|
||||
|
||||
RealmModel master = model.getRealmByName(Config.getAdminRealm());
|
||||
RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm());
|
||||
session.users().removeUser(master, "admin2");
|
||||
assertNotAuthenticated(userProvider, model, Config.getAdminRealm(), "admin2", "admin2");
|
||||
assertNotAuthenticated(userProvider, model, "test", "test-user@localhost", "password");
|
||||
assertNotAuthenticated(userProvider, model, "test", "user1", "password");
|
||||
assertNotAuthenticated(userProvider, model, "test", "user2", "password");
|
||||
assertNotAuthenticated(userProvider, model, "test", "user3", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, Config.getAdminRealm(), "admin2", "admin2");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "test-user@localhost", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "user1", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "user2", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "user3", "password");
|
||||
} finally {
|
||||
keycloakRule.stopSession(session, true);
|
||||
}
|
||||
|
@ -234,7 +233,7 @@ public class ExportImportTest {
|
|||
// Ensure data are imported back
|
||||
session = keycloakRule.startSession();
|
||||
try {
|
||||
ModelProvider model = session.model();
|
||||
RealmProvider model = session.realms();
|
||||
UserProvider userProvider = session.users();
|
||||
Assert.assertEquals(2, model.getRealms().size());
|
||||
|
||||
|
@ -259,19 +258,19 @@ public class ExportImportTest {
|
|||
// Delete some realm (and some data in admin realm)
|
||||
KeycloakSession session = keycloakRule.startSession();
|
||||
try {
|
||||
ModelProvider model = session.model();
|
||||
RealmProvider realmProvider = session.realms();
|
||||
UserProvider userProvider = session.users();
|
||||
new RealmManager(session).removeRealm(model.getRealmByName("test"));
|
||||
Assert.assertEquals(1, model.getRealms().size());
|
||||
new RealmManager(session).removeRealm(realmProvider.getRealmByName("test"));
|
||||
Assert.assertEquals(1, realmProvider.getRealms().size());
|
||||
|
||||
RealmModel master = model.getRealmByName(Config.getAdminRealm());
|
||||
RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm());
|
||||
session.users().removeUser(master, "admin2");
|
||||
|
||||
assertNotAuthenticated(userProvider, model, Config.getAdminRealm(), "admin2", "admin2");
|
||||
assertNotAuthenticated(userProvider, model, "test", "test-user@localhost", "password");
|
||||
assertNotAuthenticated(userProvider, model, "test", "user1", "password");
|
||||
assertNotAuthenticated(userProvider, model, "test", "user2", "password");
|
||||
assertNotAuthenticated(userProvider, model, "test", "user3", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, Config.getAdminRealm(), "admin2", "admin2");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "test-user@localhost", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "user1", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "user2", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, "test", "user3", "password");
|
||||
} finally {
|
||||
keycloakRule.stopSession(session, true);
|
||||
}
|
||||
|
@ -285,24 +284,24 @@ public class ExportImportTest {
|
|||
// Ensure data are imported back, but just for "test" realm
|
||||
session = keycloakRule.startSession();
|
||||
try {
|
||||
ModelProvider model = session.model();
|
||||
RealmProvider realmProvider = session.realms();
|
||||
UserProvider userProvider = session.users();
|
||||
Assert.assertEquals(2, model.getRealms().size());
|
||||
Assert.assertEquals(2, realmProvider.getRealms().size());
|
||||
|
||||
assertNotAuthenticated(userProvider, model, Config.getAdminRealm(), "admin2", "admin2");
|
||||
assertAuthenticated(userProvider, model, "test", "test-user@localhost", "password");
|
||||
assertAuthenticated(userProvider, model, "test", "user1", "password");
|
||||
assertAuthenticated(userProvider, model, "test", "user2", "password");
|
||||
assertAuthenticated(userProvider, model, "test", "user3", "password");
|
||||
assertNotAuthenticated(userProvider, realmProvider, Config.getAdminRealm(), "admin2", "admin2");
|
||||
assertAuthenticated(userProvider, realmProvider, "test", "test-user@localhost", "password");
|
||||
assertAuthenticated(userProvider, realmProvider, "test", "user1", "password");
|
||||
assertAuthenticated(userProvider, realmProvider, "test", "user2", "password");
|
||||
assertAuthenticated(userProvider, realmProvider, "test", "user3", "password");
|
||||
|
||||
addUser(userProvider, model.getRealmByName(Config.getAdminRealm()), "admin2", "admin2");
|
||||
addUser(userProvider, realmProvider.getRealmByName(Config.getAdminRealm()), "admin2", "admin2");
|
||||
} finally {
|
||||
keycloakRule.stopSession(session, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertAuthenticated(UserProvider userProvider, ModelProvider model, String realmName, String username, String password) {
|
||||
RealmModel realm = model.getRealmByName(realmName);
|
||||
private void assertAuthenticated(UserProvider userProvider, RealmProvider realmProvider, String realmName, String username, String password) {
|
||||
RealmModel realm = realmProvider.getRealmByName(realmName);
|
||||
if (realm == null) {
|
||||
Assert.fail("realm " + realmName + " not found");
|
||||
}
|
||||
|
@ -312,11 +311,11 @@ public class ExportImportTest {
|
|||
Assert.fail("user " + username + " not found");
|
||||
}
|
||||
|
||||
Assert.assertTrue(realm.validatePassword(user, password));
|
||||
Assert.assertTrue(userProvider.validCredentials(realm, user, UserCredentialModel.password(password)));
|
||||
}
|
||||
|
||||
private void assertNotAuthenticated(UserProvider userProvider, ModelProvider model, String realmName, String username, String password) {
|
||||
RealmModel realm = model.getRealmByName(realmName);
|
||||
private void assertNotAuthenticated(UserProvider userProvider, RealmProvider realmProvider, String realmName, String username, String password) {
|
||||
RealmModel realm = realmProvider.getRealmByName(realmName);
|
||||
if (realm == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -326,7 +325,7 @@ public class ExportImportTest {
|
|||
return;
|
||||
}
|
||||
|
||||
Assert.assertFalse(realm.validatePassword(user, password));
|
||||
Assert.assertFalse(userProvider.validCredentials(realm, user, UserCredentialModel.password(password)));
|
||||
}
|
||||
|
||||
private static void addUser(UserProvider userProvider, RealmModel appRealm, String username, String password) {
|
||||
|
@ -345,11 +344,11 @@ public class ExportImportTest {
|
|||
String relativeDirExportImportPath = "testsuite" + File.separator + "integration" + File.separator + "target" + File.separator + "export-import";
|
||||
|
||||
if (System.getProperties().containsKey("maven.home")) {
|
||||
dirPath = System.getProperty("user.dir").replaceFirst("testsuite.integration.*", relativeDirExportImportPath);
|
||||
dirPath = System.getProperty("user.dir").replaceFirst("testsuite.integration.*", Matcher.quoteReplacement(relativeDirExportImportPath));
|
||||
} else {
|
||||
for (String c : System.getProperty("java.class.path").split(File.pathSeparator)) {
|
||||
if (c.contains(File.separator + "testsuite" + File.separator + "integration")) {
|
||||
dirPath = c.replaceFirst("testsuite.integration.*", relativeDirExportImportPath);
|
||||
dirPath = c.replaceFirst("testsuite.integration.*", Matcher.quoteReplacement(relativeDirExportImportPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -357,5 +356,5 @@ public class ExportImportTest {
|
|||
String absolutePath = new File(dirPath).getAbsolutePath();
|
||||
return absolutePath;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.keycloak.representations.idm.RealmRepresentation;
|
|||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.models.utils.ModelToRepresentation;
|
||||
import org.keycloak.services.managers.RealmManager;
|
||||
import org.keycloak.testsuite.Retry;
|
||||
import org.keycloak.testutils.KeycloakServer;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
|
@ -21,6 +22,8 @@ import javax.servlet.Servlet;
|
|||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
|
@ -114,15 +117,7 @@ public abstract class AbstractKeycloakRule extends ExternalResource {
|
|||
|
||||
@Override
|
||||
protected void after() {
|
||||
server.stop();
|
||||
|
||||
// Add some variable delay (Some windows envs have issues as server is not stopped immediately after server.stop)
|
||||
try {
|
||||
int sleepInterval = Integer.parseInt(System.getProperty("testsuite.delay", "0"));
|
||||
Thread.sleep(sleepInterval);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
stopServer();
|
||||
}
|
||||
|
||||
public RealmRepresentation loadJson(String path) throws IOException {
|
||||
|
@ -151,10 +146,32 @@ public abstract class AbstractKeycloakRule extends ExternalResource {
|
|||
|
||||
public void restartServer() {
|
||||
try {
|
||||
server.stop();
|
||||
stopServer();
|
||||
server.start();
|
||||
} catch (Throwable t) {
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
}
|
||||
|
||||
private void stopServer() {
|
||||
server.stop();
|
||||
|
||||
// Add some variable delay (Some windows envs have issues as server is not stopped immediately after server.stop)
|
||||
try {
|
||||
Retry.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
new Socket(server.getConfig().getHost(), server.getConfig().getPort());
|
||||
throw new RuntimeException();
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
}, 10, 500);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue