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

Adapt some tests to make it feasible to run it under IPv6 environments #1510

Merged
merged 17 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.test.acceptance.docker.fixtures;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.jenkinsci.test.acceptance.docker.Docker;
import org.jenkinsci.test.acceptance.docker.DockerContainer;
Expand All @@ -23,17 +24,18 @@ public int port() {
}

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

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

@Deprecated
public String getRepoUrlInsideDocker() throws IOException {
return "ssh://git@" + getIpAddress() + REPO_DIR;
return "ssh://git@" + addBracketsIfNeeded(getIpAddress()) + REPO_DIR;
}

/**
Expand All @@ -53,5 +55,8 @@ public void addSSHCertificate(String pubKey) throws IOException, InterruptedExce
.popen()
.verifyOrDieWith("Unable to add SSH public key to authorized keys");
}


public static String addBracketsIfNeeded(String host) throws MalformedURLException {
return new URL("http", host, 0, "").getHost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public URL getURL() throws IOException {
}

public URL getHttpUrl() throws IOException {
String url = "http://" + httpHost() + ':' + httpPort();
return new URL(url);
return new URL("http", httpHost(), httpPort(), "");
}

/** URL visible from the host. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.jenkinsci.test.acceptance.docker.fixtures;

import java.net.MalformedURLException;
import java.net.URL;

import org.jenkinsci.test.acceptance.docker.DockerContainer;
import org.jenkinsci.test.acceptance.docker.DockerFixture;
import org.jenkinsci.test.acceptance.plugins.ldap.LdapDetails;

/**
* openLDAP (slapd) Container with a small user directory.
* The configuration of this ldap is located next to its Dockerfile.
* To edit/add users or groups just add them to config/base.ldif and rebuild the image.
* openLDAP (slapd) Container with a small user directory. The configuration of
* this ldap is located next to its Dockerfile. To edit/add users or groups just
* add them to config/base.ldif and rebuild the image.
*
* @author Michael Prankl
*/
@DockerFixture(id = "ldap", ports = {389, 636})
@DockerFixture(id = "ldap", ports = { 389, 636 })
public class LdapContainer extends DockerContainer {

public String getHost() {
Expand All @@ -35,10 +38,16 @@ public String getManagerPassword() {
}

/**
* @return default ldap connection details from current running docker LdapContainer.
* @return default ldap connection details from current running docker
* LdapContainer.
* @throws MalformedURLException
*/
public LdapDetails createDefault() {
return new LdapDetails(getHost(), getPort(), getManagerDn(), getManagerPassword(), getRootDn());
public LdapDetails createDefault() throws MalformedURLException {
return new LdapDetails(addBracketsIfNeeded(getHost()), getPort(), getManagerDn(), getManagerPassword(),
getRootDn());
}

public static String addBracketsIfNeeded(String host) throws MalformedURLException {
return new URL("http", host, 0, "").getHost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void assertMail(final Pattern expectedSubject, String recipient, Pattern

private JsonNode fetchJsonMessages() {
try {
URL url = new URL("http://" + ipBound(8025) + ":" + port(8025) + "/api/v1/messages");
URL url = new URL("http", ipBound(8025), port(8025), "/api/v1/messages");
HttpURLConnection c = IOUtil.openConnection(url);
try {
return new ObjectMapper().readTree(c.getInputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void transferToDockerContainer(String host, int port) {
JSch jSch = new JSch();
jSch.addIdentity(privateKey.getAbsolutePath());

Session session = jSch.getSession("git", host, port);
Session session = jSch.getSession("git", GitContainer.addBracketsIfNeeded(host), port);
session.setConfig(props);
session.connect();

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/plugins/GitPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ public void name_remote_repo() {
job.addShellStep("test -f foo && git remote -v");
job.save();

job.startBuild().shouldSucceed().shouldContainsConsoleOutput("custom_origin\\s+" + repoUrl);
job.startBuild().shouldSucceed().shouldContainsConsoleOutput("custom_origin\\s+" + Pattern.quote(repoUrl));
}


@Test
public void checkout_local_branch() {
buildGitRepo()
Expand Down
55 changes: 31 additions & 24 deletions src/test/java/plugins/LdapPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import jakarta.inject.Inject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jenkinsci.test.acceptance.docker.DockerContainerHolder;
import org.jenkinsci.test.acceptance.docker.fixtures.LdapContainer;
import org.jenkinsci.test.acceptance.junit.*;
Expand Down Expand Up @@ -51,8 +54,9 @@ private void useLdapAsSecurityRealm(LdapDetails ldapDetails) {
*
* @param ldapContainer a docker LdapContainer
* @return default ldap connection details
* @throws MalformedURLException
*/
private LdapDetails createDefaults(LdapContainer ldapContainer) {
private LdapDetails createDefaults(LdapContainer ldapContainer) throws MalformedURLException {
return ldapContainer.createDefault();
}

Expand All @@ -61,13 +65,14 @@ private LdapDetails createDefaults(LdapContainer ldapContainer) {
*
* @param ldapContainer
* @return default ldap connection details without the manager credentials
* @throws MalformedURLException
*/
private LdapDetails createDefaultsWithoutManagerCred(LdapContainer ldapContainer) {
return new LdapDetails(ldapContainer.getHost(), ldapContainer.getPort(), "", "", ldapContainer.getRootDn());
private LdapDetails createDefaultsWithoutManagerCred(LdapContainer ldapContainer) throws MalformedURLException {
return new LdapDetails(LdapContainer.addBracketsIfNeeded(ldapContainer.getHost()), ldapContainer.getPort(), "", "", ldapContainer.getRootDn());
jtnord marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void login_ok() {
public void login_ok() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand All @@ -78,7 +83,7 @@ public void login_ok() {
}

@Test
public void login_ok_anonymous_binding() {
public void login_ok_anonymous_binding() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaultsWithoutManagerCred(ldap.get()));
// When
Expand All @@ -89,7 +94,7 @@ public void login_ok_anonymous_binding() {
}

@Test
public void login_wrong_password() {
public void login_wrong_password() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand All @@ -100,7 +105,7 @@ public void login_wrong_password() {
}

@Test
public void login_no_such_user() {
public void login_no_such_user() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand Down Expand Up @@ -130,7 +135,7 @@ public void login_no_ldap() throws InterruptedException {
}

@Test
public void login_search_base_people_ok() {
public void login_search_base_people_ok() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).userSearchBase("ou=People"));
// When
Expand All @@ -141,7 +146,7 @@ public void login_search_base_people_ok() {
}

@Test
public void login_search_base_people_not_found() {
public void login_search_base_people_not_found() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).userSearchBase("ou=People"));
// When
Expand All @@ -152,7 +157,7 @@ public void login_search_base_people_not_found() {
}

@Test
public void login_email_ok() {
public void login_email_ok() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).userSearchFilter("mail={0}"));
// When
Expand All @@ -163,7 +168,7 @@ public void login_email_ok() {
}

@Test
public void invalid_user_search_filter() {
public void invalid_user_search_filter() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).userSearchFilter("invalid={0}"));
// When
Expand All @@ -179,7 +184,7 @@ public void invalid_user_search_filter() {
}

@Test
public void login_use_fallback_server() {
public void login_use_fallback_server() throws MalformedURLException {
// Given
LdapContainer ldapContainer = ldap.get();
GlobalSecurityConfig securityConfig = new GlobalSecurityConfig(jenkins);
Expand All @@ -188,7 +193,9 @@ public void login_use_fallback_server() {
int freePort = this.findAvailablePort();
LdapDetails ldapDetails = new LdapDetails("", 0, ldapContainer.getManagerDn(), ldapContainer.getManagerPassword(), ldapContainer.getRootDn());
// Fallback-Config: primary server is not running, alternative server is running docker fixture
ldapDetails.setHostWithPort("localhost:" + freePort + ' ' + ldapContainer.getHost() + ':' + ldapContainer.getPort());
String host = LdapContainer.addBracketsIfNeeded(ldapContainer.getHost());
ldapDetails.setHostWithPort((
LdapContainer.ipv6Enabled() ? "[::1]:" : "localhost:") + freePort + ' ' + host + ':' + ldapContainer.getPort());
realm.configure(ldapDetails);
securityConfig.save();

Expand All @@ -202,7 +209,7 @@ public void login_use_fallback_server() {
}

@Test
public void resolve_email() {
public void resolve_email() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand All @@ -215,7 +222,7 @@ public void resolve_email() {
}

@Test
public void do_not_resolve_email() {
public void do_not_resolve_email() throws MalformedURLException {
// Given
LdapDetails details = createDefaults(ldap.get());
details.setDisableLdapEmailResolver(true);
Expand Down Expand Up @@ -243,7 +250,7 @@ public void enable_cache() throws IOException {
}

@Test
public void use_environment_varibales() {
public void use_environment_varibales() throws MalformedURLException {
// Given
LdapDetails details = createDefaultsWithoutManagerCred(ldap.get());
details.addEnvironmentVariable(new LdapEnvironmentVariable("java.naming.security.protocol", "ssl"));
Expand All @@ -256,7 +263,7 @@ public void use_environment_varibales() {
}

@Test
public void resolve_group_memberships_with_defaults() {
public void resolve_group_memberships_with_defaults() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand All @@ -269,7 +276,7 @@ public void resolve_group_memberships_with_defaults() {
}

@Test
public void resolve_group_memberships_with_defaults_negative() {
public void resolve_group_memberships_with_defaults_negative() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand All @@ -282,7 +289,7 @@ public void resolve_group_memberships_with_defaults_negative() {
}

@Test
public void custom_group_search_base() {
public void custom_group_search_base() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).groupSearchBase("ou=Applications"));
// When
Expand All @@ -296,7 +303,7 @@ public void custom_group_search_base() {

@Test
@Issue("JENKINS-18355")
public void resolve_display_name_with_defaults() {
public void resolve_display_name_with_defaults() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()));
// When
Expand All @@ -309,7 +316,7 @@ public void resolve_display_name_with_defaults() {

@Test
@Issue("JENKINS-18355")
public void custom_display_name() {
public void custom_display_name() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).displayNameAttributeName("cn"));
// When
Expand All @@ -321,7 +328,7 @@ public void custom_display_name() {
}

@Test
public void custom_invalid_group_membership_filter() {
public void custom_invalid_group_membership_filter() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).groupMembershipStrategy(SearchForGroupsLdapGroupMembershipStrategy.class).groupMembershipStrategyParam("(member={0})"));
// When
Expand All @@ -334,7 +341,7 @@ public void custom_invalid_group_membership_filter() {
}

@Test
public void custom_valid_group_membership_filter() {
public void custom_valid_group_membership_filter() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).groupMembershipStrategy(SearchForGroupsLdapGroupMembershipStrategy.class).groupMembershipStrategyParam("memberUid={1}"));
// When
Expand All @@ -347,7 +354,7 @@ public void custom_valid_group_membership_filter() {
}

@Test
public void custom_mail_filter() {
public void custom_mail_filter() throws MalformedURLException {
// Given
useLdapAsSecurityRealm(createDefaults(ldap.get()).mailAdressAttributeName("givenName"));
// When
Expand Down