Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@
import com.flow.platform.api.dao.user.UserDao;
import com.flow.platform.api.dao.user.UserFlowDao;
import com.flow.platform.api.dao.user.UserRoleDao;
import com.flow.platform.api.domain.job.Job;
import com.flow.platform.api.domain.job.JobCategory;
import com.flow.platform.api.domain.job.JobStatus;
import com.flow.platform.api.domain.job.NodeResult;
import com.flow.platform.api.domain.job.NodeStatus;
import com.flow.platform.api.domain.job.NodeTag;
import com.flow.platform.api.domain.node.Node;
import com.flow.platform.api.domain.node.NodeTree;
import com.flow.platform.api.domain.user.User;
import com.flow.platform.api.envs.FlowEnvs;
import com.flow.platform.api.envs.FlowEnvs.StatusValue;
import com.flow.platform.api.envs.FlowEnvs.YmlStatusValue;
import com.flow.platform.api.envs.GitEnvs;
import com.flow.platform.api.envs.JobEnvs;
import com.flow.platform.api.initializers.Initializer;
import com.flow.platform.api.service.job.JobNodeService;
import com.flow.platform.api.service.job.JobSearchService;
import com.flow.platform.api.service.job.JobService;
import com.flow.platform.api.service.job.NodeResultService;
Expand All @@ -58,10 +67,12 @@
import java.net.URL;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
Expand Down Expand Up @@ -164,6 +175,9 @@ public abstract class TestBase {
@Autowired
private User superUser;

@Autowired
private JobNodeService jobNodeService;

@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);

Expand Down Expand Up @@ -294,4 +308,36 @@ public static void afterClass() throws IOException {
FileSystemUtils.deleteRecursively(WORKSPACE.toFile());
}

protected Job createMockJob(String nodePath) {
// create job
Job job = jobService.createFromFlowYml(nodePath, JobCategory.TAG, null, mockUser);
Assert.assertNotNull(job.getId());
Assert.assertNotNull(job.getSessionId());
Assert.assertNotNull(job.getNumber());
Assert.assertEquals(mockUser.getEmail(), job.getCreatedBy());
Assert.assertEquals(JobStatus.SESSION_CREATING, job.getStatus());

Assert.assertEquals(job.getNumber().toString(), job.getEnv(JobEnvs.FLOW_JOB_BUILD_NUMBER));

// verify root node result for job
NodeResult rootResult = job.getRootResult();
Assert.assertNotNull(rootResult);
Assert.assertEquals(NodeTag.FLOW, rootResult.getNodeTag());
Assert.assertNotNull(rootResult.getOutputs());
Assert.assertEquals(NodeStatus.PENDING, rootResult.getStatus());

NodeTree nodeTree = jobNodeService.get(job);

// verify child node result list
List<NodeResult> childrenResult = job.getChildrenResult();
Assert.assertNotNull(childrenResult);
Assert.assertEquals(nodeTree.childrenSize(), childrenResult.size());

return job;
}

