-
Notifications
You must be signed in to change notification settings - Fork 62
Onboarding Unit Testing with Mocking Framework for Kruize #1772
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
Open
pinkygupta-hub
wants to merge
12
commits into
kruize:mvp_demo
Choose a base branch
from
pinkygupta-hub:mocking-integration
base: mvp_demo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
024bf0a
integrating mocking in kruize with sample mocked examples
pinkygupta-hub 77277ea
integrating mocking in kruize with sample mocked examples
pinkygupta-hub fa74b47
skip running test while dependency caching layer
pinkygupta-hub 09e67a5
skip running test while dependency caching layer
pinkygupta-hub 0486939
skip running test while dependency caching layer
pinkygupta-hub 1555338
skip running test while dependency caching layer
pinkygupta-hub 5879730
skip running test while dependency caching layer
pinkygupta-hub fead2db
skip running test while dependency caching layer
pinkygupta-hub f6b6719
skip running test while dependency caching layer
pinkygupta-hub c955114
skip running test while dependency caching layer
pinkygupta-hub 3ecbab3
addressing review comment updating year in license text
pinkygupta-hub b3bb088
addressing review comment updating year in license text
pinkygupta-hub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/test/java/com/autotune/analyzer/workerimpl/BulkJobManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 Red Hat, IBM Corporation and others. | ||
| * | ||
| * 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 com.autotune.analyzer.workerimpl; | ||
dinogun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.autotune.analyzer.serviceObjects.BulkInput; | ||
| import com.autotune.analyzer.serviceObjects.BulkJobStatus; | ||
| import com.autotune.common.data.dataSourceMetadata.DataSourceCluster; | ||
| import com.autotune.common.data.dataSourceMetadata.DataSourceContainer; | ||
| import com.autotune.common.data.dataSourceMetadata.DataSourceNamespace; | ||
| import com.autotune.common.data.dataSourceMetadata.DataSourceWorkload; | ||
| import com.autotune.operator.KruizeDeploymentInfo; | ||
|
|
||
| /** | ||
| * Unit Test with Mocking Template Principles | ||
| * | ||
| * 1. One class under test: | ||
| * Each test class should focus on validating the behavior of a single class. | ||
| * | ||
| * 2. Mock only direct dependencies: | ||
| * External collaborators are mocked using Mockito to keep tests isolated, | ||
| * fast, and deterministic. | ||
| * | ||
| * 3. No static or global side effects: | ||
| * Tests must not depend on or mutate shared global state to avoid flakiness | ||
| * when executed in parallel. | ||
| * | ||
| * 4. Clear Given–When–Then structure: | ||
| * - Given: test setup and mocked behavior | ||
| * - When: execution of the method under test | ||
| * - Then: assertions on the expected outcome | ||
| * | ||
| * 5. Deterministic assertions: | ||
| * Assertions must be predictable and repeatable across environments | ||
| * and test runs. | ||
| */ | ||
| class BulkJobManagerMockedTest { | ||
|
|
||
| private BulkJobManager bulkJobManager; | ||
| private BulkInput bulkInput; | ||
| private BulkJobStatus jobStatus; | ||
|
|
||
| private DataSourceCluster cluster; | ||
| private DataSourceNamespace namespace; | ||
| private DataSourceWorkload workload; | ||
| private DataSourceContainer container; | ||
|
|
||
| // Preserve static state to avoid cross-test leakage | ||
| private String originalExperimentNameFormat; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| // Capture static/global state | ||
| originalExperimentNameFormat = KruizeDeploymentInfo.experiment_name_format; | ||
|
|
||
| bulkInput = mock(BulkInput.class); | ||
| when(bulkInput.getDatasource()).thenReturn("prometheus"); | ||
bharathappali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| jobStatus = mock(BulkJobStatus.class); | ||
|
|
||
| cluster = mock(DataSourceCluster.class); | ||
| when(cluster.getDataSourceClusterName()).thenReturn("cluster1"); | ||
|
|
||
| namespace = mock(DataSourceNamespace.class); | ||
| when(namespace.getNamespace()).thenReturn("default"); | ||
|
|
||
| workload = mock(DataSourceWorkload.class); | ||
| when(workload.getWorkloadName()).thenReturn("sysbench"); | ||
| when(workload.getWorkloadType()).thenReturn("deployment"); | ||
|
|
||
| container = mock(DataSourceContainer.class); | ||
| when(container.getContainerName()).thenReturn("sysbench"); | ||
|
|
||
| KruizeDeploymentInfo.experiment_name_format = | ||
| "%datasource%-%clustername%-%namespace%-%workloadname%-%workloadtype%-%containername%"; | ||
|
|
||
| bulkJobManager = new BulkJobManager("job-123", jobStatus, bulkInput); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| // Restore static/global state | ||
| KruizeDeploymentInfo.experiment_name_format = originalExperimentNameFormat; | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Frame experiment name without labels") | ||
| void shouldFrameExperimentNameWithoutLabels() { | ||
| // When | ||
| String experimentName = bulkJobManager.frameExperimentName( | ||
| null, cluster, namespace, workload, container | ||
| ); | ||
|
|
||
| // Then | ||
| assertEquals( | ||
| "prometheus-cluster1-default-sysbench-deployment-sysbench", | ||
| experimentName | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Frame experiment name with labels") | ||
| void shouldFrameExperimentNameWithLabels() { | ||
| // Given | ||
| KruizeDeploymentInfo.experiment_name_format = | ||
| "%datasource%-%clustername%-%namespace%-%workloadname%-%workloadtype%-%containername%-%label:env%-%label:version%"; | ||
|
|
||
| String labelString = "env=prod,version=v1.2"; | ||
|
|
||
| // When | ||
| String experimentName = bulkJobManager.frameExperimentName( | ||
| labelString, cluster, namespace, workload, container | ||
| ); | ||
|
|
||
| // Then | ||
| assertEquals( | ||
| "prometheus-cluster1-default-sysbench-deployment-sysbench-prod-v1.2", | ||
| experimentName | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I see 5.21.0 available, can you update to the latest versions and test it