8000 Adding database suppliers by miquelsi · Pull Request #31073 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Adding database suppliers - #31073

Merged
stianst merged 2 commits into
keycloak:mainfrom
miquelsi:main_db
Jul 16, 2024
Merged

stianst merged 2 commits into
keycloak:mainfrom
miquelsi:main_db

Conversation

@miquelsi
@miquelsi miquelsi commented Jul 5, 2024 •
Copy link
Copy Markdown
Contributor

Closes #30616

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.federation.ldap.LDAPReadOnlyTest#testReadOnlyUserGetsPermanentlyLocked

Keycloak CI - Base IT (5)

java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:87)
	at org.junit.Assert.assertTrue(Assert.java:42)
	at org.junit.Assert.assertFalse(Assert.java:65)
	at org.junit.Assert.assertFalse(Assert.java:75)
...

Report flaky test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be left like this because the dbconfig() returns the default value (DefaultDatabaseConfig.class) if it is not entered.

Suggested change
DatabaseConfig databaseConfig = annotation.dbconfig() != null ? SupplierHelpers.getInstance(annotation.dbconfig()) : new DefaultDatabaseConfig();
DatabaseConfig databaseConfig = SupplierHelpers.getInstance(annotation.dbconfig());

@miquelsi
miquelsi force-pushed the main_db branch 2 times, most recently from 9ca00b2 to 93f632e Compare July 8, 2024 15:57
@stianst stianst left a comment •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I had in mind is close to this, but not quite.

My idea was that we would have multiple database suppliers:

  • PostgresDatabaseSupplier
  • MySQLDatabaseSupplier
  • DevFileDatabaseSupplier
  • DevMemDatabaseSupplier

Each supplier would supply a TestDatabase with start/stop methods, and a getConfig() method.

You'd select which DB supplier to use with #30609 in the same way as we would select a different server run-mode or browser.

TestDatabase would be responsible for starting/stopping the DB container using test containers.

For now it's fine that DBs are not configurable (we just let it auto configure itself to some sensible defaults), and that we don't support connecting to external DBs, we can do that as a follow-up.

@miquelsi
miquelsi force-pushed the main_db branch 3 times, most recently from 18f01af to cf6eae4 Compare July 10, 2024 08:38
@miquelsi miquelsi changed the title DRAFT. Adding database configuration. DRAFT. Adding database suppliers. Jul 10, 2024
@miquelsi
miquelsi force-pushed the main_db branch 2 times, most recently from 20c5a3f to a7f61f5 Compare July 10, 2024 11:15
@miquelsi miquelsi changed the title DRAFT. Adding database suppliers. Adding database suppliers Jul 11, 2024
@miquelsi
miquelsi force-pushed the main_db branch 2 times, most recently from db709fe to 9869df8 Compare July 11, 2024 10:15
@miquelsi
miquelsi marked this pull request as ready for review July 11, 2024 11:05
@miquelsi
miquelsi requested a review from a team as a code owner July 11, 2024 11:05
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this class?

@stianst stianst Jul 11, 2024 •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not create the database supplier, but it should just use the registry to look it up:

TestDatabase database = registry.getDependency(TestDatabase.class, wrapper);

See

InstanceWrapper<RealmResource, TestRealm> wrapper = new InstanceWrapper<>(this, annotation);
LifeCycle lifecycle = annotation.lifecycle();
Keycloak adminClient = registry.getDependency(Keycloak.class, wrapper);
RealmConfig config = SupplierHelpers.getInstance(annotation.config());
RealmRepresentation realmRepresentation = config.getRepresentation();
if (realmRepresentation.getRealm() == null) {
String realmName = lifecycle.equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : registry.getCurrentContext().getRequiredTestClass().getSimpleName();
realmRepresentation.setRealm(realmName);
}
String realmName = realmRepresentation.getRealm();
wrapper.addNote(REALM_NAME_KEY, realmName);
adminClient.realms().create(realmRepresentation);
RealmResource realmResource = adminClient.realm(realmRepresentation.getRealm());
wrapper.setValue(realmResource, lifecycle);
return wrapper;
as an example on how to get a dependency in a supplier.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather then getting supplier I am thinking of fully manged DB called within the KeycloakIntegrationTest annotation as a prerequisite, do you think @stianst it makes sense?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to introduce rather DefaultDatabaseConfig class?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider some TestManagedDatabase annotation which will be reused within KeycloakIntegrationTest annotation for ServiceLoader injection?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be cleaner to just pass the DatabaseConfig? Makes it clear that it shouldn't be stopping/starting the db or anything like that ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to provide default implementations here? Or would all database providers have to implement this? Perhaps we can provide something else than an empty string such as null or Optional.empty()?

Comment on lines 8 to 40
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both vendor and containerImage are not used anywhere, perhaps remove these until we need them to support a functionality?

Suggested change
default String vendor() {
return "";
}
default String containerImage() {
return "";
}

Comment on lines 35 to 37
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is never used anywhere.

Suggested change
public TestDatabase getTestDatabase() {
return testDatabase;
}

Comment on lines 11 to 25
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these methods currently do anything? Is there a place where we actually need to start a database and tear it down? Also it seems that stop() is never called.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miquelsi dont't forget to rebase to main, because this change was added few days ago: 4970a9b

miquelsi and others added 2 commits July 15, 2024 17:09
Signed-off-by: Miquel Simon <msimonma@redhat.com>
@stianst
stianst merged commit 73247f5 into keycloak:main Jul 16, 2024
@miquelsi
miquelsi deleted the main_db branch July 16, 2024 07:40
stianst added a commit to stianst/keycloak that referenced this pull request Jul 25, 2024
* Closes keycloak#30616. Added database suppliers.

Signed-off-by: Miquel Simon <msimonma@redhat.com>
Co-authored-by: stianst <stianst@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Testsuite PoC - Add database suppliers

5 participants

0