protected Job reload(Job job) {
return jobService.find(job.getNodePath(), job.getNumber());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void should_on_callback_with_failure_but_allow_failure() {
// TODO:
}

private Job reload(Job job) {
protected Job reload(Job job) {
return jobService.find(job.getNodePath(), job.getNumber());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,35 @@

package com.flow.platform.api.test.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.flow.platform.api.domain.CmdCallbackQueueItem;
import com.flow.platform.api.domain.job.Job;
import com.flow.platform.api.domain.job.NodeStatus;
import com.flow.platform.api.domain.node.Node;
import com.flow.platform.api.domain.node.NodeTree;
import com.flow.platform.api.domain.response.BooleanValue;
import com.flow.platform.api.envs.FlowEnvs;
import com.flow.platform.api.envs.GitEnvs;
import com.flow.platform.api.service.job.JobNodeService;
import com.flow.platform.api.util.CommonUtil;
import com.flow.platform.api.util.PathUtil;
import com.flow.platform.core.exception.NotFoundException;
import com.flow.platform.core.response.ResponseError;
import com.flow.platform.domain.Cmd;
import com.flow.platform.domain.CmdStatus;
import com.flow.platform.domain.CmdType;
import com.flow.platform.util.StringUtil;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult;
Expand All @@ -45,6 +57,9 @@ public class FlowControllerTest extends ControllerTestWithoutAuth {

private final String flowName = "flow_default";

@Autowired
private JobNodeService jobNodeService;

@Before
public void initToCreateEmptyFlow() throws Throwable {
MockHttpServletRequestBuilder request = post("/flows/" + flowName)
Expand All @@ -55,6 +70,7 @@ public void initToCreateEmptyFlow() throws Throwable {
Assert.assertNotNull(flowNode);
Assert.assertNotNull(flowNode.getEnv(GitEnvs.FLOW_GIT_WEBHOOK));
Assert.assertEquals("PENDING", flowNode.getEnv(FlowEnvs.FLOW_STATUS));
stubDemo();
}

@Test
Expand All @@ -68,6 +84,48 @@ public void should_get_flow_node_detail() throws Throwable {
Assert.assertEquals(flowName, flowNode.getName());
}

@Test(expected = NotFoundException.class)
public void should_delete_flow_success() throws Exception {
String flow = "flow1";

// mock user
setCurrentUser(mockUser);

Node rootForFlow = createRootFlow(flow, "demo_flow2.yaml");
Job job = createMockJob(rootForFlow.getPath());

NodeTree nodeTree = nodeService.find("flow1");
Node stepFirst = nodeTree.find(flow + "/step1");

// first create session success
final String sessionId = CommonUtil.randomId().toString();
Cmd cmd = new Cmd("default", null, CmdType.CREATE_SESSION, null);
cmd.setSessionId(sessionId);
cmd.setStatus(CmdStatus.SENT);
jobService.callback(new CmdCallbackQueueItem(job.getId(), cmd));
job = reload(job);

// first step should running
cmd = new Cmd("default", null, CmdType.RUN_SHELL, stepFirst.getScript());
cmd.setStatus(CmdStatus.RUNNING);
cmd.setType(CmdType.RUN_SHELL);
cmd.setExtra(stepFirst.getPath());
jobService.callback(new CmdCallbackQueueItem(job.getId(), cmd));

job = reload(job);
Assert.assertEquals(NodeStatus.RUNNING, job.getRootResult().getStatus());

// delete flow
MvcResult result = mockMvc.perform(delete("/flows/" + flow))
.andExpect(status().isOk())
.andReturn();

Assert.assertNotNull(result.getResponse());

// job should delete
reload(job);
}

@Test
public void should_get_env_value() throws Throwable {
MockHttpServletRequestBuilder request = get("/flows/" + flowName + "/env")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import com.flow.platform.api.domain.job.JobStatus;
import com.flow.platform.api.domain.job.NodeResult;
import com.flow.platform.api.domain.job.NodeStatus;
import com.flow.platform.api.domain.job.NodeTag;
import com.flow.platform.api.domain.node.Node;
import com.flow.platform.api.domain.node.NodeTree;
import com.flow.platform.api.envs.JobEnvs;
import com.flow.platform.api.service.job.JobNodeService;
import com.flow.platform.api.test.TestBase;
import com.flow.platform.api.util.CommonUtil;
import com.flow.platform.core.exception.IllegalStatusException;
import com.flow.platform.domain.Cmd;
import com.flow.platform.domain.CmdResult;
Expand Down Expand Up @@ -224,6 +223,44 @@ public void should_stop_success() throws IOException {
Assert.assertEquals(NodeStatus.STOPPED, stoppedJob.getRootResult().getStatus());
}

@Test
public void should_stop_running_job_success() throws IOException {

// init flow
Node rootForFlow = createRootFlow("flow1", "demo_flow2.yaml");
NodeTree nodeTree = nodeService.find("flow1");
Node stepFirst = nodeTree.find("flow1/step1");

// create job
Job job = createMockJob(rootForFlow.getPath());

// mock callback cmd
final String sessionId = CommonUtil.randomId().toString();
Cmd cmd = new Cmd("default", null, CmdType.CREATE_SESSION, null);
cmd.setSessionId(sessionId);
cmd.setStatus(CmdStatus.SENT);
jobService.callback(new CmdCallbackQueueItem(job.getId(), cmd));
job = reload(job);

// first step should running
cmd = new Cmd("default", null, CmdType.RUN_SHELL, stepFirst.getScript());
cmd.setStatus(CmdStatus.RUNNING);
cmd.setType(CmdType.RUN_SHELL);
cmd.setExtra(stepFirst.getPath());
jobService.callback(new CmdCallbackQueueItem(job.getId(), cmd));

// job should running
job = reload(job);
Assert.assertEquals(NodeStatus.RUNNING, job.getRootResult().getStatus());

// job should stop
Job stoppedJob = jobService.stop(job.getNodeName(), job.getNumber());
Assert.assertNotNull(stoppedJob);
stoppedJob = jobService.find(stoppedJob.getId());
Assert.assertEquals(NodeStatus.STOPPED, stoppedJob.getRootResult().getStatus());
}


@Test
public void should_job_time_out_and_reject_callback() throws IOException, InterruptedException {
Node rootForFlow = createRootFlow("flow1", "demo_flow2.yaml");
Expand Down Expand Up @@ -266,36 +303,4 @@ public void should_get_latest_job_by_node_path() throws IOException {
Assert.assertEquals(1, jobs.size());
Assert.assertEquals("2", jobs.get(0).getNumber().toString());
}

private Job createMockJob(String nodePath) {
// create job
Job job = jobService.createFromFlowYml(nodePath, JobCategory.TAG, null, mockUser);
Assert.assertNotNull(job.getId());
Assert.assertNotNull(job.getSessionId());
Assert.assertNotNull(job.getNumber());
Assert.assertEquals(mockUser.getEmail(), job.getCreatedBy());
Assert.assertEquals(JobStatus.SESSION_CREATING, job.getStatus());

Assert.assertEquals(job.getNumber().toString(), job.getEnv(JobEnvs.FLOW_JOB_BUILD_NUMBER));

// verify root node result for job
NodeResult rootResult = job.getRootResult();
Assert.assertNotNull(rootResult);
Assert.assertEquals(NodeTag.FLOW, rootResult.getNodeTag());
Assert.assertNotNull(rootResult.getOutputs());
Assert.assertEquals(NodeStatus.PENDING, rootResult.getStatus());

NodeTree nodeTree = jobNodeService.get(job);

// verify child node result list
List<NodeResult> childrenResult = job.getChildrenResult();
Assert.assertNotNull(childrenResult);
Assert.assertEquals(nodeTree.childrenSize(), childrenResult.size());

return job;
}

private Job reload(Job job) {
return jobService.find(job.getNodePath(), job.getNumber());
}
}