-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(bots): skip bot upsert when nothing changed to stop team-strip + reindex loop on boot #28128
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright 2026 Collate | ||
| * Licensed 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.openmetadata.service.util; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.openmetadata.schema.entity.teams.User; | ||
| import org.openmetadata.schema.type.EntityReference; | ||
| import org.openmetadata.service.Entity; | ||
| import org.openmetadata.service.jdbi3.UserRepository; | ||
|
|
||
| /** | ||
| * Unit coverage for the boot-time bot-team-strip loop. | ||
| * | ||
| * <p>{@code BotResource.initialize()} calls {@link UserUtil#addOrUpdateBotUser(User)} for every | ||
| * bot on every OM boot. The in-memory User built by {@code UserUtil.user(...)} does not have | ||
| * the {@code teams} field populated, so without the short-circuit guard the call falls through | ||
| * to {@code userRepository.createOrUpdate}, which runs {@code UserUpdater.updateTeams} with | ||
| * {@code updated.teams == null} and wipes the bot's stored team relationships, bumps the | ||
| * version, and triggers an Elasticsearch reindex on every boot. | ||
| */ | ||
| class UserUtilBotTest { | ||
|
|
||
| @Test | ||
| void addOrUpdateBotUserShortCircuitsWhenNothingChanged() { | ||
| UserRepository userRepository = mock(UserRepository.class); | ||
|
|
||
| User stored = | ||
| new User() | ||
| .withId(UUID.randomUUID()) | ||
| .withName("ingestion-bot") | ||
| .withFullyQualifiedName("ingestion-bot") | ||
| .withDisplayName("ingestion-bot") | ||
| .withDescription(null) | ||
| .withIsBot(true) | ||
| .withRoles(new ArrayList<>()) | ||
| // Pre-populate authMechanism so that if the short-circuit guard regresses, the | ||
| // fall-through path doesn't immediately blow up in JWTTokenGenerator (which | ||
| // isn't initialized in a pure unit test). With the bug present, the test still | ||
| // proceeds into addOrUpdateUser and the `never()` verify below fires the | ||
| // regression signal. | ||
| .withAuthenticationMechanism( | ||
| new org.openmetadata.schema.entity.teams.AuthenticationMechanism() | ||
| .withAuthType(org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.JWT)); | ||
|
Comment on lines
+66
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Quality: Fully qualified class names used instead of importsThe test uses fully qualified names Import the class and use short names:
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎 |
||
| User incoming = | ||
| new User() | ||
| .withId(UUID.randomUUID()) | ||
| .withName("ingestion-bot") | ||
| .withFullyQualifiedName("ingestion-bot") | ||
| .withDisplayName("ingestion-bot") | ||
| .withDescription(null) | ||
| .withIsBot(true) | ||
| .withRoles(null); | ||
|
|
||
| when(userRepository.getByName(any(), eq("ingestion-bot"), any())).thenReturn(stored); | ||
|
|
||
| try (MockedStatic<Entity> entityStatic = mockStatic(Entity.class)) { | ||
| entityStatic.when(() -> Entity.getEntityRepository(Entity.USER)).thenReturn(userRepository); | ||
| User result = UserUtil.addOrUpdateBotUser(incoming); | ||
| assertSame( | ||
| stored, | ||
| result, | ||
| "Short-circuit must return the original user without going through the PUT path"); | ||
| } | ||
|
|
||
| // The whole point of the fix: never hit createOrUpdate when nothing changed. | ||
| verify(userRepository, never()).createOrUpdate(any(), any(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| void addOrUpdateBotUserGoesThroughUpsertWhenDisplayNameChanged() { | ||
| UserRepository userRepository = mock(UserRepository.class); | ||
|
|
||
| User stored = | ||
| new User() | ||
| .withId(UUID.randomUUID()) | ||
| .withName("ingestion-bot") | ||
| .withFullyQualifiedName("ingestion-bot") | ||
| .withDisplayName("Old Display Name") | ||
| .withIsBot(true) | ||
| // Provide an authMechanism on the persisted row so the upsert path doesn't try | ||
| // to generate a fresh JWT via JWTTokenGenerator (which isn't initialized in a | ||
| // pure unit test). | ||
| .withAuthenticationMechanism( | ||
| new org.openmetadata.schema.entity.teams.AuthenticationMechanism() | ||
| .withAuthType(org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.JWT)); | ||
| User incoming = | ||
| new User() | ||
| .withId(UUID.randomUUID()) | ||
| .withName("ingestion-bot") | ||
| .withFullyQualifiedName("ingestion-bot") | ||
| .withDisplayName("New Display Name") | ||
| .withIsBot(true); | ||
|
|
||
| when(userRepository.getByName(any(), eq("ingestion-bot"), any())).thenReturn(stored); | ||
| // Returning null from createOrUpdate path is fine: addOrUpdateUser wraps exceptions but | ||
| // we only care that the method was invoked (i.e. the short-circuit did NOT fire). | ||
| when(userRepository.findByNameOrNull(any(), any())).thenReturn(null); | ||
|
|
||
| try (MockedStatic<Entity> entityStatic = mockStatic(Entity.class)) { | ||
| entityStatic.when(() -> Entity.getEntityRepository(Entity.USER)).thenReturn(userRepository); | ||
| try { | ||
| User result = UserUtil.addOrUpdateBotUser(incoming); | ||
| assertNotEquals( | ||
| stored, | ||
| result, | ||
| "When fields differ the upsert path must run and produce a different User"); | ||
| } catch (RuntimeException ignored) { | ||
| // The downstream createOrUpdate call may throw against the mock; the assertion we | ||
| // care about is that the short-circuit guard did NOT fire, which we verify below. | ||
| } | ||
| } | ||
|
Comment on lines
+125
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Quality: Swallowed RuntimeException uses flow-control exception patternThe test at line 131 catches Stub the mock properly instead of catching RuntimeException:
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎 |
||
|
|
||
| verify(userRepository).createOrUpdate(any(), any(User.class), any()); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| private static List<EntityReference> roleRef(String name) { | ||
| List<EntityReference> refs = new ArrayList<>(); | ||
| refs.add(new EntityReference().withId(UUID.randomUUID()).withName(name).withType("role")); | ||
| return refs; | ||
| } | ||
|
Comment on lines
+140
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Quality: Unused private helper method
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emailfield changesThe guard at line 342-346 compares
roles,description, anddisplayNamebut notemail. TheUserUtil.user(...)method (which builds the in-memory bot user) may set an email based ondomain. If an admin changes the domain configuration between restarts, the email update would be silently skipped by the short-circuit. Consider whetheremailshould be included in the comparison, or document why it's excluded.Was this helpful? React with 👍 / 👎