@Override public boolean apply(ComputeMetadata computeMetadata) { // Not all list calls return NodeMetadata (e.g. VCloud) if (computeMetadata instanceof NodeMetadata) { NodeMetadata nodeMetadata = (NodeMetadata) computeMetadata; return group.equals(nodeMetadata.getGroup()) && nodeMetadata.getStatus() == NodeMetadata.Status.RUNNING; } return false; }
/** {@inheritDoc} */ @Override public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException { initComputeService(); Collection<InetSocketAddress> addresses = new LinkedList<>(); try { Set<NodeMetadata> nodes; if (nodesFilter != null) nodes = (Set<NodeMetadata>)computeService.listNodesDetailsMatching(nodesFilter); else { nodes = new HashSet<>(); for (ComputeMetadata metadata : computeService.listNodes()) nodes.add(computeService.getNodeMetadata(metadata.getId())); } for (NodeMetadata metadata : nodes) { if (metadata.getStatus() != NodeMetadata.Status.RUNNING) continue; for (String addr : metadata.getPrivateAddresses()) addresses.add(new InetSocketAddress(addr, 0)); for (String addr : metadata.getPublicAddresses()) addresses.add(new InetSocketAddress(addr, 0)); } } catch (Exception e) { throw new IgniteSpiException("Failed to get registered addresses for the provider: " + provider, e); } return addresses; }
@Override public String apply(NodeMetadata in) { return in.getProviderId(); } };
@Test public void testReturnsNodeWithElasticIpWhenFoundInCacheAndNodeHadAPublicIp() 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(node).getPublicAddresses(), ImmutableSet.of("1.1.1.1")); }
@Test(groups = { "integration", "live" }, dependsOnMethods = "testCreateSecurityGroup") public void testCreateNodeWithSecurityGroup() throws RunNodesException, InterruptedException, ExecutionException { ComputeService computeService = view.getComputeService(); Optional<SecurityGroupExtension> securityGroupExtension = computeService.getSecurityGroupExtension(); assertTrue(securityGroupExtension.isPresent(), "security group extension was not present"); NodeMetadata node = getOnlyElement(computeService.createNodesInGroup(nodeGroup, 1, options().securityGroups(groupId))); try { Set<SecurityGroup> groups = securityGroupExtension.get().listSecurityGroupsForNode(node.getId()); assertEquals(groups.size(), 1, "node has " + groups.size() + " groups"); assertEquals(getOnlyElement(groups).getId(), groupId); } finally { computeService.destroyNodesMatching(inGroup(node.getGroup())); } }
@Test(groups = { "integration", "live" }, singleThreaded = true) public void testCreateImage() throws RunNodesException, InterruptedException, ExecutionException { ComputeService computeService = view.getComputeService(); Optional<ImageExtension> imageExtension = computeService.getImageExtension(); assertTrue(imageExtension.isPresent(), "image extension was not present"); Template template = getNodeTemplate(); NodeMetadata node = Iterables.getOnlyElement(computeService.createNodesInGroup("test-create-image", 1, template)); checkReachable(node); logger.info("Creating image from node %s, started with template: %s", node, template); ImageTemplate newImageTemplate = imageExtension.get().buildImageTemplateFromNode("test-create-image", node.getId()); Image image = imageExtension.get().createImage(newImageTemplate).get(); logger.info("Image created: %s", image); assertEquals("test-create-image", image.getName()); imageId = image.getId(); computeService.destroyNode(node.getId()); Optional<? extends Image> optImage = getImage(); assertTrue(optImage.isPresent()); }
@Test public void testNodeRunningReturnsTrueWhenRunning() { expect(node.getStatus()).andReturn(Status.RUNNING).atLeastOnce(); expect(node.getBackendStatus()).andReturn(null).atLeastOnce(); replay(node); replay(computeService); AtomicNodeRunning nodeRunning = new AtomicNodeRunning(computeService); AtomicReference<NodeMetadata> reference = Atomics.newReference(node); Assert.assertTrue(nodeRunning.apply(reference)); Assert.assertEquals(reference.get(), node); }
@Test(enabled = true, dependsOnMethods = "testDestroyNodes", alwaysRun = true) public void testCreateTwoNodesWithOneSpecifiedName() throws Exception { template = buildTemplate(templateBuilder()); template.getOptions().nodeNames(ImmutableSet.of("first-node")); try { this.nodes = newTreeSet(client.createNodesInGroup(group, 2, template)); } catch (RunNodesException e) { this.nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet())); throw e; } assertEquals(this.nodes.size(), 2, "expected two nodes but was " + nodes); NodeMetadata node1 = Iterables.getFirst(this.nodes, null); NodeMetadata node2 = Iterables.getLast(this.nodes, null); // credentials aren't always the same // assertEquals(node1.getCredentials(), node2.getCredentials()); assertTrue(node1.getName().equals("first-node") || node2.getName().equals("first-node"), "one node should be named 'first-node'"); assertFalse(node1.getName().equals("first-node") && node2.getName().equals("first-node"), "one node should be named something other than 'first-node"); }
@Test(groups = { "integration", "live" }, singleThreaded = true, dependsOnMethods = "testCreateImage") public void testSpawnNodeFromImage() throws RunNodesException { ComputeService computeService = view.getComputeService(); Optional<? extends Image> optImage = getImage(); assertTrue(optImage.isPresent()); NodeMetadata node = Iterables.getOnlyElement(computeService.createNodesInGroup("test-create-image", 1, view .getComputeService() // fromImage does not use the arg image's id (but we do need to set location) .templateBuilder().imageId(optImage.get().getId()).fromImage(optImage.get()).build())); checkReachable(node); view.getComputeService().destroyNode(node.getId()); }
@Test public void testVirtualGuestToNodeMetadata() { VirtualGuest virtualGuest = createVirtualGuest(); NodeMetadata nodeMetadata = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention, virtualGuestToImage, virtualGuestToHardware).apply(virtualGuest); assertNotNull(nodeMetadata); assertEquals(nodeMetadata.getName(), virtualGuest.getHostname()); assertNotNull(nodeMetadata.getLocation()); assertEquals(nodeMetadata.getLocation().getId(), location.getId()); assertEquals(nodeMetadata.getHostname(), virtualGuest.getFullyQualifiedDomainName()); assertEquals(nodeMetadata.getHardware().getRam(), virtualGuest.getMaxMemory()); assertTrue(nodeMetadata.getHardware().getProcessors().size() == 1); assertEquals(Iterables.get(nodeMetadata.getHardware().getProcessors(), 0).getCores(), (double) virtualGuest.getStartCpus()); assertEquals(nodeMetadata.getOperatingSystem().getFamily(), OsFamily.UBUNTU); assertEquals(nodeMetadata.getOperatingSystem().getVersion(), "12.04"); assertEquals(nodeMetadata.getOperatingSystem().is64Bit(), true); }
@Test public void testWindowsAdminWorks() throws Exception { String group = "winadm"; // Spin up a new node. Make sure to open the RDP port 3389 Template template = view.getComputeService().templateBuilder().from(windowsTemplate).options(inboundPorts(3389)) .build(); try { NodeMetadata node = Iterables.getOnlyElement(view.getComputeService().createNodesInGroup(group, 1, template)); assertEquals(node.getCredentials().getUser(), "Administrator"); assertFalse(Strings.isNullOrEmpty(node.getCredentials().getPassword())); } finally { view.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group)); } }
@Test(enabled = false) public void testCreateNodeWithGeneratedKeyPairAndOverriddenLoginUserWithTemplateBuilder() throws Exception { Builder<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder(); requestResponseMap.put(describeRegionsRequest, describeRegionsResponse); requestResponseMap.put(describeAvailabilityZonesRequest, describeAvailabilityZonesResponse); requestResponseMap.put(describeImagesRequest, describeImagesResponse); requestResponseMap.put(createKeyPairRequest, createKeyPairResponse); requestResponseMap.put(createSecurityGroupRequest, createSecurityGroupResponse); requestResponseMap.put(describeSecurityGroupRequest, describeSecurityGroupResponse); requestResponseMap.put(authorizeSecurityGroupIngressRequest22, authorizeSecurityGroupIngressResponse); requestResponseMap.put(authorizeSecurityGroupIngressRequestGroup, authorizeSecurityGroupIngressResponse); requestResponseMap.put(runInstancesRequest, runInstancesResponse); requestResponseMap.put(describeInstanceRequest, describeInstanceResponse); requestResponseMap.put(describeInstanceMultiIdsRequest, describeInstanceMultiIdsResponse); requestResponseMap.put(describeImageRequest, describeImagesResponse); ComputeService apiThatCreatesNode = requestsSendResponses(requestResponseMap.build()); NodeMetadata node = Iterables.getOnlyElement( apiThatCreatesNode.createNodesInGroup("test", 1, apiThatCreatesNode.templateBuilder().from("osDescriptionMatches=.*fedora.*,loginUser=ec2-user").build())); assertEquals(node.getCredentials().getUser(), "ec2-user"); assertNotNull(node.getCredentials().getPrivateKey()); }
@Test(enabled = true, dependsOnMethods = { "testListNodes", "testGetNodesWithDetails", "testListNodesByIds" }) public void testDestroyNodes() { int toDestroy = refreshNodes().size(); Set<? extends NodeMetadata> destroyed = client.destroyNodesMatching(inGroup(group)); assertEquals(toDestroy, destroyed.size()); for (NodeMetadata node : filter(client.listNodesDetailsMatching(all()), inGroup(group))) { assert node.getStatus() == Status.TERMINATED : node; assert view.utils().credentialStore().get("node#" + node.getId()) == null : "credential should have been null for " + "node#" + node.getId(); } }
@Test(enabled = true, dependsOnMethods = "testConcurrentUseOfComputeServiceToCreateNodes") public void testCreateTwoNodesWithRunScript() throws Exception { try { client.destroyNodesMatching(inGroup(group)); } catch (NoSuchElementException e) { } refreshTemplate(); try { nodes = newTreeSet(client.createNodesInGroup(group, 2, template)); } catch (RunNodesException e) { nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet())); throw e; } assertEquals(nodes.size(), 2); checkNodes(nodes, group, "bootstrap"); NodeMetadata node1 = nodes.first(); NodeMetadata node2 = nodes.last(); // credentials aren't always the same // assertEquals(node1.getCredentials(), node2.getCredentials()); assertLocationSameOrChild(checkNotNull(node1.getLocation(), "location of %s", node1), template.getLocation()); assertLocationSameOrChild(checkNotNull(node2.getLocation(), "location of %s", node2), template.getLocation()); checkImageIdMatchesTemplate(node1); checkImageIdMatchesTemplate(node2); checkOsMatchesTemplate(node1); checkOsMatchesTemplate(node2); }
@Test(groups = "Live") public void testVmMetadata() { loc = (JcloudsLocation) mgmt().getLocationRegistry().getLocationManaged(PROVIDER + (REGION_NAME == null ? "" : ":" + REGION_NAME)); SshMachineLocation machine = obtainMachine(MutableMap.of("imageId", IMAGE_ID, "userMetadata", MutableMap.of("mykey", "myval"), JcloudsLocation.MACHINE_CREATE_ATTEMPTS, 2)); LOG.info("Provisioned {} vm {}; checking metadata and if ssh'able", PROVIDER, machine); Map<String,String> userMetadata = ((JcloudsSshMachineLocation)machine).getNode().getUserMetadata(); assertEquals(userMetadata.get("mykey"), "myval", "metadata="+userMetadata); assertTrue(machine.isSshable()); } }
@Test(enabled = true, dependsOnMethods = "testSuspendResume") public void testGetNodesWithDetails() throws Exception { for (NodeMetadata node : client.listNodesDetailsMatching(all())) { assert node.getProviderId() != null : node; assert node.getLocation() != null : node; assertEquals(node.getType(), ComputeType.NODE); assert node instanceof NodeMetadata; NodeMetadata nodeMetadata = node; assert nodeMetadata.getProviderId() != null : nodeMetadata; // nullable // assert nodeMetadata.getImage() != null : node; // user specified name is not always supported // assert nodeMetadata.getName() != null : nodeMetadata; if (nodeMetadata.getStatus() == Status.RUNNING) { assert nodeMetadata.getPublicAddresses() != null : nodeMetadata; assert nodeMetadata.getPublicAddresses().size() > 0 || nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata; assertNotNull(nodeMetadata.getPrivateAddresses()); } } }
@Test(expectedExceptions = NoSuchElementException.class) public void testGracefulWhenVDCIsNotLocation() { Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/11111").description("description") .scope(LocationScope.PROVIDER).build(); Injector injector = createInjectorWithLocation(location); InputStream is = getClass().getResourceAsStream("/vapp-pool.xml"); Factory factory = injector.getInstance(ParseSax.Factory.class); VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is); VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class); NodeMetadata node = converter.apply(result); assertNotNull(node); assertEquals(node.getLocation(), location); } }
@Test public void testCreateImage() throws RunNodesException { NodeMetadata node = getOnlyElement(view.getComputeService().createNodesInGroup(group, 1, resourceGroup(group))); IdReference vmIdRef = IdReference.create(node.getProviderId()); view.getComputeService().suspendNode(node.getId()); api.getVirtualMachineApi(group).generalize(node.getName()); image = imageApi.createOrUpdate(imageName, location, ImageProperties.builder() .sourceVirtualMachine(vmIdRef).build()); assertNotNull(image); }
@Test(dependsOnMethods = "testCanUpdateRunList") public void testRunNodesWithBootstrap() throws IOException { Statement bootstrap = view.getChefService().createBootstrapScriptForGroup(group); try { nodes = computeContext.getComputeService().createNodesInGroup(group, 1, runScript(bootstrap)); } catch (RunNodesException e) { nodes = concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()); } for (NodeMetadata node : nodes) { URI uri = URI.create("http://" + getLast(node.getPublicAddresses())); InputStream content = computeContext.utils().http().get(uri); String string = Strings2.toStringAndClose(content); assert string.indexOf("It works!") >= 0 : string; } }
private void checkSoftlayer(String spec) { FixedListMachineProvisioningLocation<JcloudsSshMachineLocation> loc = resolve(spec); Set<JcloudsSshMachineLocation> machines = loc.getAllMachines(); JcloudsSshMachineLocation machine = Iterables.getOnlyElement(machines); assertEquals(machine.getParent().getProvider(), "softlayer"); assertEquals(machine.getNode().getId(), slVmInstanceId); assertEquals(machine.getAddress().getHostAddress(), slVmIp); assertTrue(slVmHostname.equals(machine.getAddress().getHostName()) || slVmIp.equals(machine.getAddress().getHostName()), "address hostname is: "+machine.getAddress().getHostName()); assertTrue(slVmHostname.equals(machine.getNode().getHostname()) || slVmIp.equals(machine.getNode().getHostname()), "node hostname is: "+machine.getNode().getHostname()); // could also assert this, given a user credential, but not currently set up // assertTrue(machine.isSshable()); }