Skip to content

Commit

Permalink
Merge pull request #81 from jglick/temp-dir-JENKINS-27152
Browse files Browse the repository at this point in the history
[JENKINS-27152] Test of standardized temp directory with Git operations
  • Loading branch information
jglick committed Mar 4, 2016
2 parents 500a376 + eb4a0bd commit 6681f40
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 34 deletions.
20 changes: 20 additions & 0 deletions pom.xml
Expand Up @@ -695,6 +695,26 @@
</plugins> </plugins>
</build> </build>
</profile> </profile>
<profile>
<id>RECORDER</id>
<activation>
<property>
<name>RECORDER</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<RECORDER>${RECORDER}</RECORDER>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- TODO could be laboriously repeated for $TYPE, $DISPLAY, etc. (seems Maven provides no generic way to do this) --> <!-- TODO could be laboriously repeated for $TYPE, $DISPLAY, etc. (seems Maven provides no generic way to do this) -->


<profile> <profile>
Expand Down
Expand Up @@ -15,15 +15,26 @@ public class GitContainer extends DockerContainer {
private static final String REPO_DIR = "/home/git/gitRepo"; private static final String REPO_DIR = "/home/git/gitRepo";
public static final String REPO_NAME = "gitRepo"; public static final String REPO_NAME = "gitRepo";


public String host() {
return ipBound(22);
}

public int port() { public int port() {
return port(22); return port(22);
} }


public URL getUrl() throws IOException { public URL getUrl() throws IOException {
return new URL("http://localhost:" + port(22)); return new URL("http://" + host() + ":" + port());
} }


/** URL visible from the host. */
public String getRepoUrl() { public String getRepoUrl() {
return "ssh://git@localhost:" + port(22) + REPO_DIR; return "ssh://git@" + host() + ":" + port() + REPO_DIR;
}

/** URL visible from other Docker containers. */
public String getRepoUrlInsideDocker() throws IOException {
return "ssh://git@" + getIpAddress() + REPO_DIR;
} }

} }
Expand Up @@ -20,6 +20,7 @@
import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
import org.jenkinsci.test.acceptance.plugins.credentials.BaseStandardCredentials;


/** /**
* Indicates that a test requires credentials. * Indicates that a test requires credentials.
Expand Down Expand Up @@ -53,6 +54,9 @@


String[] values(); String[] values();


/** Optional ID to specify. */
String id() default "";

