Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile.autotune
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ WORKDIR $AUTOTUNE_HOME/src/autotune

# Copy only the pom.xml and download the dependencies
COPY pom.xml $AUTOTUNE_HOME/src/autotune/
RUN mvn -f pom.xml install dependency:copy-dependencies
RUN mvn -f pom.xml install dependency:copy-dependencies -DskipTests

# Now copy the sources and compile and package them
COPY src $AUTOTUNE_HOME/src/autotune/src/
Expand Down
28 changes: 25 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<hibernate-Validator>8.0.1.Final</hibernate-Validator>
<micrometer-version>1.9.9</micrometer-version>
<awssdk-version>2.40.5</awssdk-version>
<argLine></argLine>
<surefireArgLine>-Dnet.bytebuddy.experimental=true</surefireArgLine>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -136,8 +138,24 @@
<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>

<!-- Mockito core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.8.0</version>
Copy link
Contributor

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

<scope>test</scope>
</dependency>

<!-- Mockito integration with JUnit 5 -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -268,7 +286,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<version>3.1.2</version>
<configuration>
<failIfNoTests>true</failIfNoTests>
<argLine>${argLine} ${surefireArgLine}</argLine>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
141 changes: 141 additions & 0 deletions src/test/java/com/autotune/analyzer/workerimpl/BulkJobManagerTest.java
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;

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");

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
);
}
}