private void moveToCompleteState(Closure currentState) { Closure completedState = new Closure(); completedState.state = TaskState.TaskStage.FINISHED; completedState.closureSemaphore = currentState.closureSemaphore; completedState.inputs = currentState.inputs; final Map outputs = new HashMap(); completedState.inputs.forEach((k, v) -> { outputs.put(k, v); }); completedState.outputs = outputs; getHost().schedule(() -> sendSelfPatch(completedState), 3, TimeUnit.SECONDS); } }
@Override public void executeClosure(Closure closureRequest, ClosureDescription taskDef, String token, Consumer<Throwable> errorHandler) { // Lease the closure and proceed Closure leasedClosure = new Closure(); closureRequest.copyTo(leasedClosure); leasedClosure.state = TaskStage.STARTED; leasedClosure.inputs = closureRequest.inputs; leasedClosure.outputs = closureRequest.outputs; URI uri = UriUtils.buildUri(getServiceHost(), leasedClosure.documentSelfLink); logInfo("Leasing closure with uri: %s -> %s", uri, leasedClosure.state); getServiceHost().sendRequest(Operation .createPatch(uri) .setReferer(getServiceHost().getUri()) .setBody(leasedClosure) .setCompletion((o, ex) -> { if (ex != null) { Utils.logWarning("Closure execution aborted! Unable to lease closure with" + " URI: %s, Reason: %s", uri.toString(), Utils.toString(ex)); errorHandler.accept(ex); return; } proceedWithExecution(closureRequest, taskDef); })); }
private void moveToStartedState(Closure currentState) { Closure startedState = new Closure(); startedState.state = TaskState.TaskStage.STARTED; startedState.closureSemaphore = currentState.closureSemaphore; getHost().schedule(() -> sendSelfPatch(startedState), 1, TimeUnit.SECONDS); }
@Override public Closure doExecute(Closure closure, ClosureDescription taskDef) { logInfo("Submitting closure for execution: %s", closure.documentSelfLink); Closure closureResult = new Closure(); Map<String, JsonElement> outputs = new HashMap<>(); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName(DriverConstants.RUNTIME_NASHORN); if (engine == null) { throw new IllegalStateException("Unable to execute script with runtime: " + taskDef.runtime); } try { setBindings(closure, engine); executeScript(closure, taskDef, engine); closureResult.state = TaskStage.FINISHED; } catch (ScriptException e) { Utils.logWarning("Exception thrown while executing script: %s", e.getMessage()); closureResult.state = TaskStage.FAILED; closureResult.errorMsg = e.getMessage(); } // populate outputs populateOutputs(engine, taskDef.outputNames, outputs); closureResult.outputs = outputs; return closureResult; }
@Test public void testEvaluateSingleClosureBinding() { Closure closure = new Closure(); closure.name = "Closure"; JsonPrimitive inStr = new JsonPrimitive("localhost"); closure.inputs = new HashMap<>(); closure.inputs.put("hostname", inStr); ContainerDescription secondDescription = new ContainerDescription(); secondDescription.name = "Container"; List<Binding> bindings = Arrays.asList( binding(Arrays.asList("hostname"), "Closure~inputs~hostname")); Binding.ComponentBinding componentBinding = new Binding.ComponentBinding("Container", bindings); CompositeTemplate compositeTemplate = createCompositeTemplate(Arrays .asList(closure, secondDescription), Arrays.asList(componentBinding)); BindingEvaluator.evaluateBindings(compositeTemplate); closure = (Closure) compositeTemplate.components.get("Closure").data; secondDescription = (ContainerDescription) compositeTemplate.components .get("Container").data; assertEquals(closure.inputs.get("hostname").getAsString(), secondDescription.hostname); }
@Test public void testEvaluateSimpleClosureTimingBindingToObject() { List<Binding> bindings = Arrays .asList(binding(Arrays.asList("inputs", "test"), "_resource~A~inputs~hostname")); Map<String, NestedState> closures = new HashMap<>(); Closure closure = new Closure(); closure.name = "Closure"; JsonPrimitive inStr = new JsonPrimitive("localhost"); closure.inputs = new HashMap<>(); closure.inputs.put("hostname", inStr); closures.put("A", new NestedState(closure)); NestedState nestedState = new NestedState(closure); NestedState evalObj = BindingEvaluator .evaluateProvisioningTimeBindings(nestedState, bindings, closures); assertNotNull(evalObj); }
private void createClosure(ClosureAllocationTaskState state) { Closure closureState = new Closure(); closureState.descriptionLink = state.resourceDescriptionLink; String contextId = state.getCustomProperty(FIELD_NAME_CONTEXT_ID_KEY); closureState.customProperties = new HashMap<>(2); closureState.customProperties.put(FIELD_NAME_CONTEXT_ID_KEY, contextId); closureState.customProperties.put(ComputeConstants.FIELD_NAME_COMPOSITE_COMPONENT_LINK_KEY, UriUtils.buildUriPath(CompositeComponentFactoryService.SELF_LINK, contextId)); closureState.tenantLinks = state.tenantLinks; sendRequest(Operation.createPost(this, ClosureFactoryService.FACTORY_LINK) .setBody(closureState) .setCompletion((o, ex) -> { if (ex != null) { logWarning("Failed to create closure: %s", Utils.toString(ex)); proceedTo(SubStage.ERROR); return; } Closure createdClosure = o.getBody(Closure.class); proceedTo(SubStage.COMPLETED, (s) -> { s.closureLink = createdClosure.documentSelfLink; }); })); }
@Test public void inspectImageClosureTest() { final Object[] errors = { null }; Closure closure = new Closure(); dockerDriver.inspectImage("test_image", "image_link", (error) -> errors[0] = error ); assertNull(errors[0]); }
@Test public void inspectImageNoImageLinkTest() { final Object[] errors = { null }; Closure closure = new Closure(); dockerDriver.inspectImage("test_image", null, (error) -> errors[0] = error ); assertNotNull(errors[0]); }
@Test public void cleanClosureNoLinkTest() { final Object[] errors = { null }; Closure closure = new Closure(); dockerDriver.cleanClosure(closure, (error) -> errors[0] = error ); assertNotNull(errors[0]); }
@Test public void invalidNegativeTest() throws Throwable { URI factoryUri = UriUtils.buildFactoryUri(this.host, ClosureFactoryService.class); this.host.testStart(1); Closure initialState = new Closure(); initialState.documentSelfLink = UUID.randomUUID().toString(); Operation post = Operation .createPost(factoryUri) .setBody(initialState) .setCompletion( BasicReusableHostTestCase.getSafeHandler((o, e) -> assertNotNull(e))); this.host.send(post); this.host.testWait(); }
URI factoryTaskUri = UriUtils.buildFactoryUri(this.host, ClosureFactoryService.class); Closure closureState = new Closure(); closureState.descriptionLink = ClosureDescriptionFactoryService.FACTORY_LINK + "/" Closure closureRequest = new Closure(); Map<String, JsonElement> inputs = new HashMap<>(); inputs.put("a", new JsonPrimitive(expectedInVar));