public class RuleImpl implements TestRule { public class RuleImpl implements TestRule {


@Inject @Inject
Expand All @@ -76,14 +80,14 @@ private boolean enterCredentials(WithCredentials wp) {
switch (wp.credentialType()) { switch (wp.credentialType()) {
case USERNAME_PASSWORD: case USERNAME_PASSWORD:
if (wp.values().length == 2) { if (wp.values().length == 2) {
addUsernamePasswordCredentials(wp.values()[0], wp.values()[1]); addUsernamePasswordCredentials(wp.values()[0], wp.values()[1], wp.id());
} else { } else {
throw new RuntimeException("@WithCredentials: Wrong amount of values. Expected username,password. "); throw new RuntimeException("@WithCredentials: Wrong amount of values. Expected username,password. ");
} }
break; break;
case SSH_USERNAME_PRIVATE_KEY: case SSH_USERNAME_PRIVATE_KEY:
if (wp.values().length == 2) { if (wp.values().length == 2) {
addSshUsernamePrivateKeyCredentials(wp.values()[0], wp.values()[1]); addSshUsernamePrivateKeyCredentials(wp.values()[0], wp.values()[1], wp.id());
} else { } else {
throw new RuntimeException("@WithCredentials: Wrong amount of values. Expected username,sshKeyPath."); throw new RuntimeException("@WithCredentials: Wrong amount of values. Expected username,sshKeyPath.");
} }
Expand All @@ -100,12 +104,13 @@ private boolean enterCredentials(WithCredentials wp) {
* @param username ssh username * @param username ssh username
* @param sshKeyPath path to the ssh key * @param sshKeyPath path to the ssh key
*/ */
private void addSshUsernamePrivateKeyCredentials(String username, String sshKeyPath) { private void addSshUsernamePrivateKeyCredentials(String username, String sshKeyPath, String id) {
ManagedCredentials c = new ManagedCredentials(jenkins); ManagedCredentials c = new ManagedCredentials(jenkins);
c.open(); c.open();
SshPrivateKeyCredential sc = c.add(SshPrivateKeyCredential.class); SshPrivateKeyCredential sc = c.add(SshPrivateKeyCredential.class);
sc.username.set(username); sc.username.set(username);
sc.selectEnterDirectly().privateKey.set(resource(sshKeyPath).asText()); sc.selectEnterDirectly().privateKey.set(resource(sshKeyPath).asText());
maybeSetId(sc, id);
c.save(); c.save();
} }


Expand All @@ -114,15 +119,21 @@ private void addSshUsernamePrivateKeyCredentials(String username, String sshKeyP
* @param username username * @param username username
* @param password password * @param password password
*/ */
private void addUsernamePasswordCredentials(String username, String password) { private void addUsernamePasswordCredentials(String username, String password, String id) {
final ManagedCredentials c = new ManagedCredentials(jenkins); final ManagedCredentials c = new ManagedCredentials(jenkins);
c.open(); c.open();
final UserPwdCredential upc = c.add(UserPwdCredential.class); final UserPwdCredential upc = c.add(UserPwdCredential.class);
upc.username.set(username); upc.username.set(username);
upc.password.set(password); upc.password.set(password);
maybeSetId(upc, id);
c.save(); c.save();
} }


private void maybeSetId(BaseStandardCredentials creds, String id) {
if (!id.isEmpty()) {
creds.setId(id);
}
}


/** /**
* Obtains a resource in a wrapper. * Obtains a resource in a wrapper.
Expand Down
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright 2016 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.credentials;

import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.PageArea;
import org.jenkinsci.test.acceptance.po.PageObject;

public abstract class BaseStandardCredentials extends Credential {

public final Control description = control("description");

protected BaseStandardCredentials(PageObject context, String path) {
super(context, path);
}

protected BaseStandardCredentials(PageArea area, String relativePath) {
super(area, relativePath);
}

public void setId(String id) {
control("advanced-button").click();
control("id").set(id);
}

}
Expand Up @@ -20,4 +20,12 @@ protected Credential(PageObject context, String path) {
protected Credential(PageArea area, String relativePath) { protected Credential(PageArea area, String relativePath) {
super(area, relativePath); super(area, relativePath);
} }

/**
* Adds this credential and close the dialog.
*/
public void add() {
find(by.id("credentials-add-submit-button")).click();
}

} }
@@ -1,6 +1,5 @@
package org.jenkinsci.test.acceptance.plugins.credentials; package org.jenkinsci.test.acceptance.plugins.credentials;


import org.jenkinsci.test.acceptance.plugins.credentials.Credential;
import org.jenkinsci.test.acceptance.po.Control; import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.Describable; import org.jenkinsci.test.acceptance.po.Describable;
import org.jenkinsci.test.acceptance.po.PageArea; import org.jenkinsci.test.acceptance.po.PageArea;
Expand All @@ -10,9 +9,8 @@
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
@Describable("Username with password") @Describable("Username with password")
public class UserPwdCredential extends Credential { public class UserPwdCredential extends BaseStandardCredentials {
public final Control username = control("username"); public final Control username = control("username");
public final Control description = control("description");
public final Control password = control("password"); public final Control password = control("password");


public UserPwdCredential(PageObject context, String path) { public UserPwdCredential(PageObject context, String path) {
Expand All @@ -23,10 +21,4 @@ public UserPwdCredential(PageArea area, String relativePath) {
super(area, relativePath); super(area, relativePath);
} }


/**
* Adds this credential and close the dialog.
*/
public void add() {
find(by.id("credentials-add-submit-button")).click();
}
} }
@@ -1,6 +1,6 @@
package org.jenkinsci.test.acceptance.plugins.ssh_credentials; package org.jenkinsci.test.acceptance.plugins.ssh_credentials;


import org.jenkinsci.test.acceptance.plugins.credentials.Credential; import org.jenkinsci.test.acceptance.plugins.credentials.BaseStandardCredentials;
import org.jenkinsci.test.acceptance.po.Control; import org.jenkinsci.test.acceptance.po.Control;
import org.jenkinsci.test.acceptance.po.Describable; import org.jenkinsci.test.acceptance.po.Describable;
import org.jenkinsci.test.acceptance.po.PageAreaImpl; import org.jenkinsci.test.acceptance.po.PageAreaImpl;
Expand All @@ -11,9 +11,8 @@
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
@Describable("SSH Username with private key") @Describable("SSH Username with private key")
public class SshPrivateKeyCredential extends Credential { public class SshPrivateKeyCredential extends BaseStandardCredentials {
public final Control username = control("username"); public final Control username = control("username");
public final Control description = control("description");


public SshPrivateKeyCredential(PageObject context, String path) { public SshPrivateKeyCredential(PageObject context, String path) {
super(context, path); super(context, path);
Expand Down Expand Up @@ -41,10 +40,4 @@ public Direct(PageObject parent, String path) {
} }
} }


/**
* Adds this credential and close the dialog.
*/
public void add() {
find(by.id("credentials-add-submit-button")).click();
}
} }
21 changes: 11 additions & 10 deletions src/test/java/plugins/GitPluginTest.java
Expand Up @@ -51,7 +51,6 @@
public class GitPluginTest extends AbstractJUnitTest { public class GitPluginTest extends AbstractJUnitTest {


private static final String USERNAME = "gitplugin"; private static final String USERNAME = "gitplugin";
private static final String HOST = "localhost";


@Inject @Inject
DockerContainerHolder<GitContainer> gitServer; DockerContainerHolder<GitContainer> gitServer;
Expand All @@ -60,13 +59,15 @@ public class GitPluginTest extends AbstractJUnitTest {


private GitContainer container; private GitContainer container;
private String repoUrl; private String repoUrl;
private String host;
private int port; private int port;


@Before @Before
public void init() { public void init() {
container = gitServer.get(); container = gitServer.get();
repoUrl = container.getRepoUrl(); repoUrl = container.getRepoUrl();
port = container.port(22); host = container.host();
port = container.port();
job = jenkins.jobs.create(); job = jenkins.jobs.create();
job.configure(); job.configure();
} }
Expand All @@ -75,7 +76,7 @@ public void init() {
@Category(SmokeTest.class) @Category(SmokeTest.class)
public void simple_checkout() throws InterruptedException, JSchException, SftpException, IOException { public void simple_checkout() throws InterruptedException, JSchException, SftpException, IOException {
buildGitRepo() buildGitRepo()
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand All @@ -90,7 +91,7 @@ public void simple_checkout() throws InterruptedException, JSchException, SftpEx
public void checkout_branch() throws InterruptedException, JSchException, SftpException, IOException { public void checkout_branch() throws InterruptedException, JSchException, SftpException, IOException {
GitRepo repo = buildGitRepo(); GitRepo repo = buildGitRepo();
repo.git("branch", "svn"); repo.git("branch", "svn");
repo.transferToDockerContainer(HOST, port); repo.transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand All @@ -105,7 +106,7 @@ public void checkout_branch() throws InterruptedException, JSchException, SftpEx
@Test @Test
public void name_remote_repo() throws IOException, InterruptedException, SftpException, JSchException { public void name_remote_repo() throws IOException, InterruptedException, SftpException, JSchException {
buildGitRepo() buildGitRepo()
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand All @@ -121,7 +122,7 @@ public void name_remote_repo() throws IOException, InterruptedException, SftpExc
@Test @Test
public void checkout_local_branch() throws IOException, InterruptedException, SftpException, JSchException { public void checkout_local_branch() throws IOException, InterruptedException, SftpException, JSchException {
buildGitRepo() buildGitRepo()
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand All @@ -136,7 +137,7 @@ public void checkout_local_branch() throws IOException, InterruptedException, Sf
@Test @Test
public void checkout_to_local_dir() throws IOException, InterruptedException, SftpException, JSchException { public void checkout_to_local_dir() throws IOException, InterruptedException, SftpException, JSchException {
buildGitRepo() buildGitRepo()
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand All @@ -151,7 +152,7 @@ public void checkout_to_local_dir() throws IOException, InterruptedException, Sf
@Test @Test
public void poll_for_changes() throws IOException, InterruptedException, SftpException, JSchException { public void poll_for_changes() throws IOException, InterruptedException, SftpException, JSchException {
buildGitRepo() buildGitRepo()
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) // job.useScm(GitScm.class) //
.url(container.getRepoUrl()) .url(container.getRepoUrl())
Expand All @@ -169,7 +170,7 @@ public void poll_for_changes() throws IOException, InterruptedException, SftpExc
@Test @Test
public void check_revision() throws IOException, InterruptedException, SftpException, JSchException { public void check_revision() throws IOException, InterruptedException, SftpException, JSchException {
buildGitRepo() buildGitRepo()
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand All @@ -189,7 +190,7 @@ public void update_submodules_recursively() throws IOException, InterruptedExcep
String name = "submodule"; String name = "submodule";
buildGitRepo() buildGitRepo()
.addSubmodule(name) .addSubmodule(name)
.transferToDockerContainer(HOST, port); .transferToDockerContainer(host, port);


job.useScm(GitScm.class) job.useScm(GitScm.class)
.url(repoUrl) .url(repoUrl)
Expand Down

0 comments on commit 6681f40

Please sign in to comment.