Skip to content

Commit

Permalink
Add more ways to configure wsman-cli test system
Browse files Browse the repository at this point in the history
Besides using the property file to configure the target winrm test
system, also allow use of maven properties.
  • Loading branch information
CMoH authored and kcris committed Jan 7, 2016
1 parent f92e2b7 commit 8f70ab1
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 33 deletions.
5 changes: 3 additions & 2 deletions wsman-cli/src/main/java/org/apifocal/wsman/cli/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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:
*
* <code>
* &lt;properties&gt;
* &lt;winrm.host&gt;localhost&lt;/winrm.host&gt; &lt;!-- some defaults -- &gt;
* &lt;winrm.host&gt;localhost&lt;/winrm.host&gt;
* .......
* &lt;/properties&gt;
* ......
* &lt;plugin&gt;
* &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
* &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
* &lt;configuration&gt;
* &lt;systemProperties&gt;
* &lt;property&gt;
* &lt;name&gt;winrm.host&lt;/name&gt;
* &lt;value&gt;${winrm.host}&lt;/value&gt;
* &lt;/property&gt;
* &lt;property&gt;
* &lt;name&gt;winrm.port&lt;/name&gt;
* &lt;value&gt;${winrm.port}&lt;/value&gt;
* &lt;/property&gt;
* ......
* &lt;/systemProperties&gt;
* &lt;/configuration&gt;
* &lt;/plugin&gt;
* </code>
*
* Next run it with:
*
* <code>mvn -Dwinrm.host=someHost test </code>
*/
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;
}
}
34 changes: 9 additions & 25 deletions wsman-cli/src/test/java/org/apifocal/wsman/cli/TestWsman.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +36,7 @@
*/
public class TestWsman {

private final WsmanCli cli = new WsmanCli();
private WsmanCli cli;

public TestWsman() {
}
Expand All @@ -60,31 +57,18 @@ 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
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();
Expand All @@ -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 {
//
Expand Down
38 changes: 38 additions & 0 deletions wsman-cli/src/test/java/org/apifocal/wsman/cli/WinRMFixture.java
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 6 additions & 6 deletions wsman-cli/src/test/resources/configTests.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
host=localhost
port=5985
user=username
pass=password
#cmd=ipconfig
#cmdArgs=/all
winrm.host=localhost
winrm.port=5985
winrm.user=username
winrm.pass=password
#winrm.cmd=ipconfig
#winrm.cmdArgs=/all

0 comments on commit 8f70ab1

Please sign in to comment.