Skip to content

Refactor: Replace Whitebox with standard reflection in Analyzer, Server-Cluster-Plugin and Receiver & Fetcher Plugins.#13718

Closed
Hyeon-moGu wants to merge 4 commits intoapache:masterfrom
Hyeon-moGu:replace-whitebox-phase-4-6
Closed

Refactor: Replace Whitebox with standard reflection in Analyzer, Server-Cluster-Plugin and Receiver & Fetcher Plugins.#13718
Hyeon-moGu wants to merge 4 commits intoapache:masterfrom
Hyeon-moGu:replace-whitebox-phase-4-6

Conversation

@Hyeon-moGu
Copy link
Copy Markdown
Contributor

[Analyzer]

General Refactoring (agent-analyzer, log-analyzer):

  • Systematically eliminated Whitebox usages across various analyzer tests.
  • Migrated internal state manipulations to standard Java Reflection API (Field.setAccessible() etc.), ensuring proper checked exception handling to prevent polluting test method signatures.

AnalyzerTest (meter-analyzer):

  • Replaced Whitebox with MockedStatic (using Mockito) to safely inject and mock the MetricsStreamProcessor singleton.
  • Added explicit .close() calls in the @AfterEach (or tearDownEach) lifecycle methods to properly release the static mocks and prevent resource leaks or interference between tests.

K8sTagTest (meter-analyzer):

  • Since KubernetesPods and KubernetesServices are implemented as Enum singletons, mocking the entire objects was problematic. To resolve this:
    • Handled the internal lazy-loading LoadingCache instances by injecting Mock objects directly into these fields via standard reflection.
    • Fixed potential NullPointerExceptions during cache misses by explicitly stubbing the mocked caches to return Optional.empty() or empty collections for non-matching keys, ensuring better test isolation and stability.

[Server-Cluster-Plugin]

  • Replaced all Whitebox.setInternalState() and getInternalState() usages with standard java.lang.reflect.Field to inject internal dependencies (e.g., client, config, loadedProvider, healthChecker) into Coordinators and Providers across all 5 cluster plugins (Zookeeper, Nacos, Etcd, Kubernetes, Consul).

  • Handled Checked Exceptions Safely: Since standard Reflection APIs strictly throw checked exceptions (NoSuchFieldException, IllegalAccessException), explicitly wrapped them in RuntimeException within try-catch blocks. This successfully removed the PowerMock dependency while preserving the existing test lifecycle method signatures (like @BeforeEach or @Test) without adding unnecessary throws declarations.

[Receiver & Fetcher Plugins]

General Refactoring (server-receiver-plugin, server-fetcher-plugin):

  • Systematically removed Whitebox dependencies and migrated to standard Java Reflection API.

  • Centralized reflection logic within try-catch blocks to wrap checked exceptions into RuntimeException, ensuring no changes were required to existing test method signatures.

ZabbixMetricsTest & ZabbixBaseTest:

  • Static Mock Lifecycle Management: Replaced manual Whitebox field manipulation of the MetricsStreamProcessor singleton with MockedStatic.

  • Implemented strict .close() logic within the @AfterEach lifecycle to prevent memory leaks and resource conflicts, which is particularly critical for stability in newer JVM environments.

  • Resolved a legacy NullPointerException by combining MockedStatic with a Mockito.spy() of the actual processor instance, ensuring a safe and stateful mock return.

TelegrafMetricsTest:

  • Eliminated Redundant Mocks: Removed unused CoreModule and MetricsStreamProcessor stubbings.

  • Benefit: Removing these dead mock configurations simplifies the test setup, adheres to the minimal mocking principle, and ensures full compatibility with modern Mockito strictness.

ClusterManagerMetricsAdapterTest:

  • Static State Isolation: Addressed potential flaky test behavior by explicitly clearing shared static maps (e.g., initialized flag in FieldsHelper) during @BeforeEach.

  • This ensures complete test isolation by preventing state leakage between test cases, leading to a more deterministic and reliable test suite.

CiliumNodeManagerTest & Others:

  • Successfully migrated internal state injections(remoteInstances and allNodes) to standard reflection, maintaining the original testing intent while removing the PowerMock requirement.

