Skip to content

Commit

Permalink
[jenkinsci#1053] Add test for windows agents plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rsandell committed Mar 14, 2023
1 parent 91eb46d commit 7ba681f
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License
*
* Copyright (c) 2023 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.test.acceptance.plugins.windowsslaves;

import org.jenkinsci.test.acceptance.po.ComputerLauncher;
import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.Describable;
import org.jenkinsci.test.acceptance.po.PageObject;

@Describable({"hudson.os.windows.ManagedWindowsServiceLauncher"})
public class WindowsSlaveLauncher extends ComputerLauncher {

private final Control user = this.control("userName");
private final Control password = this.control("password");
private final Control host = this.control("host");
private final Control runAs = this.control("/");

public WindowsSlaveLauncher(PageObject context, String path) {
super(context, path);
}

public WindowsSlaveLauncher user(String usernameToConnect) {
user.set(usernameToConnect);
return this;
}

public WindowsSlaveLauncher password(String passwordToConnect) {
password.set(passwordToConnect);
return this;
}

public WindowsSlaveLauncher host(String hostToConnect) {
host.set(hostToConnect);
return this;
}

public WindowsSlaveLauncher runAs(String runAs) {
this.runAs.select(runAs);
return this;
}
}

146 changes: 146 additions & 0 deletions src/test/java/plugins/WindowsAgentsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* The MIT License
*
* Copyright (c) 2023 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package plugins;

import org.jenkinsci.test.acceptance.junit.AbstractJUnitTest;
import org.jenkinsci.test.acceptance.junit.WithPlugins;
import org.jenkinsci.test.acceptance.plugins.windowsslaves.WindowsSlaveLauncher;
import org.jenkinsci.test.acceptance.po.DumbSlave;
import org.jenkinsci.test.acceptance.po.FreeStyleJob;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.openqa.selenium.TimeoutException;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

@WithPlugins({ "windows-slaves" }) @Ignore("Enable when ATH testing on windows is available.")
public class WindowsAgentsTest extends AbstractJUnitTest {
public static final String WINDOWS_SLAVES_HOST_PROPERTY = "plugins.windowsslaves.host";
public static final String WINDOWS_SLAVES_USER_PROPERTY = "plugins.windowsslaves.user";
public static final String WINDOWS_SLAVES_PASSWORD_PROPERTY = "plugins.windowsslaves.pass";
public static final String WINDOWS_SLAVES_REMOTE_PATH_PROPERTY = "plugins.windowsslaves.path";
public static final String LABEL = "windows";

private DumbSlave agent;
private WindowsSlaveLauncher launcher;
private String user = getWindowsProperty(WINDOWS_SLAVES_USER_PROPERTY);
private String pass = getWindowsProperty(WINDOWS_SLAVES_PASSWORD_PROPERTY);
private String remoteFS = getWindowsRemoteFSPathBase();
private String host = getWindowsProperty(WINDOWS_SLAVES_HOST_PROPERTY);

@After
public void tearDown() throws ExecutionException, InterruptedException {
agent.disconnect("Finishing test");
agent.delete();
}

@Test
public void shouldConnect() {
setUpAgent(remoteFS, user, pass, host);
agent.waitUntilOnline();
assertTiedJobSucceeds();
}

@Test
public void shouldNoConnectOnWrongUser() {
setUpAgent(remoteFS, user + "wrong", pass, host);
assertAgentOffline();
agent.configure();
launcher.user(user);
agent.save();
agent.waitUntilOnline();
}

@Test
public void shouldNoConnectOnWrongPass() {
setUpAgent(remoteFS, user, pass + "wrong", host);
assertAgentOffline();
agent.configure();
launcher.password(pass);
agent.save();
agent.waitUntilOnline();
}

private void setUpAgent(String remotePath, String user, String password, String host) {
agent = jenkins.slaves.create(DumbSlave.class);
agent.setExecutors(1);
agent.remoteFS.set(remotePath + "\\" + agent.getName());
agent.setLabels(LABEL);
launcher = agent.setLauncher(WindowsSlaveLauncher.class);
launcher.user(user).password(password).host(host).runAs("Use Administrator account given above");
agent.save();
}

private void assertTiedJobSucceeds() {
FreeStyleJob job = jenkins.jobs.create();
job.configure();
job.setLabelExpression(LABEL);
job.save();
job.scheduleBuild().shouldSucceed();
assertBuiltOnAgent(job);

}

private void assertBuiltOnAgent(FreeStyleJob job) {
agent.open();
waitFor(by.link("Build History")).click();
waitFor(by.href("/job/" + job.name + "/"));
}

private void assertAgentOffline() {
try {
waitFor().withTimeout(30, TimeUnit.SECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() {
return agent.isOnline();
}
});
fail("Slave has become online when it should remain offline");
} catch (TimeoutException timeout) {
//empty by design this is the expected behaviour
}
}

private String getWindowsProperty(String property) {
String p = System.getProperty(property);
if (p == null || p.isEmpty()) {
throw new AssumptionViolatedException(String.format("No windows property (%s) found, ignoring windows slaves test", property));
}
return p;
}

private String getWindowsRemoteFSPathBase() {
String path = getWindowsProperty(WINDOWS_SLAVES_REMOTE_PATH_PROPERTY);
if (path.endsWith("\\")) { //sanity check to avoid problems with last \ characters
return path.substring(0, path.length() - 1);
}
return path;
}
}

0 comments on commit 7ba681f

Please sign in to comment.