@AfterMethod(alwaysRun=true) @Override public void tearDown() throws Exception { super.tearDown(); RecordingSshTool.clear(); }
protected boolean isUseraddExecuted() { for (ExecCmd cmds : RecordingSshTool.getExecCmds()) { if (cmds.commands.toString().contains("useradd")) { return true; } } return false; }
private void initSshCustomResponses() { RecordingSshTool.setCustomResponse("cat /proc/uptime", new RecordingSshTool.CustomResponseGenerator() { private final AtomicInteger counter = new AtomicInteger(1); @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 { String stdout = Joiner.on("\n").join( "name:centos", "version:7.0", "architecture:myarch", "ram:1024", "cpus:1"); return new CustomResponse(0, stdout, ""); }}); }
@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"); }
RecordingSshTool.setCustomResponse(".*", new RecordingSshTool.CustomResponseGenerator() { @Override public RecordingSshTool.CustomResponse generate(RecordingSshTool.ExecParams execParams) throws Exception { assertFalse(RecordingSshTool.getExecCmds().isEmpty(), "expected ssh connection to be made when testing credentials"); RecordingSshTool.clear(); customizer = new DefaultConnectivityResolver(ImmutableMap.of( DefaultConnectivityResolver.CHECK_CREDENTIALS, false)); result = customizer.resolve(jcloudsLocation, newNodeMetadata(), config, options); assertEquals(result.hostAndPort(), otherHostAndPort); assertTrue(RecordingSshTool.getExecCmds().isEmpty(), "expected no ssh connection to be made when not testing credentials: " + Iterables.toString(RecordingSshTool.getExecCmds()));
@Test public void testAllCmds() throws Exception { app.createAndManageChild(EntitySpec.create(VanillaSoftwareProcess.class) .configure(VanillaSoftwareProcess.PRE_INSTALL_COMMAND, "preInstallCommand") .configure(VanillaSoftwareProcess.INSTALL_COMMAND, "installCommand") .configure(VanillaSoftwareProcess.POST_INSTALL_COMMAND, "postInstallCommand") .configure(VanillaSoftwareProcess.PRE_CUSTOMIZE_COMMAND, "preCustomizeCommand") .configure(VanillaSoftwareProcess.CUSTOMIZE_COMMAND, "customizeCommand") .configure(VanillaSoftwareProcess.POST_CUSTOMIZE_COMMAND, "postCustomizeCommand") .configure(VanillaSoftwareProcess.PRE_LAUNCH_COMMAND, "preLaunchCommand") .configure(VanillaSoftwareProcess.LAUNCH_COMMAND, "launchCommand") .configure(VanillaSoftwareProcess.POST_LAUNCH_COMMAND, "postLaunchCommand") .configure(VanillaSoftwareProcess.CHECK_RUNNING_COMMAND, "checkRunningCommand") .configure(VanillaSoftwareProcess.STOP_COMMAND, "stopCommand")); app.start(ImmutableList.of(loc)); assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "preInstallCommand", "installCommand", "postInstallCommand", "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", "preLaunchCommand", "launchCommand", "postLaunchCommand", "checkRunningCommand")); app.stop(); assertExecContains(RecordingSshTool.getLastExecCmd(), "stopCommand"); }
@Test public void testSubTypeUsesDefaultsFromSuperInConfigMerging() throws Exception { RecordingSshTool.setCustomResponse(".*myCommand.*", new RecordingSshTool.CustomResponse(0, "myResponse", null)); createStartWaitAndLogApplication(yaml); ExecCmd cmd = ExecCmdAsserts.findExecContaining(RecordingSshTool.getExecCmds(), "myLaunchCmd"); assertEquals(cmd.env.get("KEY_IN_SUPER"), "myDefaultVal", "cmd="+cmd); assertEquals(cmd.env.get("KEY_IN_SUB"), "myBoringVal", "cmd="+cmd);
@Test public void testCommandExecutedWithEnvironment() throws Exception { TestEntity member = entityManager.createEntity(EntitySpec.create(TestEntity.class).parent(group)); assertExecSizeEventually(1); assertEquals(RecordingSshTool.getLastExecCmd().commands, ImmutableList.of("echo ignored")); Map<?, ?> env = RecordingSshTool.getLastExecCmd().env; assertTrue(env.containsKey(SshCommandMembershipTrackingPolicy.EVENT_TYPE)); assertEquals(env.get(SshCommandMembershipTrackingPolicy.EVENT_TYPE), EventType.ENTITY_ADDED.name()); assertTrue(env.containsKey(SshCommandMembershipTrackingPolicy.MEMBER_ID)); assertEquals(env.get(SshCommandMembershipTrackingPolicy.MEMBER_ID), member.getId()); assertTrue(env.containsKey("TEST")); assertEquals(env.get("TEST"), "test"); member.sensors().set(Startable.SERVICE_UP, true); Duration.seconds(1).countdownTimer().waitForExpiry(); assertExecSizeEventually(2); assertEquals(RecordingSshTool.getLastExecCmd().env.get(SshCommandMembershipTrackingPolicy.EVENT_TYPE), EventType.ENTITY_CHANGE.name()); member.clearParent(); assertExecSizeEventually(3); assertEquals(RecordingSshTool.getLastExecCmd().env.get(SshCommandMembershipTrackingPolicy.EVENT_TYPE), EventType.ENTITY_REMOVED.name()); }
"ram:16384", "cpus:8"); RecordingSshTool.setCustomResponse( ".*os-release.*", new RecordingSshTool.CustomResponse(0, osDetailsResponse, "")); RecordingSshTool.clear();
fail("useradd not found in: "+RecordingSshTool.getExecCmds()); RecordingSshTool.clear();
@Override protected JcloudsMachineLocation obtainMachine(JcloudsLocation jcloudsLoc, Map<?,?> props) throws Exception { final MachineEntity entity = (MachineEntity) Iterables.getOnlyElement(origApp.getChildren()); origApp.invoke(Startable.START, ImmutableMap.<String, Object>of()).get(); // Execute ssh (with RecordingSshTool), and confirm was given resolved password entity.execCommand("mycmd"); Map<?, ?> constructorProps = RecordingSshTool.getLastConstructorProps(); ExecCmd execCmd = RecordingSshTool.getLastExecCmd(); assertEquals(constructorProps.get("password"), "myYamlPassword", "constructorProps: "+constructorProps+"; execProps: "+execCmd.props); return Machines.findUniqueMachineLocation(entity.getLocations(), JcloudsMachineLocation.class).get(); } }
.get(); RecordingSshTool.clearCmdHistory(); .get(); assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "checkRunningCommand", "stopCommand", "preLaunchCommand", "launchCommand", "postLaunchCommand",
@Test public void testNodeSetupCustomizer() throws Exception { final String testMetadata = "test-metadata"; obtainMachine(ImmutableMap.of(JCLOUDS_LOCATION_CUSTOMIZERS, ImmutableList.of(new BasicJcloudsLocationCustomizer(){ @Override public void customize(JcloudsLocation location, NodeMetadata node, ConfigBag setup) { assertNotNull(node, "node"); assertNotNull(location, "location"); setup.configure(ADDITIONAL_CONNECTION_METADATA, testMetadata); } }))); Map<?, ?> lastConstructorProps = RecordingSshTool.getLastConstructorProps(); assertEquals(lastConstructorProps.get(ADDITIONAL_CONNECTION_METADATA.getName()), testMetadata); }
@Override public int execCommands(Map<String, ?> props, List<String> commands) { return execCommands(props, commands, ImmutableMap.<String,Object>of()); } @Override public int copyToServer(Map<String, ?> props, File localFile, String pathAndFileOnRemoteServer) {
@Test public void testSshCommandEffectorWithShellEnv() throws Exception { RecordingSshTool.setCustomResponse(".*myCommand.*", new RecordingSshTool.CustomResponse(0, "myResponse", null)); assertEquals(((String)result).trim(), "myResponse"); ExecCmd lastExecCmd = RecordingSshTool.getLastExecCmd(); assertIncludesCmd(lastExecCmd.commands, "myCommand"); assertEquals(lastExecCmd.env, ImmutableMap.of("env1", "myEnv1Val"));
assertExecsSatisfy(RecordingSshTool.getExecCmds(), ImmutableList.of( Predicates.and(ExecCmdPredicates.containsCmd("preInstallCommand"), ExecCmdPredicates.containsEnv(expectedEnv)), Predicates.and(ExecCmdPredicates.containsCmd("installCommand"), ExecCmdPredicates.containsEnv(expectedEnv)), RecordingSshTool.getLastExecCmd(), Predicates.and(ExecCmdPredicates.containsCmd("stopCommand"), ExecCmdPredicates.containsEnv(expectedEnv)));
RecordingSshTool.setCustomResponse(".*checkRunningCommand.*", 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() { @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) { isStarted.set(false); app.start(ImmutableList.of(loc)); assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "checkRunningCommand", "preInstallCommand", "installCommand", "postInstallCommand",
Map<?, ?> env = RecordingSshTool.getLastExecCmd().env; assertEquals(env.get("TEST"), "myDefaultParamVal", "env="+env);
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); RecordingSshTool.clear(); }
@Test public void testSshCommandEffectorWithParameter() throws Exception { RecordingSshTool.setCustomResponse(".*myCommand.*", new RecordingSshTool.CustomResponse(0, "myResponse", null)); assertEquals(((String)result).trim(), "myResponse"); ExecCmd lastExecCmd = RecordingSshTool.getLastExecCmd(); assertIncludesCmd(lastExecCmd.commands, "myCommand"); assertEquals(lastExecCmd.env, ImmutableMap.of("param1", "myCustomParam1Val")); assertEquals(((String)result).trim(), "myResponse"); ExecCmd lastExecCmd = RecordingSshTool.getLastExecCmd(); assertIncludesCmd(lastExecCmd.commands, "myCommand"); assertEquals(lastExecCmd.env, ImmutableMap.of("param1", "defaultValForParam1"));