|
|
@ -16,37 +16,95 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.api.ApiApplicationServer; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.api.enums.Status; |
|
|
|
import org.apache.dolphinscheduler.api.enums.Status; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.api.log.LogClient; |
|
|
|
import org.apache.dolphinscheduler.api.utils.Result; |
|
|
|
import org.apache.dolphinscheduler.api.utils.Result; |
|
|
|
import org.apache.dolphinscheduler.common.enums.UserType; |
|
|
|
import org.apache.dolphinscheduler.dao.ProcessDao; |
|
|
|
import org.apache.dolphinscheduler.dao.entity.User; |
|
|
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Assert; |
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
|
|
|
|
import org.mockito.InjectMocks; |
|
|
|
|
|
|
|
import org.mockito.Mock; |
|
|
|
|
|
|
|
import org.mockito.Mockito; |
|
|
|
|
|
|
|
import org.powermock.api.mockito.PowerMockito; |
|
|
|
|
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
|
|
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class) |
|
|
|
@RunWith(PowerMockRunner.class) |
|
|
|
@SpringBootTest(classes = ApiApplicationServer.class) |
|
|
|
@PrepareForTest({LoggerService.class}) |
|
|
|
public class LoggerServiceTest { |
|
|
|
public class LoggerServiceTest { |
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LoggerServiceTest.class); |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LoggerServiceTest.class); |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@InjectMocks |
|
|
|
private LoggerService loggerService; |
|
|
|
private LoggerService loggerService; |
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
private ProcessDao processDao; |
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
private LogClient logClient; |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Before |
|
|
|
public void queryDataSourceList(){ |
|
|
|
public void setUp() { |
|
|
|
|
|
|
|
|
|
|
|
User loginUser = new User(); |
|
|
|
try { |
|
|
|
loginUser.setId(27); |
|
|
|
PowerMockito.whenNew(LogClient.class).withAnyArguments().thenReturn(logClient); |
|
|
|
loginUser.setUserType(UserType.GENERAL_USER); |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
logger.error("setUp error: {}",e.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Result result = loggerService.queryLog(-1, 0, 100); |
|
|
|
@Test |
|
|
|
|
|
|
|
public void testQueryDataSourceList(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TaskInstance taskInstance = new TaskInstance(); |
|
|
|
|
|
|
|
Mockito.when(processDao.findTaskInstanceById(1)).thenReturn(taskInstance); |
|
|
|
|
|
|
|
Result result = loggerService.queryLog(2,1,1); |
|
|
|
|
|
|
|
//TASK_INSTANCE_NOT_FOUND
|
|
|
|
Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(),result.getCode().intValue()); |
|
|
|
Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(),result.getCode().intValue()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//HOST NOT FOUND
|
|
|
|
|
|
|
|
result = loggerService.queryLog(1,1,1); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(),result.getCode().intValue()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//SUCCESS
|
|
|
|
|
|
|
|
taskInstance.setHost("127.0.0.1"); |
|
|
|
|
|
|
|
taskInstance.setLogPath("/temp/log"); |
|
|
|
|
|
|
|
Mockito.when(logClient.rollViewLog("/temp/log",1,1 )).thenReturn("test"); |
|
|
|
|
|
|
|
Mockito.when(processDao.findTaskInstanceById(1)).thenReturn(taskInstance); |
|
|
|
|
|
|
|
result = loggerService.queryLog(1,1,1); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testGetLogBytes(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TaskInstance taskInstance = new TaskInstance(); |
|
|
|
|
|
|
|
Mockito.when(processDao.findTaskInstanceById(1)).thenReturn(taskInstance); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//task instance is null
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
loggerService.getLogBytes(2); |
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
|
|
|
|
|
logger.error("testGetLogBytes error: {}","task instance is null"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//task instance host is null
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
|
|
|
loggerService.getLogBytes(1); |
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
|
|
|
|
|
logger.error("testGetLogBytes error: {}","task instance host is null"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//success
|
|
|
|
|
|
|
|
Mockito.when(logClient.getLogBytes("/temp/log")).thenReturn(new byte[]{}); |
|
|
|
|
|
|
|
taskInstance.setHost("127.0.0.1"); |
|
|
|
|
|
|
|
taskInstance.setLogPath("/temp/log"); |
|
|
|
|
|
|
|
byte [] result = loggerService.getLogBytes(1); |
|
|
|
|
|
|
|
Assert.assertEquals(0,result.length); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |