Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devabhishekpal committed Dec 30, 2024
1 parent 6f47856 commit b83f4d8
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,68 @@

*** Settings ***
Documentation Test to validate the recon task status API works properly
Library RequestsLibrary
Library Process
Library Collections
Library OperatingSystem
Library String
Library BuiltIn
Resource ../commonlib.robot
Resource ../ozone-lib/freon.robot
Test Timeout 10 minutes
Resource ../commonlib.robot
Test Timeout 5 minutes

*** Variables ***
${BASE_URL} http://localhost:8080
${TASK_STATUS_ENDPOINT} ${BASE_URL}/task/status
${TRIGGER_SYNC_ENDPOINT} ${BASE_URL}/triggerdbsync/om
${TASK_NAME_1} test-task-1
${TASK_NAME_2} test-task-2
${FREON_COMMAND} Freon DFSG -n=1000 -s=100000 -path=${PFS_PATH}
${BASE_URL} http://recon:9888
${TASK_STATUS_ENDPOINT} ${BASE_URL}/api/v1/task/status
${TRIGGER_SYNC_ENDPOINT} ${BASE_URL}/api/v1/triggerdbsync/om
${TASK_NAME_1} ContainerHealthTask
${TASK_NAME_2} OmDeltaRequest
${PFS_PATH} /tmp/test-recon-freon

*** Keywords ***
Kinit as ozone admin
Run Keyword Kinit test user testuser testuser.keytab

Freon DFSG
[arguments] ${prefix}=dfsg ${n}=1000 ${path}={PFS_PATH} ${size}=10240
${result} = Execute ozone freon dfsg -n ${n} -s ${size} --path ${path}

Fetch Task Status
${result} = Execute curl -H "Accepts: application/json" --negotiate -u : -LSs ${TASK_STATUS_ENDPOINT}
${parsed_response} = Evaluate json.loads("""${result}""") json
${tasks} = Evaluate [task for task in ${parsed_response}]
[return] ${tasks}

*** Test Cases ***

# Test Case 1: Prepopulate Data and Trigger OM DB Sync
Prepopulate Data and Trigger OM DB Sync
[Documentation] Use Freon to prepopulate the OM DB with data and trigger OM DB sync.
[Tags] prepopulate dbsync
Log To Console Running Freon to populate OM DB...
Run Process ${FREON_COMMAND} shell=True cwd=/tmp
Log To Console Freon completed successfully.
Freon DFSG

Log To Console Triggering OM DB Sync...
${response}= GET ${TRIGGER_SYNC_ENDPOINT}
Should Be Equal As Integers ${response.status_code} 200
Should Be True ${response.json()} # Sync should return true if successful
Log To Console OM DB Sync triggered successfully.
${result} = Execute curl --negotiate -u : -LSs ${TRIGGER_SYNC_ENDPOINT}
Should contain ${result} true # Sync should return true if successful

# Test Case 2: Validate Task Status After Sync
Validate Task Status After Sync
[Documentation] Validate that task status is updated after triggering the OM DB sync.
[Tags] dbsync validation
Log To Console Fetching task status after OM DB sync...
${response}= GET ${TASK_STATUS_ENDPOINT}
Should Be Equal As Integers ${response.status_code} 200
${tasks}= Evaluate [task for task in ${response.json()}]
${tasks} = Fetch Task Status
Should Not Be Empty ${tasks}

Log To Console Validating task statistics...
# Loop through tasks and check key properties
FOR ${task} IN @{tasks}
Dictionary Should Contain Key ${task} taskName
Dictionary Should Contain Key ${task} successCount
Dictionary Should Contain Key ${task} failureCount
Dictionary Should Contain Key ${task} lastUpdatedTimestamp
Dictionary Should Contain Key ${task} isCurrentTaskRunning
Dictionary Should Contain Key ${task} lastTaskRunStatus

Log To Console Task: ${task["taskName"]}, SuccessCount: ${task["successCount"]}, FailureCount: ${task["failureCount"]}
END

# Test Case 3: Validate Success and Failure Counters for Specific Task
Validate Counters for Specific Task
[Documentation] Validate success and failure counters for a specific task after OM DB sync.
[Tags] validation counters
Log To Console Fetching task stats for ${TASK_NAME_1}...
${response}= GET ${TASK_STATUS_ENDPOINT}
Should Be Equal As Integers ${response.status_code} 200
${tasks} = Fetch Task Status

