Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C6DB9200BA7 for ; Thu, 6 Oct 2016 13:50:40 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id C58B7160AAD; Thu, 6 Oct 2016 11:50:40 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 46CA3160AED for ; Thu, 6 Oct 2016 13:50:39 +0200 (CEST) Received: (qmail 392 invoked by uid 500); 6 Oct 2016 11:50:38 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 341 invoked by uid 99); 6 Oct 2016 11:50:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Oct 2016 11:50:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E6A6AE04BE; Thu, 6 Oct 2016 11:50:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aledsage@apache.org To: commits@brooklyn.apache.org Date: Thu, 06 Oct 2016 11:50:38 -0000 Message-Id: <2585bb0d3b6145d99756ff87b3064185@git.apache.org> In-Reply-To: <6e4b180e3a3d4a53b37c1c29f28bcf80@git.apache.org> References: <6e4b180e3a3d4a53b37c1c29f28bcf80@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] brooklyn-server git commit: Extract AbstractJcloudsStubbedUnitTest archived-at: Thu, 06 Oct 2016 11:50:41 -0000 Extract AbstractJcloudsStubbedUnitTest Extracted from JcloudsReachableAddressStubbedTest, and makes StubbedComputeServiceRegistry configurable for whether it should delegate at all to the cloud provider (or stub everything). Also updates JcloudsSshMachineLocation, so we only call computeContext().getContext().utils().injector().getInstance(RunScriptOnNode.Factory) if we really need to. This change only affects deprecated code paths. Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/c2669da8 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/c2669da8 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/c2669da8 Branch: refs/heads/master Commit: c2669da89c79ff62cfb0082b040274494e59170f Parents: adf71d7 Author: Aled Sage Authored: Tue Oct 4 16:54:44 2016 +0100 Committer: Aled Sage Committed: Thu Oct 6 11:06:02 2016 +0100 ---------------------------------------------------------------------- .../jclouds/JcloudsSshMachineLocation.java | 27 +-- .../jclouds/AbstractJcloudsStubbedUnitTest.java | 107 +++++++++ .../jclouds/StubbedComputeServiceRegistry.java | 60 ++++- .../jclouds/UnsupportedComputeService.java | 222 +++++++++++++++++++ .../JcloudsReachableAddressStubbedTest.java | 131 ++++------- 5 files changed, 444 insertions(+), 103 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2669da8/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocation.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocation.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocation.java index 3ef596a..0ec99e5 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocation.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsSshMachineLocation.java @@ -132,8 +132,6 @@ public class JcloudsSshMachineLocation extends SshMachineLocation implements Jcl private transient Optional _image; - private RunScriptOnNode.Factory runScriptFactory; - public JcloudsSshMachineLocation() { } @@ -152,8 +150,6 @@ public class JcloudsSshMachineLocation extends SshMachineLocation implements Jcl public void init() { if (jcloudsParent != null) { super.init(); - ComputeServiceContext context = jcloudsParent.getComputeService().getContext(); - runScriptFactory = context.utils().injector().getInstance(RunScriptOnNode.Factory.class); } else { // TODO Need to fix the rebind-detection, and not call init() on rebind. // This will all change when locations become entities. @@ -167,13 +163,10 @@ public class JcloudsSshMachineLocation extends SshMachineLocation implements Jcl public void rebind() { super.rebind(); - if (jcloudsParent != null) { + if (jcloudsParent == null) { // can be null on rebind, if location has been "orphaned" somehow - ComputeServiceContext context = jcloudsParent.getComputeService().getContext(); - runScriptFactory = context.utils().injector().getInstance(RunScriptOnNode.Factory.class); - } else { - LOG.warn("Location {} does not have parent; cannot retrieve jclouds compute-service or " - + "run-script factory; later operations may fail (continuing)", this); + LOG.warn("Location {} does not have parent; cannot retrieve jclouds compute-service; " + + "later operations may fail (continuing)", this); } clearDeprecatedProperties(); } @@ -443,12 +436,20 @@ public class JcloudsSshMachineLocation extends SshMachineLocation implements Jcl */ @Deprecated public ListenableFuture submitRunScript(Statement script, RunScriptOptions options) { + JcloudsLocation jcloudsParent = getParent(); Optional node = getOptionalNode(); - if (node.isPresent()) { - return runScriptFactory.submit(node.get(), script, options); - } else { + + if (!node.isPresent()) { throw new IllegalStateException("Node "+nodeId+" not present in "+getParent()); } + if (jcloudsParent == null) { + throw new IllegalStateException("No jclouds parent location for "+this+"; cannot retrieve jclouds script-runner"); + } + + ComputeServiceContext context = jcloudsParent.getComputeService().getContext(); + RunScriptOnNode.Factory runScriptFactory = context.utils().injector().getInstance(RunScriptOnNode.Factory.class); + + return runScriptFactory.submit(node.get(), script, options); } /** http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2669da8/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java new file mode 100644 index 0000000..71111d1 --- /dev/null +++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/AbstractJcloudsStubbedUnitTest.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.location.jclouds; + +import java.util.Map; + +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.NodeCreator; +import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.location.winrm.WinRmMachineLocation; +import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; +import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; + +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableMap; + +/** + * Stubs out all comms with the cloud provider. + * + * Expects sub-classes to call {@link #initNodeCreatorAndJcloudsLocation(NodeCreator, Map)} before + * the test methods are called. + */ +public abstract class AbstractJcloudsStubbedUnitTest extends AbstractJcloudsLiveTest { + + @SuppressWarnings("unused") + private static final Logger LOG = LoggerFactory.getLogger(AbstractJcloudsStubbedUnitTest.class); + + // TODO These values are hard-coded into the JcloudsStubTemplateBuilder, so best not to mess! + public static final String LOCATION_SPEC = "jclouds:aws-ec2:us-east-1"; + + protected NodeCreator nodeCreator; + protected ComputeServiceRegistry computeServiceRegistry; + + @BeforeMethod(alwaysRun=true) + @Override + public void setUp() throws Exception { + super.setUp(); + RecordingSshTool.clear(); + RecordingWinRmTool.clear(); + } + + @AfterMethod(alwaysRun=true) + @Override + public void tearDown() throws Exception { + try { + super.tearDown(); + } finally { + RecordingSshTool.clear(); + RecordingWinRmTool.clear(); + } + } + + @Override + protected LocalManagementContext newManagementContext() { + return LocalManagementContextForTests.builder(true).build(); + } + + /** + * Expect sub-classes to call this - either in their {@link BeforeMethod} or at the very + * start of the test method (to allow custom config per test). + */ + protected void initNodeCreatorAndJcloudsLocation(NodeCreator nodeCreator, Map jcloudsLocationConfig) throws Exception { + this.nodeCreator = nodeCreator; + this.computeServiceRegistry = new StubbedComputeServiceRegistry(nodeCreator, false); + + this.jcloudsLocation = (JcloudsLocation)managementContext.getLocationRegistry().getLocationManaged( + getLocationSpec(), + ImmutableMap.builder() + .put(JcloudsLocationConfig.COMPUTE_SERVICE_REGISTRY, computeServiceRegistry) + .put(JcloudsLocationConfig.TEMPLATE_BUILDER, JcloudsStubTemplateBuilder.create()) + .put(JcloudsLocationConfig.ACCESS_IDENTITY, "stub-identity") + .put(JcloudsLocationConfig.ACCESS_CREDENTIAL, "stub-credential") + .put(SshMachineLocation.SSH_TOOL_CLASS, RecordingSshTool.class.getName()) + .put(WinRmMachineLocation.WINRM_TOOL_CLASS, RecordingWinRmTool.class.getName()) + .put(JcloudsLocation.POLL_FOR_FIRST_REACHABLE_ADDRESS_PREDICATE, Predicates.alwaysTrue()) + .putAll(jcloudsLocationConfig) + .build()); + } + + /** + * For overriding. + */ + protected String getLocationSpec() { + return LOCATION_SPEC; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2669da8/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/StubbedComputeServiceRegistry.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/StubbedComputeServiceRegistry.java b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/StubbedComputeServiceRegistry.java index 1d18a5f..99cd321 100644 --- a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/StubbedComputeServiceRegistry.java +++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/StubbedComputeServiceRegistry.java @@ -78,10 +78,10 @@ public class StubbedComputeServiceRegistry implements ComputeServiceRegistry { } } - static class StubbedComputeService extends DelegatingComputeService { + static class MinimalComputeService extends DelegatingComputeService { private final NodeCreator nodeCreator; - public StubbedComputeService(ComputeService delegate, NodeCreator nodeCreator) { + public MinimalComputeService(ComputeService delegate, NodeCreator nodeCreator) { super(delegate); this.nodeCreator = nodeCreator; } @@ -111,19 +111,69 @@ public class StubbedComputeServiceRegistry implements ComputeServiceRegistry { } } + static class StubbedComputeService extends UnsupportedComputeService { + private final NodeCreator nodeCreator; + + public StubbedComputeService(NodeCreator nodeCreator) { + this.nodeCreator = nodeCreator; + } + @Override + public Set createNodesInGroup(String group, int count, Template template) throws RunNodesException { + return nodeCreator.createNodesInGroup(group, count, template); + } + @Override + public void destroyNode(String id) { + nodeCreator.destroyNode(id); + } + @Override + public Set listNodesDetailsMatching(Predicate filter) { + return nodeCreator.listNodesDetailsMatching(filter); + } + @Override + public Set createNodesInGroup(String group, int count) { + throw new UnsupportedOperationException(); + } + @Override + public Set createNodesInGroup(String group, int count, TemplateOptions templateOptions) { + throw new UnsupportedOperationException(); + } + @Override + public Set destroyNodesMatching(Predicate filter) { + throw new UnsupportedOperationException(); + } + } + private final NodeCreator nodeCreator; - + private final boolean allowCloudQueries; + public StubbedComputeServiceRegistry(NodeMetadata node) throws Exception { this(new SingleNodeCreator(node)); } public StubbedComputeServiceRegistry(NodeCreator nodeCreator) throws Exception { + this(nodeCreator, true); + } + + public StubbedComputeServiceRegistry(NodeCreator nodeCreator, boolean allowCloudQueries) throws Exception { this.nodeCreator = nodeCreator; + this.allowCloudQueries = allowCloudQueries; } + /** + * If using {@link #allowCloudQueries}, then we'll go through the jclouds code to instantiate + * a delegate {@link ComputeService}. That takes about a second (because of everything guice + * does), so is unpleasant to do in unit tests. + * + * Better is to create the {@link StubbedComputeServiceRegistry} with that disabled, which will + * throw an exception if any unexpected method is called on {@link ComputeService}. + */ @Override public ComputeService findComputeService(ConfigBag conf, boolean allowReuse) { - ComputeService delegate = ComputeServiceRegistryImpl.INSTANCE.findComputeService(conf, allowReuse); - return new StubbedComputeService(delegate, nodeCreator); + if (allowCloudQueries) { + ComputeService delegate = ComputeServiceRegistryImpl.INSTANCE.findComputeService(conf, allowReuse); + return new MinimalComputeService(delegate, nodeCreator); + } else { + return new StubbedComputeService(nodeCreator); + } } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2669da8/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/UnsupportedComputeService.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/UnsupportedComputeService.java b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/UnsupportedComputeService.java new file mode 100644 index 0000000..6bd89a6 --- /dev/null +++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/UnsupportedComputeService.java @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.location.jclouds; + +import java.util.Map; +import java.util.Set; + +import org.jclouds.compute.ComputeService; +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.RunNodesException; +import org.jclouds.compute.RunScriptOnNodesException; +import org.jclouds.compute.domain.ComputeMetadata; +import org.jclouds.compute.domain.ExecResponse; +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.Template; +import org.jclouds.compute.domain.TemplateBuilder; +import org.jclouds.compute.extensions.ImageExtension; +import org.jclouds.compute.extensions.SecurityGroupExtension; +import org.jclouds.compute.options.RunScriptOptions; +import org.jclouds.compute.options.TemplateOptions; +import org.jclouds.domain.Location; +import org.jclouds.scriptbuilder.domain.Statement; + +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.util.concurrent.ListenableFuture; + +public class UnsupportedComputeService implements ComputeService { + + @Override + public ComputeServiceContext getContext() { + throw new UnsupportedOperationException(); + } + + @Override + public TemplateBuilder templateBuilder() { + throw new UnsupportedOperationException(); + } + + @Override + public TemplateOptions templateOptions() { + throw new UnsupportedOperationException(); + } + + @Override + public Set listHardwareProfiles() { + throw new UnsupportedOperationException(); + } + + @Override + public Set listImages() { + throw new UnsupportedOperationException(); + } + + @Override + public Image getImage(String id) { + throw new UnsupportedOperationException(); + } + + @Override + public Set listNodes() { + throw new UnsupportedOperationException(); + } + + @Override + public Set listNodesByIds(Iterable ids) { + throw new UnsupportedOperationException(); + } + + @Override + public Set listAssignableLocations() { + throw new UnsupportedOperationException(); + } + + @Override + public Set createNodesInGroup(String group, int count, Template template) throws RunNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public Set createNodesInGroup(String group, int count, TemplateOptions templateOptions) + throws RunNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public Set createNodesInGroup(String group, int count) throws RunNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public void resumeNode(String id) { + throw new UnsupportedOperationException(); + } + + @Override + public Set resumeNodesMatching(Predicate filter) { + throw new UnsupportedOperationException(); + } + + @Override + public void suspendNode(String id) { + throw new UnsupportedOperationException(); + } + + @Override + public Set suspendNodesMatching(Predicate filter) { + throw new UnsupportedOperationException(); + } + + @Override + public void destroyNode(String id) { + throw new UnsupportedOperationException(); + } + + @Override + public Set destroyNodesMatching(Predicate filter) { + throw new UnsupportedOperationException(); + } + + @Override + public void rebootNode(String id) { + throw new UnsupportedOperationException(); + } + + @Override + public Set rebootNodesMatching(Predicate filter) { + throw new UnsupportedOperationException(); + } + + @Override + public NodeMetadata getNodeMetadata(String id) { + throw new UnsupportedOperationException(); + } + + @Override + public Set listNodesDetailsMatching(Predicate filter) { + throw new UnsupportedOperationException(); + } + + @Override + public Map runScriptOnNodesMatching(Predicate filter, String runScript) + throws RunScriptOnNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public Map runScriptOnNodesMatching(Predicate filter, Statement runScript) + throws RunScriptOnNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public Map runScriptOnNodesMatching(Predicate filter, + String runScript, RunScriptOptions options) throws RunScriptOnNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public Map runScriptOnNodesMatching(Predicate filter, + Statement runScript, RunScriptOptions options) throws RunScriptOnNodesException { + throw new UnsupportedOperationException(); + } + + @Override + public ExecResponse runScriptOnNode(String id, Statement runScript, RunScriptOptions options) { + throw new UnsupportedOperationException(); + } + + @Override + public ListenableFuture submitScriptOnNode(String id, String runScript, RunScriptOptions options) { + throw new UnsupportedOperationException(); + } + + @Override + public ListenableFuture submitScriptOnNode(String id, Statement runScript, RunScriptOptions options) { + throw new UnsupportedOperationException(); + } + + @Override + public ExecResponse runScriptOnNode(String id, Statement runScript) { + throw new UnsupportedOperationException(); + } + + @Override + public ExecResponse runScriptOnNode(String id, String runScript, RunScriptOptions options) { + throw new UnsupportedOperationException(); + } + + @Override + public ExecResponse runScriptOnNode(String id, String runScript) { + throw new UnsupportedOperationException(); + } + + @Override + public Optional getImageExtension() { + throw new UnsupportedOperationException(); + } + + @Override + public Optional getSecurityGroupExtension() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2669da8/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/networking/JcloudsReachableAddressStubbedTest.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/networking/JcloudsReachableAddressStubbedTest.java b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/networking/JcloudsReachableAddressStubbedTest.java index 9a5f168..8171921 100644 --- a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/networking/JcloudsReachableAddressStubbedTest.java +++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/networking/JcloudsReachableAddressStubbedTest.java @@ -28,19 +28,14 @@ import java.util.Map; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.location.access.PortForwardManager; import org.apache.brooklyn.core.location.access.PortForwardManagerImpl; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; -import org.apache.brooklyn.location.jclouds.AbstractJcloudsLiveTest; -import org.apache.brooklyn.location.jclouds.ComputeServiceRegistry; +import org.apache.brooklyn.location.jclouds.AbstractJcloudsStubbedUnitTest; import org.apache.brooklyn.location.jclouds.JcloudsLocation; import org.apache.brooklyn.location.jclouds.JcloudsLocationConfig; import org.apache.brooklyn.location.jclouds.JcloudsSshMachineLocation; -import org.apache.brooklyn.location.jclouds.JcloudsStubTemplateBuilder; import org.apache.brooklyn.location.jclouds.JcloudsWinRmMachineLocation; -import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry; import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.AbstractNodeCreator; -import org.apache.brooklyn.location.jclouds.networking.JcloudsPortForwardingStubbedLiveTest.RecordingJcloudsPortForwarderExtension; -import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.location.jclouds.StubbedComputeServiceRegistry.SingleNodeCreator; +import org.apache.brooklyn.location.jclouds.networking.JcloudsPortForwardingStubbedTest.RecordingJcloudsPortForwarderExtension; import org.apache.brooklyn.location.winrm.WinRmMachineLocation; import org.apache.brooklyn.test.Asserts; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; @@ -54,11 +49,9 @@ import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.OsFamily; -import org.jclouds.compute.domain.Template; import org.jclouds.domain.LoginCredentials; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -69,13 +62,9 @@ import com.google.common.collect.Lists; import com.google.common.net.HostAndPort; /** - * The VM creation is stubbed out, but it still requires live access (i.e. real account credentials) - * to generate the template etc. - * * Simulates the creation of a VM that has multiple IPs. Checks that we choose the right address. - * */ -public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest { +public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsStubbedUnitTest { // TODO Aim is to test the various situations/permutations, where we pass in different config. // More tests still need to be added. @@ -83,9 +72,6 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest @SuppressWarnings("unused") private static final Logger LOG = LoggerFactory.getLogger(JcloudsReachableAddressStubbedTest.class); - protected StubbedComputeServiceRegistry.NodeCreator nodeCreator; - protected ComputeServiceRegistry computeServiceRegistry; - protected String reachableIp; protected AddressChooser addressChooser; protected CustomResponseGeneratorImpl customResponseGenerator; @@ -93,66 +79,22 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest @BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { + super.setUp(); reachableIp = null; // expect test method to set this addressChooser = new AddressChooser(); customResponseGenerator = new CustomResponseGeneratorImpl(); - RecordingSshTool.clear(); - RecordingWinRmTool.clear(); - super.setUp(); } - @AfterMethod(alwaysRun=true) - @Override - public void tearDown() throws Exception { - try { - super.tearDown(); - } finally { - RecordingSshTool.clear(); - RecordingWinRmTool.clear(); - } - } - - @Override - protected LocalManagementContext newManagementContext() { - return LocalManagementContextForTests.builder(true).build(); - } - protected AbstractNodeCreator newNodeCreator(final List publicAddresses, final List privateAddresses) { - return new AbstractNodeCreator() { - int nextIpSuffix = 2; - @Override - protected NodeMetadata newNode(String group, Template template) { - int ipSuffix = nextIpSuffix++; - NodeMetadata result = new NodeMetadataBuilder() - .id("myid-"+ipSuffix) - .credentials(LoginCredentials.builder().identity("myuser").credential("mypassword").build()) - .loginPort(22) - .status(Status.RUNNING) - .publicAddresses(publicAddresses) - .privateAddresses(privateAddresses) - .build(); - return result; - } - }; - } - - public String getLocationSpec() { - return "jclouds:aws-ec2"; - } - - protected void initNodeCreatorAndJcloudsLocation(List publicAddresses, List privateAddresses, Map jcloudsLocationConfig) throws Exception { - nodeCreator = newNodeCreator(publicAddresses, privateAddresses); - computeServiceRegistry = new StubbedComputeServiceRegistry(nodeCreator); - - jcloudsLocation = (JcloudsLocation)managementContext.getLocationRegistry().getLocationManaged( - getLocationSpec(), - ImmutableMap.builder() - .put(JcloudsLocationConfig.COMPUTE_SERVICE_REGISTRY, computeServiceRegistry) - .put(JcloudsLocationConfig.TEMPLATE_BUILDER, JcloudsStubTemplateBuilder.create()) - .put(JcloudsLocationConfig.ACCESS_IDENTITY, "stub-identity") - .put(JcloudsLocationConfig.ACCESS_CREDENTIAL, "stub-credential") - .putAll(jcloudsLocationConfig) - .build()); + NodeMetadata node = new NodeMetadataBuilder() + .id("myid-1") + .credentials(LoginCredentials.builder().identity("myuser").credential("mypassword").build()) + .loginPort(22) + .status(Status.RUNNING) + .publicAddresses(publicAddresses) + .privateAddresses(privateAddresses) + .build(); + return new SingleNodeCreator(node); } /** @@ -161,8 +103,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testMachineUsesVanillaPublicAddress() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = "1.1.1.1"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) @@ -182,8 +126,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testWindowsMachineUsesVanillaPublicAddress() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = "1.1.1.1"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsWinRmMachineLocation machine = newWinrmMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_WINRM_AVAILABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) @@ -204,8 +150,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testMachineUsesVanillaPrivateAddress() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = "2.1.1.1"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) @@ -226,8 +174,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testMachineUsesReachablePublicAddress() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.2"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.3"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = "1.1.1.2"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) @@ -248,8 +198,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testMachineUsesReachablePrivateAddress() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1"), ImmutableList.of("2.1.1.1", "2.1.1.2", "2.1.1.2"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1"); + List privateAddresses = ImmutableList.of("2.1.1.1", "2.1.1.2", "2.1.1.3"); reachableIp = "2.1.1.2"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) @@ -270,8 +222,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testNoWaitFroSshable() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.2"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.3"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = null; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, "false") @@ -291,8 +245,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testNoPollForFirstReachable() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.2"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.3"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = null; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) @@ -308,8 +264,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest @Test public void testReachabilityChecksWithPortForwarding() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = "1.2.3.4"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); PortForwardManager pfm = new PortForwardManagerImpl(); RecordingJcloudsPortForwarderExtension portForwarder = new RecordingJcloudsPortForwarderExtension(pfm); @@ -337,8 +295,10 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest */ @Test public void testWindowsReachabilityChecksWithPortForwarding() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("1.1.1.1"), ImmutableList.of("2.1.1.1"), ImmutableMap.of()); + List publicAddresses = ImmutableList.of("1.1.1.1"); + List privateAddresses = ImmutableList.of("2.1.1.1"); reachableIp = "1.2.3.4"; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); PortForwardManager pfm = new PortForwardManagerImpl(); RecordingJcloudsPortForwarderExtension portForwarder = new RecordingJcloudsPortForwarderExtension(pfm); @@ -362,15 +322,17 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest @Test public void testMachineUsesFirstPublicAddress() throws Exception { - initNodeCreatorAndJcloudsLocation(ImmutableList.of("10.10.10.1", "10.10.10.2"), ImmutableList.of("1.1.1.1", "1.1.1.2", "1.1.1.2"), ImmutableMap.of()); - reachableIp = "10.10.10.1"; + List publicAddresses = ImmutableList.of("1.1.1.1", "1.1.1.2"); + List privateAddresses = ImmutableList.of("2.1.1.1", "2.1.1.2", "2.1.1.3"); + reachableIp = null; + initNodeCreatorAndJcloudsLocation(newNodeCreator(publicAddresses, privateAddresses), ImmutableMap.of()); JcloudsSshMachineLocation machine = newMachine(ImmutableMap.,Object>builder() .put(JcloudsLocationConfig.WAIT_FOR_SSHABLE, Asserts.DEFAULT_LONG_TIMEOUT.toString()) .put(JcloudsLocation.POLL_FOR_FIRST_REACHABLE_ADDRESS, false) .build()); - assertEquals(machine.getAddress().getHostAddress(), reachableIp); + assertEquals(machine.getAddress().getHostAddress(), "1.1.1.1"); } protected JcloudsSshMachineLocation newMachine() throws Exception { @@ -379,7 +341,6 @@ public class JcloudsReachableAddressStubbedTest extends AbstractJcloudsLiveTest protected JcloudsSshMachineLocation newMachine(Map, ?> additionalConfig) throws Exception { return obtainMachine(ImmutableMap.,Object>builder() - .put(SshMachineLocation.SSH_TOOL_CLASS, RecordingSshTool.class.getName()) .put(JcloudsLocation.POLL_FOR_FIRST_REACHABLE_ADDRESS_PREDICATE, addressChooser) .putAll(additionalConfig) .build());