add new unit tests for plugin
This commit adds a few unit tests for the gearman-plugin. A few things were refactored from creating the tests. 1. move methods from GearmanPluginUtil to GearmanPluginConfig 2. removed a few unused methods. Change-Id: I8ccd0000ff528867c66958294192818f35d83383
This commit is contained in:
parent
3c3851665a
commit
2b175701d2
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="src" path="src/test/java"/>
|
||||
<classpathentry kind="src" path="src/main/resources"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
|
|
21
pom.xml
21
pom.xml
|
@ -137,6 +137,9 @@
|
|||
<xml-maven-plugin.version>1.0-beta-3</xml-maven-plugin.version>
|
||||
<gson.version>2.2.2</gson.version>
|
||||
<gearman.version>0.6-SNAPSHOT</gearman.version>
|
||||
<hamcrest.version>1.3</hamcrest.version>
|
||||
<mockito.version>1.9.0</mockito.version>
|
||||
<powermock.version>1.4.12</powermock.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
@ -341,5 +344,23 @@
|
|||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -165,4 +165,8 @@ public abstract class AbstractWorkerThread implements Runnable {
|
|||
// Thread exits
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return thread.isAlive();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,6 +22,8 @@ import hudson.model.Descriptor;
|
|||
import hudson.util.FormValidation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
|
@ -33,6 +35,8 @@ import org.kohsuke.stapler.StaplerRequest;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
/**
|
||||
* This class is used to set the global configuration for the gearman-plugin It
|
||||
* is also used to enable/disable the gearman plugin.
|
||||
|
@ -73,7 +77,7 @@ public class GearmanPluginConfig extends GlobalConfiguration {
|
|||
@QueryParameter("port") final int port) throws IOException,
|
||||
ServletException {
|
||||
|
||||
if (GearmanPluginUtil.connectionIsAvailable(host, port, 5000)) {
|
||||
if (connectionIsAvailable(host, port, 5000)) {
|
||||
return FormValidation.ok("Success");
|
||||
} else {
|
||||
return FormValidation.error("Failed: Unable to Connect");
|
||||
|
@ -98,7 +102,7 @@ public class GearmanPluginConfig extends GlobalConfiguration {
|
|||
// check for a valid connection to gearman server
|
||||
logger.info("---- Check connection to Gearman Server " + host + ":"
|
||||
+ port);
|
||||
if (!GearmanPluginUtil.connectionIsAvailable(host, port, 5000)) {
|
||||
if (!connectionIsAvailable(host, port, 5000)) {
|
||||
launchWorker = false;
|
||||
throw new FormException("Unable to connect to Gearman server. "
|
||||
+ "Please check the server connection settings and retry.",
|
||||
|
@ -121,21 +125,71 @@ public class GearmanPluginConfig extends GlobalConfiguration {
|
|||
* launch worker.
|
||||
*/
|
||||
public boolean launchWorker() {
|
||||
return launchWorker;
|
||||
return Objects.firstNonNull(launchWorker, Constants.GEARMAN_DEFAULT_LAUNCH_WORKER);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the value from the server host text box
|
||||
*/
|
||||
public String getHost() {
|
||||
return host;
|
||||
return Objects.firstNonNull(host, Constants.GEARMAN_DEFAULT_TCP_HOST);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the value from the server port text box
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
|
||||
if (port == 0){ // Change default value
|
||||
return Constants.GEARMAN_DEFAULT_TCP_PORT;
|
||||
} else {
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method checks whether a connection open and available
|
||||
* on $host:$port
|
||||
*
|
||||
* @param host
|
||||
* the host name
|
||||
*
|
||||
* @param port
|
||||
* the host port
|
||||
*
|
||||
* @param timeout
|
||||
* the timeout (milliseconds) to try the connection
|
||||
*
|
||||
* @return boolean
|
||||
* true if a socket connection can be established otherwise false
|
||||
*/
|
||||
private boolean connectionIsAvailable(String host, int port,
|
||||
int timeout) {
|
||||
|
||||
InetSocketAddress endPoint = new InetSocketAddress(host, port);
|
||||
Socket socket = new Socket();
|
||||
|
||||
if (endPoint.isUnresolved()) {
|
||||
System.out.println("Failure " + endPoint);
|
||||
} else {
|
||||
try {
|
||||
socket.connect(endPoint, timeout);
|
||||
logger.info("Connection Success: " + endPoint);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.info("Connection Failure: " + endPoint + " message: "
|
||||
+ e.getClass().getSimpleName() + " - " + e.getMessage());
|
||||
} finally {
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
logger.info(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,102 +0,0 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||
*
|
||||
* 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 hudson.plugins.gearman;
|
||||
|
||||
import hudson.model.Computer;
|
||||
import hudson.model.Node;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import jenkins.model.Jenkins;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This class contains some useful utilities for this plugin
|
||||
*
|
||||
* @author Khai Do
|
||||
*/
|
||||
public class GearmanPluginUtil {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(Constants.PLUGIN_LOGGER_NAME);
|
||||
|
||||
/*
|
||||
* This method checks whether a connection can be made to a host:port
|
||||
*
|
||||
* @param host the host name
|
||||
*
|
||||
* @param port the host port
|
||||
*
|
||||
* @param timeout the timeout (milliseconds) to try the connection
|
||||
*
|
||||
* @return true if a socket connection can be established otherwise false
|
||||
*/
|
||||
public static boolean connectionIsAvailable(String host, int port,
|
||||
int timeout) {
|
||||
|
||||
InetSocketAddress endPoint = new InetSocketAddress(host, port);
|
||||
Socket socket = new Socket();
|
||||
|
||||
if (endPoint.isUnresolved()) {
|
||||
System.out.println("Failure " + endPoint);
|
||||
} else {
|
||||
try {
|
||||
socket.connect(endPoint, timeout);
|
||||
logger.info("---- Connection Success: " + endPoint);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.info("---- Connection Failure: " + endPoint + " message: "
|
||||
+ e.getClass().getSimpleName() + " - " + e.getMessage());
|
||||
} finally {
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
logger.info(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* This method returns the total number of nodes that are active in jenkins,
|
||||
* active mean that it's been created, but not necessarily online.
|
||||
*/
|
||||
public static int getNumTotalNodes() {
|
||||
|
||||
// check whether master is enabled
|
||||
Node masterNode = null;
|
||||
try {
|
||||
masterNode = Computer.currentComputer().getNode();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
if (masterNode != null) { // master is enabled, count it
|
||||
return Jenkins.getInstance().getNodes().size() + 1;
|
||||
} else { // only slaves, no master
|
||||
return Jenkins.getInstance().getNodes().size();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||
*
|
||||
* 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 hudson.plugins.gearman;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.gearman.common.GearmanNIOJobServerConnection;
|
||||
import org.gearman.worker.GearmanWorker;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
|
||||
/**
|
||||
* Test for the {@link AbstractWorkerThread} class.
|
||||
*
|
||||
* @author Khai Do
|
||||
*/
|
||||
@PrepareForTest(GearmanWorker.class)
|
||||
public class AbstractWorkerThreadTest {
|
||||
|
||||
/**
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
GearmanWorker gearmanWorker = mock(GearmanWorker.class);
|
||||
GearmanNIOJobServerConnection conn = new GearmanNIOJobServerConnection("localhost", 4730);
|
||||
doNothing().when(gearmanWorker).work();
|
||||
when(gearmanWorker.addServer(conn)).thenReturn(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnonymousThread() {
|
||||
AbstractWorkerThread fakeWorker = new FakeWorkerThread("GearmanServer", 4730);
|
||||
assertEquals("anonymous", fakeWorker.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedThread() {
|
||||
AbstractWorkerThread fakeWorker = new FakeWorkerThread("GearmanServer", 4730, "faker");
|
||||
assertEquals("faker", fakeWorker.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartStopThread() {
|
||||
AbstractWorkerThread fakeWorker = new FakeWorkerThread("GearmanServer", 4730);
|
||||
fakeWorker.start();
|
||||
assertTrue(fakeWorker.isAlive());
|
||||
fakeWorker.stop();
|
||||
assertFalse(fakeWorker.isAlive());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||
*
|
||||
* 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 hudson.plugins.gearman;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/*
|
||||
* This a fake worker which is only used for testing
|
||||
*
|
||||
* @author Khai Do
|
||||
*
|
||||
*/
|
||||
public class FakeWorkerThread extends AbstractWorkerThread{
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(Constants.PLUGIN_LOGGER_NAME);
|
||||
|
||||
// constructor
|
||||
public FakeWorkerThread(String host, int port) {
|
||||
super(host, port);
|
||||
}
|
||||
|
||||
public FakeWorkerThread(String host, int port, String name) {
|
||||
super(host, port, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fake registerJobs
|
||||
*/
|
||||
@Override
|
||||
public void registerJobs() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fake run
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||
*
|
||||
* 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 hudson.plugins.gearman;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import jenkins.model.Jenkins;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
/**
|
||||
* Test for the {@link GearmanPluginConfig} class.
|
||||
*
|
||||
* @author Khai Do
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(Jenkins.class)
|
||||
public class GearmanPluginConfigTest {
|
||||
|
||||
private GearmanPluginConfig gpc;
|
||||
|
||||
/**
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
TemporaryFolder folder = new TemporaryFolder();
|
||||
Jenkins jenkins = mock(Jenkins.class);
|
||||
PowerMockito.mockStatic(Jenkins.class);
|
||||
when(Jenkins.getInstance()).thenReturn(jenkins);
|
||||
|
||||
gpc = new GearmanPluginConfig();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultGearmanHost() {
|
||||
assertEquals(Constants.GEARMAN_DEFAULT_TCP_HOST, gpc.getHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultGearmanPort() {
|
||||
assertEquals(Constants.GEARMAN_DEFAULT_TCP_PORT, gpc.getPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultLaunchWorker() {
|
||||
assertEquals(Constants.GEARMAN_DEFAULT_LAUNCH_WORKER,
|
||||
gpc.launchWorker());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||
*
|
||||
* 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 hudson.plugins.gearman;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.gearman.common.GearmanNIOJobServerConnection;
|
||||
import org.gearman.worker.GearmanWorker;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
|
||||
/**
|
||||
* Test for the {@link ManagementWorkerThread} class.
|
||||
*
|
||||
* @author Khai Do
|
||||
*/
|
||||
@PrepareForTest(GearmanWorker.class)
|
||||
public class ManagementWorkerThreadTest {
|
||||
|
||||
/**
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
GearmanWorker gearmanWorker = mock(GearmanWorker.class);
|
||||
GearmanNIOJobServerConnection conn = new GearmanNIOJobServerConnection("localhost", 4730);
|
||||
doNothing().when(gearmanWorker).work();
|
||||
when(gearmanWorker.addServer(conn)).thenReturn(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterJobs() {
|
||||
AbstractWorkerThread manager = new ManagementWorkerThread("GearmanServer", 4730, "MyWorker" );
|
||||
manager.registerJobs();
|
||||
Set<String> functions = manager.worker.getRegisteredFunctions();
|
||||
assertEquals("stop:GearmanServer", functions.toArray()[0]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue