private AsyncJob<?> refresh(String jobId) { return client.getAsyncJobApi().getAsyncJob(jobId); } }
@Test(enabled = true) public void testListAsyncJobs() throws Exception { Set<AsyncJob<?>> response = client.getAsyncJobApi().listAsyncJobs(); assert null != response; long asyncJobCount = response.size(); assertTrue(asyncJobCount >= 0); for (AsyncJob<?> asyncJob : response) { assert asyncJob.getCmd() != null : asyncJob; assert asyncJob.getUserId() != null : asyncJob; checkJob(asyncJob); AsyncJob<?> query = client.getAsyncJobApi().getAsyncJob(asyncJob.getId()); assertEquals(query.getId(), asyncJob.getId()); assert query.getResultType() != null : query; checkJob(query); } }
public AsyncJob<Object> waitForJobDone(String job) { do { AsyncJob<Object> j = getAsyncJobClient().getAsyncJob(job); if (j.getStatus() != Status.IN_PROGRESS) return j; LOG.debug("cloudstack waiting on job " + job + ": " + j); } while (true); }
AsyncJob<Object> job = getAsyncJobClient().getAsyncJob(jobId); LOG.debug("waiting: " + job); if (job.hasFailed()) throw new IllegalStateException("Failed job: " + job);
/** * * @param job * @return result of the job's execution * @throws ExecutionException * if the job contained an error */ public <T> T apply(AsyncCreateResponse job) { boolean completed = jobComplete.apply(job.getJobId()); logger.trace("<< job(%s) complete(%s)", job, completed); AsyncJob<T> jobWithResult = client.getAsyncJobApi().<T> getAsyncJob(job.getJobId()); checkState(completed, "job %s failed to complete in time %s", job.getJobId(), jobWithResult); if (jobWithResult.getError() != null) throw new UncheckedExecutionException(String.format("job %s failed with exception %s", job.getJobId(), jobWithResult.getError().toString())) { }; return jobWithResult.getResult(); } }
@Test(dependsOnMethods = "testCreateLoadBalancerRule") public void testAssignToLoadBalancerRule() throws Exception { if (networksDisabled) return; String jobId = client.getLoadBalancerApi().assignVirtualMachinesToLoadBalancerRule(rule.getId(), vm.getId()); assertTrue(jobComplete.apply(jobId)); AsyncJob<JobResult> result = client.getAsyncJobApi().getAsyncJob(jobId); assertTrue(result.hasSucceed()); Set<VirtualMachine> machines = client.getLoadBalancerApi().listVirtualMachinesAssignedToLoadBalancerRule( rule.getId()); assertEquals(machines.size(), 1); assertTrue(loadBalancerRuleActive.apply(rule), rule.toString()); }
@Test public void testJobComplete() { AsyncJob<?> job = AsyncJob.builder().id("100") .status(Status.SUCCEEDED).resultCode(ResultCode.SUCCESS).build(); expect((Object) asyncJobClient.getAsyncJob(job.getId())).andReturn(job); replay(client, asyncJobClient); assertTrue(new JobComplete(client).apply(job.getId())); verify(client, asyncJobClient); }
public void testApply() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply(AsyncCreateResponse.builder().id(id).jobId( jobId).build()), "foo"); verify(client); verify(jobClient); }
@Test(expectedExceptions = IllegalStateException.class) public void testJobDoesntCompleteThrowsIllegalStateException() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); // the alwaysfalse predicate should blow up with IllegalStateException Predicate<String> jobComplete = Predicates.alwaysFalse(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); }
@Test(enabled = true, dependsOnMethods = "testRegisterTemplate") public void testExtractTemplate() throws Exception { // Initiate the extraction and wait for it to complete AsyncCreateResponse response = client.getTemplateApi().extractTemplate(registeredTemplate.getId(), ExtractMode.HTTP_DOWNLOAD, registeredTemplate.getZoneId()); assertTrue(jobComplete.apply(response.getJobId()), registeredTemplate.toString()); // Get the result AsyncJob<TemplateExtraction> asyncJob = client.getAsyncJobApi().getAsyncJob(response.getJobId()); TemplateExtraction extract = asyncJob.getResult(); assertNotNull(extract); // Check that the URL can be retrieved String extractUrl = extract.getUrl(); assertNotNull(extractUrl); URI uri = new URI(URLDecoder.decode(extractUrl, "utf-8")); assertTrue(cloudStackContext.utils().http().exists(uri), "does not exist: " + uri); }
@Test public void testFailedJobComplete() { AsyncJob<?> job = AsyncJob.builder().id("100") .status(Status.FAILED).resultCode(ResultCode.FAIL) .error(AsyncJobError.builder().errorCode(ErrorCode.INTERNAL_ERROR).errorText("Dummy test error").build()).build(); expect((Object) asyncJobClient.getAsyncJob(job.getId())).andReturn(job); replay(client, asyncJobClient); try { new JobComplete(client).apply(job.getId()); fail("No exception thrown"); } catch (AsyncJobException e) { assertTrue(e.toString().contains("Dummy test error")); } verify(client, asyncJobClient); } }
@Test(expectedExceptions = UncheckedExecutionException.class) public void testJobWithErrorThrowsUncheckedExecutionException() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn( AsyncJob.builder().id(jobId) .error(AsyncJobError.builder().errorCode(ErrorCode.INTERNAL_ERROR).errorText("ERRROR").build()) .result("foo").build()) .atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); } }
public void testAssociateDisassociatePublicIPAddress() throws Exception { if (!networksEnabled) return; AsyncCreateResponse job = client.getAddressApi().associateIPAddressInZone( Iterables.get(client.getNetworkApi().listNetworks(), 0).getZoneId()); checkState(jobComplete.apply(job.getJobId()), "job %s failed to complete", job.getJobId()); ip = client.getAsyncJobApi().<PublicIPAddress> getAsyncJob(job.getJobId()).getResult(); checkIP(ip); }
@Test(dependsOnMethods = "testCreateVm") public void testCreateLoadBalancerRule() throws Exception { if (networksDisabled) return; int attempts = 0; while (rule == null && attempts < 10) { ip = reuseOrAssociate.apply(network); try { String jobId = client.getLoadBalancerApi().createLoadBalancerRuleForPublicIP(ip.getId(), Algorithm.LEASTCONN, prefix, 22, 22); assertTrue(jobComplete.apply(jobId)); AsyncJob<LoadBalancerRule> asyncJob = client.getAsyncJobApi().getAsyncJob(jobId); LoadBalancerRule result = asyncJob.getResult(); rule = result; } catch (IllegalStateException e) { // very likely an ip conflict, so retry; attempts++; } } assertNotNull(rule, "Failed to get a load balancer rule after " + attempts + " attempts"); assertEquals(rule.getPublicIPId(), ip.getId()); assertEquals(rule.getPublicPort(), 22); assertEquals(rule.getPrivatePort(), 22); assertEquals(rule.getAlgorithm(), Algorithm.LEASTCONN); assertEquals(rule.getName(), prefix); assertEquals(rule.getState(), State.ADD); assertEquals(client.getLoadBalancerApi().listVirtualMachinesAssignedToLoadBalancerRule(rule.getId()).size(), 0); checkRule(rule); }
public static VirtualMachine createVirtualMachineWithOptionsInZone(DeployVirtualMachineOptions options, String zoneId, String templateId, CloudStackApi client, Predicate<String> jobComplete, Predicate<VirtualMachine> virtualMachineRunning) { String serviceOfferingId = DEFAULT_SIZE_ORDERING.min(client.getOfferingApi().listServiceOfferings()).getId(); System.out.printf("serviceOfferingId %s, templateId %s, zoneId %s, options %s%n", serviceOfferingId, templateId, zoneId, options); AsyncCreateResponse job = client.getVirtualMachineApi().deployVirtualMachineInZone(zoneId, serviceOfferingId, templateId, options); assertTrue(jobComplete.apply(job.getJobId())); AsyncJob<VirtualMachine> jobWithResult = client.getAsyncJobApi().<VirtualMachine> getAsyncJob(job.getJobId()); if (jobWithResult.getError() != null) Throwables.propagate(new ExecutionException(String.format("job %s failed with exception %s", job.getId(), jobWithResult.getError().toString())) { }); VirtualMachine vm = jobWithResult.getResult(); if (vm.isPasswordEnabled()) { assert vm.getPassword() != null : vm; } assertTrue(virtualMachineRunning.apply(vm)); assertEquals(vm.getServiceOfferingId(), serviceOfferingId); assertEquals(vm.getTemplateId(), templateId); assertEquals(vm.getZoneId(), zoneId); return vm; }
@Test(dependsOnMethods = "testCreateVirtualMachine") public void testLifeCycle() throws Exception { String job = client.getVirtualMachineApi().stopVirtualMachine(vm.getId()); assertTrue(jobComplete.apply(job)); vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); assertEquals(vm.getState(), VirtualMachine.State.STOPPED); if (vm.isPasswordEnabled()) { job = client.getVirtualMachineApi().resetPasswordForVirtualMachine(vm.getId()); assertTrue(jobComplete.apply(job)); vm = client.getAsyncJobApi().<VirtualMachine> getAsyncJob(job).getResult(); if (vm.getPassword() != null) { conditionallyCheckSSH(); } } job = client.getVirtualMachineApi().startVirtualMachine(vm.getId()); assertTrue(jobComplete.apply(job)); vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); assertEquals(vm.getState(), VirtualMachine.State.RUNNING); job = client.getVirtualMachineApi().rebootVirtualMachine(vm.getId()); assertTrue(jobComplete.apply(job)); vm = client.getVirtualMachineApi().getVirtualMachine(vm.getId()); assertEquals(vm.getState(), VirtualMachine.State.RUNNING); }
@Test public void testEnableDisableAccount() { skipIfNotGlobalAdmin(); Account testAccount = null; try { testAccount = createTestAccount(globalAdminClient, prefix); AsyncCreateResponse response = domainAdminClient.getAccountApi() .disableAccount(testAccount.getName(), testAccount.getDomainId(), false); assertNotNull(response); assertTrue(adminJobComplete.apply(response.getJobId())); AsyncJob<Account> job = domainAdminClient.getAsyncJobApi().getAsyncJob(response.getJobId()); assertEquals(job.getResult().getState(), Account.State.DISABLED); Account updated = domainAdminClient.getAccountApi() .enableAccount(testAccount.getName(), testAccount.getDomainId()); assertNotNull(updated); assertEquals(updated.getState(), Account.State.ENABLED); } finally { if (testAccount != null) { globalAdminClient.getAccountApi().deleteAccount(testAccount.getId()); } } }
assertTrue(adminJobComplete.apply(response.getJobId())); AsyncJob<User> job = domainAdminClient.getAsyncJobApi().getAsyncJob(response.getJobId()); assertNotNull(job); assertEquals(job.getResult().getState(), User.State.DISABLED);