Refactor: Replace Whitebox with standard reflection in Analyzer, Server-Cluster-Plugin and Receiver & Fetcher Plugins.#13718
Conversation
|
Could you remove the changes of log-analyzer and meter-analyzer? I am working on to big changes to replace them, ref to #13716 |
There was a problem hiding this comment.
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/getInternalStatecalls with standard JavaField.setAccessible()+Field.get/set()patterns across 20+ test files in theanalyzer,server-cluster-plugin,server-receiver-plugin, andserver-fetcher-pluginmodules - Replaced
Whitebox-based singleton manipulation ofMetricsStreamProcessorwithMockedStatic(Mockito), adding proper@AfterEachlifecycle cleanup for resource management - Updated the
changes.mdchangelog 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.
...t/java/org/apache/skywalking/oap/server/receiver/envoy/ClusterManagerMetricsAdapterTest.java
Outdated
Show resolved
Hide resolved
...er/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/K8sTagTest.java
Outdated
Show resolved
Hide resolved
.../test/java/org/apache/skywalking/oap/server/cluster/plugin/consul/ConsulCoordinatorTest.java
Show resolved
Hide resolved
...java/org/apache/skywalking/oap/server/cluster/plugin/zookeeper/ZookeeperCoordinatorTest.java
Show resolved
Hide resolved
.../meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/AnalyzerTest.java
Outdated
Show resolved
Hide resolved
...c/test/java/org/apache/skywalking/oap/server/receiver/zabbix/provider/ZabbixMetricsTest.java
Outdated
Show resolved
Hide resolved
…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.
155ed8e to
49b434c
Compare
Updated as requested to remove |
|
Please follow copolit review(check whether they are reasonable) |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
I've applied the reasonable Copilot suggestions, including fixing the import order and removing redundant exception declarations. |
|
A lot of conflicts, as we have MAL/LAL v2, you need to adopt on latest codes. |
|
Powermock should have been replaced out by me. |
|
I checked.
|
|
Got it, thanks for the heads-up |
CHANGESlog.[Analyzer]
General Refactoring (
agent-analyzer,log-analyzer):Whiteboxusages across various analyzer tests.Field.setAccessible()etc.), ensuring proper checked exception handling to prevent polluting test method signatures.AnalyzerTest(meter-analyzer):WhiteboxwithMockedStatic(using Mockito) to safely inject and mock theMetricsStreamProcessorsingleton..close()calls in the@AfterEach(ortearDownEach) lifecycle methods to properly release the static mocks and prevent resource leaks or interference between tests.K8sTagTest(meter-analyzer):KubernetesPodsandKubernetesServicesare implemented as Enum singletons, mocking the entire objects was problematic. To resolve this:LoadingCacheinstances by injecting Mock objects directly into these fields via standard reflection.NullPointerExceptions during cache misses by explicitly stubbing the mocked caches to returnOptional.empty()or empty collections for non-matching keys, ensuring better test isolation and stability.[Server-Cluster-Plugin]
Replaced all
Whitebox.setInternalState()andgetInternalState()usages with standardjava.lang.reflect.Fieldto 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 inRuntimeExceptionwithintry-catchblocks. This successfully removed the PowerMock dependency while preserving the existing test lifecycle method signatures (like@BeforeEachor@Test) without adding unnecessarythrowsdeclarations.[Receiver & Fetcher Plugins]
General Refactoring (
server-receiver-plugin,server-fetcher-plugin):Systematically removed
Whiteboxdependencies and migrated to standard Java Reflection API.Centralized reflection logic within
try-catchblocks to wrap checked exceptions intoRuntimeException, ensuring no changes were required to existing test method signatures.ZabbixMetricsTest&ZabbixBaseTest:Static Mock Lifecycle Management: Replaced manual
Whiteboxfield manipulation of theMetricsStreamProcessorsingleton withMockedStatic.Implemented strict
.close()logic within the@AfterEachlifecycle to prevent memory leaks and resource conflicts, which is particularly critical for stability in newer JVM environments.Resolved a legacy
NullPointerExceptionby combiningMockedStaticwith aMockito.spy()of the actual processor instance, ensuring a safe and stateful mock return.TelegrafMetricsTest:Eliminated Redundant Mocks: Removed unused
CoreModuleandMetricsStreamProcessorstubbings.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.,
initializedflag inFieldsHelper) 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: