You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2018/06/29 10:47:38 UTC

[2/3] brooklyn-server git commit: Add yaml test for MultiLocation

Add yaml test for MultiLocation


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/977bcc31
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/977bcc31
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/977bcc31

Branch: refs/heads/master
Commit: 977bcc310773e02a8a17fb47dbcfe83785f008d6
Parents: 59a0b91
Author: Aled Sage <al...@gmail.com>
Authored: Wed Jun 6 07:45:14 2018 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Jun 7 20:54:18 2018 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/MultiLocationYamlTest.java    | 160 +++++++++++++++++++
 1 file changed, 160 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/977bcc31/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/MultiLocationYamlTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/MultiLocationYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/MultiLocationYamlTest.java
new file mode 100644
index 0000000..91ee576
--- /dev/null
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/MultiLocationYamlTest.java
@@ -0,0 +1,160 @@
+/*
+ * 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.camp.brooklyn;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.api.location.MachineLocation;
+import org.apache.brooklyn.api.location.MachineProvisioningLocation;
+import org.apache.brooklyn.api.location.NoMachinesAvailableException;
+import org.apache.brooklyn.core.location.cloud.AvailabilityZoneExtension;
+import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation;
+import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation;
+import org.apache.brooklyn.location.multi.MultiLocation;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.net.UserAndHostAndPort;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+
+public class MultiLocationYamlTest extends AbstractYamlTest {
+    private static final Logger log = LoggerFactory.getLogger(MultiLocationYamlTest.class);
+
+    @Test
+    public void testSimpleInSingleLine() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "location: multi:(targets=\"localhost,localhost\")",
+                "services:",
+                "- type: org.apache.brooklyn.entity.stock.BasicApplication");
+        runSimple(yaml);
+    }
+
+    @Test
+    public void testSimpleMultiLine() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "location:",
+                "  multi:",
+                "    targets:",
+                "    - localhost",
+                "    - localhost",
+                "services:",
+                "- type: org.apache.brooklyn.entity.stock.BasicApplication");
+        runSimple(yaml);
+    }
+
+    @SuppressWarnings("unchecked")
+    protected void runSimple(String yaml) throws Exception {
+        Entity app = createStartWaitAndLogApplication(yaml);
+        MultiLocation<MachineLocation> multiLoc = (MultiLocation<MachineLocation>) Iterables.get(app.getLocations(), 0);
+        
+        assertMultiLocation(multiLoc, 2, 
+                Collections.nCopies(2, Predicates.instanceOf(LocalhostMachineProvisioningLocation.class)));
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testComplex() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "location:",
+                "  multi:",
+                "    targets:",
+                "    - byon:",
+                "        hosts:",
+                "        - \"myuser@1.1.1.1\"",
+                "    - byon:",
+                "        hosts:",
+                "        - \"myuser2@2.2.2.2\"",
+                "services:",
+                "- type: org.apache.brooklyn.entity.stock.BasicApplication");
+        
+        Entity app = createStartWaitAndLogApplication(yaml);
+        MultiLocation<MachineLocation> multiLoc = (MultiLocation<MachineLocation>) Iterables.get(app.getLocations(), 0);
+
+        assertMultiLocation(multiLoc, 2, ImmutableList.of(
+                byonEqualTo(UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 22)),
+                byonEqualTo(UserAndHostAndPort.fromParts("myuser2", "2.2.2.2", 22))));
+    }
+
+    
+    private void assertMultiLocation(MultiLocation<?> multiLoc, int expectedSize, List<? extends Predicate<? super Location>> expectedSubLocationPredicates) {
+        AvailabilityZoneExtension zones = multiLoc.getExtension(AvailabilityZoneExtension.class);
+        List<Location> subLocs = zones.getAllSubLocations();
+        assertEquals(subLocs.size(), expectedSize, "zones="+subLocs);
+        for (int i = 0; i < subLocs.size(); i++) {
+            Location subLoc = subLocs.get(i);
+            assertTrue(expectedSubLocationPredicates.get(i).apply(subLoc), "index="+i+"; subLocs="+subLocs);
+        }
+    }
+    
+    public static Predicate<? super Location> byonEqualTo(UserAndHostAndPort... conns) {
+        return Predicates.and(
+                Predicates.instanceOf(FixedListMachineProvisioningLocation.class),
+                new Predicate<Location>() {
+                    @SuppressWarnings("unchecked")
+                    @Override public boolean apply(Location rawInput) {
+                        MachineProvisioningLocation<SshMachineLocation> input = (MachineProvisioningLocation<SshMachineLocation>) rawInput;
+                        List<SshMachineLocation> obtainedMachines = new ArrayList<>();
+                        try {
+                            for (UserAndHostAndPort conn : conns) {
+                                SshMachineLocation machine = (SshMachineLocation) input.obtain(ImmutableMap.of());
+                                obtainedMachines.add(machine);
+                                if (!machineEqualTo(machine, conn)) {
+                                    return false;
+                                }
+                            }
+                        } catch (NoMachinesAvailableException e) {
+                            throw Exceptions.propagate(e);
+                        } finally {
+                            for (SshMachineLocation machine : obtainedMachines) {
+                                input.release(machine);
+                            }
+                        }
+                        return true;
+                    }
+                    private boolean machineEqualTo(SshMachineLocation machine, UserAndHostAndPort conn) {
+                        String addr = machine.getAddress().getHostAddress();
+                        int port = machine.getPort();
+                        String user = machine.getUser();
+                        return addr != null && addr.equals(conn.getHostAndPort().getHostText())
+                                && port == conn.getHostAndPort().getPortOrDefault(22)
+                                && user != null && user.equals(conn.getUser());
+                    }
+                });
+    }
+    
+    @Override
+    protected Logger getLogger() {
+        return log;
+    }
+}