Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freshen up realistic tests #78

Merged
merged 4 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<licenses>
<license>
<name>The MIT license</name>
<url>https://www.opensource.org/licenses/mit-license.php</url>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>
Expand All @@ -28,9 +28,8 @@
<changelist>-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<apacheds.version>2.0.0.AM25</apacheds.version>
<jenkins.version>2.266</jenkins.version>
<jenkins.version>2.277.1</jenkins.version>
<java.level>8</java.level>
<configuration-as-code.version>1.47</configuration-as-code.version>
</properties>

<scm>
Expand Down Expand Up @@ -83,18 +82,20 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>1.32.1</version>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supersedes #76

</dependency>
<dependency>
<groupId>org.jenkins-ci.test</groupId>
<artifactId>docker-fixtures</artifactId>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deprecated.

<version>1.10</version>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.15.2</version>
<scope>test</scope>
<exclusions>
<!-- Required: 2.2.3, JCasC includes: 2.10.2 -->
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down Expand Up @@ -279,18 +280,28 @@
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.277.x</artifactId>
<version>26</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down
33 changes: 16 additions & 17 deletions src/test/java/hudson/security/docker/MultiServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@
import hudson.tasks.MailAddressResolver;
import hudson.tasks.Mailer;
import hudson.util.Secret;
import java.util.Arrays;
import jenkins.model.IdStrategy;
import jenkins.security.plugins.ldap.*;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.jenkinsci.test.acceptance.docker.DockerRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.jvnet.hudson.test.JenkinsRule;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.testcontainers.containers.GenericContainer;

