@Test public void testApplyWhereTagDoesntMatchAndLocationFoundAndImageAndHardwareNotFound() throws UnknownHostException { RunningInstanceToNodeMetadata parser = createNodeParser(ImmutableSet.<Hardware> of(), ImmutableSet.of(provider), ImmutableSet.<Image> of(), ImmutableMap.<String, Credentials> of()); RunningInstance server = firstInstanceFromResource("/describe_instances_running.xml"); NodeMetadata expected = new NodeMetadataBuilder().hostname("ip-10-243-42-70").status(Status.RUNNING).backendStatus("running") .privateAddresses(ImmutableSet.of("10.243.42.70")).publicAddresses(ImmutableSet.of("174.129.81.68")) .imageId("us-east-1/ami-82e4b5c7").id("us-east-1/i-0799056f").providerId("i-0799056f") .location(provider).build(); assertEquals(parser.apply(server).toString(), expected.toString()); }
@Override public NodeMetadata apply(Server from) { // convert the result object to a jclouds NodeMetadata NodeMetadataBuilder builder = new NodeMetadataBuilder(); builder.ids(from.id + ""); builder.name(from.name); builder.location(from(locations.get()).firstMatch(LocationPredicates.idEquals(from.datacenter)).orNull()); builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.name)); builder.imageId(from.imageId + ""); Image image = from(images.get()).firstMatch(ImagePredicates.idEquals(from.imageId + "")).orNull(); if (image != null) builder.operatingSystem(image.getOperatingSystem()); builder.hardware(from(hardware.get()).firstMatch(HardwarePredicates.idEquals(from.hardwareId + "")).orNull()); builder.status(serverStatusToNodeStatus.get(from.status)); builder.publicAddresses(ImmutableSet.<String> of(from.publicAddress)); builder.privateAddresses(ImmutableSet.<String> of(from.privateAddress)); builder.credentials(LoginCredentials.fromCredentials(credentialStore.get(from.id + ""))); return builder.build(); } }
private static NodeMetadata addTagsForInstance(Map<String, String> tags, NodeMetadata input) { NodeMetadataBuilder builder = NodeMetadataBuilder.fromNodeMetadata(input).name(tags.get("Name")); return addMetadataAndParseTagsFromValuesOfEmptyString(builder, tags).build(); }
@Override public NodeMetadata apply(NodeMetadata arg0) { return credentialStore.containsKey("node#" + arg0.getId()) ? NodeMetadataBuilder.fromNodeMetadata(arg0) .credentials(LoginCredentials.fromCredentials(credentialStore.get("node#" + arg0.getId()))).build() : arg0; }
public static NodeMetadataBuilder fromNodeMetadata(NodeMetadata node) { return new NodeMetadataBuilder().providerId(node.getProviderId()).name(node.getName()).id(node.getId()).location( node.getLocation()).uri(node.getUri()).userMetadata(node.getUserMetadata()).tags(node.getTags()).group( node.getGroup()).hardware(node.getHardware()).imageId(node.getImageId()).operatingSystem( node.getOperatingSystem()).status(node.getStatus()).backendStatus(node.getBackendStatus()).loginPort( node.getLoginPort()).publicAddresses(node.getPublicAddresses()).privateAddresses( node.getPrivateAddresses()).credentials(node.getCredentials()).hostname(node.getHostname()); }
public void testRefreshCredentialsForNodeIfRanAdminAccessWhenStatementIsAdminAccessNewCredentialsAndDoesCache() { @SuppressWarnings("unchecked") Map<String, Credentials> credstore = createMock(Map.class); AdminAccess statement = createMock(AdminAccess.class); LoginCredentials credentials = LoginCredentials.builder().user("foo").build(); expect(statement.getAdminCredentials()).andReturn(credentials).atLeastOnce(); expect(credstore.put("node#id", credentials)).andReturn(null); replay(statement); replay(credstore); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).build(); RefreshCredentialsForNodeIfRanAdminAccess fn = new PersistNodeCredentialsModule.RefreshCredentialsForNodeIfRanAdminAccess( credstore, statement); assertEquals(fn.apply(node).getCredentials(), credentials); verify(statement); verify(credstore); }
@Test public void testRefreshUpdatesAtomicReferenceOnRecheckRunning() { NodeMetadata running = new NodeMetadataBuilder().id("myid").status(Status.RUNNING).build(); NodeMetadata pending = new NodeMetadataBuilder().id("myid").status(Status.PENDING).build(); GetNodeMetadataStrategy computeService = createMock(GetNodeMetadataStrategy.class); expect(computeService.getNode("myid")).andReturn(running); replay(computeService); AtomicNodeRunning nodeRunning = new AtomicNodeRunning(computeService); AtomicReference<NodeMetadata> reference = Atomics.newReference(pending); Assert.assertTrue(nodeRunning.apply(reference)); Assert.assertEquals(reference.get(), running); verify(computeService); }
@Test public void testApplyWhereVirtualGuestWithNoPassword() { // notice if we've already parsed this properly here, we can rely on it. VirtualGuest guest = new ParseVirtualGuestWithNoPasswordTest().expected(); // setup so that we have an expected Location to be parsed from the guest. Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter()); Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(expectedLocation)); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata( locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("416788").name("node1000360500").hostname("node1000360500") .location(expectedLocation).status(Status.PENDING) .publicAddresses(ImmutableSet.of("173.192.29.186")) .privateAddresses(ImmutableSet.of("10.37.102.194")) .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }
@Test(expectedExceptions = IllegalStateException.class) public void testWithoutInitThrowsIllegalStateException() { Statement command = exec("doFoo"); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).credentials( LoginCredentials.builder().user("tester").password("notalot").build()).build(); SshClient sshClient = createMock(SshClient.class); replay(sshClient); RunScriptOnNodeAsInitScriptUsingSsh testMe = new RunScriptOnNodeAsInitScriptUsingSsh(Functions .forMap(ImmutableMap.of(node, sshClient)), eventBus, InitScriptConfigurationForTasks.create() .appendIncrementingNumberToAnonymousTaskNames(), node, command, new RunScriptOptions()); testMe.call(); }
public static NodeMetadata expectedNodeMetadataFromResource(int id, String resource, Location location, int loginPort) { return new NodeMetadataBuilder() .ids("cluster-" + id) .group("hadoop") .name("cluster-" + id) .loginPort(loginPort) .hostname("cluster-" + id + ".mydomain.com") .location(location) .userMetadata(ImmutableMap.of("Name", "foo")) .tags(ImmutableSet.of("vanilla")) .status(Status.RUNNING) .operatingSystem( OperatingSystem.builder().description("redhat").family(OsFamily.RHEL).arch("x86").version("5.3") .build()) .publicAddresses(ImmutableSet.of("cluster-" + id + ".mydomain.com")) .credentials( LoginCredentials.builder().user("myUser").privateKey(NodesFromYamlTest.key).password("happy bear") .authenticateSudo(true).build()).build(); }
@Override protected NodeMetadata newNode(String group, Template template) { NodeMetadata result = new NodeMetadataBuilder() .id("myid") .credentials(LoginCredentials.builder().identity("myuser").credential("mypassword").build()) .loginPort(22) .status(Status.RUNNING) .publicAddresses(publicAddresses) .privateAddresses(privateAddresses) .build(); return result; } };
@Test public void testReturnsNodeWithIpWhenFoundInCacheAndNodeHadNoPublicIp() throws Exception { RegionAndName key = new RegionAndName("us-east-1", node.getProviderId()); String val = "1.1.1.1"; LoadingCache<RegionAndName, String> cache = cacheOf(key, val); AddElasticIpsToNodemetadata fn = new AddElasticIpsToNodemetadata(cache); assertEquals(fn.apply( NodeMetadataBuilder.fromNodeMetadata(node).publicAddresses(ImmutableSet.<String> of()).build()) .getPublicAddresses(), ImmutableSet.of("1.1.1.1")); }
@Test public void testApplyWhereVirtualIsBad() { // notice if we've already parsed this properly here, we can rely on it. VirtualGuest guest = new ParseBadVirtualGuest().expected(); // no location here Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of()); VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention); NodeMetadata node = parser.apply(guest); assertEquals( node, new NodeMetadataBuilder().ids("413348").name("foo-ef4").hostname("foo-ef4").group("foo") .status(Status.PENDING).hardware(new GetHardwareForVirtualGuestMock().getHardware(guest)) .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId()) .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build()); }
@Override public NodeWithInitialCredentials createNodeWithGroupEncodedIntoName(String group, String name, Template template) { NodeMetadataBuilder builder = new NodeMetadataBuilder(); String id = idProvider.get() + ""; builder.ids(id); builder.name(name); // using a predictable name so tests will pass builder.hostname(group); builder.tags(template.getOptions().getTags()); builder.userMetadata(template.getOptions().getUserMetadata()); builder.group(group); builder.location(location.get()); builder.imageId(template.getImage().getId()); builder.operatingSystem(template.getImage().getOperatingSystem()); builder.status(Status.PENDING); builder.publicAddresses(ImmutableSet.<String> of(publicIpPrefix + id)); builder.privateAddresses(ImmutableSet.<String> of(privateIpPrefix + id)); builder.credentials(LoginCredentials.builder().user("root").password(passwordPrefix + id).build()); NodeMetadata node = builder.build(); nodes.put(node.getId(), node); setStateOnNodeAfterDelay(Status.RUNNING, node, 100); return new NodeWithInitialCredentials(node); }
private NodeMetadata newNodeMetadata() { return new NodeMetadataBuilder() .id("id") .backendStatus("backendStatus") .credentials(credential) .group("group") .hostname("hostname") .loginPort(22) .name("DefaultConnectivityResolverTest") .publicAddresses(ImmutableList.of("10.0.0.1", "10.0.0.2")) .privateAddresses(ImmutableList.of("192.168.0.1", "192.168.0.2")) .status(NodeMetadata.Status.RUNNING) .tags(ImmutableList.<String>of()) .userMetadata(ImmutableMap.<String, String>of()) .build(); }
public void testRefreshCredentialsForNodeWhenStatementIsNullSameCredentialsAndDoesCache() { @SuppressWarnings("unchecked") Map<String, Credentials> credstore = createMock(Map.class); LoginCredentials credentials = createMock(LoginCredentials.class); expect(credstore.put("node#id", credentials)).andReturn(null); replay(credstore); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).credentials(credentials).build(); RefreshCredentialsForNode fn = new PersistNodeCredentialsModule.RefreshCredentialsForNode(credstore, null); assertEquals(node, fn.apply(node)); verify(credstore); }
public void testRefreshCredentialsForNodeIfRanAdminAccessWhenStatementIsNullSameCredentialsAndNoCaching() { @SuppressWarnings("unchecked") Map<String, Credentials> credstore = createMock(Map.class); replay(credstore); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).build(); RefreshCredentialsForNodeIfRanAdminAccess fn = new PersistNodeCredentialsModule.RefreshCredentialsForNodeIfRanAdminAccess( credstore, null); assertEquals(node, fn.apply(node)); verify(credstore); }
private void toStringAndEventBusExpectations(SudoAwareInitManager commandRunner, InitScript initScript) { toStringExpectations(commandRunner, initScript); expect(commandRunner.getStatement()).andReturn(initScript); expect(commandRunner.getNode()).andReturn( new NodeMetadataBuilder().ids("id").status(NodeMetadata.Status.RUNNING).build()); }
@Override protected NodeMetadata newNode(String group, Template template) { NodeMetadata result = new NodeMetadataBuilder() .id("myNodeId") .credentials(LoginCredentials.builder().identity("myuser").credential("mypassword").build()) .loginPort(serverSocket.getLocalPort()) .status(Status.RUNNING) .publicAddresses(ImmutableList.of(serverSocket.getInetAddress().getHostAddress())) .privateAddresses(ImmutableList.of("1.2.3.4")) .imageId(template.getImage().getId()) .tags(template.getOptions().getTags()) .hardware(includeNodeHardwareMetadata ? template.getHardware() : null) .group(template.getOptions().getGroups().isEmpty() ? "myGroup" : Iterables.get(template.getOptions().getGroups(), 0)) .build(); return result; } };
public void testRefreshCredentialsForNodeWhenStatementIsAdminAccessNewCredentialsAndDoesCache() { @SuppressWarnings("unchecked") Map<String, Credentials> credstore = createMock(Map.class); AdminAccess statement = createMock(AdminAccess.class); LoginCredentials credentials = LoginCredentials.builder().user("foo").build(); expect(statement.getAdminCredentials()).andReturn(credentials).atLeastOnce(); expect(credstore.put("node#id", credentials)).andReturn(null); expect(credstore.put("node#id", credentials)).andReturn(null); // TODO // optimize // this replay(statement); replay(credstore); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).build(); RefreshCredentialsForNode fn = new PersistNodeCredentialsModule.RefreshCredentialsForNode(credstore, statement); assertEquals(fn.apply(node).getCredentials(), credentials); verify(statement); verify(credstore); }