|
|
|
@ -18,8 +18,10 @@
|
|
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
|
|
|
|
|
|
import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; |
|
|
|
|
import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; |
|
|
|
|
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.DOWNLOAD_LOG; |
|
|
|
|
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.VIEW_LOG; |
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
|
|
import static org.mockito.Mockito.doNothing; |
|
|
|
|
import static org.mockito.Mockito.doThrow; |
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
@ -109,11 +111,25 @@ public class LoggerServiceTest {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public TaskInstanceLogFileDownloadResponse getTaskInstanceWholeLogFileBytes(TaskInstanceLogFileDownloadRequest taskInstanceLogFileDownloadRequest) { |
|
|
|
|
return new TaskInstanceLogFileDownloadResponse(new byte[0]); |
|
|
|
|
if (taskInstanceLogFileDownloadRequest.getTaskInstanceId() == 1) { |
|
|
|
|
return new TaskInstanceLogFileDownloadResponse(new byte[0]); |
|
|
|
|
} else if (taskInstanceLogFileDownloadRequest.getTaskInstanceId() == 10) { |
|
|
|
|
return new TaskInstanceLogFileDownloadResponse("log content".getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
throw new ServiceException("download error"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public TaskInstanceLogPageQueryResponse pageQueryTaskInstanceLog(TaskInstanceLogPageQueryRequest taskInstanceLogPageQueryRequest) { |
|
|
|
|
if (taskInstanceLogPageQueryRequest.getTaskInstanceId() != null) { |
|
|
|
|
if (taskInstanceLogPageQueryRequest.getTaskInstanceId() == 100) { |
|
|
|
|
throw new ServiceException("query log error"); |
|
|
|
|
} else if (taskInstanceLogPageQueryRequest.getTaskInstanceId() == 10) { |
|
|
|
|
return new TaskInstanceLogPageQueryResponse("log content"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return new TaskInstanceLogPageQueryResponse(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -177,6 +193,13 @@ public class LoggerServiceTest {
|
|
|
|
|
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); |
|
|
|
|
result = loggerService.queryLog(loginUser, 1, 1, 1); |
|
|
|
|
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); |
|
|
|
|
|
|
|
|
|
result = loggerService.queryLog(loginUser, 1, 0, 1); |
|
|
|
|
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); |
|
|
|
|
|
|
|
|
|
taskInstance.setLogPath(""); |
|
|
|
|
assertThrowsServiceException(Status.QUERY_TASK_INSTANCE_LOG_ERROR, |
|
|
|
|
() -> loggerService.queryLog(loginUser, 1, 1, 1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -237,9 +260,15 @@ public class LoggerServiceTest {
|
|
|
|
|
loginUser.setUserType(UserType.GENERAL_USER); |
|
|
|
|
TaskInstance taskInstance = new TaskInstance(); |
|
|
|
|
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); |
|
|
|
|
when(taskInstanceDao.queryById(10)).thenReturn(null); |
|
|
|
|
|
|
|
|
|
assertThrowsServiceException(Status.TASK_INSTANCE_NOT_FOUND, |
|
|
|
|
() -> loggerService.queryLog(loginUser, projectCode, 10, 1, 1)); |
|
|
|
|
|
|
|
|
|
TaskDefinition taskDefinition = new TaskDefinition(); |
|
|
|
|
taskDefinition.setProjectCode(projectCode); |
|
|
|
|
taskDefinition.setCode(1L); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
taskInstance.setTaskCode(1L); |
|
|
|
|
taskInstance.setId(1); |
|
|
|
@ -249,13 +278,27 @@ public class LoggerServiceTest {
|
|
|
|
|
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); |
|
|
|
|
when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); |
|
|
|
|
assertDoesNotThrow(() -> loggerService.queryLog(loginUser, projectCode, 1, 1, 1)); |
|
|
|
|
|
|
|
|
|
taskDefinition.setProjectCode(10); |
|
|
|
|
assertThrowsServiceException(Status.TASK_INSTANCE_NOT_FOUND, |
|
|
|
|
() -> loggerService.queryLog(loginUser, projectCode, 1, 1, 1)); |
|
|
|
|
|
|
|
|
|
taskDefinition.setProjectCode(1); |
|
|
|
|
taskInstance.setId(10); |
|
|
|
|
when(taskInstanceDao.queryById(10)).thenReturn(taskInstance); |
|
|
|
|
String result = loggerService.queryLog(loginUser, projectCode, 10, 1, 1); |
|
|
|
|
assertEquals("log content", result); |
|
|
|
|
|
|
|
|
|
taskInstance.setId(100); |
|
|
|
|
when(taskInstanceDao.queryById(100)).thenReturn(taskInstance); |
|
|
|
|
assertThrowsServiceException(Status.QUERY_TASK_INSTANCE_LOG_ERROR, |
|
|
|
|
() -> loggerService.queryLog(loginUser, projectCode, 10, 1, 1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testGetLogBytesInSpecifiedProject() { |
|
|
|
|
long projectCode = 1L; |
|
|
|
|
when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode)); |
|
|
|
|
Project project = getProject(projectCode); |
|
|
|
|
|
|
|
|
|
User loginUser = new User(); |
|
|
|
|
loginUser.setId(-1); |
|
|
|
@ -272,9 +315,24 @@ public class LoggerServiceTest {
|
|
|
|
|
taskInstance.setHost("127.0.0.1:" + nettyServerPort); |
|
|
|
|
taskInstance.setLogPath("/temp/log"); |
|
|
|
|
doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, DOWNLOAD_LOG); |
|
|
|
|
|
|
|
|
|
when(taskInstanceDao.queryById(1)).thenReturn(null); |
|
|
|
|
assertThrowsServiceException( |
|
|
|
|
Status.INTERNAL_SERVER_ERROR_ARGS, () -> loggerService.getLogBytes(loginUser, projectCode, 1)); |
|
|
|
|
|
|
|
|
|
when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); |
|
|
|
|
when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); |
|
|
|
|
assertDoesNotThrow(() -> loggerService.getLogBytes(loginUser, projectCode, 1)); |
|
|
|
|
|
|
|
|
|
taskDefinition.setProjectCode(2L); |
|
|
|
|
assertThrowsServiceException(Status.INTERNAL_SERVER_ERROR_ARGS, |
|
|
|
|
() -> loggerService.getLogBytes(loginUser, projectCode, 1)); |
|
|
|
|
|
|
|
|
|
taskDefinition.setProjectCode(1L); |
|
|
|
|
taskInstance.setId(100); |
|
|
|
|
when(taskInstanceDao.queryById(100)).thenReturn(taskInstance); |
|
|
|
|
assertThrowsServiceException(Status.DOWNLOAD_TASK_INSTANCE_LOG_FILE_ERROR, |
|
|
|
|
() -> loggerService.getLogBytes(loginUser, projectCode, 100)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|