@wu-sheng
Copy link
Copy Markdown
Member

wu-sheng commented Mar 2, 2026

Could you remove the changes of log-analyzer and meter-analyzer? I am working on to big changes to replace them, ref to #13716
Most likely, everything in there will be changed.

@wu-sheng wu-sheng added the test Test requirements about performance, feature or before release. label Mar 2, 2026
@wu-sheng wu-sheng requested a review from Copilot March 2, 2026 00:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR is part of the ongoing effort (issue #13709) to remove the deprecated powermock-reflect library and migrate test code to standard Java Reflection APIs and modern Mockito features. This improves compatibility with JDK 17+ and reduces unmaintained dependencies.

Changes:

  • Replaced Whitebox.setInternalState/getInternalState calls with standard Java Field.setAccessible() + Field.get/set() patterns across 20+ test files in the analyzer, server-cluster-plugin, server-receiver-plugin, and server-fetcher-plugin modules
  • Replaced Whitebox-based singleton manipulation of MetricsStreamProcessor with MockedStatic (Mockito), adding proper @AfterEach lifecycle cleanup for resource management
  • Updated the changes.md changelog to include the newly migrated modules

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
ZabbixMetricsTest.java Replaces Whitebox singleton injection with MockedStatic; adds @AfterEach cleanup
TelegrafMetricsTest.java Removes unused CoreModuleProvider mock and dead Whitebox/MetricsStreamProcessor stubs
ClusterManagerMetricsAdapterTest.java Clears HELPER_MAP instead of resetting initialized flag on a specific FieldsHelper instance
AgentConfigurationsWatcherTest.java Replaces Whitebox field access with standard reflection in test methods
CiliumNodeManagerTest.java Replaces Whitebox field injection with standard reflection
ZookeeperCoordinatorTest.java Replaces Whitebox field injection with standard reflection
ClusterModuleZookeeperProviderFunctionalIT.java Replaces Whitebox with reflection; adds throws Exception to init()
NacosCoordinatorTest.java Replaces Whitebox with try-catch-wrapped reflection
ClusterModuleNacosProviderFunctionalIT.java Replaces Whitebox with try-catch-wrapped reflection
ClusterModuleKubernetesProviderTest.java Replaces Whitebox with reflection; groups setup in try-catch
ClusterModuleEtcdProviderFunctionalIT.java Replaces Whitebox with try-catch-wrapped reflection
ClusterEtcdPluginIT.java Replaces Whitebox with standard reflection
ConsulCoordinatorTest.java Replaces Whitebox with try-catch-wrapped reflection
ClusterModuleConsulProviderTest.java Replaces Whitebox with try-catch-wrapped reflection
ClusterModuleConsulProviderFunctionalIT.java Replaces Whitebox with try-catch-wrapped reflection
K8sTagTest.java Injects mock LoadingCache instances into KubernetesServices/KubernetesPods enum fields via reflection
AnalyzerTest.java Replaces Whitebox singleton injection with MockedStatic; adds teardown
DSLTest.java Replaces Whitebox with standard reflection for filterSpec.sinkListenerFactories
DSLSecurityTest.java Replaces Whitebox with standard reflection wrapped in try-catch
UninstrumentedGatewaysConfigTest.java Replaces Whitebox.invokeMethod with Method.invoke() via reflection
TraceSamplingPolicyWatcherTest.java Replaces Whitebox field access with standard reflection
MeterProcessorTest.java Replaces Whitebox singleton with MockedStatic; adds @AfterEach
changes.md Updates changelog to include newly migrated modules

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…er-Cluster-Plugin and Receiver & Fetcher Plugins.

**General Refactoring (`agent-analyzer`):**
- Systematically eliminated `Whitebox` usages across various analyzer tests.
- Migrated internal state manipulations to standard Java Reflection API (`Field.setAccessible()` etc.), ensuring proper checked exception handling to prevent polluting test method signatures.

- Replaced all `Whitebox.setInternalState()` and `getInternalState()` usages with standard `java.lang.reflect.Field` to inject internal dependencies (e.g., `client`, `config`, `loadedProvider`, `healthChecker`) into Coordinators and Providers across all 5 cluster plugins (Zookeeper, Nacos, Etcd, Kubernetes, Consul).

- **Handled Checked Exceptions Safely:** Since standard Reflection APIs strictly throw checked exceptions (`NoSuchFieldException`, `IllegalAccessException`), explicitly wrapped them in `RuntimeException` within `try-catch` blocks. This successfully removed the PowerMock dependency while preserving the existing test lifecycle method signatures (like `@BeforeEach` or `@Test`) without adding unnecessary `throws` declarations.

** General Refactoring (`server-receiver-plugin`, `server-fetcher-plugin`):**

- Systematically removed `Whitebox` dependencies and migrated to standard Java Reflection API.

- Centralized reflection logic within `try-catch` blocks to wrap checked exceptions into `RuntimeException`, ensuring no changes were required to existing test method signatures.

`ZabbixMetricsTest` & `ZabbixBaseTest`:

- Static Mock Lifecycle Management: Replaced manual `Whitebox` field manipulation of the `MetricsStreamProcessor` singleton with `MockedStatic`.

- Implemented strict `.close()` logic within the `@AfterEach` lifecycle to prevent memory leaks and resource conflicts, which is particularly critical for stability in newer JVM environments.

- Resolved a legacy `NullPointerException` by combining `MockedStatic` with a `Mockito.spy()` of the actual processor instance, ensuring a safe and stateful mock return.

`TelegrafMetricsTest`:

- Clean Mocking: Eliminated redundant `CoreModule` mocks and unused stubbings, resolving `UnnecessaryStubbingException` and improving test performance

- Modernization: Fixed anti-patterns (spying on class literals) and aligned with JUnit 5 best practices.

`ClusterManagerMetricsAdapterTest`:

- Static State Isolation: Addressed potential flaky test behavior by explicitly clearing shared static maps (e.g., `initialized` flag in `FieldsHelper`) during `@BeforeEach`.

- This ensures complete test isolation by preventing state leakage between test cases, leading to a more deterministic and reliable test suite.

`CiliumNodeManagerTest` & Others:

- Successfully migrated internal state injections(remoteInstances and allNodes) to standard reflection, maintaining the original testing intent while removing the PowerMock requirement.
@Hyeon-moGu Hyeon-moGu force-pushed the replace-whitebox-phase-4-6 branch from 155ed8e to 49b434c Compare March 2, 2026 02:55
@Hyeon-moGu
Copy link
Copy Markdown
Contributor Author

Could you remove the changes of log-analyzer and meter-analyzer? I am working on to big changes to replace them, ref to #13716 Most likely, everything in there will be changed.

Updated as requested to remove log-analyzer and meter-analyzer. Thanks!

@wu-sheng
Copy link
Copy Markdown
Member

wu-sheng commented Mar 3, 2026

Please follow copolit review(check whether they are reasonable)

Hyeon-moGu and others added 2 commits March 3, 2026 19:25
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Hyeon-moGu
Copy link
Copy Markdown
Contributor Author

I've applied the reasonable Copilot suggestions, including fixing the import order and removing redundant exception declarations.

@wu-sheng
Copy link
Copy Markdown
Member

wu-sheng commented Mar 6, 2026

A lot of conflicts, as we have MAL/LAL v2, you need to adopt on latest codes.

@wu-sheng
Copy link
Copy Markdown
Member

wu-sheng commented Mar 6, 2026

Powermock should have been replaced out by me.

@wu-sheng
Copy link
Copy Markdown
Member

wu-sheng commented Mar 6, 2026

I checked.

The groovy-replace branch has already replaced all Whitebox usages with ReflectUtil. PR #13718 is no longer required — the work it proposes has already been done on this branch.

@wu-sheng wu-sheng closed this Mar 6, 2026
@Hyeon-moGu
Copy link
Copy Markdown
Contributor Author

Got it, thanks for the heads-up

@Hyeon-moGu Hyeon-moGu deleted the replace-whitebox-phase-4-6 branch March 6, 2026 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test Test requirements about performance, feature or before release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Remove deprecated PowerMock (Whitebox) and migrate to standard Mockito/Reflection

3 participants