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 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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
98 changes: 68 additions & 30 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";
private static final ComputeLogs COMPUTE_LOGS = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR);

@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);
taskLogsService.addComputeLogs(CHAIN_TASK_ID, COMPUTE_LOGS);
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,37 +83,57 @@ void shouldAddComputeLogs() {
@Test
void shouldNotAddComputeLogsSinceNull() {
taskLogsService.addComputeLogs(CHAIN_TASK_ID, null);
verifyNoInteractions(taskLogsRepository);
assertThat(taskLogsRepository.count()).isZero();
}

@Test
void shouldNotAddComputeLogsSinceLogsAlreadyKnown() {
final ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR);
final TaskLogs taskLogs = TaskLogs.builder()
.chainTaskId(CHAIN_TASK_ID)
.computeLogsList(Collections.singletonList(computeLogs))
.computeLogsList(Collections.singletonList(COMPUTE_LOGS))
.build();
taskLogsRepository.save(taskLogs);
assertThat(taskLogs.containsWalletAddress(WORKER_ADDRESS)).isTrue();
taskLogsService.addComputeLogs(CHAIN_TASK_ID, COMPUTE_LOGS);
assertThat(taskLogsRepository.count()).isOne();
assertThat(taskLogsRepository.findOneByChainTaskId(CHAIN_TASK_ID)).contains(taskLogs);
}
//endregion

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

taskLogsService.addComputeLogs(CHAIN_TASK_ID, computeLogs);
//region delete
@Test
void shouldDeleteKnownTask() {
final TaskLogs taskLogs = TaskLogs.builder()
.chainTaskId(CHAIN_TASK_ID)
.computeLogsList(List.of(COMPUTE_LOGS))
.build();
taskLogsRepository.save(taskLogs);
assertThat(taskLogsRepository.count()).isOne();
taskLogsService.delete(List.of(CHAIN_TASK_ID));
assertThat(taskLogsRepository.count()).isZero();
}

verify(taskLogsRepository).findOneByChainTaskId(CHAIN_TASK_ID);
verify(taskLogsRepository, times(0)).save(any());
@Test
void shouldNotDeleteUnknownTask() {
final TaskLogs taskLogs = TaskLogs.builder()
.chainTaskId(CHAIN_TASK_ID)
.computeLogsList(List.of(COMPUTE_LOGS))
.build();
taskLogsRepository.save(taskLogs);
assertThat(taskLogsRepository.count()).isOne();
taskLogsService.delete(List.of("0x00"));
assertThat(taskLogsRepository.count()).isOne();
}
//endregion

//region getComputeLogs
@Test
void shouldGetComputeLogs() {
ComputeLogs computeLogs = new ComputeLogs(WORKER_ADDRESS, STDOUT, STDERR);
TaskLogs taskLogs = TaskLogs.builder()
final TaskLogs taskLogs = TaskLogs.builder()
.chainTaskId(CHAIN_TASK_ID)
.computeLogsList(List.of(computeLogs))
.computeLogsList(List.of(COMPUTE_LOGS))
.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