brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [4/5] brooklyn-server git commit: Adds VanillaWindowsProcessTest (non-live)
Date Tue, 19 Jul 2016 19:05:03 GMT
Adds VanillaWindowsProcessTest (non-live)


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

Branch: refs/heads/master
Commit: d24e0de157b0d666e3ad570a1ed7b1c5b13fba7c
Parents: 915ffa7
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Jun 28 21:40:51 2016 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue Jun 28 22:05:37 2016 +0100

----------------------------------------------------------------------
 software/base/pom.xml                           |   7 +
 .../base/VanillaWindowsProcessTest.java         | 161 +++++++++++++++++++
 .../core/internal/winrm/RecordingWinRmTool.java | 107 ++++++++++++
 3 files changed, 275 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d24e0de1/software/base/pom.xml
----------------------------------------------------------------------
diff --git a/software/base/pom.xml b/software/base/pom.xml
index 04306fc..7e0d4fd 100644
--- a/software/base/pom.xml
+++ b/software/base/pom.xml
@@ -138,6 +138,13 @@
         </dependency>
         <dependency>
             <groupId>org.apache.brooklyn</groupId>
+            <artifactId>brooklyn-software-winrm</artifactId>
+            <version>${project.version}</version>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.brooklyn</groupId>
             <artifactId>brooklyn-locations-jclouds</artifactId>
             <version>${project.version}</version>
             <classifier>tests</classifier>

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d24e0de1/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
new file mode 100644
index 0000000..4523e11
--- /dev/null
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
@@ -0,0 +1,161 @@
+/*
+ * 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.entity.software.base;
+
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.util.List;
+
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.api.location.MachineLocation;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation;
+import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
+import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool;
+import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.ExecParams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport {
+
+    @SuppressWarnings("unused")
+    private static final Logger LOG = LoggerFactory.getLogger(VanillaWindowsProcessTest.class);
+
+    private Location loc;
+    
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        loc = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
+                .configure(FixedListMachineProvisioningLocation.MACHINE_SPECS, ImmutableList.<LocationSpec<?
extends MachineLocation>>of(
+                        LocationSpec.create(WinRmMachineLocation.class)
+                                .configure("address", "1.2.3.4")
+                                .configure(WinRmMachineLocation.WINRM_TOOL_CLASS, RecordingWinRmTool.class.getName()))));
+        
+        RecordingWinRmTool.clear();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        RecordingWinRmTool.clear();
+    }
+    
+    @Test
+    public void testAllCmds() throws Exception {
+        app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
+                .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
+                .configure(VanillaWindowsProcess.PRE_INSTALL_COMMAND, "preInstallCommand")
+                .configure(VanillaWindowsProcess.INSTALL_COMMAND, "installCommand")
+                .configure(VanillaWindowsProcess.POST_INSTALL_COMMAND, "postInstallCommand")
+                .configure(VanillaWindowsProcess.PRE_CUSTOMIZE_COMMAND, "preCustomizeCommand")
+                .configure(VanillaWindowsProcess.CUSTOMIZE_COMMAND, "customizeCommand")
+                .configure(VanillaWindowsProcess.POST_CUSTOMIZE_COMMAND, "postCustomizeCommand")
+                .configure(VanillaWindowsProcess.PRE_LAUNCH_COMMAND, "preLaunchCommand")
+                .configure(VanillaWindowsProcess.LAUNCH_COMMAND, "launchCommand")
+                .configure(VanillaWindowsProcess.POST_LAUNCH_COMMAND, "postLaunchCommand")
+                .configure(VanillaWindowsProcess.CHECK_RUNNING_COMMAND, "checkRunningCommand")
+                .configure(VanillaWindowsProcess.STOP_COMMAND, "stopCommand"));
+        app.start(ImmutableList.of(loc));
+
+        assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
+                "preInstallCommand", "installCommand", "postInstallCommand", 
+                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", 
+                "preLaunchCommand", "launchCommand", "postLaunchCommand", 
+                "checkRunningCommand"));
+        
+        app.stop();
+
+        assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
+    }
+    
+    @Test
+    public void testAllPowershell() throws Exception {
+        app.createAndManageChild(EntitySpec.create(VanillaWindowsProcess.class)
+                .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true)
+                .configure(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND, "preInstallCommand")
+                .configure(VanillaWindowsProcess.INSTALL_POWERSHELL_COMMAND, "installCommand")
+                .configure(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND, "postInstallCommand")
+                .configure(VanillaWindowsProcess.PRE_CUSTOMIZE_POWERSHELL_COMMAND, "preCustomizeCommand")
+                .configure(VanillaWindowsProcess.CUSTOMIZE_POWERSHELL_COMMAND, "customizeCommand")
+                .configure(VanillaWindowsProcess.POST_CUSTOMIZE_POWERSHELL_COMMAND, "postCustomizeCommand")
+                .configure(VanillaWindowsProcess.PRE_LAUNCH_POWERSHELL_COMMAND, "preLaunchCommand")
+                .configure(VanillaWindowsProcess.LAUNCH_POWERSHELL_COMMAND, "launchCommand")
+                .configure(VanillaWindowsProcess.POST_LAUNCH_POWERSHELL_COMMAND, "postLaunchCommand")
+                .configure(VanillaWindowsProcess.CHECK_RUNNING_POWERSHELL_COMMAND, "checkRunningCommand")
+                .configure(VanillaWindowsProcess.STOP_POWERSHELL_COMMAND, "stopCommand"));
+        app.start(ImmutableList.of(loc));
+
+        assertExecsContain(RecordingWinRmTool.getExecs(), ImmutableList.of(
+                "preInstallCommand", "installCommand", "postInstallCommand", 
+                "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", 
+                "preLaunchCommand", "launchCommand", "postLaunchCommand", 
+                "checkRunningCommand"));
+        
+        app.stop();
+
+        assertExecContains(RecordingWinRmTool.getLastExec(), "stopCommand");
+    }
+    
+    protected void assertExecsContain(List<? extends ExecParams> actuals, List<String>
expectedCmds) {
+        String errMsg = "actuals="+actuals+"; expected="+expectedCmds;
+        assertTrue(actuals.size() >= expectedCmds.size(), "actualSize="+actuals.size()+";
expectedSize="+expectedCmds.size()+"; "+errMsg);
+        for (int i = 0; i < expectedCmds.size(); i++) {
+            assertExecContains(actuals.get(i), expectedCmds.get(i), errMsg);
+        }
+    }
+
+    protected void assertExecContains(ExecParams actual, String expectedCmdRegex) {
+        assertExecContains(actual, expectedCmdRegex, null);
+    }
+    
+    protected void assertExecContains(ExecParams actual, String expectedCmdRegex, String
errMsg) {
+        for (String cmd : actual.commands) {
+            if (cmd.matches(expectedCmdRegex)) {
+                return;
+            }
+        }
+        fail(expectedCmdRegex + " not matched by any commands in " + actual+(errMsg != null
? "; "+errMsg : ""));
+    }
+
+    protected void assertExecsNotContains(List<? extends ExecParams> actuals, List<String>
expectedNotCmdRegexs) {
+        for (ExecParams actual : actuals) {
+            assertExecContains(actual, expectedNotCmdRegexs);
+        }
+    }
+    
+    protected void assertExecContains(ExecParams actual, List<String> expectedNotCmdRegexs)
{
+        for (String cmdRegex : expectedNotCmdRegexs) {
+            for (String subActual : actual.commands) {
+                if (subActual.matches(cmdRegex)) {
+                    fail("Exec should not contain " + cmdRegex + ", but matched by " + actual);
+                }
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d24e0de1/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
----------------------------------------------------------------------
diff --git a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.java
new file mode 100644
index 0000000..14fcd76
--- /dev/null
+++ b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/RecordingWinRmTool.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.util.core.internal.winrm;
+
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.brooklyn.util.stream.Streams;
+
+import com.google.common.base.Objects;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+
+/**
+ * For stubbing out the {@link WinRmTool}, so that no real winrm commands are executed.
+ * Records all the commands that are executed, so that assertions can subsequently be made.
+ * 
+ * By default, all commands return exit code 0, and no stdout/stderr.
+ */
+public class RecordingWinRmTool implements WinRmTool {
+
+    public enum ExecType {
+        COMMAND,
+        POWER_SHELL,
+        COPY_TO_SERVER;
+    }
+    
+    public static class ExecParams {
+        public final ExecType type;
+        public final List<String> commands;
+        
+        public ExecParams(ExecType type, List<String> commands) {
+            this.type = type;
+            this.commands = commands;
+        }
+        
+        @Override
+        public String toString() {
+            return Objects.toStringHelper(this)
+                    .add("type", type)
+                    .add("commands", commands)
+                    .toString();
+        }
+    }
+
+    
+    public static List<ExecParams> execs = Lists.newCopyOnWriteArrayList();
+    public static List<Map<?,?>> constructorProps = Lists.newCopyOnWriteArrayList();
+    
+    public static void clear() {
+        execs.clear();
+        constructorProps.clear();
+    }
+    
+    public static List<ExecParams> getExecs() {
+        return ImmutableList.copyOf(execs);
+    }
+    
+    public static ExecParams getLastExec() {
+        return execs.get(execs.size()-1);
+    }
+    
+    public RecordingWinRmTool(Map<?,?> props) {
+        constructorProps.add(props);
+    }
+    
+    @Override
+    public WinRmToolResponse executeCommand(List<String> commands) {
+        execs.add(new ExecParams(ExecType.COMMAND, commands));
+        return new WinRmToolResponse("", "", 0);
+    }
+
+    @Override
+    public WinRmToolResponse executePs(List<String> commands) {
+        execs.add(new ExecParams(ExecType.POWER_SHELL, commands));
+        return new WinRmToolResponse("", "", 0);
+    }
+
+    @Override
+    public WinRmToolResponse copyToServer(InputStream source, String destination) {
+        execs.add(new ExecParams(ExecType.COPY_TO_SERVER, ImmutableList.of(new String(Streams.readFully(source)))));
+        return new WinRmToolResponse("", "", 0);
+    }
+
+    @Override
+    @Deprecated
+    public WinRmToolResponse executeScript(List<String> commands) {
+        throw new UnsupportedOperationException();
+    }
+}


Mime
View raw message