use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.
the class NomadSchedulerTest method testOnKill.
@Test
public void testOnKill() throws Exception {
PowerMockito.mockStatic(NomadScheduler.class);
Scheduler.KillTopologyRequest killTopologyRequest = Scheduler.KillTopologyRequest.newBuilder().setTopologyName(TOPOLOGY_NAME).build();
scheduler.initialize(this.mockConfig, this.mockRuntime);
Job[] jobs = { new Job(), new Job(), new Job() };
List<Job> jobList = Arrays.asList(jobs);
// Fail to kill
PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "killJobs", Mockito.any(NomadApiClient.class), anyVararg());
Assert.assertFalse(scheduler.onKill(killTopologyRequest));
// Succeed to kill
PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
PowerMockito.doNothing().when(NomadScheduler.class, "killJobs", Mockito.any(NomadApiClient.class), anyVararg());
Assert.assertTrue(scheduler.onKill(killTopologyRequest));
}
use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.
the class NomadSchedulerTest method testOnRestart.
@Test
public void testOnRestart() throws Exception {
PowerMockito.mockStatic(NomadScheduler.class);
// Construct RestartTopologyRequest to restart container
Scheduler.RestartTopologyRequest restartTopologyRequest = Scheduler.RestartTopologyRequest.newBuilder().setTopologyName(TOPOLOGY_NAME).setContainerIndex(CONTAINER_INDEX).build();
scheduler.initialize(this.mockConfig, this.mockRuntime);
// Fail to restart
PowerMockito.when(NomadScheduler.getTopologyContainerJob(Mockito.any(NomadApiClient.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(new Job());
PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "restartJobs", Mockito.any(NomadApiClient.class), Mockito.any(Job.class));
Assert.assertFalse(scheduler.onRestart(restartTopologyRequest));
// Succeed to restart
PowerMockito.when(NomadScheduler.getTopologyContainerJob(Mockito.any(NomadApiClient.class), Mockito.anyString(), Mockito.anyInt())).thenReturn(new Job());
PowerMockito.doNothing().when(NomadScheduler.class, "restartJobs", Mockito.any(NomadApiClient.class), Mockito.any(Job.class));
Assert.assertTrue(scheduler.onRestart(restartTopologyRequest));
// Construct RestartTopologyRequest to restart whole topology
restartTopologyRequest = Scheduler.RestartTopologyRequest.newBuilder().setTopologyName(TOPOLOGY_NAME).setContainerIndex(-1).build();
scheduler.initialize(this.mockConfig, this.mockRuntime);
Job[] jobs = { new Job(), new Job(), new Job() };
List<Job> jobList = Arrays.asList(jobs);
// Fail to restart
PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "restartJobs", Mockito.any(), anyVararg());
Assert.assertFalse(scheduler.onRestart(restartTopologyRequest));
// Succeed to restart
PowerMockito.when(NomadScheduler.getTopologyJobs(Mockito.any(NomadApiClient.class), Mockito.anyString())).thenReturn(jobList);
PowerMockito.doNothing().when(NomadScheduler.class, "restartJobs", Mockito.any(NomadApiClient.class), anyVararg());
Assert.assertTrue(scheduler.onRestart(restartTopologyRequest));
}
use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.
the class NomadSchedulerTest method testGetJob.
@Test
public void testGetJob() {
Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
containers.add(Mockito.mock(PackingPlan.ContainerPlan.class));
PackingPlan.ContainerPlan containerPlan = new PackingPlan.ContainerPlan(CONTAINER_INDEX, new HashSet<>(), Mockito.mock(Resource.class));
Optional<PackingPlan.ContainerPlan> plan = Optional.of(containerPlan);
Resource resource = new Resource(CPU_RESOURCE, MEMORY_RESOURCE, DISK_RESOURCE);
scheduler.initialize(this.mockConfig, this.mockRuntime);
Mockito.doReturn(new TaskGroup()).when(scheduler).getTaskGroup(Mockito.anyString(), Mockito.anyInt(), Mockito.any());
Job job = scheduler.getJob(CONTAINER_INDEX, plan, resource);
LOG.info("job: " + job);
Assert.assertEquals(TOPOLOGY_ID + "-" + CONTAINER_INDEX, job.getId());
Assert.assertEquals(TOPOLOGY_NAME + "-" + CONTAINER_INDEX, job.getName());
Assert.assertArrayEquals(Arrays.asList(NomadConstants.NOMAD_DEFAULT_DATACENTER).toArray(), job.getDatacenters().toArray());
Assert.assertNotNull(job.getTaskGroups());
}
use of com.hashicorp.nomad.apimodel.Job in project heron by twitter.
the class NomadSchedulerTest method testOnSchedule.
@Test
public void testOnSchedule() throws Exception {
PowerMockito.mockStatic(NomadScheduler.class);
Mockito.doReturn(new LinkedList<>()).when(scheduler).getJobs(Mockito.any(PackingPlan.class));
scheduler.initialize(this.mockConfig, this.mockRuntime);
// Fail to schedule due to null PackingPlan
Assert.assertFalse(scheduler.onSchedule(null));
PackingPlan pplan = new PackingPlan(PACKING_PLAN_ID, new HashSet<>());
Assert.assertTrue(pplan.getContainers().isEmpty());
// Fail to schedule due to PackingPlan is empty
Assert.assertFalse(scheduler.onSchedule(pplan));
Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
containers.add(Mockito.mock(PackingPlan.ContainerPlan.class));
PackingPlan validPlan = new PackingPlan(PACKING_PLAN_ID, containers);
// Fail to submit due to client failure
Job[] jobs = { new Job(), new Job(), new Job() };
List<Job> jobList = Arrays.asList(jobs);
Mockito.doReturn(jobList).when(scheduler).getJobs(validPlan);
PowerMockito.doThrow(new RuntimeException()).when(NomadScheduler.class, "startJobs", Mockito.any(NomadApiClient.class), anyVararg());
Assert.assertFalse(scheduler.onSchedule(validPlan));
// Succeed
Mockito.doReturn(jobList).when(scheduler).getJobs(validPlan);
PowerMockito.doNothing().when(NomadScheduler.class, "startJobs", Mockito.any(NomadApiClient.class), anyVararg());
Assert.assertTrue(scheduler.onSchedule(validPlan));
}
use of com.hashicorp.nomad.apimodel.Job in project twister2 by DSC-SPIDAL.
the class NomadController method kill.
@Override
public boolean kill(JobAPI.Job job) {
String jobID = job.getJobId();
String uri = NomadContext.nomadSchedulerUri(config);
LOG.log(Level.INFO, "Killing Job " + jobID);
NomadApiClient nomadApiClient = new NomadApiClient(new NomadApiConfiguration.Builder().setAddress(uri).build());
try {
Job nomadJob = getRunningJob(nomadApiClient, job.getJobId());
if (nomadJob == null) {
LOG.log(Level.INFO, "Cannot find the running job: " + job.getJobId());
return false;
}
nomadApiClient.getJobsApi().deregister(nomadJob.getId());
} catch (RuntimeException | IOException | NomadException e) {
LOG.log(Level.SEVERE, "Failed to terminate job " + jobID + " with error: " + e.getMessage(), e);
return false;
} finally {
closeClient(nomadApiClient);
}
return true;
}
Aggregations