@Override public CustomResponse generate(ExecParams execParams) throws Exception { launchCalledLatch.countDown(); launchBlockedLatch.await(); return new CustomResponse(0, "", ""); }});
@Override public RecordingSshTool.CustomResponse generate(RecordingSshTool.ExecParams execParams) throws Exception { boolean valid = allowedUser.equals(execParams.constructorProps.get("user")); return new RecordingSshTool.CustomResponse(valid ? 0 : 1, "", ""); } });
@Override public CustomResponse generate(ExecParams execParams) throws Exception { stopCalledLatch.countDown(); stopBlockedLatch.await(); return new CustomResponse(0, "", ""); }});
@Test public void testSshConfigFromDefault() throws Exception { RecordingSshTool.setCustomResponse(".*myCommand.*", new RecordingSshTool.CustomResponse(0, "myResponse", null)); String bp = loadYaml("config-type-coercion-test.yaml", "location:", " localhost:", " sshToolClass: "+RecordingSshTool.class.getName()); // remove all lines referring to "exact" -- that's useful for expository and running in UI // but it will fail (timeout) if the port isn't available so not good in tests bp = Strings.removeLines(bp, StringPredicates.containsLiteralIgnoreCase("exact")); Entity app = createAndStartApplication(bp); waitForApplicationTasks(app); Map<?, ?> props = RecordingSshTool.getLastExecCmd().env; Assert.assertEquals(props.get("RANGE_PORT_SENSOR"), "20003"); Asserts.assertStringContains((String)props.get("RANGE_PORT_CONFIG"), "{\"start\"", "20003"); Assert.assertEquals(props.get("INT_PORT_CONFIG"), "20001"); Assert.assertEquals(props.get("INT_PORT_DEFAULT_CONFIG"), "30001"); Assert.assertEquals(props.get("RANGE_PORT_DEFAULT_SENSOR"), "30003"); // NB: change in Oct 2016, default values are now coerced just like explicit value // (previous to Oct 2016 this would have returned just "30003+", no json) Asserts.assertStringContains((String)props.get("RANGE_PORT_DEFAULT_CONFIG"), "{\"start\"", "30003"); }
@Test public void testSshCommandSensorWithEffectorInEnv() throws Exception { RecordingSshTool.setCustomResponse(".*myCommand.*", new RecordingSshTool.CustomResponse(0, "myResponse", null)); Entity app = createAndStartApplication( "location:", " localhost:", " sshToolClass: "+RecordingSshTool.class.getName(), "services:", "- type: " + VanillaSoftwareProcess.class.getName(), " brooklyn.config:", " onbox.base.dir.skipResolution: true", " brooklyn.initializers:", " - type: org.apache.brooklyn.core.sensor.ssh.SshCommandSensor", " brooklyn.config:", " name: mySensor", " command: myCommand", " period: 10ms", " onlyIfServiceUp: false"); waitForApplicationTasks(app); VanillaSoftwareProcess entity = (VanillaSoftwareProcess) Iterables.getOnlyElement(app.getChildren()); EntityAsserts.assertAttributeEqualsEventually(entity, Sensors.newStringSensor("mySensor"), "myResponse"); }
@Test public void testTestCredentialWithWindowsMachine() throws Exception { final String allowedUser = "Mr. Big"; // Match every command. RecordingWinRmTool.setCustomResponse(".*", new RecordingWinRmTool.CustomResponseGenerator() { @Override public RecordingWinRmTool.CustomResponse generate(RecordingWinRmTool.ExecParams execParams) { boolean valid = allowedUser.equals(execParams.constructorProps.get("user")); return new RecordingWinRmTool.CustomResponse(valid ? 0 : 1, "", ""); } }); initNodeCreatorAndJcloudsLocation(newNodeCreator(), ImmutableMap.of()); DefaultConnectivityResolver customizer = new DefaultConnectivityResolver(); final ConfigBag config = ConfigBag.newInstanceExtending(jcloudsLocation.config().getBag(), ImmutableMap.of( JcloudsLocationConfig.WAIT_FOR_WINRM_AVAILABLE, "1ms", JcloudsLocationConfig.POLL_FOR_FIRST_REACHABLE_ADDRESS, "1ms")); assertTrue(customizer.checkCredential( jcloudsLocation, HostAndPort.fromParts("10.0.0.234", 22), LoginCredentials.builder().user(allowedUser).password("password1").build(), config, true)); assertFalse(customizer.checkCredential( jcloudsLocation, HostAndPort.fromParts("10.0.0.234", 22), credential, config, true)); }
@Test public void testTestCredentialWithLinuxMachine() throws Exception { final String allowedUser = "Mr. Big"; // Match every command. RecordingSshTool.setCustomResponse(".*", new RecordingSshTool.CustomResponseGenerator() { @Override public RecordingSshTool.CustomResponse generate(RecordingSshTool.ExecParams execParams) throws Exception { boolean valid = allowedUser.equals(execParams.constructorProps.get("user")); return new RecordingSshTool.CustomResponse(valid ? 0 : 1, "", ""); } }); initNodeCreatorAndJcloudsLocation(newNodeCreator(), ImmutableMap.of()); DefaultConnectivityResolver customizer = new DefaultConnectivityResolver(); final ConfigBag config = ConfigBag.newInstanceExtending(jcloudsLocation.config().getBag(), ImmutableMap.of( JcloudsLocationConfig.WAIT_FOR_SSHABLE, "1ms", JcloudsLocationConfig.POLL_FOR_FIRST_REACHABLE_ADDRESS, "1ms")); assertTrue(customizer.checkCredential( jcloudsLocation, HostAndPort.fromParts("10.0.0.234", 22), LoginCredentials.builder().user(allowedUser).password("password1").build(), config, false)); assertFalse(customizer.checkCredential( jcloudsLocation, HostAndPort.fromParts("10.0.0.234", 22), credential, config, false)); }
@Test public void testObtainsHostnameFromAwsMachine() throws Exception { final String expectedHostname = "ec2-awshostname"; RecordingSshTool.setCustomResponse(".*curl.*169.254.169.254.*", new RecordingSshTool.CustomResponse(0, expectedHostname, "")); initNodeCreatorAndJcloudsLocation(newNodeCreator(), ImmutableMap.of( JcloudsLocationConfig.LOOKUP_AWS_HOSTNAME, true)); ConnectivityResolverOptions options = newResolveOptions() .waitForConnectable(true) .pollForReachableAddresses(Predicates.<HostAndPort>alwaysTrue(), Duration.millis(1), true) .userCredentials(credential) .build(); DefaultConnectivityResolver customizer = new DefaultConnectivityResolver(); ConfigBag configBag = jcloudsLocation.config().getBag(); ManagementAddressResolveResult result = customizer.resolve( jcloudsLocation, newNodeMetadata(), configBag, options); assertEquals(result.hostAndPort().getHostText(), expectedHostname); }
@Override public RecordingSshTool.CustomResponse generate(RecordingSshTool.ExecParams execParams) throws Exception { HostAndPort hap = HostAndPort.fromParts( (String) execParams.constructorProps.get("host"), (Integer) execParams.constructorProps.get("port")); int exitCode = authorisedHostAndPort.equals(hap) ? 0 : 1; return new RecordingSshTool.CustomResponse(exitCode, "", ""); } });
@Override public CustomResponse generate(ExecParams execParams) throws Exception { String stdout = Joiner.on("\n").join( "name:centos", "version:7.0", "architecture:myarch", "ram:1024", "cpus:1"); return new CustomResponse(0, stdout, ""); }}); }
@Override public CustomResponse generate(ExecParams execParams) throws Exception { return new CustomResponse(0, Integer.toString(counter.getAndIncrement()), ""); }}); RecordingSshTool.setCustomResponse(".*/etc/os-release.*", new RecordingSshTool.CustomResponseGenerator() {
@Override public CustomResponse generate(ExecParams execParams) throws Exception { launchCalledLatch.countDown(); launchBlockedLatch.await(); return new CustomResponse(0, "", ""); }});
@Test public void testSshExecScript() throws Exception { String expectedName = Os.user(); RecordingSshTool.setCustomResponse(".*whoami.*", new CustomResponse(0, expectedName, "")); OutputStream outStream = new ByteArrayOutputStream(); host.execScript(MutableMap.of("out", outStream), "mysummary", ImmutableList.of("whoami; exit")); String outString = outStream.toString(); assertTrue(outString.contains(expectedName), outString); }
@Override public CustomResponse generate(ExecParams execParams) { isStarted.set(false); return new CustomResponse(0, "", ""); }});
@Override public CustomResponse generate(ExecParams execParams) throws Exception { System.out.println("ssh call: "+execParams); Object host = execParams.constructorProps.get(SshTool.PROP_HOST.getName()); if (isReachable(host.toString())) { return new CustomResponse(0, "", ""); } else { throw new IOException("Simulate VM not reachable for host '"+host+"'"); } } }
@Test public void testCustomOneOffResponse() throws Exception { RecordingSshTool.setCustomOneOffResponse(".*mycmd.*", new CustomResponse(1, "mystdout", "mystderr")); ExecResult result1 = execScript(machine, "mycmd"); ExecResult result2 = execScript(machine, "mycmd"); assertEquals(result1, new ExecResult(1, "mystdout", "mystderr")); assertEquals(result2, new ExecResult(0, "", "")); }
@Test public void testSshExecCommands() throws Exception { String expectedName = Os.user(); RecordingSshTool.setCustomResponse(".*whoami.*", new CustomResponse(0, expectedName, "")); OutputStream outStream = new ByteArrayOutputStream(); host.execCommands(MutableMap.of("out", outStream), "mysummary", ImmutableList.of("whoami; exit")); String outString = outStream.toString(); assertTrue(outString.contains(expectedName), outString); }
@Override public CustomResponse generate(ExecParams execParams) { checkRunningCalledLatch.countDown(); return new CustomResponse(1, "", ""); }});
@Override public CustomResponse generate(ExecParams execParams) { isStarted.set(true); return new CustomResponse(0, "", ""); }}); RecordingSshTool.setCustomResponse(".*stopCommand.*", new RecordingSshTool.CustomResponseGenerator() {
@Override public CustomResponse generate(ExecParams execParams) { int exitCode = isStarted.get() ? 0 : 1; return new CustomResponse(exitCode, "", ""); }}); RecordingSshTool.setCustomResponse(".*launchCommand.*", new RecordingSshTool.CustomResponseGenerator() {