Skip to content
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

Use @DataMongoTest and @Testcontainers annotations in replicates and compute logs tests #662

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ All notable changes to this project will be documented in this file.
- Filter out `CONTRIBUTE_AND_FINALIZE` tasks when detecting missed `REVEALED` status update. (#658)
- Fetch `results` on-chain when updating a replicate status in `CONTRIBUTE_AND_FINALIZE` workflow. (#659 #660)

### Quality

- Use `@DataMongoTest` and `@Testcontainers` annotations in replicates and compute logs tests. (#662)


## [[8.3.0]](https://github.com/iExecBlockchainComputing/iexec-core/releases/tag/v8.3.0) 2024-01-11

### New Features
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ tasks.named("bootJar") {

test {
useJUnitPlatform()
systemProperty "mongo.image", "mongo:4.4.28-focal"
}

tasks.register('itest') {
Expand Down
59 changes: 36 additions & 23 deletions src/test/java/com/iexec/core/logs/TaskLogsServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
import com.iexec.common.replicate.ComputeLogs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.util.Collections;
import java.util.List;
Expand All @@ -31,32 +35,46 @@
import static com.iexec.commons.poco.utils.TestUtils.CHAIN_TASK_ID;
import static com.iexec.commons.poco.utils.TestUtils.WORKER_ADDRESS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.spy;

@DataMongoTest
@Testcontainers
class TaskLogsServiceTests {

private static final String STDOUT = "This is an stdout string";
private static final String STDERR = "This is an stderr string";

@Mock
private TaskLogsRepository taskLogsRepository;
@InjectMocks
private TaskLogsService taskLogsService;
@Container
private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(System.getProperty("mongo.image")));

@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.mongodb.host", mongoDBContainer::getHost);
registry.add("spring.data.mongodb.port", () -> mongoDBContainer.getMappedPort(27017));
}

private final TaskLogsRepository taskLogsRepository;
private final TaskLogsService taskLogsService;

@Autowired
public TaskLogsServiceTests(TaskLogsRepository taskLogsRepository) {
this.taskLogsRepository = taskLogsRepository;
this.taskLogsService = new TaskLogsService(taskLogsRepository);
spy(taskLogsRepository);
}

@BeforeEach
void init() {
MockitoAnnotations.openMocks(this);
taskLogsRepository.deleteAll();
}

//region addComputeLogs
@Test
void shouldAddComputeLogs() {
final ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR);

ArgumentCaptor<TaskLogs> argumentCaptor = ArgumentCaptor.forClass(TaskLogs.class);
taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs);
verify(taskLogsRepository, times(1)).save(argumentCaptor.capture());
TaskLogs capturedEvent = argumentCaptor.getAllValues().get(0);
assertThat(taskLogsRepository.count()).isOne();
TaskLogs capturedEvent = taskLogsRepository.findOneByChainTaskId(CHAIN_TASK_ID).orElseThrow();
assertThat(capturedEvent.getComputeLogsList().get(0).getStdout()).isEqualTo(STDOUT);
assertThat(capturedEvent.getComputeLogsList().get(0).getStderr()).isEqualTo(STDERR);
assertThat(capturedEvent.getComputeLogsList().get(0).getWalletAddress()).isEqualTo(WORKER_ADDRESS);
Expand All @@ -65,7 +83,7 @@ void shouldAddComputeLogs() {
@Test
void shouldNotAddComputeLogsSinceNull() {
taskLogsService.addComputeLogs(CHAIN_TASK_ID, null);
verifyNoInteractions(taskLogsRepository);
assertThat(taskLogsRepository.count()).isZero();
}

@Test
Expand All @@ -75,14 +93,10 @@ void shouldNotAddComputeLogsSinceLogsAlreadyKnown() {
.chainTaskId(CHAIN_TASK_ID)
.computeLogsList(Collections.singletonList(computeLogs))
.build();

when(taskLogsService.getTaskLogs(CHAIN_TASK_ID))
.thenReturn(Optional.of(taskLogs));
taskLogsRepository.save(taskLogs);

taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs);

verify(taskLogsRepository).findOneByChainTaskId(CHAIN_TASK_ID);
verify(taskLogsRepository, times(0)).save(any());
assertThat(taskLogsRepository.count()).isOne();
}
//endregion

Expand All @@ -94,8 +108,7 @@ void shouldGetComputeLogs() {
.chainTaskId(CHAIN_TASK_ID)
.computeLogsList(List.of(computeLogs))
.build();
when(taskLogsRepository.findByChainTaskIdAndWalletAddress(CHAIN_TASK_ID, WORKER_ADDRESS))
.thenReturn(Optional.of(taskLogs));
taskLogsRepository.save(taskLogs);
Optional<ComputeLogs> optional = taskLogsService.getComputeLogs(CHAIN_TASK_ID, WORKER_ADDRESS);
assertThat(optional).isPresent();
final ComputeLogs actualLogs = optional.get();
Expand Down
Loading