/**
* Tests connecting to two different servers
*/
@LDAPTestConfiguration
public class MultiServerTest {

@BeforeClass public static void linuxOnly() {
PlanetExpressTest.linuxOnly();
@BeforeClass public static void requiresDocker() {
PlanetExpressTest.requiresDocker();
}

@SuppressWarnings("rawtypes")
@Rule
public DockerRule<PlanetExpressTest.PlanetExpress> docker = new DockerRule<>(PlanetExpressTest.PlanetExpress.class);
public GenericContainer container = new GenericContainer(PlanetExpressTest.TEST_IMAGE).withExposedPorts(389);

public JenkinsRule j = new JenkinsRule();
public LDAPRule ads = new LDAPRule();
@Rule
Expand Down Expand Up @@ -64,13 +64,12 @@ public void userLookup() throws Exception {
adsConf.setDisplayNameAttributeName("cn");
adsConf.setMailAddressAttributeName(null);

PlanetExpressTest.PlanetExpress d = docker.get();
LDAPConfiguration plExprs = new LDAPConfiguration(
d.ipBound(389) + ":" + d.port(389),
PlanetExpressTest.PlanetExpress.DN,
container.getHost() + ":" + container.getFirstMappedPort(),
PlanetExpressTest.DN,
false,
PlanetExpressTest.PlanetExpress.MANAGER_DN,
Secret.fromString(PlanetExpressTest.PlanetExpress.MANAGER_SECRET));
PlanetExpressTest.MANAGER_DN,
Secret.fromString(PlanetExpressTest.MANAGER_SECRET));
plExprs.setUserSearchBase(null);
plExprs.setUserSearch(null);
plExprs.setGroupSearchBase(null);
Expand Down
73 changes: 42 additions & 31 deletions src/test/java/hudson/security/docker/PlanetExpressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,69 @@
import hudson.security.LDAPSecurityRealm;
import hudson.tasks.MailAddressResolver;
import hudson.util.Secret;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.jenkinsci.test.acceptance.docker.DockerContainer;
import org.jenkinsci.test.acceptance.docker.DockerFixture;
import org.jenkinsci.test.acceptance.docker.DockerRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RealJenkinsRule;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;

/**
* Tests the plugin when logging in to rroemhild/test-openldap
*/
public class PlanetExpressTest {

static final String TEST_IMAGE = "rroemhild/test-openldap@sha256:b4e433bbcba1f17899d6bcb0a8e854bbe52c754faa4e785d0c27a2b55eb12cd8";
static final String DN = "dc=planetexpress,dc=com";
static final String MANAGER_DN = "cn=admin,dc=planetexpress,dc=com";
static final String MANAGER_SECRET = "GoodNewsEveryone";

@BeforeClass public static void requiresDocker() {
assumeTrue(DockerClientFactory.instance().isDockerAvailable());
}

@BeforeClass public static void linuxOnly() {
assumeFalse("Windows CI builders now have Docker installed…but it does not support Linux images", Functions.isWindows() && System.getenv("JENKINS_URL") != null);
}

@SuppressWarnings("rawtypes")
@Rule
public DockerRule<PlanetExpress> docker = new DockerRule<>(PlanetExpress.class);
public GenericContainer container = new GenericContainer(TEST_IMAGE).withExposedPorts(389);

@Rule
public JenkinsRule j = new JenkinsRule();
public RealJenkinsRule rr = new RealJenkinsRule();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See jenkinsci/jenkins-test-harness#280. Would have increased my confidence about class loading issues during #48 in particular.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I merged #77 to make this possible.)


@Test
public void login() throws Exception {
PlanetExpress d = docker.get();
LDAPSecurityRealm realm = new LDAPSecurityRealm(d.ipBound(389) + ":" + d.port(389), PlanetExpress.DN, null, null, null, null, null, PlanetExpress.MANAGER_DN, Secret.fromString(PlanetExpress.MANAGER_SECRET), false, false, null, null, "cn", "mail", null,null);
j.jenkins.setSecurityRealm(realm);
j.configRoundtrip();
String content = j.createWebClient().login("fry", "fry").goTo("whoAmI").getBody().getTextContent();
assertThat(content, containsString("Philip J. Fry"));


LdapUserDetails zoidberg = (LdapUserDetails) j.jenkins.getSecurityRealm().loadUserByUsername2("zoidberg");
assertEquals("cn=John A. Zoidberg,ou=people,dc=planetexpress,dc=com", zoidberg.getDn());

String leelaEmail = MailAddressResolver.resolve(j.jenkins.getUser("leela"));
assertEquals("leela@planetexpress.com", leelaEmail);

public void login() throws Throwable {
String server = container.getHost() + ":" + container.getFirstMappedPort();
rr.then(new Login(server));
}
private static class Login implements RealJenkinsRule.Step {
private final String server;
Login(String server) {
this.server = server;
}
@Override
public void run(JenkinsRule j) throws Throwable {
LDAPSecurityRealm realm = new LDAPSecurityRealm(server, DN, null, null, null, null, null, MANAGER_DN, Secret.fromString(MANAGER_SECRET), false, false, null, null, "cn", "mail", null,null);
j.jenkins.setSecurityRealm(realm);
j.configRoundtrip();
String content = j.createWebClient().login("fry", "fry").goTo("whoAmI").getBody().getTextContent();
assertThat(content, containsString("Philip J. Fry"));

@DockerFixture(id = "openldap-express", ports = {389, 636})
public static class PlanetExpress extends DockerContainer {

static final String DN = "dc=planetexpress,dc=com";
static final String MANAGER_DN = "cn=admin,dc=planetexpress,dc=com";
static final String MANAGER_SECRET = "GoodNewsEveryone";
LdapUserDetails zoidberg = (LdapUserDetails) j.jenkins.getSecurityRealm().loadUserByUsername2("zoidberg");
assertEquals("cn=John A. Zoidberg,ou=people,dc=planetexpress,dc=com", zoidberg.getDn());

String leelaEmail = MailAddressResolver.resolve(j.jenkins.getUser("leela"));
assertEquals("leela@planetexpress.com", leelaEmail);
}
}

}

This file was deleted.