${task_list}= Evaluate [task for task in ${response.json()} if task["taskName"] == "${TASK_NAME_1}"]
${task_list} = Evaluate [task for task in ${tasks} if task["taskName"] == "${TASK_NAME_1}"]
Should Be Equal As Integers ${len(task_list)} 1

${task}= Get From List ${task_list} 0
Log To Console Task Stats: ${task}
${task} = Get From List ${task_list} 0

# Validate success and failure counters
Should Be Greater Than Or Equal As Integers ${task["successCount"]} 0
Expand All @@ -96,19 +86,13 @@ Validate Counters for Specific Task
Should Not Be None ${task["lastUpdatedTimestamp"]}
Should Not Be None ${task["lastUpdatedSeqNumber"]}

# Test Case 4: Validate All Tasks Updated After Sync
Validate All Tasks Updated After Sync
[Documentation] Ensure all tasks have been updated after an OM DB sync operation.
[Tags] dbsync validation
Log To Console Fetching all task stats...
${response}= GET ${TASK_STATUS_ENDPOINT}
Should Be Equal As Integers ${response.status_code} 200
${tasks}= Evaluate [task for task in ${response.json()}]
${tasks} = Fetch Task Status
Should Not Be Empty ${tasks}

FOR ${task} IN @{tasks}
Log To Console Validating task: ${task["taskName"]}
Should Not Be None ${task["lastUpdatedTimestamp"]}
Should Not Be None ${task["lastUpdatedSeqNumber"]}
Should Be Greater Than Or Equal As Integers ${task["successCount"]} 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public final class ReconServerConfigKeys {
OZONE_RECON_SCM_CLIENT_FAILOVER_MAX_RETRY_DEFAULT = 3;

public static final String OZONE_RECON_TASK_STATUS_COUNTER_CYCLES_LIMIT =
"ozone.recon.task.status.counter.duration";
"ozone.recon.task.status.counter.cycles.limit";

public static final int OZONE_RECON_TASK_STATUS_COUNTER_CYCLES_LIMIT_DEFAULT = 5;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.hadoop.ozone.recon.metrics;

import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
Expand Down Expand Up @@ -64,6 +65,11 @@ public ReconTaskStatusCounter(OzoneConfiguration conf) {
timeoutDuration = taskSyncInterval * countCycles;
}

@VisibleForTesting
public long getTimeoutDuration() {
return this.timeoutDuration;
}

/**
* Checks if the counter has exceeded the number of OM DB sync cycles for which it is configured
* to store the count. <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.inject.AbstractModule;
import com.google.inject.Injector;

import org.apache.hadoop.ozone.recon.ReconTestInjector;
import org.apache.hadoop.ozone.recon.api.types.ReconTaskStatusResponse;
import org.apache.hadoop.ozone.recon.api.types.ReconTaskStatusStat;
import org.apache.hadoop.ozone.recon.metrics.ReconTaskStatusCounter;
Expand All @@ -34,10 +35,12 @@
import org.hadoop.ozone.recon.schema.tables.pojos.ReconTaskStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import javax.ws.rs.core.Response;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -47,6 +50,9 @@
* Test for Task Status Service.
*/
public class TestTaskStatusService extends AbstractReconSqlDBTest {

@TempDir
private Path temporaryFolder;
private TaskStatusService taskStatusService;

public TestTaskStatusService() {
Expand Down Expand Up @@ -94,19 +100,31 @@ public void testGetTaskTimes(int lastTaskRunStatus) {

@Test
@SuppressWarnings("unchecked")
public void testTaskStatistics() {
public void testTaskStatistics() throws Exception {
ReconTaskStatusDao reconTaskStatusDao = getDao(ReconTaskStatusDao.class);
String taskName = "DummyTask_" + System.currentTimeMillis();

ReconTaskStatus reconTaskStatusRecord = new ReconTaskStatus(
taskName, System.currentTimeMillis(), 0L, 0, 0);
reconTaskStatusDao.insert(reconTaskStatusRecord);

ReconTaskStatusCounter taskStatusCounter = mock(ReconTaskStatusCounter.class);
ReconTaskStatusCounter mockedTaskStatusCounter = mock(ReconTaskStatusCounter.class);
ReconTaskStatusStat mockedTaskStats = new ReconTaskStatusStat(new AtomicInteger(10), new AtomicInteger(2));
String taskName = "DummyTask_" + System.currentTimeMillis();
when(mockedTaskStatusCounter.getTaskStatsFor(anyString())).thenReturn(mockedTaskStats);

when(taskStatusCounter.getTaskStatsFor(anyString())).thenReturn(mockedTaskStats);
ReconTestInjector testInjector = new ReconTestInjector.Builder(temporaryFolder.toFile())
.withReconSqlDb()
.addBinding(ReconTaskStatusCounter.class, mockedTaskStatusCounter)
.addBinding(TaskStatusService.class)
.build();

taskStatusService = testInjector.getInstance(TaskStatusService.class);
Response response = taskStatusService.getTaskStats();

List<ReconTaskStatusResponse> tasks = (List<ReconTaskStatusResponse>) response.getEntity();
assertEquals(tasks.size(), 1);
assertEquals(tasks.get(0).getTaskName(), taskName);
assertEquals(tasks.get(0).getSuccessCount(), 10);
assertEquals(tasks.get(0).getFailureCount(), 2);
assertEquals(1, tasks.size());
assertEquals(taskName, tasks.get(0).getTaskName());
assertEquals(10, tasks.get(0).getSuccessCount());
assertEquals(2, tasks.get(0).getFailureCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -566,19 +566,14 @@ public void testMissingAndEmptyMissingContainerDeletion() throws Exception {
});
}

private ReconTaskStatusDao getTaskStatusDao() {
return getDao(ReconTaskStatusDao.class);
}

private ReconTaskStatusCounter getMockTaskStatusCounter() {
return mock(ReconTaskStatusCounter.class);
}

private ReconTaskStatusUpdaterManager getMockTaskStatusUpdaterManager() {
ReconTaskStatusUpdaterManager reconTaskStatusUpdaterManager = mock(ReconTaskStatusUpdaterManager.class);
when(reconTaskStatusUpdaterManager.getTaskStatusUpdater(anyString())).thenReturn(new ReconTaskStatusUpdater(
getDao(ReconTaskStatusDao.class), mock(ReconTaskStatusCounter.class),
"mockedTask-" + System.currentTimeMillis()));
when(reconTaskStatusUpdaterManager.getTaskStatusUpdater(anyString())).thenAnswer(inv -> {
String taskName = inv.getArgument(0);
return new ReconTaskStatusUpdater(
getDao(ReconTaskStatusDao.class), mock(ReconTaskStatusCounter.class),
taskName);
});
return reconTaskStatusUpdaterManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.hadoop.ozone.recon.api.types.ReconTaskStatusStat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -35,7 +37,7 @@ public class TestReconTaskStatusCounter {
void setup() {
OzoneConfiguration config = new OzoneConfiguration();
config.setInt("ozone.recon.task.status.counter.cycles.limit", 5);
config.setLong("ozone.recon.om.snapshot.task.interval.delay", 60000); // 1 min
config.setStrings("ozone.recon.om.snapshot.task.interval.delay", "1m"); // 1 min
counter = new ReconTaskStatusCounter(config);
}

Expand All @@ -59,11 +61,13 @@ void testUpdateCounterFailure() {
void testCounterExpiration() throws InterruptedException {
OzoneConfiguration config = new OzoneConfiguration();
config.setInt("ozone.recon.task.status.counter.cycles.limit", 1);
config.setLong("ozone.recon.om.snapshot.task.interval.delay", 100); // 100 ms
config.set("ozone.recon.om.snapshot.task.interval.delay", "100ms");
ReconTaskStatusCounter shortTTLCounter = new ReconTaskStatusCounter(config);
Logger log = LoggerFactory.getLogger(TestReconTaskStatusCounter.class);
log.info("Timeout duration: {}", shortTTLCounter.getTimeoutDuration());

shortTTLCounter.updateCounter("task1", true);
Thread.sleep(200); // Wait for expiration
Thread.sleep(200); // Wait for expiration for 1m 5s

shortTTLCounter.updateCounter("task1", false);
ReconTaskStatusStat stat = shortTTLCounter.getTaskStatsFor("task1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void testSchemaCreated() throws Exception {
Types.BIGINT));
expectedPairs.add(new ImmutablePair<>("last_updated_seq_number",
Types.BIGINT));
expectedPairs.add(new ImmutablePair<>("last_task_run_status",
Types.INTEGER));
expectedPairs.add(new ImmutablePair<>("is_current_task_running",
Types.INTEGER));

List<Pair<String, Integer>> actualPairs = new ArrayList<>();

Expand Down
Loading

0 comments on commit b83f4d8

Please sign in to comment.