From 8f70ab1609caa09487b38d479e9bf032e5f0b823 Mon Sep 17 00:00:00 2001 From: Ciprian Ciubotariu Date: Mon, 4 Jan 2016 02:00:59 +0200 Subject: [PATCH] Add more ways to configure wsman-cli test system Besides using the property file to configure the target winrm test system, also allow use of maven properties. --- .../java/org/apifocal/wsman/cli/Session.java | 5 +- .../wsman/cli/PropfileWinRMFixture.java | 52 ++++++++++++++ .../wsman/cli/SyspropsWinRMFixture.java | 71 +++++++++++++++++++ .../org/apifocal/wsman/cli/TestWsman.java | 34 +++------ .../org/apifocal/wsman/cli/WinRMFixture.java | 38 ++++++++++ .../src/test/resources/configTests.properties | 12 ++-- 6 files changed, 179 insertions(+), 33 deletions(-) create mode 100644 wsman-cli/src/test/java/org/apifocal/wsman/cli/PropfileWinRMFixture.java create mode 100644 wsman-cli/src/test/java/org/apifocal/wsman/cli/SyspropsWinRMFixture.java create mode 100644 wsman-cli/src/test/java/org/apifocal/wsman/cli/WinRMFixture.java diff --git a/wsman-cli/src/main/java/org/apifocal/wsman/cli/Session.java b/wsman-cli/src/main/java/org/apifocal/wsman/cli/Session.java index 78a35c4..279f724 100644 --- a/wsman-cli/src/main/java/org/apifocal/wsman/cli/Session.java +++ b/wsman-cli/src/main/java/org/apifocal/wsman/cli/Session.java @@ -8,7 +8,8 @@ /** * establish a wsman session using a {@link Protocol} */ -public class Session { +public class Session { + private final Protocol protocol; public Session(URL url, ITransport transport) { @@ -24,7 +25,7 @@ public CommandOutput runCmd(String command, String... args) { protocol.closeShell(shellId); return out; } - + /* execute a Powershell script (first encode it using base64) */ public CommandOutput runPs(String script) { //@TODO@ must use utf16 little endian on windows diff --git a/wsman-cli/src/test/java/org/apifocal/wsman/cli/PropfileWinRMFixture.java b/wsman-cli/src/test/java/org/apifocal/wsman/cli/PropfileWinRMFixture.java new file mode 100644 index 0000000..634335a --- /dev/null +++ b/wsman-cli/src/test/java/org/apifocal/wsman/cli/PropfileWinRMFixture.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016 apifocal. + * + * Licensed 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.apifocal.wsman.cli; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * Provides WinRM test fixture from a properties file. + */ +public class PropfileWinRMFixture implements WinRMFixture { + + @Override + public WsmanCli createClient() throws IOException { + //load user configuration + Properties prop = new Properties(); + InputStream is = getClass().getClassLoader().getResourceAsStream("configTests.properties"); + if (is != null) { + prop.load(is); + } else { + throw new FileNotFoundException("tests config file missing"); + } + + WsmanCli cli = new WsmanCli(); + cli.host = prop.getProperty(WINRM_HOST); + cli.port = Integer.parseInt(prop.getProperty(WINRM_PORT)); + cli.user = prop.getProperty(WINRM_USER); + cli.pass = prop.getProperty(WINRM_PASS); + cli.transport = Transport.plaintext; + //cli.cmd = prop.getProperty(WINRM_CMD); + //cli.cmdArgs = Arrays.asList(prop.getProperty(WINRM_CMDARGS).split(" ")); + + return cli; + } + +} diff --git a/wsman-cli/src/test/java/org/apifocal/wsman/cli/SyspropsWinRMFixture.java b/wsman-cli/src/test/java/org/apifocal/wsman/cli/SyspropsWinRMFixture.java new file mode 100644 index 0000000..667d206 --- /dev/null +++ b/wsman-cli/src/test/java/org/apifocal/wsman/cli/SyspropsWinRMFixture.java @@ -0,0 +1,71 @@ +/* + * Copyright 2016 apifocal. + * + * Licensed 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.apifocal.wsman.cli; + +/** + * Provides WinRM test fixture from a set of Java system properties. + * + * Usage sample: + * + * In pom.xml: + * + * + * <properties> + * <winrm.host>localhost</winrm.host> <!-- some defaults -- > + * <winrm.host>localhost</winrm.host> + * ....... + * </properties> + * ...... + * <plugin> + * <groupId>org.apache.maven.plugins</groupId> + * <artifactId>maven-surefire-plugin</artifactId> + * <configuration> + * <systemProperties> + * <property> + * <name>winrm.host</name> + * <value>${winrm.host}</value> + * </property> + * <property> + * <name>winrm.port</name> + * <value>${winrm.port}</value> + * </property> + * ...... + * </systemProperties> + * </configuration> + * </plugin> + * + * + * Next run it with: + * + * mvn -Dwinrm.host=someHost test + */ +public class SyspropsWinRMFixture implements WinRMFixture { + + @Override + public WsmanCli createClient() { + WsmanCli cli = new WsmanCli(); + cli.host = System.getProperty(WINRM_HOST); + cli.port = Integer.parseInt(System.getProperty(WINRM_PORT)); + cli.user = System.getProperty(WINRM_USER); + cli.pass = System.getProperty(WINRM_PASS); + cli.transport = Transport.plaintext; + //cli.cmd = prop.getProperty("cmd"); + //cli.cmdArgs = Arrays.asList(prop.getProperty("cmdArgs").split(" ")); + + return cli; + } +} diff --git a/wsman-cli/src/test/java/org/apifocal/wsman/cli/TestWsman.java b/wsman-cli/src/test/java/org/apifocal/wsman/cli/TestWsman.java index 4a3c4c7..868dcb4 100644 --- a/wsman-cli/src/test/java/org/apifocal/wsman/cli/TestWsman.java +++ b/wsman-cli/src/test/java/org/apifocal/wsman/cli/TestWsman.java @@ -17,12 +17,9 @@ import com.microsoft.schemas.wbem.wsman._1.windows.shell.Shell; import java.io.File; -import java.io.FileNotFoundException; -import java.io.InputStream; import java.io.StringReader; import java.net.URL; import java.util.HashMap; -import java.util.Properties; import java.util.Scanner; import java.util.UUID; import javax.xml.bind.JAXB; @@ -39,7 +36,7 @@ */ public class TestWsman { - private final WsmanCli cli = new WsmanCli(); + private WsmanCli cli; public TestWsman() { } @@ -60,22 +57,9 @@ public static void tearDownClass() { @Before public void setUp() throws Exception { - //load user configuration - Properties prop = new Properties(); - InputStream is = getClass().getClassLoader().getResourceAsStream("configTests.properties"); - if (is != null) { - prop.load(is); - } else { - throw new FileNotFoundException("tests config file missing"); - } - - cli.host = prop.getProperty("host"); - cli.port = Integer.parseInt(prop.getProperty("port")); - cli.user = prop.getProperty("user"); - cli.pass = prop.getProperty("pass"); - cli.transport = Transport.plaintext; - //cli.cmd = prop.getProperty("cmd"); - //cli.cmdArgs = Arrays.asList(prop.getProperty("cmdArgs").split(" ")); + // TODO configure the fixture externally + WinRMFixture fixture = new PropfileWinRMFixture(); + cli = fixture.createClient(); } @After @@ -83,8 +67,8 @@ public void tearDown() { } /* - * basic test that checks that a command is properly executed by wsman - */ + * basic test that checks that a command is properly executed by wsman + */ @Test public void testWsman() throws Exception { Session s = cli.createSession(); @@ -96,9 +80,9 @@ public void testWsman() throws Exception { } /* - * make sure that CXF generated code loads the same ws response as pywinrm - * compares Shell objects inside ws responses - */ + * make sure that CXF generated code loads the same ws response as pywinrm + * compares Shell objects inside ws responses + */ @Test public void testCXF() throws Exception { // diff --git a/wsman-cli/src/test/java/org/apifocal/wsman/cli/WinRMFixture.java b/wsman-cli/src/test/java/org/apifocal/wsman/cli/WinRMFixture.java new file mode 100644 index 0000000..5b08015 --- /dev/null +++ b/wsman-cli/src/test/java/org/apifocal/wsman/cli/WinRMFixture.java @@ -0,0 +1,38 @@ +/* + * Copyright 2016 apifocal. + * + * Licensed 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.apifocal.wsman.cli; + +/** + * The WinRM system for tests. + */ +public interface WinRMFixture { + + // convenience common property names for various config files + public static final String WINRM_HOST = "winrm.host"; + public static final String WINRM_PORT = "winrm.port"; + public static final String WINRM_USER = "winrm.user"; + public static final String WINRM_PASS = "winrm.pass"; + public static final String WINRM_CMD = "winrm.cmd"; + public static final String WINRM_CMDARGS = "winrm.cmdArgs"; + + /** + * Create a configured client for tests. + * @return A configured {@link WsmanCli} + * @throws java.lang.Exception On exceptions + */ + WsmanCli createClient() throws Exception; +} diff --git a/wsman-cli/src/test/resources/configTests.properties b/wsman-cli/src/test/resources/configTests.properties index 246ec37..31b047c 100644 --- a/wsman-cli/src/test/resources/configTests.properties +++ b/wsman-cli/src/test/resources/configTests.properties @@ -1,6 +1,6 @@ -host=localhost -port=5985 -user=username -pass=password -#cmd=ipconfig -#cmdArgs=/all \ No newline at end of file +winrm.host=localhost +winrm.port=5985 +winrm.user=username +winrm.pass=password +#winrm.cmd=ipconfig +#winrm.cmdArgs=/all \ No newline at end of file