-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
control-service: unit tests for data job persistence classes (#2935)
what: added unit tests and improved coverage for the following classes - DataJobsSynchronizer, DeploymentServiceV2, and JobImageDeployerV2 why: Easier issue identification testing: n/a --------- Signed-off-by: mrMoZ1 <[email protected]> Co-authored-by: github-actions <> Co-authored-by: dakodakov <[email protected]>
- Loading branch information
Momchil Z
and
dakodakov
authored
Dec 4, 2023
1 parent
240eb69
commit b67ce66
Showing
3 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
...service/src/test/java/com/vmware/taurus/service/deploy/DataJobsSynchronizerWriteTest.java
This file contains 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,138 @@ | ||
/* | ||
* Copyright 2021-2023 VMware, Inc. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.vmware.taurus.service.deploy; | ||
|
||
import com.vmware.taurus.ControlplaneApplication; | ||
import com.vmware.taurus.controlplane.model.data.DataJobResources; | ||
import com.vmware.taurus.datajobs.TestUtils; | ||
import com.vmware.taurus.datajobs.ToModelApiConverter; | ||
import com.vmware.taurus.service.kubernetes.ControlKubernetesService; | ||
import com.vmware.taurus.service.model.ActualDataJobDeployment; | ||
import com.vmware.taurus.service.model.DataJobDeploymentResources; | ||
import com.vmware.taurus.service.model.JobDeployment; | ||
import com.vmware.taurus.service.repository.ActualJobDeploymentRepository; | ||
import com.vmware.taurus.service.repository.DesiredJobDeploymentRepository; | ||
import com.vmware.taurus.service.repository.JobsRepository; | ||
import java.time.OffsetDateTime; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
@SpringBootTest(classes = ControlplaneApplication.class) | ||
@TestPropertySource( | ||
properties = { | ||
"datajobs.deployment.configuration.persistence.writeTos=DB", | ||
"datajobs.deployment.configuration.persistence.readDataSource=DB", | ||
"datajobs.deployment.configuration.synchronization.task.enabled:true" | ||
}) | ||
public class DataJobsSynchronizerWriteTest { | ||
|
||
@SpyBean DeploymentServiceV2 deploymentServiceV2; | ||
@SpyBean JobImageBuilder jobImageBuilder; | ||
@MockBean JobImageDeployerV2 jobImageDeployer; | ||
@MockBean ControlKubernetesService controlKubernetesService; | ||
@Autowired JobsRepository jobsRepository; | ||
@Autowired DesiredJobDeploymentRepository desiredJobDeploymentRepository; | ||
@Autowired DataJobsSynchronizer dataJobsSynchronizer; | ||
@Autowired ActualJobDeploymentRepository actualJobDeploymentRepository; | ||
|
||
@BeforeEach | ||
public void setup() throws Exception { | ||
Mockito.doReturn(Set.of("jobName")) | ||
.when(deploymentServiceV2) | ||
.findAllActualDeploymentNamesFromKubernetes(); | ||
Mockito.doReturn(true) | ||
.when(jobImageBuilder) | ||
.buildImage(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); | ||
Mockito.doReturn(getTestDeployment()) | ||
.when(jobImageDeployer) | ||
.scheduleJob( | ||
Mockito.any(), | ||
Mockito.any(), | ||
Mockito.any(), | ||
Mockito.anyBoolean(), | ||
Mockito.anyBoolean(), | ||
Mockito.anyString()); | ||
} | ||
|
||
@AfterEach | ||
public void cleanup() { | ||
jobsRepository.deleteAll(); | ||
} | ||
|
||
@Test | ||
public void testUpdateDesiredDeployment_expectNewDeployment() { | ||
var dataJob = ToModelApiConverter.toDataJob(TestUtils.getDataJob("teamName", "jobName")); | ||
jobsRepository.save(dataJob); | ||
JobDeployment jobDeployment = generateTestDeployment(); | ||
deploymentServiceV2.updateDesiredDbDeployment(dataJob, jobDeployment, "user"); | ||
|
||
Assertions.assertEquals(0, actualJobDeploymentRepository.findAll().size()); | ||
|
||
dataJobsSynchronizer.synchronizeDataJobs(); | ||
|
||
var actualDataJobDeployment = actualJobDeploymentRepository.findById(dataJob.getName()).get(); | ||
|
||
Assertions.assertEquals( | ||
jobDeployment.getDataJobName(), actualDataJobDeployment.getDataJobName()); | ||
Assertions.assertEquals( | ||
jobDeployment.getGitCommitSha(), actualDataJobDeployment.getGitCommitSha()); | ||
Assertions.assertEquals( | ||
jobDeployment.getPythonVersion(), actualDataJobDeployment.getPythonVersion()); | ||
Assertions.assertEquals("user", actualDataJobDeployment.getLastDeployedBy()); | ||
} | ||
|
||
@Test | ||
public void testUpdateDesiredDeployment_expectNoDeployment() { | ||
Mockito.doReturn(Set.of()) | ||
.when(deploymentServiceV2) | ||
.findAllActualDeploymentNamesFromKubernetes(); | ||
|
||
Assertions.assertEquals(0, actualJobDeploymentRepository.findAll().size()); | ||
|
||
dataJobsSynchronizer.synchronizeDataJobs(); | ||
|
||
Assertions.assertEquals(0, actualJobDeploymentRepository.findAll().size()); | ||
} | ||
|
||
private JobDeployment generateTestDeployment() { | ||
JobDeployment jobDeployment = new JobDeployment(); | ||
jobDeployment.setSchedule("testSched"); | ||
jobDeployment.setDataJobName("jobName"); | ||
jobDeployment.setDataJobTeam("teamName"); | ||
jobDeployment.setPythonVersion("testPython"); | ||
jobDeployment.setGitCommitSha("testSha"); | ||
jobDeployment.setEnabled(true); | ||
var resources = new DataJobResources(); | ||
resources.setCpuLimit(1f); | ||
resources.setCpuRequest(1f); | ||
resources.setMemoryLimit(1); | ||
resources.setMemoryRequest(1); | ||
jobDeployment.setResources(resources); | ||
return jobDeployment; | ||
} | ||
|
||
private ActualDataJobDeployment getTestDeployment() { | ||
var testDeployment = new ActualDataJobDeployment(); | ||
var testJob = generateTestDeployment(); | ||
testDeployment.setDataJobName(testJob.getDataJobName()); | ||
testDeployment.setDeploymentVersionSha(testJob.getGitCommitSha()); | ||
testDeployment.setPythonVersion(testJob.getPythonVersion()); | ||
testDeployment.setGitCommitSha(testJob.getGitCommitSha()); | ||
testDeployment.setLastDeployedDate(OffsetDateTime.now()); | ||
testDeployment.setResources(new DataJobDeploymentResources()); | ||
testDeployment.setLastDeployedBy("user"); | ||
return testDeployment; | ||
} | ||
} |
This file contains 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
80 changes: 80 additions & 0 deletions
80
...ontrol_service/src/test/java/com/vmware/taurus/service/deploy/JobImageDeployerV2Test.java
This file contains 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,80 @@ | ||
/* | ||
* Copyright 2021-2023 VMware, Inc. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.vmware.taurus.service.deploy; | ||
|
||
import com.vmware.taurus.ControlplaneApplication; | ||
import com.vmware.taurus.datajobs.TestUtils; | ||
import com.vmware.taurus.datajobs.ToModelApiConverter; | ||
import com.vmware.taurus.service.kubernetes.DataJobsKubernetesService; | ||
import com.vmware.taurus.service.model.ActualDataJobDeployment; | ||
import com.vmware.taurus.service.model.DataJob; | ||
import com.vmware.taurus.service.model.DataJobDeploymentResources; | ||
import com.vmware.taurus.service.model.DesiredDataJobDeployment; | ||
import io.kubernetes.client.openapi.ApiException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
|
||
@SpringBootTest(classes = ControlplaneApplication.class) | ||
public class JobImageDeployerV2Test { | ||
|
||
@Autowired private JobImageDeployerV2 jobImageDeployerV2; | ||
@SpyBean private DataJobsKubernetesService dataJobsKubernetesService; | ||
|
||
@Test | ||
public void testScheduleJob() throws ApiException { | ||
var dataJob = ToModelApiConverter.toDataJob(TestUtils.getDataJob("teamName", "jobName")); | ||
Mockito.doNothing().when(dataJobsKubernetesService).createCronJob(Mockito.any()); | ||
|
||
var actualDeployment = | ||
jobImageDeployerV2.scheduleJob( | ||
dataJob, getDesiredDeployment(dataJob), null, false, false, "test-job"); | ||
|
||
verifyActualDeployment(actualDeployment); | ||
} | ||
|
||
@Test | ||
public void testScheduleJob_presentInK8S() throws ApiException { | ||
var dataJob = ToModelApiConverter.toDataJob(TestUtils.getDataJob("teamName", "jobName")); | ||
Mockito.doNothing().when(dataJobsKubernetesService).updateCronJob(Mockito.any()); | ||
|
||
var actualDeployment = | ||
jobImageDeployerV2.scheduleJob( | ||
dataJob, getDesiredDeployment(dataJob), null, false, true, "test-job"); | ||
|
||
verifyActualDeployment(actualDeployment); | ||
} | ||
|
||
private void verifyActualDeployment(ActualDataJobDeployment actualDeployment) { | ||
Assertions.assertNotNull(actualDeployment); | ||
Assertions.assertNotNull(actualDeployment.getDeploymentVersionSha()); | ||
Assertions.assertEquals("jobName", actualDeployment.getDataJobName()); | ||
Assertions.assertEquals("testPython", actualDeployment.getPythonVersion()); | ||
Assertions.assertEquals("testSha", actualDeployment.getGitCommitSha()); | ||
Assertions.assertEquals("testSched", actualDeployment.getSchedule()); | ||
Assertions.assertNotNull(actualDeployment.getResources()); | ||
Assertions.assertEquals("user", actualDeployment.getLastDeployedBy()); | ||
Assertions.assertTrue(actualDeployment.getEnabled()); | ||
} | ||
|
||
private DesiredDataJobDeployment getDesiredDeployment(DataJob dataJob) { | ||
var initialDeployment = new DesiredDataJobDeployment(); | ||
initialDeployment.setDataJob(dataJob); | ||
initialDeployment.setDataJobName(dataJob.getName()); | ||
initialDeployment.setEnabled(true); | ||
initialDeployment.setResources(new DataJobDeploymentResources()); | ||
initialDeployment.setSchedule("testSched"); | ||
initialDeployment.setUserInitiated(true); | ||
initialDeployment.setGitCommitSha("testSha"); | ||
initialDeployment.setPythonVersion("testPython"); | ||
initialDeployment.setLastDeployedBy("user"); | ||
|
||
return initialDeployment; | ||
} | ||
} |